Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXArray.h
1 /********************************************************************************
2 * *
3 * G e n e r i c A r r a y *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1997,2024 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXARRAY_H
22 #define FXARRAY_H
23 
24 #ifndef FXELEMENT_H
25 #include "FXElement.h"
26 #endif
27 
28 namespace FX {
29 
30 
32 class FXAPI FXArrayBase {
33 protected:
34  FXptr ptr;
35 protected:
36  FXArrayBase();
37  FXbool resize(FXival num,FXival sz);
38  ~FXArrayBase();
39  };
40 
41 
43 template<typename EType>
44 class FXArray : public FXArrayBase {
45 public:
46 
48  FXArray(){ }
49 
51  FXArray(FXival n){ no(n); }
52 
54  FXArray(const FXArray<EType>& src){
55  if(no(src.no())){ copyElms(data(),src.data(),src.no()); }
56  }
57 
59  FXArray(const EType& src,FXival n){
60  if(no(n)){ fillElms(data(),src,n); }
61  }
62 
64  FXArray(const EType* src,FXival n){
65  if(no(n)){ copyElms(data(),src,n); }
66  }
67 
69  FXival no() const { return *(((FXival*)ptr)-1); }
70 
72  FXbool no(FXival n){
73  FXival m=no();
74  if((n-m)>0){
75  if(!resize(n,sizeof(EType))) return false;
76  constructElms(&at(m),n-m);
77  }
78  else if((m-n)>0){
79  destructElms(&at(n),m-n);
80  if(!resize(n,sizeof(EType))) return false;
81  }
82  return true;
83  }
84 
87  if(data()!=src.data() && no(src.no())){ copyElms(data(),src.data(),src.no()); }
88  return *this;
89  }
90 
92  EType* data(){ return reinterpret_cast<EType*>(ptr); }
93  const EType* data() const { return reinterpret_cast<const EType*>(ptr); }
94 
96  EType& operator[](FXival i){ return data()[i]; }
97  const EType& operator[](FXival i) const { return data()[i]; }
98 
100  EType& at(FXival i){ return data()[i]; }
101  const EType& at(FXival i) const { return data()[i]; }
102 
104  EType& head(){ return data()[0]; }
105  const EType& head() const { return data()[0]; }
106 
108  EType& tail(){ return data()[no()-1]; }
109  const EType& tail() const { return data()[no()-1]; }
110 
113  if(ptr!=src.ptr && no(0)){ swap(ptr,src.ptr); }
114  return *this;
115  }
116 
118  FXbool assign(const EType& src){
119  if(no(1)){ head()=src; return true; }
120  return false;
121  }
122 
124  FXbool assign(const EType& src,FXival n){
125  if(no(n)){ fillElms(data(),src,n); return true; }
126  return false;
127  }
128 
130  FXbool assign(const EType* src,FXival n){
131  if(no(n)){ copyElms(data(),src,n); return true; }
132  return false;
133  }
134 
136  FXbool assign(const FXArray<EType>& src){
137  return assign(src.data(),src.no());
138  }
139 
141  FXbool insert(FXival pos,const EType& src){
142  if(no(no()+1)){ moveElms(data()+pos+1,data()+pos,no()-pos-1); at(pos)=src; return true; }
143  return false;
144  }
145 
147  FXbool insert(FXival pos,const EType& src,FXival n){
148  if(no(no()+n)){ moveElms(data()+pos+n,data()+pos,no()-pos-n); fillElms(data()+pos,src,n); return true; }
149  return false;
150  }
151 
153  FXbool insert(FXival pos,const EType* src,FXival n){
154  if(no(no()+n)){ moveElms(data()+pos+n,data()+pos,no()-pos-n); copyElms(data()+pos,src,n); return true; }
155  return false;
156  }
157 
159  FXbool insert(FXival pos,const FXArray<EType>& src){
160  return insert(pos,src.data(),src.no());
161  }
162 
164  FXbool prepend(const EType& src){
165  if(no(no()+1)){ moveElms(data()+1,data(),no()-1); head()=src; return true; }
166  return false;
167  }
168 
170  FXbool prepend(const EType& src,FXival n){
171  if(no(no()+n)){ moveElms(data()+n,data(),no()-n); fillElms(data(),src,n); return true; }
172  return false;
173  }
174 
176  FXbool prepend(const EType* src,FXival n){
177  if(no(no()+n)){ moveElms(data()+n,data(),no()-n); copyElms(data(),src,n); return true; }
178  return false;
179  }
180 
182  FXbool prepend(const FXArray<EType>& src){
183  return prepend(src.data(),src.no());
184  }
185 
187  FXbool append(const EType& src){
188  if(no(no()+1)){ tail()=src; return true; }
189  return false;
190  }
191 
193  FXbool append(const EType& src,FXival n){
194  if(no(no()+n)){ fillElms(data()+no()-n,src,n); return true; }
195  return false;
196  }
197 
199  FXbool append(const EType* src,FXival n){
200  if(no(no()+n)){ copyElms(data()+no()-n,src,n); return true; }
201  return false;
202  }
203 
205  FXbool append(const FXArray<EType>& src){
206  return append(src.data(),src.no());
207  }
208 
210  FXbool replace(FXival pos,const EType& src){
211  at(pos)=src;
212  return true;
213  }
214 
216  FXbool replace(FXival pos,FXival m,const EType& src,FXival n){
217  if(m<n){
218  if(!no(no()-m+n)) return false;
219  moveElms(data()+pos+n,data()+pos+m,no()-pos-n);
220  }
221  else if(m>n){
222  moveElms(data()+pos+n,data()+pos+m,no()-pos-m);
223  if(!no(no()-m+n)) return false;
224  }
225  fillElms(data()+pos,src,n);
226  return true;
227  }
228 
230  FXbool replace(FXival pos,FXival m,const EType* src,FXival n){
231  if(m<n){
232  if(!no(no()-m+n)) return false;
233  moveElms(data()+pos+n,data()+pos+m,no()-pos-n);
234  }
235  else if(m>n){
236  moveElms(data()+pos+n,data()+pos+m,no()-pos-m);
237  if(!no(no()-m+n)) return false;
238  }
239  copyElms(data()+pos,src,n);
240  return true;
241  }
242 
244  FXbool replace(FXival pos,FXival m,const FXArray<EType>& src){
245  return replace(pos,m,src.data(),src.no());
246  }
247 
249  FXbool erase(FXival pos){
250  moveElms(data()+pos,data()+pos+1,no()-pos-1);
251  return no(no()-1);
252  }
253 
255  FXbool erase(FXival pos,FXival n){
256  moveElms(data()+pos,data()+pos+n,no()-pos-n);
257  return no(no()-n);
258  }
259 
261  FXbool push(const EType& src){
262  if(no(no()+1)){ tail()=src; return true; }
263  return false;
264  }
265 
267  FXbool pop(){
268  return no(no()-1);
269  }
270 
272  FXbool clear(){
273  return no(0);
274  }
275 
278  clear();
279  }
280  };
281 
282 }
283 
284 #endif
FXbool assign(const EType &src)
Assign object p to array.
Definition: FXArray.h:118
EType & operator[](FXival i)
Index into array.
Definition: FXArray.h:96
FXbool prepend(const FXArray< EType > &src)
Prepend n objects.
Definition: FXArray.h:182
FXArray< EType > & operator=(const FXArray< EType > &src)
Assign from another array.
Definition: FXArray.h:86
FXbool prepend(const EType &src)
Prepend object.
Definition: FXArray.h:164
FXbool replace(FXival pos, const EType &src)
Replace an object by other object.
Definition: FXArray.h:210
FXbool append(const FXArray< EType > &src)
Append n objects.
Definition: FXArray.h:205
FXbool append(const EType &src, FXival n)
Append n copies of object.
Definition: FXArray.h:193
FXbool pop()
Pop object from end.
Definition: FXArray.h:267
FXbool prepend(const EType *src, FXival n)
Prepend n objects.
Definition: FXArray.h:176
FXArray< EType > & adopt(FXArray< EType > &src)
Adopt array from another one; the other array becomes empty.
Definition: FXArray.h:112
FXbool insert(FXival pos, const EType *src, FXival n)
Insert n objects at specified position.
Definition: FXArray.h:153
Array of some generic type.
Definition: FXArray.h:44
FXArray(FXival n)
Allocate array of n elements.
Definition: FXArray.h:51
FXArray(const FXArray< EType > &src)
Allocate array copied from another.
Definition: FXArray.h:54
FXArray()
Allocate initially empty array.
Definition: FXArray.h:48
FXbool assign(const EType *src, FXival n)
Assign n objects to list.
Definition: FXArray.h:130
FXbool clear()
Remove all objects.
Definition: FXArray.h:272
FXbool no(FXival n)
Change number of elements in array to n.
Definition: FXArray.h:72
FXbool erase(FXival pos, FXival n)
Remove n objects starting at pos.
Definition: FXArray.h:255
FXbool insert(FXival pos, const EType &src)
Insert an object.
Definition: FXArray.h:141
FXbool push(const EType &src)
Push object to end.
Definition: FXArray.h:261
Definition: FX4Splitter.h:28
EType & at(FXival i)
Index into array.
Definition: FXArray.h:100
FXbool replace(FXival pos, FXival m, const FXArray< EType > &src)
Replace m objects at pos by other objects.
Definition: FXArray.h:244
FXbool replace(FXival pos, FXival m, const EType &src, FXival n)
Replace the m objects at pos with n copies of other object.
Definition: FXArray.h:216
FXbool insert(FXival pos, const EType &src, FXival n)
Insert n copies of object at specified position.
Definition: FXArray.h:147
FXbool replace(FXival pos, FXival m, const EType *src, FXival n)
Replace m objects at pos by n other objects.
Definition: FXArray.h:230
FXbool assign(const FXArray< EType > &src)
Assign n objects to list.
Definition: FXArray.h:136
FXbool append(const EType *src, FXival n)
Append n objects.
Definition: FXArray.h:199
EType & tail()
Last element in array.
Definition: FXArray.h:108
EType & head()
First element in array.
Definition: FXArray.h:104
FXbool assign(const EType &src, FXival n)
Assign n copies of object to array.
Definition: FXArray.h:124
FXArray(const EType *src, FXival n)
Allocate initialized with array of n objects.
Definition: FXArray.h:64
FXbool insert(FXival pos, const FXArray< EType > &src)
Insert n objects at specified position.
Definition: FXArray.h:159
FXArray(const EType &src, FXival n)
Allocate initialized with n copies of object.
Definition: FXArray.h:59
ArrayBase manages a memory buffer.
Definition: FXArray.h:32
FXbool append(const EType &src)
Append object.
Definition: FXArray.h:187
FXbool prepend(const EType &src, FXival n)
Prepend n copies of object.
Definition: FXArray.h:170
FXbool erase(FXival pos)
Remove object at pos.
Definition: FXArray.h:249
~FXArray()
Delete data.
Definition: FXArray.h:277
FXival no() const
Return number of items.
Definition: FXArray.h:69
EType * data()
Return pointer to array.
Definition: FXArray.h:92

Copyright © 1997-2022 Jeroen van der Zijp