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

FXArray.h

00001 /******************************************************************************** 00002 * * 00003 * G e n e r i c A r r a y * 00004 * * 00005 ********************************************************************************* 00006 * Copyright (C) 1997,2005 by Jeroen van der Zijp. All Rights Reserved. * 00007 ********************************************************************************* 00008 * This library is free software; you can redistribute it and/or * 00009 * modify it under the terms of the GNU Lesser General Public * 00010 * License as published by the Free Software Foundation; either * 00011 * version 2.1 of the License, or (at your option) any later version. * 00012 * * 00013 * This library is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00016 * Lesser General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU Lesser General Public * 00019 * License along with this library; if not, write to the Free Software * 00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * 00021 ********************************************************************************* 00022 * $Id: FXArray.h,v 1.18 2005/01/16 16:06:06 fox Exp $ * 00023 ********************************************************************************/ 00024 #ifndef FXARRAY_H 00025 #define FXARRAY_H 00026 00027 #ifndef FXELEMENT_H 00028 #include "FXElement.h" 00029 #endif 00030 00031 namespace FX { 00032 00033 00034 /************************* D e f i n i t i o n *******************************/ 00035 00036 00037 template<class TYPE> 00038 class FXArray { 00039 TYPE *list; // List of items 00040 FXint number; // Used slots 00041 FXint total; // Total slots 00042 FXint grow; // Grow amount 00043 public: 00044 FXArray(); 00045 FXArray(const FXArray<TYPE>& orig); 00046 FXArray<TYPE>& operator=(const FXArray<TYPE> &orig); 00047 inline FXint no() const; 00048 inline TYPE* data() const; 00049 inline FXint size() const; 00050 void size(FXint n); 00051 FXint inc() const; 00052 void inc(FXint n); 00053 inline const TYPE& operator [](FXint i) const; 00054 inline TYPE& operator [](FXint i); 00055 void insert(FXint pos,const TYPE& p); 00056 void append(const TYPE& p); 00057 void prepend(const TYPE& p); 00058 void remove(FXint pos); 00059 void extract(const TYPE& p); 00060 FXint find(const TYPE& p,FXint pos=0); 00061 FXint rfind(const TYPE& p,FXint pos=2147483647); 00062 void trunc(); 00063 void clear(); 00064 void save(FXStream& store) const; 00065 void load(FXStream& store); 00066 ~FXArray(); 00067 }; 00068 00069 00070 /********************** I m p l e m e n t a t i o n ************************/ 00071 00072 00073 // Construct as empty 00074 template<class TYPE> 00075 FXArray<TYPE>::FXArray(){ 00076 list=NULL; 00077 number=0; 00078 total=0; 00079 grow=0; 00080 } 00081 00082 00083 // Copy construct 00084 template<class TYPE> 00085 FXArray<TYPE>::FXArray(const FXArray<TYPE>& orig){ 00086 number=orig.number; 00087 total=orig.total; 00088 grow=orig.grow; 00089 allocElms(list,total); 00090 constructElms(list,number); 00091 copyElms(list,orig.list,number); 00092 } 00093 00094 00095 // Return number of elements 00096 template<class TYPE> 00097 FXint FXArray<TYPE>::no() const { 00098 return number; 00099 } 00100 00101 00102 // Return pointer to the list 00103 template<class TYPE> 00104 TYPE* FXArray<TYPE>::data() const { 00105 return list; 00106 } 00107 00108 00109 // Return size of list 00110 template<class TYPE> 00111 FXint FXArray<TYPE>::size() const { 00112 return total; 00113 } 00114 00115 00116 // Return grow delta 00117 template<class TYPE> 00118 FXint FXArray<TYPE>::inc() const { 00119 return grow; 00120 } 00121 00122 00123 // Set grow delta 00124 template<class TYPE> 00125 void FXArray<TYPE>::inc(FXint g){ 00126 FXASSERT(g>=0); 00127 grow=g; 00128 } 00129 00130 00131 // Return element rvalue 00132 template<class TYPE> 00133 const TYPE& FXArray<TYPE>::operator[](FXint i) const { 00134 FXASSERT(0<=i&&i<number); 00135 return list[i]; 00136 } 00137 00138 00139 // Return element lvalue 00140 template<class TYPE> 00141 TYPE& FXArray<TYPE>::operator[](FXint i){ 00142 FXASSERT(0<=i&&i<number); 00143 return list[i]; 00144 } 00145 00146 00147 // Assign an array 00148 template<class TYPE> 00149 FXArray<TYPE>& FXArray<TYPE>::operator=(const FXArray<TYPE>& orig){ 00150 if(this!=&orig){ 00151 destructElms(list,number); 00152 freeElms(list); 00153 number=orig.number; 00154 total=orig.total; 00155 grow=orig.grow; 00156 allocElms(list,total); 00157 constructElms(list,number); 00158 copyElms(list,orig.list,number); 00159 } 00160 return *this; 00161 } 00162 00163 00164 // Set new size 00165 template<class TYPE> 00166 void FXArray<TYPE>::size(FXint n){ 00167 FXASSERT(n>=0); 00168 if(n!=number){ 00169 if(n<number){ 00170 destructElms(&list[n],number-n); // Destruct old elements 00171 if(n==0){ 00172 freeElms(list); 00173 total=0; 00174 } 00175 } 00176 else{ 00177 if(n>total){ 00178 TYPE *ptr; 00179 FXint s=total+grow; 00180 if(grow==0) s=total+total; 00181 if(s<n) s=n; 00182 allocElms(ptr,s); 00183 constructElms(ptr,n); // Construct blank elements 00184 copyElms(ptr,list,number); // Uses assignment operator 00185 destructElms(list,number); // Destruct old ones 00186 freeElms(list); 00187 list=ptr; 00188 total=s; 00189 } 00190 FXASSERT(n<=total); 00191 constructElms(&list[number],n-number); // Construct new elements 00192 } 00193 number=n; 00194 } 00195 } 00196 00197 00198 // Insert element anywhere 00199 template<class TYPE> 00200 void FXArray<TYPE>::insert(FXint pos,const TYPE& p){ 00201 FXint s=number-pos; 00202 size(number+1); 00203 moveElms(&list[pos+1],&list[pos],s); 00204 list[pos]=p; 00205 } 00206 00207 00208 // Append element at end 00209 template<class TYPE> 00210 void FXArray<TYPE>::append(const TYPE& p){ 00211 FXint s=number; 00212 size(number+1); 00213 list[s]=p; 00214 } 00215 00216 00217 // Prepend element at begin 00218 template<class TYPE> 00219 void FXArray<TYPE>::prepend(const TYPE& p){ 00220 FXint s=number; 00221 size(number+1); 00222 moveElms(&list[1],&list[0],s); 00223 list[0]=p; 00224 } 00225 00226 00227 // Remove element at pos 00228 template<class TYPE> 00229 void FXArray<TYPE>::remove(FXint pos){ 00230 FXint s=number; 00231 moveElms(&list[pos],&list[pos+1],s-pos-1); // Uses assignment operator 00232 size(s-1); 00233 } 00234 00235 00236 // Find element, -1 if not found 00237 template<class TYPE> 00238 FXint FXArray<TYPE>::find(const TYPE& p,FXint pos){ 00239 if(pos<0) pos=0; 00240 while(pos<number){ if(list[pos]==p){ return pos; } ++pos; } 00241 return -1; 00242 } 00243 00244 00245 // Find element, -1 if not found 00246 template<class TYPE> 00247 FXint FXArray<TYPE>::rfind(const TYPE& p,FXint pos){ 00248 if(pos>=number) pos=number-1; 00249 while(0<=pos){ if(list[pos]==p){ return pos; } --pos; } 00250 return -1; 00251 } 00252 00253 00254 // Extract element 00255 template<class TYPE> 00256 void FXArray<TYPE>::extract(const TYPE& p){ 00257 FXint s=number; 00258 while(s-- != 0){ 00259 if(list[s]==p){ 00260 moveElms(&list[s],&list[s+1],number-s); // Uses assignment operator 00261 size(number-1); 00262 break; 00263 } 00264 } 00265 } 00266 00267 00268 // Trunc excess 00269 template<class TYPE> 00270 void FXArray<TYPE>::trunc(){ 00271 size(number); 00272 } 00273 00274 00275 // Clear the list 00276 template<class TYPE> 00277 void FXArray<TYPE>::clear(){ 00278 size(0); 00279 } 00280 00281 00282 // Save operator 00283 template<class TYPE> 00284 void FXArray<TYPE>::save(FXStream& store) const { 00285 store << number << total << grow; 00286 saveElms(store,list,number); // Uses save operator 00287 } 00288 00289 00290 // Load operator 00291 template<class TYPE> 00292 void FXArray<TYPE>::load(FXStream& store){ 00293 destructElms(list,number); // Destruct elements 00294 freeElms(list); 00295 store >> number >> total >> grow; 00296 allocElms(list,total); 00297 constructElms(list,number); // Fresh elements 00298 loadElms(store,list,number); // Uses load operator 00299 } 00300 00301 00302 // Destruct list 00303 template<class TYPE> 00304 FXArray<TYPE>::~FXArray(){ 00305 destructElms(list,number); 00306 freeElms(list); 00307 } 00308 00309 } 00310 00311 #endif

Copyright © 1997-2005 Jeroen van der Zijp