Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members
|
00001 /******************************************************************************** 00002 * * 00003 * G e n e r i c A r r a y * 00004 * * 00005 ********************************************************************************* 00006 * Copyright (C) 1997,2006 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.24 2006/01/22 17:57:58 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 /// Array of some generic type 00035 template<class TYPE> 00036 class FXArray { 00037 protected: 00038 TYPE *ptr; // Data array 00039 FXint num; // Number in array 00040 public: 00041 00042 /// Create as empty 00043 FXArray():ptr(NULL),num(0){ 00044 } 00045 00046 /// Create with given size n 00047 FXArray(FXint n):ptr(NULL),num(0){ 00048 if(allocElms(ptr,n)){ constructElms(ptr,n); num=n; } 00049 } 00050 00051 /// Create initialized from another array 00052 FXArray(const FXArray<TYPE>& src):ptr(NULL),num(0){ 00053 if(allocElms(ptr,src.num)){ constructElms(ptr,src.num); copyElms(ptr,src.ptr,src.num); num=src.num; } 00054 } 00055 00056 /// Create initialized with n copies of object 00057 FXArray(const TYPE& src,FXint n):ptr(NULL),num(0){ 00058 if(allocElms(ptr,n)){ constructElms(ptr,n); fillElms(ptr,src,n); num=n; } 00059 } 00060 00061 /// Create initialized with array of n objects 00062 FXArray(const TYPE* src,FXint n):ptr(NULL),num(0){ 00063 if(allocElms(ptr,n)){ constructElms(ptr,n); copyElms(ptr,src,n); num=n; } 00064 } 00065 00066 /// Return number of elements 00067 FXint no() const { return num; } 00068 00069 /// Change number of elements to n 00070 bool no(FXint n){ 00071 if(n!=num){ 00072 if(n<num){ 00073 destructElms(ptr+n,num-n); 00074 if(!resizeElms(ptr,n)) return false; 00075 } 00076 else{ 00077 if(!resizeElms(ptr,n)) return false; 00078 constructElms(ptr+num,n-num); 00079 } 00080 num=n; 00081 } 00082 return true; 00083 } 00084 00085 /// Assign from another list 00086 FXArray<TYPE>& operator=(const FXArray<TYPE>& src){ 00087 if(ptr!=src.ptr){ no(src.num); copyElms(ptr,src.ptr,src.num); } 00088 return *this; 00089 } 00090 00091 /// Index into array 00092 TYPE& operator[](FXint i){ return ptr[i]; } 00093 const TYPE& operator[](FXint i) const { return ptr[i]; } 00094 00095 /// Index into list 00096 TYPE& at(FXint i){ return ptr[i]; } 00097 const TYPE& at(FXint i) const { return ptr[i]; } 00098 00099 /// Return pointer to list 00100 TYPE* data() const { return ptr; } 00101 00102 /// Adopt array from source 00103 FXArray<TYPE>& adopt(FXArray<TYPE>& src){ 00104 no(0); 00105 ptr=src.ptr; src.ptr=NULL; 00106 num=src.num; src.num=0; 00107 return *this; 00108 } 00109 00110 /// Assign object p to list 00111 FXArray<TYPE>& assign(const TYPE& src){ 00112 if(no(1)){ ptr[0]=src; } 00113 return *this; 00114 } 00115 00116 /// Assign n copies of object to list 00117 FXArray<TYPE>& assign(const TYPE& src,FXint n){ 00118 if(no(n)){ fillElms(ptr,src,n); } 00119 return *this; 00120 } 00121 00122 /// Assign n objects to list 00123 FXArray<TYPE>& assign(const TYPE* src,FXint n){ 00124 if(no(n)){ copyElms(ptr,src,n); } 00125 return *this; 00126 } 00127 00128 /// Assign n objects to list 00129 FXArray<TYPE>& assign(const FXArray<TYPE>& src){ 00130 if(no(src.num)){ copyElms(ptr,src.ptr,src.num); } 00131 return *this; 00132 } 00133 00134 /// Insert an object 00135 FXArray<TYPE>& insert(FXint pos,const TYPE& src){ 00136 if(no(num+1)){ moveElms(ptr+pos+1,ptr+pos,num-pos-1); ptr[pos]=src; } 00137 return *this; 00138 } 00139 00140 /// Insert n copies of object at specified position 00141 FXArray<TYPE>& insert(FXint pos,const TYPE& src,FXint n){ 00142 if(no(num+n)){ moveElms(ptr+pos+n,ptr+pos,num-pos-n); fillElms(ptr+pos,src,n); } 00143 return *this; 00144 } 00145 00146 /// Insert n objects at specified position 00147 FXArray<TYPE>& insert(FXint pos,const TYPE* src,FXint n){ 00148 if(no(num+n)){ moveElms(ptr+pos+n,ptr+pos,num-pos-n); copyElms(ptr+pos,src,n); } 00149 return *this; 00150 } 00151 00152 /// Insert n objects at specified position 00153 FXArray<TYPE>& insert(FXint pos,const FXArray<TYPE>& src){ 00154 if(no(num+src.num)){ moveElms(ptr+pos+src.num,ptr+pos,num-pos-src.num); copyElms(ptr+pos,src.ptr,src.num); } 00155 return *this; 00156 } 00157 00158 /// Prepend object 00159 FXArray<TYPE>& prepend(const TYPE& src){ 00160 if(no(num+1)){ moveElms(ptr+1,ptr,num-1); ptr[0]=src; } 00161 return *this; 00162 } 00163 00164 /// Prepend n copies of object 00165 FXArray<TYPE>& prepend(const TYPE& src,FXint n){ 00166 if(no(num+n)){ moveElms(ptr+n,ptr,num-n); fillElms(ptr,src,n); } 00167 return *this; 00168 } 00169 00170 /// Prepend n objects 00171 FXArray<TYPE>& prepend(const TYPE* src,FXint n){ 00172 if(no(num+n)){ moveElms(ptr+n,ptr,num-n); copyElms(ptr,src,n); } 00173 return *this; 00174 } 00175 00176 /// Prepend n objects 00177 FXArray<TYPE>& prepend(const FXArray<TYPE>& src){ 00178 if(no(num+src.num)){ moveElms(ptr+src.num,ptr,num-src.num); copyElms(ptr,src.ptr,src.num); } 00179 return *this; 00180 } 00181 00182 /// Append object 00183 FXArray<TYPE>& append(const TYPE& src){ 00184 if(no(num+1)){ ptr[num-1]=src; } 00185 return *this; 00186 } 00187 00188 /// Append n copies of object 00189 FXArray<TYPE>& append(const TYPE& src,FXint n){ 00190 if(no(num+n)){ fillElms(ptr+num-n,src,n); } 00191 return *this; 00192 } 00193 00194 /// Append n objects 00195 FXArray<TYPE>& append(const TYPE* src,FXint n){ 00196 if(no(num+n)){ copyElms(ptr+num-n,src,n); } 00197 return *this; 00198 } 00199 00200 /// Append n objects 00201 FXArray<TYPE>& append(const FXArray<TYPE>& src){ 00202 if(no(num+src.num)){ copyElms(ptr+num-src.num,src.ptr,src.num); } 00203 return *this; 00204 } 00205 00206 /// Remove object at pos 00207 FXArray<TYPE>& erase(FXint pos){ 00208 moveElms(ptr+pos,ptr+pos+1,num-pos-1); no(num-1); 00209 return *this; 00210 } 00211 00212 /// Remove n objects starting at pos 00213 FXArray<TYPE>& erase(FXint pos,FXint n){ 00214 moveElms(ptr+pos,ptr+pos+n,num-n-pos); no(num-n); 00215 return *this; 00216 } 00217 00218 /// Remove all objects 00219 FXArray<TYPE>& clear(){ 00220 destructElms(ptr,num); freeElms(ptr); num=0; 00221 return *this; 00222 } 00223 00224 /// Delete data 00225 ~FXArray(){ 00226 destructElms(ptr,num); freeElms(ptr); 00227 } 00228 }; 00229 00230 } 00231 00232 #endif
Copyright © 1997-2005 Jeroen van der Zijp |