File: FXArray.h

package info (click to toggle)
gogglesmm 1.2.5-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 16,812 kB
  • sloc: cpp: 231,960; ansic: 893; xml: 222; makefile: 33
file content (284 lines) | stat: -rw-r--r-- 8,393 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/********************************************************************************
*                                                                               *
*                          G e n e r i c   A r r a y                            *
*                                                                               *
*********************************************************************************
* Copyright (C) 1997,2022 by Jeroen van der Zijp.   All Rights Reserved.        *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify          *
* it under the terms of the GNU Lesser General Public License as published by   *
* the Free Software Foundation; either version 3 of the License, or             *
* (at your option) any later version.                                           *
*                                                                               *
* This library is distributed in the hope that it will be useful,               *
* but WITHOUT ANY WARRANTY; without even the implied warranty of                *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
* GNU Lesser General Public License for more details.                           *
*                                                                               *
* You should have received a copy of the GNU Lesser General Public License      *
* along with this program.  If not, see <http://www.gnu.org/licenses/>          *
********************************************************************************/
#ifndef FXARRAY_H
#define FXARRAY_H

#ifndef FXELEMENT_H
#include "FXElement.h"
#endif

namespace FX {


/// ArrayBase manages a memory buffer
class FXAPI FXArrayBase {
protected:
  FXptr ptr;
protected:
  FXArrayBase();
  FXbool resize(FXival num,FXival sz);
 ~FXArrayBase();
  };


/// Array of some generic type
template<typename EType>
class FXArray : public FXArrayBase {
public:

  /// Allocate initially empty array
  FXArray(){ }

  /// Allocate array of n elements
  FXArray(FXival n){ no(n); }

  /// Allocate array copied from another
  FXArray(const FXArray<EType>& src){
    if(no(src.no())){ copyElms(data(),src.data(),src.no()); }
    }

  /// Allocate initialized with n copies of object
  FXArray(const EType& src,FXival n){
    if(no(n)){ fillElms(data(),src,n); }
    }

  /// Allocate initialized with array of n objects
  FXArray(const EType* src,FXival n){
    if(no(n)){ copyElms(data(),src,n); }
    }

  /// Return number of items
  FXival no() const { return *(((FXival*)ptr)-1); }

  /// Change number of elements in array to n
  FXbool no(FXival n){
    FXival m=no();
    if((n-m)>0){
      if(!resize(n,sizeof(EType))) return false;
      constructElms(&at(m),n-m);
      }
    else if((m-n)>0){
      destructElms(&at(n),m-n);
      if(!resize(n,sizeof(EType))) return false;
      }
    return true;
    }

  /// Assign from another array
  FXArray<EType>& operator=(const FXArray<EType>& src){
    if(data()!=src.data() && no(src.no())){ copyElms(data(),src.data(),src.no()); }
    return *this;
    }

  /// Return pointer to array
  EType* data(){ return reinterpret_cast<EType*>(ptr); }
  const EType* data() const { return reinterpret_cast<const EType*>(ptr); }

  /// Index into array
  EType& operator[](FXival i){ return data()[i]; }
  const EType& operator[](FXival i) const { return data()[i]; }

  /// Index into array
  EType& at(FXival i){ return data()[i]; }
  const EType& at(FXival i) const { return data()[i]; }

  /// First element in array
  EType& head(){ return data()[0]; }
  const EType& head() const { return data()[0]; }

  /// Last element in array
  EType& tail(){ return data()[no()-1]; }
  const EType& tail() const { return data()[no()-1]; }

  /// Adopt array from another one; the other array becomes empty
  FXArray<EType>& adopt(FXArray<EType>& src){
    if(ptr!=src.ptr && no(0)){ swap(ptr,src.ptr); }
    return *this;
    }

  /// Assign object p to array
  FXbool assign(const EType& src){
    if(no(1)){ head()=src; return true; }
    return false;
    }

  /// Assign n copies of object to array
  FXbool assign(const EType& src,FXival n){
    if(no(n)){ fillElms(data(),src,n); return true; }
    return false;
    }

  /// Assign n objects to list
  FXbool assign(const EType* src,FXival n){
    if(no(n)){ copyElms(data(),src,n); return true; }
    return false;
    }

  /// Assign n objects to list
  FXbool assign(const FXArray<EType>& src){
    return assign(src.data(),src.no());
    }

