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

arrayc.h

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 1999  Carsten Winkelholz
00003 
00004 Address:  FGAN Forschungsgesellschaft fr Angewandte Naturwissenschaften e. V.
00005       Neuenahrer Str. 20
00006       D - 53343 Wachtberg
00007       
00008 Email:    winkelholz@fgan.de
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 */
00024 
00025 
00026 #ifndef ARRAYC_H
00027 #define ARRAYC_H
00028 
00029 
00030 #include <assert.h>
00031 
00032 #include "../oawconfig.h"
00033 
00034 OAW_BEGIN_NAMESPACE
00035 
00036 template <class Typ> 
00037 class OAW_DLLMAPPING ArrayC
00038 {
00039 protected:
00040 
00041    Typ m_blank;
00042    long m_blocklen;
00043    long m_buflen;
00044    long m_anzahlelemente;
00045    Typ *m_array;
00046 
00047 public:
00048   ArrayC(){
00049     m_buflen= 1;
00050     m_anzahlelemente = 0;
00051     m_blocklen = 0;
00052     m_array = new Typ[1];
00053   }
00054 
00055   ArrayC(const ArrayC<Typ>& A){
00056     long i;
00057     m_blocklen=A.m_blocklen;
00058     m_buflen=A.m_buflen;
00059     m_anzahlelemente=A.m_anzahlelemente;
00060     m_array=new Typ[m_buflen];
00061     for(i=0;i<m_buflen;i++){m_array[i]=A.m_array[i];}
00062   }
00063 
00064   ArrayC(const Typ* data, long datalen){
00065     m_buflen= datalen;
00066     m_anzahlelemente = datalen;
00067     m_blocklen = 0;
00068     m_array = new Typ[datalen];
00069     for(long i=0;i<datalen;i++){
00070       m_array[i] = data[i]; 
00071     }
00072   }
00073 
00074   ArrayC(long l,long b, const Typ& bl){
00075      m_buflen= l;
00076      m_anzahlelemente = 0;
00077      m_blocklen = b;
00078      m_array = new Typ[l];
00079      SetBlankAndClear(bl);
00080   }
00081 
00082   ArrayC(long datalen){
00083     m_buflen= datalen;
00084     m_anzahlelemente = 0;
00085     m_blocklen = 0;
00086     m_array = new Typ[datalen];
00087   }
00088 
00089   ~ArrayC(){
00090     delete [] m_array;
00091   }
00092 
00093   const ArrayC<Typ>& operator = (const ArrayC<Typ>& A){
00094     if (this == &A) return *this;
00095     long i;
00096     delete [] m_array;
00097     m_blocklen=A.m_blocklen;
00098     m_buflen=A.m_buflen;
00099     m_anzahlelemente=A.m_anzahlelemente;
00100     m_array=new Typ[m_buflen];
00101     for(i=0;i<m_buflen;i++){m_array[i]=A.m_array[i];}
00102     return *this;
00103   }
00104 
00105 
00106   short operator == (const ArrayC<Typ>& a){
00107     for(long i=0;i<m_buflen;i++){
00108       if(m_array[i]!=a.m_array[i]) return 0;
00109     }
00110     return 1;
00111   }
00112 
00113 
00114   short operator != (const ArrayC<Typ>& a){
00115     for(long i=0;i<m_buflen;i++){
00116       if(m_array[i]!=a.m_array[i]) return 1;
00117     }
00118     return 0;
00119   }
00120 
00121   const Typ& get(long l) const {
00122      assert(l<m_anzahlelemente);
00123      return m_array[l];
00124   }
00125 
00126   inline long capacity() const{return m_buflen;}
00127 
00128   inline long size() const{return m_anzahlelemente;}
00129 
00130   inline Typ& operator [](long i) {return m_array[i];}
00131 
00132   inline Typ& back(){
00133     assert(m_anzahlelemente>0);
00134     return m_array[m_anzahlelemente-1];
00135   }
00136 
00137   short put(long l,const Typ& t){
00138      if(l>=m_anzahlelemente) return 0;
00139      m_array[l]=t;
00140      return 1;
00141   }
00142 
00143   void set(long l, const Typ& t){
00144     if(l>=m_anzahlelemente){SetLength(l+1);}
00145     m_array[l] = t;
00146   }
00147 
00148   void push_back(const Typ& t){
00149      if ( m_anzahlelemente >= m_buflen){MakeLonger(m_blocklen+1);}
00150      m_array[m_anzahlelemente++]=t;   
00151   }
00152 
00153   short pop_back(Typ& t)
00154   {
00155     short erg = m_anzahlelemente>0;
00156     if(m_anzahlelemente>0){
00157       m_anzahlelemente--;
00158       t = m_array[m_anzahlelemente];
00159     }
00160     return erg;
00161   }
00162 
00163   short pop_front(Typ& t)
00164   {
00165     short erg = m_anzahlelemente>0;
00166     if(erg){
00167       t = m_array[0];
00168       RemoveElementAtPos(0);
00169     }
00170     return erg;
00171   }
00172 
00173   void push(){
00174     assert(m_anzahlelemente>0);
00175     if ( m_anzahlelemente >= m_buflen){MakeLonger(m_blocklen+1);}
00176     push_back(m_array[m_anzahlelemente-1]);
00177   }
00178 
00179   void Erase(long a, long e){
00180     if(a<0){a=0;}
00181     if(e>=m_anzahlelemente){e=m_anzahlelemente-1;}
00182     if(a>e){return;}
00183     long i; 
00184     m_anzahlelemente-=e-a+1;
00185     for(i=a;i<m_anzahlelemente;i++){
00186       m_array[i]=m_array[i+(e-a)+1];
00187     }
00188   }
00189 
00190   Typ* GetBuffer(){return m_array;}
00191 
00192   void SetBlockSize(long i){m_blocklen=i;}
00193 
00194   void SetBuffer(Typ* data, long n){ // aufeigene Verantwortung
00195     delete m_array;
00196     m_array = data;
00197     m_anzahlelemente = n;
00198     m_buflen = m_anzahlelemente;
00199   }
00200 
00201   void SetLength(long l){
00202     SetBufferLength(l);
00203     m_anzahlelemente=l;
00204   }
00205 
00206   short SetBufferLength(long l){
00207     if(l>m_buflen){
00208       return MakeLonger((l-m_buflen)+m_blocklen);
00209     }
00210     return 0;
00211     //  if(l>m_anzahlelemente){m_anzahlelemente=l;}
00212   }
00213 
00214   short InsertAtPos(long l,const Typ& t){
00215      long i;
00216      if(l>=m_anzahlelemente) return 0;
00217      if (m_anzahlelemente >= m_buflen){MakeLonger(m_blocklen+1);}
00218      for(i=m_anzahlelemente;i>l;i--){m_array[i]=m_array[i-1];}
00219      m_array[l]=t;  
00220      m_anzahlelemente++;
00221      return 1;
00222   }
00223 
00224 
00225   void RemoveElementAtPos(long l)
00226   {
00227     assert(l<m_buflen);
00228     long i; 
00229     m_anzahlelemente--;
00230     //kein memcpy um, damit das ganze auch für arrays von Klassen funktioniert.
00231     //memcpy(&m_array[i],&m_array[i+1],(m_anzahlelemente-l)*sizeof(Typ));
00232     for(i=l;i<m_anzahlelemente;i++){
00233       m_array[i]=m_array[i+1];
00234     }
00235   }
00236 
00237 
00238   short RemoveElement(Typ& t){
00239     long i; 
00240     for(i=0;i<m_anzahlelemente;i++){
00241       if(m_array[i]==t){
00242         RemoveElementAtPos(i);
00243         return true;
00244       }
00245     }
00246     return false;
00247   }
00248 
00249   void Empty(){
00250     m_anzahlelemente=0;
00251   }
00252 
00253   void OptimizeMemory(){
00254     long i;
00255     Typ* tmparray=new Typ[m_anzahlelemente];
00256     m_buflen=m_anzahlelemente;
00257     for(i=0;i<m_anzahlelemente;i++){tmparray[i]=m_array[i];}
00258     delete [] m_array;
00259     m_array=tmparray;
00260   }
00261 
00262   long GetIndexOf(const Typ& t) const{ 
00263     for(long i=0;i<m_anzahlelemente;i++){if(m_array[i]==t){return long(i);}}
00264     return -1;
00265   }
00266 
00267 
00268 
00269   short Contain(const Typ& t)
00270   {
00271     for(long i=0;i<m_anzahlelemente;i++){
00272       if(m_array[i]==t){return 1;}
00273     }
00274     return 0;
00275   }
00276 
00277 
00278   long Count(const Typ& t)
00279   {
00280     long j=0;
00281     for(long i=0;i<m_anzahlelemente;i++){
00282       if(m_array[i]==t){j++;}
00283     }
00284     return j;
00285   }
00286 
00287   void SetBlankAndClear(const Typ& bl)
00288   {
00289     m_blank=bl;
00290     long i;
00291     for(i=0;i<m_buflen;i++){m_array[i]=m_blank;}
00292   }
00293 
00294   void SetBlankAndClear(const Typ& bl,long l){
00295     m_blank=bl;
00296     if(l>m_anzahlelemente) m_anzahlelemente=l;
00297     SetBufferLength(l);
00298     long i;
00299     for(i=0;i<m_buflen;i++){m_array[i]=m_blank;}
00300   }
00301 
00302 protected:
00303   int MakeLonger(unsigned int b){
00304      Typ *Ablage;
00305      long i,lAlt;
00306      Ablage = m_array;
00307      lAlt=m_buflen;
00308      m_buflen+=b;
00309      m_array = new Typ[m_buflen]; assert(m_array);
00310      if(m_array==0) return 0;
00311      for(i=0;i<lAlt;i++){m_array[i]=Ablage[i];}
00312      for(i=lAlt;i<m_buflen;i++){m_array[i]=m_blank;}
00313      delete [] Ablage;
00314      return 1;
00315   }
00316 
00317 };
00318 
00319 
00320 template <class Typ>
00321 class GridC
00322 {
00323 public:
00324   GridC()
00325   {
00326      int i,j;
00327      m_left=0;
00328      m_right=0;
00329      m_top=0;
00330      m_bottom=0;
00331      m_width=1;
00332      m_height=1;
00333      m_array=new Typ* [m_width];
00334      for(i=0;i<m_width;i++){
00335         m_array[i]=new Typ[m_height];
00336         for(j=0;j<m_height;j++){m_array[i][j]=m_blank;}
00337      }
00338   }
00339 
00340 
00341   GridC(long l, long b, long r, long t,const Typ& bl)
00342   {
00343      int i,j;
00344      m_left=l;
00345      m_right=r;
00346      m_top=t;
00347      m_bottom=b;
00348      m_width=r-l+1;assert(m_width>0);
00349      m_height=t-b+1;assert(m_height>0);
00350      m_blank=bl;
00351      m_array=new Typ* [m_width];
00352      for(i=0;i<m_width;i++){
00353         m_array[i]=new Typ[m_height];
00354         for(j=0;j<m_height;j++){m_array[i][j]=m_blank;}
00355      }
00356   }
00357 
00358   ~GridC(){
00359      int i;
00360      for(i=0;i<m_width;i++){delete [] m_array[i];}
00361      delete m_array;
00362   }
00363 
00364 
00365   const Typ& get(long i,long j) const{
00366      if((i>m_right)||(i<m_left)||(j>m_top)||(j<m_bottom)) return m_blank; 
00367      return m_array[i-m_left][j-m_bottom];
00368   }
00369 
00370 
00371   void set(long n,long m,const Typ& t){
00372     int i,j;
00373     if( ((n<m_left)||(n>m_right))&&(m<=m_top)&&(m>=m_bottom) ){
00374       Typ **ablage;
00375       long leftOld=m_left;
00376       //long rightOld=m_right;
00377       long widthOld=m_width;
00378       long s1,s2;
00379       ablage=m_array;
00380       if(n<m_left){m_left=n;s1=leftOld-n;s2=widthOld;}else{m_right=n;s1=0;s2=0;}
00381       m_width=m_right-m_left+1;
00382       m_array = new Typ* [m_width]; 
00383       for(i=0;i<widthOld;i++){m_array[i+s1]=ablage[i];}
00384       for(i=widthOld;i<m_width;i++){
00385         m_array[i]=new Typ[m_height];
00386         for(j=0;j<m_height;j++){m_array[i-s2][j]=m_blank;}
00387       }  
00388       delete ablage;
00389     }else{
00390       if( ((m>m_top)||(m<m_bottom))&&(n>=m_left)&&(n<=m_right) ){
00391       long heightOld=m_height;
00392       //long topOld=m_top;
00393       long bottomOld=m_bottom;
00394       long s1,s2;
00395       if(m<m_bottom){m_bottom=m;s1=bottomOld-m;s2=heightOld;}else{m_top=m;s1=0;s2=0;}
00396       m_height=m_top-m_bottom+1;
00397       for(i=0;i<m_width;i++){
00398         Typ *ablage;
00399         ablage=m_array[i];
00400         m_array[i]= new Typ[m_height];
00401         for(j=0;j<heightOld;j++){m_array[i][j+s1]=ablage[j];}
00402         for(j=heightOld;j<m_height;j++){m_array[i][j-s2]=m_blank;}
00403         delete [] ablage;
00404       }
00405     }else{
00406       if((m>m_top)||(m<m_bottom)||(n<m_left)||(n>m_right)){
00407       long leftOld=m_left;
00408       long bottomOld=m_bottom;
00409       long widthOld=m_width;
00410       long heightOld=m_height;
00411       Typ **ablage;
00412       ablage=m_array;
00413       if(m>m_top){m_top=m;}
00414       if(m<m_bottom){m_bottom=m;}
00415       if(n<m_left){m_left=n;}
00416       if(n>m_right){m_right=n;}
00417       m_width=m_right-m_left+1;
00418       m_height=m_top-m_bottom+1;
00419       m_array = new Typ* [m_width];
00420       for(i=0;i<m_width;i++){
00421         m_array[i]=new Typ[m_height];
00422         for(j=0;j<m_height;j++){m_array[i][j]=m_blank;}
00423       }
00424       for(i=0;i<widthOld;i++){
00425         for(j=0;j<heightOld;j++){
00426           m_array[i+leftOld-m_left][j+bottomOld-m_bottom]=ablage[i][j];
00427         }
00428       }
00429       for(i=0;i<widthOld;i++){delete [] ablage[i];}
00430       delete ablage;
00431     }}}
00432      m_array[n-m_left][m-m_bottom]=t;
00433   }
00434 
00435   void SetBlankAndClear(const Typ& bl){
00436      int i,j;
00437      m_blank=bl;
00438      for(i=0;i<m_width;i++){
00439         for(j=0;j<m_height;j++){m_array[i][j]=m_blank;}
00440      }
00441   }
00442 
00443 protected:
00444    Typ m_blank;
00445    long m_left,m_bottom,m_right,m_top;
00446    long m_width,m_height;
00447    Typ **m_array;
00448    short m_bFirstSet;
00449 };
00450 
00451 typedef ArrayC<char> ByteArrayC;
00452 
00453 OAW_END_NAMESPACE
00454 
00455 #endif
00456 
00457 
00458 
00459 
00460 
00461 

Generated on Tue Jul 29 14:24:08 2003 for Open ActiveWrl by doxygen1.3-rc2