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

Copyright © 1997-2010 Jeroen van der Zijp