Newer
Older
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
/********************************************************************************
* *
* G e n e r i c A r r a y *
* *
*********************************************************************************
* Copyright (C) 1997,2005 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 2.1 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 library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
*********************************************************************************
* $Id: FXArray.h 2335 2006-01-28 02:33:03Z lyle $ *
********************************************************************************/
#ifndef FXARRAY_H
#define FXARRAY_H
#ifndef FXELEMENT_H
#include "FXElement.h"
#endif
namespace FX {
/// Array of some generic type
template<class TYPE>
class FXArray {
protected:
TYPE *ptr; // Data array
FXint num; // Number in array
public:
/// Create as empty
FXArray():ptr(NULL),num(0){
}
/// Create with given size n
FXArray(FXint n):ptr(NULL),num(0){
if(allocElms(ptr,n)){ constructElms(ptr,n); num=n; }
}
/// Create initialized from another array
FXArray(const FXArray<TYPE>& src):ptr(NULL),num(0){
if(allocElms(ptr,src.num)){ constructElms(ptr,src.num); copyElms(ptr,src.ptr,src.num); num=src.num; }
}
/// Create initialized with n copies of object
FXArray(const TYPE& src,FXint n):ptr(NULL),num(0){
if(allocElms(ptr,n)){ constructElms(ptr,n); fillElms(ptr,src,n); num=n; }
}
/// Create initialized with array of n objects
FXArray(const TYPE* src,FXint n):ptr(NULL),num(0){
if(allocElms(ptr,n)){ constructElms(ptr,n); copyElms(ptr,src,n); num=n; }
}
/// Return number of elements
FXint no() const { return num; }
/// Change number of elements to n
bool no(FXint n){
if(n!=num){
if(n<num){
destructElms(ptr+n,num-n);
if(!resizeElms(ptr,n)) return false;
}
else{
if(!resizeElms(ptr,n)) return false;
constructElms(ptr+num,n-num);
}
num=n;
}
return true;
}
/// Assign from another list
FXArray<TYPE>& operator=(const FXArray<TYPE>& src){
if(ptr!=src.ptr){ no(src.num); copyElms(ptr,src.ptr,src.num); }
return *this;
}
/// Index into array
TYPE& operator[](FXint i){ return ptr[i]; }
const TYPE& operator[](FXint i) const { return ptr[i]; }
/// Index into list
TYPE& at(FXint i){ return ptr[i]; }
const TYPE& at(FXint i) const { return ptr[i]; }
/// Return pointer to list
TYPE* data() const { return ptr; }
/// Adopt array from source
FXArray<TYPE>& adopt(FXArray<TYPE>& src){
no(0);
ptr=src.ptr; src.ptr=NULL;
num=src.num; src.num=0;
return *this;
}
/// Assign object p to list
FXArray<TYPE>& assign(const TYPE& src){
if(no(1)){ ptr[0]=src; }
return *this;
}
/// Assign n copies of object to list
FXArray<TYPE>& assign(const TYPE& src,FXint n){
if(no(n)){ fillElms(ptr,src,n); }
return *this;
}
/// Assign n objects to list
FXArray<TYPE>& assign(const TYPE* src,FXint n){
if(no(n)){ copyElms(ptr,src,n); }
return *this;
}
/// Assign n objects to list
FXArray<TYPE>& assign(const FXArray<TYPE>& src){
if(no(src.num)){ copyElms(ptr,src.ptr,src.num); }
return *this;
}
/// Insert an object
FXArray<TYPE>& insert(FXint pos,const TYPE& src){
if(no(num+1)){ moveElms(ptr+pos+1,ptr+pos,num-pos-1); ptr[pos]=src; }
return *this;
}
/// Insert n copies of object at specified position
FXArray<TYPE>& insert(FXint pos,const TYPE& src,FXint n){
if(no(num+n)){ moveElms(ptr+pos+n,ptr+pos,num-pos-n); fillElms(ptr+pos,src,n); }
return *this;
}
/// Insert n objects at specified position
FXArray<TYPE>& insert(FXint pos,const TYPE* src,FXint n){
if(no(num+n)){ moveElms(ptr+pos+n,ptr+pos,num-pos-n); copyElms(ptr+pos,src,n); }
return *this;
}
/// Insert n objects at specified position
FXArray<TYPE>& insert(FXint pos,const FXArray<TYPE>& src){
if(no(num+src.num)){ moveElms(ptr+pos+src.num,ptr+pos,num-pos-src.num); copyElms(ptr+pos,src.ptr,src.num); }
return *this;
}
/// Prepend object
FXArray<TYPE>& prepend(const TYPE& src){
if(no(num+1)){ moveElms(ptr+1,ptr,num-1); ptr[0]=src; }
return *this;
}
/// Prepend n copies of object
FXArray<TYPE>& prepend(const TYPE& src,FXint n){
if(no(num+n)){ moveElms(ptr+n,ptr,num-n); fillElms(ptr,src,n); }
return *this;
}
/// Prepend n objects
FXArray<TYPE>& prepend(const TYPE* src,FXint n){
if(no(num+n)){ moveElms(ptr+n,ptr,num-n); copyElms(ptr,src,n); }
return *this;
}
/// Prepend n objects
FXArray<TYPE>& prepend(const FXArray<TYPE>& src){
if(no(num+src.num)){ moveElms(ptr+src.num,ptr,num-src.num); copyElms(ptr,src.ptr,src.num); }
return *this;
}
/// Append object
FXArray<TYPE>& append(const TYPE& src){
if(no(num+1)){ ptr[num-1]=src; }
return *this;
}
/// Append n copies of object
FXArray<TYPE>& append(const TYPE& src,FXint n){
if(no(num+n)){ fillElms(ptr+num-n,src,n); }
return *this;
}
/// Append n objects
FXArray<TYPE>& append(const TYPE* src,FXint n){
if(no(num+n)){ copyElms(ptr+num-n,src,n); }
return *this;
}
/// Append n objects
FXArray<TYPE>& append(const FXArray<TYPE>& src){
if(no(num+src.num)){ copyElms(ptr+num-src.num,src.ptr,src.num); }
return *this;
}
/// Remove object at pos
FXArray<TYPE>& erase(FXint pos){
moveElms(ptr+pos,ptr+pos+1,num-pos-1); no(num-1);
return *this;
}
/// Remove n objects starting at pos
FXArray<TYPE>& erase(FXint pos,FXint n){
moveElms(ptr+pos,ptr+pos+n,num-n-pos); no(num-n);
return *this;
}
/// Remove all objects
FXArray<TYPE>& clear(){
destructElms(ptr,num); freeElms(ptr); num=0;
return *this;
}
/// Delete data
~FXArray(){
destructElms(ptr,num); freeElms(ptr);
}
};
}
#endif