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 #include <dime/Basic.h>
00036 
00037 // FIXME: the #pragmas below is just a quick hack to avoid heaps of
00038 // irritating warning messages from the compiler for client code
00039 // compiled under MSVC++. Should try to find the real reason for the
00040 // warnings and fix the cause of the problem instead. 20020730 mortene.
00041 //
00042 // UPDATE 20030617 mortene: there is a Microsoft Knowledge Base
00043 // article at <URL:http://support.microsoft.com> which is related to
00044 // this problem. It's article number KB168958.
00045 //
00046 // In short, the general solution is that classes that exposes usage
00047 // of dimeArray<type> needs to declare the specific template instance
00048 // with "extern" and __declspec(dllimport/export).
00049 //
00050 // That is a lot of work to change, tho'. Another possibility which
00051 // might be better is to simply avoid using (exposing) dimeArray from
00052 // any of the other public classes. Judging from a quick look, this
00053 // seems feasible, and just a couple of hours or so of work.
00054 //
00055 #ifdef _MSC_VER // Microsoft Visual C++
00056 #pragma warning(disable:4251)
00057 #pragma warning(disable:4275)
00058 #endif // _MSC_VER
00059 
00060 
00061 template <class T>
00062 class dimeArray
00063 {
00064 public:
00065   dimeArray(const int initsize = 4);
00066   ~dimeArray();
00067 
00068   void append(const T &value);
00069   void append(const dimeArray<T> &array);
00070   void prepend(const dimeArray<T> &array);
00071   void insertElem(const int idx, const T &value);
00072   void setElem(const int index, const T &value);
00073   T getElem(const int index) const;
00074   void getElem(const int index, T &elem) const;
00075   T    getLastElem() const;
00076   void getLastElem(T &elem) const;
00077   T &operator [](const int index);
00078   T operator [](const int index) const;
00079   void removeElem(const int index);
00080   void removeElemFast(const int index);
00081   void reverse();
00082   void setCount(const int count);
00083   void makeEmpty(const int initsize = 4);
00084   void freeMemory();
00085   int  count() const;
00086   int  allocSize() const;
00087   T   *arrayPointer();
00088   const T *constArrayPointer() const;
00089   void shrinkToFit();
00090 
00091 private:
00092   void growArray();
00093   T *array;
00094   int num;
00095   int size;
00096 
00097 }; // class dimeArray<>
00098 
00099 template <class T> inline 
00100 dimeArray<T>::dimeArray(const int size)
00101 {
00102   this->array = new T[size];
00103   this->size = size;
00104   this->num = 0;
00105 }
00106 
00107 template <class T> inline 
00108 dimeArray<T>::~dimeArray()
00109 {
00110   delete [] this->array;
00111 }
00112 
00113 template <class T> inline void 
00114 dimeArray<T>::growArray()
00115 {
00116   int oldsize = this->size;
00117   T *oldarray = this->array;
00118   this->size <<= 1;
00119   this->array = new T[this->size];
00120   for (int i = 0; i < oldsize; i++) this->array[i] = oldarray[i];
00121   delete [] oldarray;
00122 }
00123 
00124 template <class T> inline void 
00125 dimeArray<T>::append(const T &elem)
00126 {
00127   if (this->num >= this->size) growArray();
00128   this->array[this->num++] = elem;
00129 }
00130 
00131 
00132 template <class T> inline void 
00133 dimeArray<T>::append(const dimeArray<T> &array)
00134 {
00135   while (this->size <= this->num+array.count()) growArray();
00136   for (int i=0;i<array.count();i++)
00137     this->array[this->num++] = array[i];
00138 }
00139 
00140 template <class T> inline void 
00141 dimeArray<T>::prepend(const dimeArray<T> &array)
00142 {
00143   int newsize=this->num+array.count();
00144   int i;
00145   if (this->size<=newsize) {
00146     T *oldarray=this->array;
00147     this->array=new T[newsize];
00148     this->size=newsize;
00149     for (i=0;i<array.count(); i++) this->array[i] = array[i];
00150     for (i=0;i<this->num;i++) this->array[i+array.count()] = oldarray[i];
00151     delete[] oldarray;
00152   }
00153   else {
00154     for (i=0;i<this->num;i++) this->array[array.count()+i]=this->array[i];
00155     for (i=0;i<array.count();i++) this->array[i] = array[i];
00156   }
00157   this->num+=array.count();
00158 }
00159 
00160 template <class T> inline void 
00161 dimeArray<T>::insertElem(const int idx, const T &elem)
00162 {
00163   int n = this->num;
00164   this->append(elem); // make room for one more
00165   if (idx < n) {
00166     for (int i = n; i > idx; i--) {
00167       this->array[i] = this->array[i-1]; 
00168     }
00169     this->array[idx] = elem;
00170   }
00171 }
00172 
00173 template <class T> inline void 
00174 dimeArray<T>::setElem(const int index, const T &elem)
00175 {
00176   while (index >= this->size) growArray();
00177   if (this->num <= index) this->num = index+1;
00178   this->array[index] = elem;
00179 }
00180 
00181 template <class T> inline T 
00182 dimeArray<T>::getElem(const int index) const
00183 {
00184   return this->array[index];
00185 }
00186 
00187 template <class T> inline void 
00188 dimeArray<T>::getElem(const int index, T &elem) const
00189 {
00190   elem = this->array[index];
00191 }
00192 
00193 template <class T> inline T 
00194 dimeArray<T>::getLastElem() const
00195 {
00196   return this->array[this->num-1];
00197 }
00198 
00199 template <class T> inline void 
00200 dimeArray<T>::getLastElem(T &elem) const
00201 {
00202   elem = this->array[this->num-1];
00203 }
00204 
00205 template <class T> inline T &
00206 dimeArray<T>::operator [](const int index)
00207 {
00208   while (index >= this->size) growArray();   
00209   if (this->num <= index) this->num = index + 1;
00210   return this->array[index];
00211 }
00212 
00213 template <class T> inline T 
00214 dimeArray<T>::operator [](const int index) const
00215 {
00216   return this->array[index];
00217 }
00218 
00219 template <class T> inline void 
00220 dimeArray<T>::removeElem(const int index)
00221 {
00222   if (this->num <= 0 || index >= this->num) return; 
00223   for (int i = index; i < this->num-1; i++)
00224     this->array[i] = this->array[i+1];
00225   this->num--;
00226 }
00227 
00228 template <class T> inline void 
00229 dimeArray<T>::removeElemFast(const int index)
00230 {
00231   this->array[index] = this->array[--this->num];
00232 }
00233 
00234 template <class T> inline void 
00235 dimeArray<T>::reverse()
00236 {
00237   T tmp;
00238   for (int i=0;i<this->num/2;i++) {
00239     tmp=this->array[i];
00240     this->array[i]=this->array[this->num-1-i];
00241     this->array[this->num-1-i]=tmp;
00242   }
00243 }
00244 
00245 template <class T> inline void 
00246 dimeArray<T>::setCount(const int count)
00247 {
00248   if (count < this->num)
00249     this->num = count;  
00250 }
00251 
00252 template <class T> inline int 
00253 dimeArray<T>::count() const
00254 {
00255   return this->num;
00256 }
00257 
00258 template <class T> inline int 
00259 dimeArray<T>::allocSize() const
00260 {
00261   return this->size;
00262 }
00263 
00264 template <class T> inline T *
00265 dimeArray<T>::arrayPointer()
00266 {
00267   return this->array;
00268 }
00269 
00270 template <class T> inline const T *
00271 dimeArray<T>::constArrayPointer() const
00272 {
00273   return this->array;
00274 }
00275 
00276 template <class T> inline void 
00277 dimeArray<T>::makeEmpty(const int initsize)
00278 {
00279   delete [] this->array;
00280   this->array = new T[initsize];
00281   this->size = initsize;
00282   this->num = 0; 
00283 }
00284 
00285 template <class T> inline void 
00286 dimeArray<T>::freeMemory()
00287 {
00288   delete [] this->array;
00289   this->array = NULL;
00290   this->size = 0;
00291   this->num = 0;
00292 }
00293 
00294 template <class T> inline void 
00295 dimeArray<T>::shrinkToFit()
00296 {
00297   T *oldarray = this->array;
00298   this->array = new T[this->num];
00299   for (int i = 0; i < this->num; i++) this->array[i] = oldarray[i];
00300   this->size = this->num;
00301   delete [] oldarray;
00302 }
00303 
00304 #endif // ! DIME_ARRAY_H
00305 

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