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

FXArray.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                          G e n e r i c   A r r a y                            *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1997,2009 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (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                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 *********************************************************************************
00021 * $Id: FXArray.h,v 1.39 2009/01/06 13:07:21 fox Exp $                           *
00022 ********************************************************************************/
00023 #ifndef FXARRAY_H
00024 #define FXARRAY_H
00025 
00026 #ifndef FXELEMENT_H
00027 #include "FXElement.h"
00028 #endif
00029 
00030 namespace FX {
00031 
00032 
00033 /// Array of some generic type
00034 template<class EType>
00035 class FXArray {
00036 protected:
00037   EType  *ptr;   // Data array
00038   FXint   num;   // Number in array
00039 public:
00040 
00041   /// Create as empty
00042   FXArray():ptr(NULL),num(0){
00043     }
00044 
00045   /// Create with given size n
00046   FXArray(FXint n):ptr(NULL),num(0){
00047     if(allocElms(ptr,n)){ constructElms(ptr,n); num=n; }
00048     }
00049 
00050   /// Create initialized from another array
00051   FXArray(const FXArray<EType>& src):ptr(NULL),num(0){
00052     if(allocElms(ptr,src.num)){ constructElms(ptr,src.num); copyElms(ptr,src.ptr,src.num); num=src.num; }
00053     }
00054 
00055   /// Create initialized with n copies of object
00056   FXArray(const EType& src,FXint n):ptr(NULL),num(0){
00057     if(allocElms(ptr,n)){ constructElms(ptr,n); fillElms(ptr,src,n); num=n; }
00058     }
00059 
00060   /// Create initialized with array of n objects
00061   FXArray(const EType* src,FXint n):ptr(NULL),num(0){
00062     if(allocElms(ptr,n)){ constructElms(ptr,n); copyElms(ptr,src,n); num=n; }
00063     }
00064 
00065   /// Return number of elements
00066   FXint no() const { return num; }
00067 
00068   /// Change number of elements to n
00069   FXbool no(FXint n){
00070     if(n!=num){
00071       if(0<num-n){
00072         destructElms(ptr+n,num-n);
00073         if(!resizeElms(ptr,n)) return false;
00074         }
00075       else{
00076         if(!resizeElms(ptr,n)) return false;
00077         constructElms(ptr+num,n-num);
00078         }
00079       num=n;
00080       }
00081     return true;
00082     }
00083 
00084   /// Assign from another list
00085   FXArray<EType>& operator=(const FXArray<EType>& src){
00086     if(ptr!=src.ptr){ no(src.num); copyElms(ptr,src.ptr,src.num); }
00087     return *this;
00088     }
00089 
00090   /// Index into array
00091   EType& operator[](FXint i){ return ptr[i]; }
00092   const EType& operator[](FXint i) const { return ptr[i]; }
00093 
00094   /// Index into list
00095   EType& at(FXint i){ return ptr[i]; }
00096   const EType& at(FXint i) const { return ptr[i]; }
00097 
00098   /// First element in list
00099   EType& head(){ return ptr[0]; }
00100   const EType& head() const { return ptr[0]; }
00101 
00102   /// Last element in list
00103   EType& tail(){ return ptr[num-1]; }
00104   const EType& tail() const { return ptr[num-1]; }
00105 
00106   /// Return pointer to list
00107   EType* data() const { return ptr; }
00108 
00109   /// Adopt array from source
00110   FXArray<EType>& adopt(FXArray<EType>& src){
00111     if(no(0)){ ptr=src.ptr; src.ptr=NULL; num=src.num; src.num=0; }
00112     return *this;
00113     }
00114 
00115   /// Assign object p to list
00116   FXArray<EType>& assign(const EType& src){
00117     if(no(1)){ ptr[0]=src; }
00118     return *this;
00119     }
00120 
00121   /// Assign n copies of object to list
00122   FXArray<EType>& assign(const EType& src,FXint n){
00123     if(no(n)){ fillElms(ptr,src,n); }
00124     return *this;
00125     }
00126 
00127   /// Assign n objects to list
00128   FXArray<EType>& assign(const EType* src,FXint n){
00129     if(no(n)){ copyElms(ptr,src,n); }
00130     return *this;
00131     }
00132 
00133   /// Assign n objects to list
00134   FXArray<EType>& assign(const FXArray<EType>& src){
00135     return assign(src.ptr,src.num);
00136     }
00137 
00138   /// Insert an object
00139   FXArray<EType>& insert(FXint pos,const EType& src){
00140     if(no(num+1)){ moveElms(ptr+pos+1,ptr+pos,num-pos-1); ptr[pos]=src; }
00141     return *this;
00142     }
00143 
00144   /// Insert n copies of object at specified position
00145   FXArray<EType>& insert(FXint pos,const EType& src,FXint n){
00146     if(no(num+n)){ moveElms(ptr+pos+n,ptr+pos,num-pos-n); fillElms(ptr+pos,src,n); }
00147     return *this;
00148     }
00149 
00150   /// Insert n objects at specified position
00151   FXArray<EType>& insert(FXint pos,const EType* src,FXint n){
00152     if(no(num+n)){ moveElms(ptr+pos+n,ptr+pos,num-pos-n); copyElms(ptr+pos,src,n); }
00153     return *this;
00154     }
00155 
00156   /// Insert n objects at specified position
00157   FXArray<EType>& insert(FXint pos,const FXArray<EType>& src){
00158     return insert(pos,src.ptr,src.num);
00159     }
00160 
00161   /// Prepend object
00162   FXArray<EType>& prepend(const EType& src){
00163     if(no(num+1)){ moveElms(ptr+1,ptr,num-1); ptr[0]=src; }
00164     return *this;
00165     }
00166 
00167   /// Prepend n copies of object
00168   FXArray<EType>& prepend(const EType& src,FXint n){
00169     if(no(num+n)){ moveElms(ptr+n,ptr,num-n); fillElms(ptr,src,n); }
00170     return *this;
00171     }
00172 
00173   /// Prepend n objects
00174   FXArray<EType>& prepend(const EType* src,FXint n){
00175     if(no(num+n)){ moveElms(ptr+n,ptr,num-n); copyElms(ptr,src,n); }
00176     return *this;
00177     }
00178 
00179   /// Prepend n objects
00180   FXArray<EType>& prepend(const FXArray<EType>& src){
00181     return prepend(src.ptr,src.num);
00182     }
00183 
00184   /// Append object
00185   FXArray<EType>& append(const EType& src){
00186     if(no(num+1)){ ptr[num-1]=src; }
00187     return *this;
00188     }
00189 
00190   /// Append n copies of object
00191   FXArray<EType>& append(const EType& src,FXint n){
00192     if(no(num+n)){ fillElms(ptr+num-n,src,n); }
00193     return *this;
00194     }
00195 
00196   /// Append n objects
00197   FXArray<EType>& append(const EType* src,FXint n){
00198     if(no(num+n)){ copyElms(ptr+num-n,src,n); }
00199     return *this;
00200     }
00201 
00202   /// Append n objects
00203   FXArray<EType>& append(const FXArray<EType>& src){
00204     return append(src.ptr,src.num);
00205     }
00206 
00207   /// Replace an object
00208   FXArray<EType>& replace(FXint pos,const EType& src){
00209     ptr[pos]=src;
00210     return *this;
00211     }
00212 
00213   /// Replace the m objects at pos with n copies of src
00214   FXArray<EType>& replace(FXint pos,FXint m,const EType& src,FXint n){
00215     if(m<n){
00216       if(!no(num-m+n)) return *this;
00217       moveElms(ptr+pos+n,ptr+pos+m,num-pos-n);
00218       }
00219     else if(m>n){
00220       moveElms(ptr+pos+n,ptr+pos+m,num-pos-m);
00221       if(!no(num-m+n)) return *this;
00222       }
00223     fillElms(ptr+pos,src,n);
00224     return *this;
00225     }
00226 
00227   /// Replace m objects at pos by n objects from src
00228   FXArray<EType>& replace(FXint pos,FXint m,const EType* src,FXint n){
00229     if(m<n){
00230       if(!no(num-m+n)) return *this;
00231       moveElms(ptr+pos+n,ptr+pos+m,num-pos-n);
00232       }
00233     else if(m>n){
00234       moveElms(ptr+pos+n,ptr+pos+m,num-pos-m);
00235       if(!no(num-m+n)) return *this;
00236       }
00237     copyElms(ptr+pos,src,n);
00238     return *this;
00239     }
00240 
00241   /// Replace m objects at pos by objects from src
00242   FXArray<EType>& replace(FXint pos,FXint m,const FXArray<EType>& src){
00243     return replace(pos,m,src.ptr,src.num);
00244     }
00245 
00246   /// Remove object at pos
00247   FXArray<EType>& erase(FXint pos){
00248     moveElms(ptr+pos,ptr+pos+1,num-pos-1); no(num-1);
00249     return *this;
00250     }
00251 
00252   /// Remove n objects starting at pos
00253   FXArray<EType>& erase(FXint pos,FXint n){
00254     moveElms(ptr+pos,ptr+pos+n,num-n-pos); no(num-n);
00255     return *this;
00256     }
00257 
00258   /// Push object to end
00259   FXArray<EType>& push(const EType& src){
00260     if(no(num+1)){ ptr[num-1]=src; }
00261     return *this;
00262     }
00263 
00264   /// Pop object from end
00265   FXArray<EType>& pop(){
00266     no(num-1);
00267     return *this;
00268     }
00269 
00270   /// Remove all objects
00271   FXArray<EType>& clear(){
00272     destructElms(ptr,num); freeElms(ptr); num=0;
00273     return *this;
00274     }
00275 
00276   /// Delete data
00277   ~FXArray(){
00278     destructElms(ptr,num); freeElms(ptr);
00279     }
00280   };
00281 
00282 }
00283 
00284 #endif

Copyright © 1997-2009 Jeroen van der Zijp