  /// Insert an object
  FXbool insert(FXival pos,const EType& src){
    if(no(no()+1)){ moveElms(data()+pos+1,data()+pos,no()-pos-1); at(pos)=src; return true; }
    return false;
    }

  /// Insert n copies of object at specified position
  FXbool insert(FXival pos,const EType& src,FXival n){
    if(no(no()+n)){ moveElms(data()+pos+n,data()+pos,no()-pos-n); fillElms(data()+pos,src,n); return true; }
    return false;
    }

  /// Insert n objects at specified position
  FXbool insert(FXival pos,const EType* src,FXival n){
    if(no(no()+n)){ moveElms(data()+pos+n,data()+pos,no()-pos-n); copyElms(data()+pos,src,n); return true; }
    return false;
    }

  /// Insert n objects at specified position
  FXbool insert(FXival pos,const FXArray<EType>& src){
    return insert(pos,src.data(),src.no());
    }

  /// Prepend object
  FXbool prepend(const EType& src){
    if(no(no()+1)){ moveElms(data()+1,data(),no()-1); head()=src; return true; }
    return false;
    }

  /// Prepend n copies of object
  FXbool prepend(const EType& src,FXival n){
    if(no(no()+n)){ moveElms(data()+n,data(),no()-n); fillElms(data(),src,n); return true; }
    return false;
    }

  /// Prepend n objects
  FXbool prepend(const EType* src,FXival n){
    if(no(no()+n)){ moveElms(data()+n,data(),no()-n); copyElms(data(),src,n); return true; }
    return false;
    }

  /// Prepend n objects
  FXbool prepend(const FXArray<EType>& src){
    return prepend(src.data(),src.no());
    }

  /// Append object
  FXbool append(const EType& src){
    if(no(no()+1)){ tail()=src; return true; }
    return false;
    }

  /// Append n copies of object
  FXbool append(const EType& src,FXival n){
    if(no(no()+n)){ fillElms(data()+no()-n,src,n); return true; }
    return false;
    }

  /// Append n objects
  FXbool append(const EType* src,FXival n){
    if(no(no()+n)){ copyElms(data()+no()-n,src,n); return true; }
    return false;
    }

  /// Append n objects
  FXbool append(const FXArray<EType>& src){
    return append(src.data(),src.no());
    }

  /// Replace an object by other object
  FXbool replace(FXival pos,const EType& src){
    at(pos)=src;
    return true;
    }

  /// Replace the m objects at pos with n copies of other object
  FXbool replace(FXival pos,FXival m,const EType& src,FXival n){
    if(m<n){
      if(!no(no()-m+n)) return false;
      moveElms(data()+pos+n,data()+pos+m,no()-pos-n);
      }
    else if(m>n){
      moveElms(data()+pos+n,data()+pos+m,no()-pos-m);
      if(!no(no()-m+n)) return false;
      }
    fillElms(data()+pos,src,n);
    return true;
    }

  /// Replace m objects at pos by n other objects
  FXbool replace(FXival pos,FXival m,const EType* src,FXival n){
    if(m<n){
      if(!no(no()-m+n)) return false;
      moveElms(data()+pos+n,data()+pos+m,no()-pos-n);
      }
    else if(m>n){
      moveElms(data()+pos+n,data()+pos+m,no()-pos-m);
      if(!no(no()-m+n)) return false;
      }
    copyElms(data()+pos,src,n);
    return true;
    }

  /// Replace m objects at pos by other objects
  FXbool replace(FXival pos,FXival m,const FXArray<EType>& src){
    return replace(pos,m,src.data(),src.no());
    }

  /// Remove object at pos
  FXbool erase(FXival pos){
    moveElms(data()+pos,data()+pos+1,no()-pos-1);
    return no(no()-1);
    }

  /// Remove n objects starting at pos
  FXbool erase(FXival pos,FXival n){
    moveElms(data()+pos,data()+pos+n,no()-pos-n);
    return no(no()-n);
    }

  /// Push object to end
  FXbool push(const EType& src){
    if(no(no()+1)){ tail()=src; return true; }
    return false;
    }

  /// Pop object from end
  FXbool pop(){
    return no(no()-1);
    }

  /// Remove all objects
  FXbool clear(){
    return no(0);
    }

  /// Delete data
 ~FXArray(){
    clear();
    }
  };

}

#endif