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

FXArray.h
Go to the documentation of this file.
1 /********************************************************************************
2 * *
3 * G e n e r i c A r r a y *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1997,2006 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Lesser General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2.1 of the License, or (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 GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this library; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
21 *********************************************************************************
22 * $Id: FXArray.h,v 1.24.2.1 2008/03/25 20:18:43 fox Exp $ *
23 ********************************************************************************/
24 #ifndef FXARRAY_H
25 #define FXARRAY_H
26 
27 #ifndef FXELEMENT_H
28 #include "FXElement.h"
29 #endif
30 
31 namespace FX {
32 
33 
34 /// Array of some generic type
35 template<class TYPE>
36 class FXArray {
37 protected:
38  TYPE *ptr; // Data array
39  FXint num; // Number in array
40 public:
41 
42  /// Create as empty
43  FXArray():ptr(NULL),num(0){
44  }
45 
46  /// Create with given size n
47  FXArray(FXint n):ptr(NULL),num(0){
48  if(allocElms(ptr,n)){ constructElms(ptr,n); num=n; }
49  }
50 
51  /// Create initialized from another array
52  FXArray(const FXArray<TYPE>& src):ptr(NULL),num(0){
53  if(allocElms(ptr,src.num)){ constructElms(ptr,src.num); copyElms(ptr,src.ptr,src.num); num=src.num; }
54  }
55 
56  /// Create initialized with n copies of object
57  FXArray(const TYPE& src,FXint n):ptr(NULL),num(0){
58  if(allocElms(ptr,n)){ constructElms(ptr,n); fillElms(ptr,src,n); num=n; }
59  }
60 
61  /// Create initialized with array of n objects
62  FXArray(const TYPE* src,FXint n):ptr(NULL),num(0){
63  if(allocElms(ptr,n)){ constructElms(ptr,n); copyElms(ptr,src,n); num=n; }
64  }
65 
66  /// Return number of elements
67  FXint no() const { return num; }
68 
69  /// Change number of elements to n
70  bool no(FXint n){
71  if(n!=num){
72  if(0<num-n){
73  destructElms(ptr+n,num-n);
74  if(!resizeElms(ptr,n)) return false;
75  }
76  else{
77  if(!resizeElms(ptr,n)) return false;
78  constructElms(ptr+num,n-num);
79  }
80  num=n;
81  }
82  return true;
83  }
84 
85  /// Assign from another list
87  if(ptr!=src.ptr){ no(src.num); copyElms(ptr,src.ptr,src.num); }
88  return *this;
89  }
90 
91  /// Index into array
92  TYPE& operator[](FXint i){ return ptr[i]; }
93  const TYPE& operator[](FXint i) const { return ptr[i]; }
94 
95  /// Index into list
96  TYPE& at(FXint i){ return ptr[i]; }
97  const TYPE& at(FXint i) const { return ptr[i]; }
98 
99  /// Return pointer to list
100  TYPE* data() const { return ptr; }
101 
102  /// Adopt array from source
104  no(0);
105  ptr=src.ptr; src.ptr=NULL;
106  num=src.num; src.num=0;
107  return *this;
108  }
109 
110  /// Assign object p to list
111  FXArray<TYPE>& assign(const TYPE& src){
112  if(no(1)){ ptr[0]=src; }
113  return *this;
114  }
115 
116  /// Assign n copies of object to list
117  FXArray<TYPE>& assign(const TYPE& src,FXint n){
118  if(no(n)){ fillElms(ptr,src,n); }
119  return *this;
120  }
121 
122  /// Assign n objects to list
123  FXArray<TYPE>& assign(const TYPE* src,FXint n){
124  if(no(n)){ copyElms(ptr,src,n); }
125  return *this;
126  }
127 
128  /// Assign n objects to list
129  FXArray<TYPE>& assign(const FXArray<TYPE>& src){
130  if(no(src.num)){ copyElms(ptr,src.ptr,src.num); }
131  return *this;
132  }
133 
134  /// Insert an object
135  FXArray<TYPE>& insert(FXint pos,const TYPE& src){
136  if(no(num+1)){ moveElms(ptr+pos+1,ptr+pos,num-pos-1); ptr[pos]=src; }
137  return *this;
138  }
139 
140  /// Insert n copies of object at specified position
141  FXArray<TYPE>& insert(FXint pos,const TYPE& src,FXint n){
142  if(no(num+n)){ moveElms(ptr+pos+n,ptr+pos,num-pos-n); fillElms(ptr+pos,src,n); }
143  return *this;
144  }
145 
146  /// Insert n objects at specified position
147  FXArray<TYPE>& insert(FXint pos,const TYPE* src,FXint n){
148  if(no(num+n)){ moveElms(ptr+pos+n,ptr+pos,num-pos-n); copyElms(ptr+pos,src,n); }
149  return *this;
150  }
151 
152  /// Insert n objects at specified position
153  FXArray<TYPE>& insert(FXint pos,const FXArray<TYPE>& src){
154  if(no(num+src.num)){ moveElms(ptr+pos+src.num,ptr+pos,num-pos-src.num); copyElms(ptr+pos,src.ptr,src.num); }
155  return *this;
156  }
157 
158  /// Prepend object
159  FXArray<TYPE>& prepend(const TYPE& src){
160  if(no(num+1)){ moveElms(ptr+1,ptr,num-1); ptr[0]=src; }
161  return *this;
162  }
163 
164  /// Prepend n copies of object
165  FXArray<TYPE>& prepend(const TYPE& src,FXint n){
166  if(no(num+n)){ moveElms(ptr+n,ptr,num-n); fillElms(ptr,src,n); }
167  return *this;
168  }
169 
170  /// Prepend n objects
171  FXArray<TYPE>& prepend(const TYPE* src,FXint n){
172  if(no(num+n)){ moveElms(ptr+n,ptr,num-n); copyElms(ptr,src,n); }
173  return *this;
174  }
175 
176  /// Prepend n objects
177  FXArray<TYPE>& prepend(const FXArray<TYPE>& src){
178  if(no(num+src.num)){ moveElms(ptr+src.num,ptr,num-src.num); copyElms(ptr,src.ptr,src.num); }
179  return *this;
180  }
181 
182  /// Append object
183  FXArray<TYPE>& append(const TYPE& src){
184  if(no(num+1)){ ptr[num-1]=src; }
185  return *this;
186  }
187 
188  /// Append n copies of object
189  FXArray<TYPE>& append(const TYPE& src,FXint n){
190  if(no(num+n)){ fillElms(ptr+num-n,src,n); }
191  return *this;
192  }
193 
194  /// Append n objects
195  FXArray<TYPE>& append(const TYPE* src,FXint n){
196  if(no(num+n)){ copyElms(ptr+num-n,src,n); }
197  return *this;
198  }
199 
200  /// Append n objects
201  FXArray<TYPE>& append(const FXArray<TYPE>& src){
202  if(no(num+src.num)){ copyElms(ptr+num-src.num,src.ptr,src.num); }
203  return *this;
204  }
205 
206  /// Remove object at pos
207  FXArray<TYPE>& erase(FXint pos){
208  moveElms(ptr+pos,ptr+pos+1,num-pos-1); no(num-1);
209  return *this;
210  }
211 
212  /// Remove n objects starting at pos
213  FXArray<TYPE>& erase(FXint pos,FXint n){
214  moveElms(ptr+pos,ptr+pos+n,num-n-pos); no(num-n);
215  return *this;
216  }
217 
218  /// Remove all objects
219  FXArray<TYPE>& clear(){
220  destructElms(ptr,num); freeElms(ptr); num=0;
221  return *this;
222  }
223 
224  /// Delete data
225  ~FXArray(){
226  destructElms(ptr,num); freeElms(ptr);
227  }
228  };
229 
230 }
231 
232 #endif
void fillElms(TYPE *dst, const TYPE &src, unsigned long n)
Fill array of elements with given element.
Definition: FXElement.h:71
FXArray< TYPE > & assign(const TYPE &src)
Assign object p to list.
Definition: FXArray.h:108
FXArray< TYPE > & adopt(FXArray< TYPE > &src)
Adopt array from source.
Definition: FXArray.h:100
FXArray< TYPE > & prepend(const TYPE &src)
Prepend object.
Definition: FXArray.h:156
FXArray< TYPE > & insert(FXint pos, const TYPE &src)
Insert an object.
Definition: FXArray.h:132
void destructElms(TYPE *ptr, unsigned long n)
Destruct some elements at a location.
Definition: FXElement.h:43
TYPE & at(FXint i)
Index into list.
Definition: FXArray.h:93
Array of some generic type.
Definition: FXArray.h:36
#define NULL
Definition: fxdefs.h:41
FXint no() const
Return number of elements.
Definition: FXArray.h:64
void constructElms(TYPE *ptr, unsigned long n)
Construct some elements at a location.
Definition: FXElement.h:36
void freeElms(TYPE *&ptr)
Free array of elements, without destruction.
Definition: FXElement.h:127
FXint resizeElms(TYPE *&ptr, unsigned long n)
Resize array of elements, without constructor or destructor.
Definition: FXElement.h:120
TYPE & operator[](FXint i)
Index into array.
Definition: FXArray.h:89
Definition: FX4Splitter.h:31
int FXint
Definition: fxdefs.h:397
FXArray< TYPE > & clear()
Remove all objects.
Definition: FXArray.h:216
FXArray< TYPE > & operator=(const FXArray< TYPE > &src)
Assign from another list.
Definition: FXArray.h:83
FXint allocElms(TYPE *&ptr, unsigned long n)
Allocate array of elements, uninitialized.
Definition: FXElement.h:99
bool no(FXint n)
Change number of elements to n.
Definition: FXArray.h:67
void copyElms(TYPE *dst, const TYPE *src, unsigned long n)
Copy some elements from one place to another.
Definition: FXElement.h:50
FXArray()
Create as empty.
Definition: FXArray.h:40
~FXArray()
Delete data.
Definition: FXArray.h:222
TYPE * data() const
Return pointer to list.
Definition: FXArray.h:97
FXArray< TYPE > & erase(FXint pos)
Remove object at pos.
Definition: FXArray.h:204
void moveElms(TYPE *dst, const TYPE *src, unsigned long n)
Move some elements from overlapping place to another.
Definition: FXElement.h:57
FXArray< TYPE > & append(const TYPE &src)
Append object.
Definition: FXArray.h:180

Copyright © 1997-2005 Jeroen van der Zijp