Main Page   Class Hierarchy   Compound List   File List   Compound Members  

Array.h

00001 /**************************************************************************\
00002  * 
00003  *  FILE: Array.h
00004  *
00005  *  This source file is part of DIME.
00006  *  Copyright (C) 1998-1999 by Systems In Motion.  All rights reserved.
00007  *
00008  *  This library is free software; you can redistribute it and/or modify it
00009  *  under the terms of the GNU General Public License, version 2, as
00010  *  published by the Free Software Foundation.
00011  *
00012  *  This library is distributed in the hope that it will be useful, but
00013  *  WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  *  General Public License (the accompanying file named COPYING) for more
00016  *  details.
00017  *
00018  **************************************************************************
00019  *
00020  *  If you need DIME for a non-GPL project, contact Systems In Motion
00021  *  to acquire a Professional Edition License:
00022  *
00023  *  Systems In Motion                                   http://www.sim.no/
00024  *  Prof. Brochs gate 6                                       sales@sim.no
00025  *  N-7030 Trondheim                                   Voice: +47 22114160
00026  *  NORWAY                                               Fax: +47 67172912
00027  *
00028 \**************************************************************************/
00029 
00030 #ifndef DIME_ARRAY_H
00031 #define DIME_ARRAY_H
00032 
00033 #include <stdlib.h>
00034 
00035 template <class T>
00036 class dimeArray
00037 {
00038 public:
00039   dimeArray(const int initsize = 4);
00040   ~dimeArray();
00041 
00042   void append(const T &value);
00043   void append(const dimeArray<T> &array);
00044   void prepend(const dimeArray<T> &array);
00045   void insertElem(const int idx, const T &value);
00046   void setElem(const int index, const T &value);
00047   T getElem(const int index) const;
00048   void getElem(const int index, T &elem) const;
00049   T    getLastElem() const;
00050   void getLastElem(T &elem) const;
00051   T &operator [](const int index);
00052   T operator [](const int index) const;
00053   void removeElem(const int index);
00054   void removeElemFast(const int index);
00055   void reverse();
00056   void setCount(const int count);
00057   void makeEmpty(const int initsize = 4);
00058   void freeMemory();
00059   int  count() const;
00060   int  allocSize() const;
00061   T   *arrayPointer();
00062   const T *constArrayPointer() const;
00063   void shrinkToFit();
00064 
00065 private:
00066   void growArray();
00067   T *array;
00068   int num;
00069   int size;
00070 
00071 }; // class dimeArray<>
00072 
00073 template <class T> inline 
00074 dimeArray<T>::dimeArray(const int size)
00075 {
00076   this->array = new T[size];
00077   this->size = size;
00078   this->num = 0;
00079 }
00080 
00081 template <class T> inline 
00082 dimeArray<T>::~dimeArray()
00083 {
00084   delete [] this->array;
00085 }
00086 
00087 template <class T> inline void 
00088 dimeArray<T>::growArray()
00089 {
00090   int oldsize = this->size;
00091   T *oldarray = this->array;
00092   this->size <<= 1;
00093   this->array = new T[this->size];
00094   for (int i = 0; i < oldsize; i++) this->array[i] = oldarray[i];
00095   delete [] oldarray;
00096 }
00097 
00098 template <class T> inline void 
00099 dimeArray<T>::append(const T &elem)
00100 {
00101   if (this->num >= this->size) growArray();
00102   this->array[this->num++] = elem;
00103 }
00104 
00105 
00106 template <class T> inline void 
00107 dimeArray<T>::append(const dimeArray<T> &array)
00108 {
00109   while (this->size <= this->num+array.count()) growArray();
00110   for (int i=0;i<array.count();i++)
00111     this->array[this->num++] = array[i];
00112 }
00113 
00114 template <class T> inline void 
00115 dimeArray<T>::prepend(const dimeArray<T> &array)
00116 {
00117   int newsize=this->num+array.count();
00118   int i;
00119   if (this->size<=newsize) {
00120     T *oldarray=this->array;
00121     this->array=new T[newsize];
00122     this->size=newsize;
00123     for (i=0;i<array.count(); i++) this->array[i] = array[i];
00124     for (i=0;i<this->num;i++) this->array[i+array.count()] = oldarray[i];
00125     delete[] oldarray;
00126   }
00127   else {
00128     for (i=0;i<this->num;i++) this->array[array.count()+i]=this->array[i];
00129     for (i=0;i<array.count();i++) this->array[i] = array[i];
00130   }
00131   this->num+=array.count();
00132 }
00133 
00134 template <class T> inline void 
00135 dimeArray<T>::insertElem(const int idx, const T &elem)
00136 {
00137   int n = this->num;
00138   this->append(elem); // make room for one more
00139   if (idx < n) {
00140     for (int i = n; i > idx; i--) {
00141       this->array[i] = this->array[i-1]; 
00142     }
00143     this->array[idx] = elem;
00144   }
00145 }
00146 
00147 template <class T> inline void 
00148 dimeArray<T>::setElem(const int index, const T &elem)
00149 {
00150   while (index >= this->size) growArray();
00151   if (this->num <= index) this->num = index+1;
00152   this->array[index] = elem;
00153 }
00154 
00155 template <class T> inline T 
00156 dimeArray<T>::getElem(const int index) const
00157 {
00158   return this->array[index];
00159 }
00160 
00161 template <class T> inline void 
00162 dimeArray<T>::getElem(const int index, T &elem) const
00163 {
00164   elem = this->array[index];
00165 }
00166 
00167 template <class T> inline T 
00168 dimeArray<T>::getLastElem() const
00169 {
00170   return this->array[this->num-1];
00171 }
00172 
00173 template <class T> inline void 
00174 dimeArray<T>::getLastElem(T &elem) const
00175 {
00176   elem = this->array[this->num-1];
00177 }
00178 
00179 template <class T> inline T &
00180 dimeArray<T>::operator [](const int index)
00181 {
00182   while (index >= this->size) growArray();   
00183   if (this->num <= index) this->num = index + 1;
00184   return this->array[index];
00185 }
00186 
00187 template <class T> inline T 
00188 dimeArray<T>::operator [](const int index) const
00189 {
00190   return this->array[index];
00191 }
00192 
00193 template <class T> inline void 
00194 dimeArray<T>::removeElem(const int index)
00195 {
00196   if (this->num <= 0 || index >= this->num) return; 
00197   for (int i = index; i < this->num-1; i++)
00198     this->array[i] = this->array[i+1];
00199   this->num--;
00200 }
00201 
00202 template <class T> inline void 
00203 dimeArray<T>::removeElemFast(const int index)
00204 {
00205   this->array[index] = this->array[--this->num];
00206 }
00207 
00208 template <class T> inline void 
00209 dimeArray<T>::reverse()
00210 {
00211   T tmp;
00212   for (int i=0;i<this->num/2;i++) {
00213     tmp=this->array[i];
00214     this->array[i]=this->array[this->num-1-i];
00215     this->array[this->num-1-i]=tmp;
00216   }
00217 }
00218 
00219 template <class T> inline void 
00220 dimeArray<T>::setCount(const int count)
00221 {
00222   if (count < this->num)
00223     this->num = count;  
00224 }
00225 
00226 template <class T> inline int 
00227 dimeArray<T>::count() const
00228 {
00229   return this->num;
00230 }
00231 
00232 template <class T> inline int 
00233 dimeArray<T>::allocSize() const
00234 {
00235   return this->size;
00236 }
00237 
00238 template <class T> inline T *
00239 dimeArray<T>::arrayPointer()
00240 {
00241   return this->array;
00242 }
00243 
00244 template <class T> inline const T *
00245 dimeArray<T>::constArrayPointer() const
00246 {
00247   return this->array;
00248 }
00249 
00250 template <class T> inline void 
00251 dimeArray<T>::makeEmpty(const int initsize)
00252 {
00253   delete [] this->array;
00254   this->array = new T[initsize];
00255   this->size = initsize;
00256   this->num = 0; 
00257 }
00258 
00259 template <class T> inline void 
00260 dimeArray<T>::freeMemory()
00261 {
00262   delete [] this->array;
00263   this->array = NULL;
00264   this->size = 0;
00265   this->num = 0;
00266 }
00267 
00268 template <class T> inline void 
00269 dimeArray<T>::shrinkToFit()
00270 {
00271   T *oldarray = this->array;
00272   this->array = new T[this->num];
00273   for (int i = 0; i < this->num; i++) this->array[i] = oldarray[i];
00274   this->size = this->num;
00275   delete [] oldarray;
00276 }
00277 
00278 #endif // ! DIME_ARRAY_H
00279 

Copyright © 1998-1999, Systems In Motion <sales@sim.no>. All rights reserved.
System documentation was generated using doxygen.