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

/home/jeroen/FOX/fox/fox-1.7.33/include/FXArray.h
00001 /********************************************************************************
00002 *                                                                               *
00003 *                          G e n e r i c   A r r a y                            *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1997,2012 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 
00032 template<class EType>
00033 class FXArray {
00034 private:
00035   EType  *ptr;   // Data array
00036   FXint   num;   // Number in array
00037 public:
00038 
00040   FXArray():ptr(NULL),num(0){
00041     }
00042 
00044   FXArray(FXint n):ptr(NULL),num(0){
00045     if(__likely(allocElms(ptr,n))){
00046       constructElms(ptr,n);
00047       num=n;
00048       }
00049     }
00050 
00052   FXArray(const FXArray<EType>& src):ptr(NULL),num(0){
00053     if(__likely(allocElms(ptr,src.num))){
00054       constructElms(ptr,src.num);
00055       copyElms(ptr,src.ptr,src.num);
00056       num=src.num;
00057       }
00058     }
00059 
00061   FXArray(const EType& src,FXint n):ptr(NULL),num(0){
00062     if(__likely(allocElms(ptr,n))){
00063       constructElms(ptr,n);
00064       fillElms(ptr,src,n);
00065       num=n;
00066       }
00067     }
00068 
00070   FXArray(const EType* src,FXint n):ptr(NULL),num(0){
00071     if(__likely(allocElms(ptr,n))){
00072       constructElms(ptr,n);
00073       copyElms(ptr,src,n);
00074       num=n;
00075       }
00076     }
00077 
00079   FXint no() const { return num; }
00080 
00082   FXbool no(FXint n){
00083     if(__likely(n!=num)){
00084       if(0<num-n){
00085         destructElms(ptr+n,num-n);
00086         if(!resizeElms(ptr,n)) return false;
00087         }
00088       else{
00089         if(!resizeElms(ptr,n)) return false;
00090         constructElms(ptr+num,n-num);
00091         }
00092       num=n;
00093       }
00094     return true;
00095     }
00096 
00098   FXArray<EType>& operator=(const FXArray<EType>& src){
00099     if(__likely(ptr!=src.ptr && no(src.num))){
00100       copyElms(ptr,src.ptr,src.num);
00101       }
00102     return *this;
00103     }
00104 
00106   EType& operator[](FXint i){ return ptr[i]; }
00107   const EType& operator[](FXint i) const { return ptr[i]; }
00108 
00110   EType& at(FXint i){ return ptr[i]; }
00111   const EType& at(FXint i) const { return ptr[i]; }
00112 
00114   EType& head(){ return ptr[0]; }
00115   const EType& head() const { return ptr[0]; }
00116 
00118   EType& tail(){ return ptr[num-1]; }
00119   const EType& tail() const { return ptr[num-1]; }
00120 
00122   EType* data(){ return ptr; }
00123   const EType* data() const { return ptr; }
00124 
00126   void adopt(FXArray<EType>& src){
00127     if(__likely(ptr!=src.ptr && no(0))){
00128       ptr=src.ptr; src.ptr=NULL; num=src.num; src.num=0;
00129       }
00130     }
00131 
00133   FXbool assign(const EType& src){
00134     if(__likely(no(1))){
00135       ptr[0]=src;
00136       return true;
00137       }
00138     return false;
00139     }
00140 
00142   FXbool assign(const EType& src,FXint n){
00143     if(__likely(no(n))){
00144       fillElms(ptr,src,n);
00145       return true;
00146       }
00147     return false;
00148     }
00149 
00151   FXbool assign(const EType* src,FXint n){
00152     if(__likely(no(n))){
00153       copyElms(ptr,src,n);
00154       return true;
00155       }
00156     return false;
00157     }
00158 
00160   FXbool assign(const FXArray<EType>& src){
00161     return assign(src.ptr,src.num);
00162     }
00163 
00165   FXbool insert(FXint pos,const EType& src){
00166     if(__likely(no(num+1))){
00167       moveElms(ptr+pos+1,ptr+pos,num-pos-1);
00168       ptr[pos]=src;
00169       return true;
00170       }
00171     return false;
00172     }
00173 
00175   FXbool insert(FXint pos,const EType& src,FXint n){
00176     if(__likely(no(num+n))){
00177       moveElms(ptr+pos+n,ptr+pos,num-pos-n);
00178       fillElms(ptr+pos,src,n);
00179       return true;
00180       }
00181     return false;
00182     }
00183 
00185   FXbool insert(FXint pos,const EType* src,FXint n){
00186     if(__likely(no(num+n))){
00187       moveElms(ptr+pos+n,ptr+pos,num-pos-n);
00188       copyElms(ptr+pos,src,n);
00189       return true;
00190       }
00191     return false;
00192     }
00193 
00195   FXbool insert(FXint pos,const FXArray<EType>& src){
00196     return insert(pos,src.ptr,src.num);
00197     }
00198 
00200   FXbool prepend(const EType& src){
00201     if(__likely(no(num+1))){
00202       moveElms(ptr+1,ptr,num-1);
00203       ptr[0]=src;
00204       return true;
00205       }
00206     return false;
00207     }
00208 
00210   FXbool prepend(const EType& src,FXint n){
00211     if(__likely(no(num+n))){
00212       moveElms(ptr+n,ptr,num-n);
00213       fillElms(ptr,src,n);
00214       return true;
00215       }
00216     return false;
00217     }
00218 
00220   FXbool prepend(const EType* src,FXint n){
00221     if(__likely(no(num+n))){
00222       moveElms(ptr+n,ptr,num-n);
00223       copyElms(ptr,src,n);
00224       return true;
00225       }
00226     return false;
00227     }
00228 
00230   FXbool prepend(const FXArray<EType>& src){
00231     return prepend(src.ptr,src.num);
00232     }
00233 
00235   FXbool append(const EType& src){
00236     if(__likely(no(num+1))){
00237       ptr[num-1]=src;
00238       return true;
00239       }
00240     return false;
00241     }
00242 
00244   FXbool append(const EType& src,FXint n){
00245     if(__likely(no(num+n))){
00246       fillElms(ptr+num-n,src,n);
00247       return true;
00248       }
00249     return false;
00250     }
00251 
00253   FXbool append(const EType* src,FXint n){
00254     if(__likely(no(num+n))){
00255       copyElms(ptr+num-n,src,n);
00256       return true;
00257       }
00258     return false;
00259     }
00260 
00262   FXbool append(const FXArray<EType>& src){
00263     return append(src.ptr,src.num);
00264     }
00265 
00267   FXbool replace(FXint pos,const EType& src){
00268     ptr[pos]=src;
00269     return true;
00270     }
00271 
00273   FXbool replace(FXint pos,FXint m,const EType& src,FXint n){
00274     if(__unlikely(m<n)){
00275       if(__unlikely(!no(num-m+n))) return false;
00276       moveElms(ptr+pos+n,ptr+pos+m,num-pos-n);
00277       }
00278     else if(__unlikely(m>n)){
00279       moveElms(ptr+pos+n,ptr+pos+m,num-pos-m);
00280       if(__unlikely(!no(num-m+n))) return false;
00281       }
00282     fillElms(ptr+pos,src,n);
00283     return true;
00284     }
00285 
00287   FXbool replace(FXint pos,FXint m,const EType* src,FXint n){
00288     if(__unlikely(m<n)){
00289       if(__unlikely(!no(num-m+n))) return false;
00290       moveElms(ptr+pos+n,ptr+pos+m,num-pos-n);
00291       }
00292     else if(__unlikely(m>n)){
00293       moveElms(ptr+pos+n,ptr+pos+m,num-pos-m);
00294       if(__unlikely(!no(num-m+n))) return false;
00295       }
00296     copyElms(ptr+pos,src,n);
00297     return true;
00298     }
00299 
00301   FXbool replace(FXint pos,FXint m,const FXArray<EType>& src){
00302     return replace(pos,m,src.ptr,src.num);
00303     }
00304 
00306   FXbool erase(FXint pos){
00307     moveElms(ptr+pos,ptr+pos+1,num-pos-1);
00308     return no(num-1);
00309     }
00310 
00312   FXbool erase(FXint pos,FXint n){
00313     moveElms(ptr+pos,ptr+pos+n,num-n-pos);
00314     return no(num-n);
00315     }
00316 
00318   FXbool push(const EType& src){
00319     if(__likely(no(num+1))){
00320       ptr[num-1]=src;
00321       return true;
00322       }
00323     return false;
00324     }
00325 
00327   FXbool pop(){
00328     return no(num-1);
00329     }
00330 
00332   void clear(){
00333     destructElms(ptr,num);
00334     freeElms(ptr);
00335     num=0;
00336     }
00337 
00339   ~FXArray(){
00340     destructElms(ptr,num);
00341     freeElms(ptr);
00342     }
00343   };
00344 
00345 }
00346 
00347 #endif

Copyright © 1997-2011 Jeroen van der Zijp