NGSolve  4.9
ngstd/table.hpp
00001 #ifndef FILE_NGS_TABLE
00002 #define FILE_NGS_TABLE
00003 
00004 /**************************************************************************/
00005 /* File:   table.hpp                                                      */
00006 /* Author: Joachim Schoeberl                                              */
00007 /* Date:   25. Mar. 2000                                                  */
00008 /**************************************************************************/
00009 
00010 
00011 namespace ngstd
00012 {
00013 
00014 
00015 
00016 
00021 class BaseTable
00022 {
00023 protected:
00025   int size;
00027   int * index;
00028 
00029 public:  
00030   NGS_DLL_HEADER BaseTable (int asize, int entrysize);
00031   NGS_DLL_HEADER BaseTable (const FlatArray<int> & entrysize);
00032   NGS_DLL_HEADER ~BaseTable ();
00033   int * Index() const { return index; }
00034 };
00035 
00036 
00042 template <class T>
00043 class Table : public BaseTable
00044 {
00045 protected:
00047   T * data;
00048 
00049 public:
00051   Table (int asize, int entrysize)
00052     : BaseTable (asize, entrysize) 
00053   { 
00054     data = new T[index[size]]; 
00055   }
00056 
00058   Table (const FlatArray<int> & entrysize)
00059     : BaseTable (entrysize)
00060   {
00061     data = new T[index[size]]; 
00062   }
00063 
00065   ~Table ()
00066   {
00067     delete [] data; 
00068   }
00069 
00071   int Size() const { return size; }
00072   
00074   int NElements() const { return index[size]; }
00075 
00076   /*
00078   FlatArray<T> operator[] (int i) 
00079   { 
00080     return FlatArray<T> (index[i+1]-index[i], data+index[i]); 
00081   }
00082   */
00084   const FlatArray<T> operator[] (int i) const 
00085   { 
00086     return FlatArray<T> (index[i+1]-index[i], data+index[i]); 
00087   }
00088 
00089   T * Data() const { return data; }
00090 
00091   FlatArray<T> AsArray() const
00092   {
00093     return FlatArray<T> (index[size], data);
00094   }
00095 };
00096 
00097 
00099 template <class T>
00100 inline ostream & operator<< (ostream & s, const Table<T> & table)
00101 {
00102   for (int i = 0; i < table.Size(); i++)
00103     {
00104       s << i << ":";
00105       for (int j = 0; j < table[i].Size(); j++)
00106         s << " " << table[i][j];
00107       s << "\n";
00108     }
00109   s << flush;
00110   return s;
00111 }
00112 
00113 
00114 
00115 
00116 template <class T>
00117   class TableCreator
00118   {
00119   protected:  
00120     int mode;    // 1 .. cnt, 2 .. cnt entries, 3 .. fill table
00121     int nd;
00122     Array<int> cnt;
00123     Table<T> * table;
00124   public:
00125     TableCreator()
00126     { nd = 0; mode = 1; table = NULL; }
00127     TableCreator(int acnt)
00128     { nd = acnt; table = NULL; SetMode(2); }
00129     
00130     Table<T> * GetTable() { return table; }
00131 
00132     bool Done () { return mode > 3; }
00133     void operator++(int) { SetMode (mode+1); }
00134 
00135     int GetMode () const { return mode; }
00136     void SetMode (int amode) 
00137     {
00138       mode = amode; 
00139       if (mode == 2) 
00140         {
00141           cnt.SetSize(nd);
00142           cnt = 0; 
00143         }
00144       if (mode == 3)
00145         {
00146           table = new Table<T> (cnt);
00147           cnt = 0;
00148         }
00149     }
00150 
00151     void Add (int blocknr, const T & data)
00152     {
00153       switch (mode)
00154         {
00155         case 1:
00156           if (blocknr+1 > nd) nd = blocknr+1; 
00157           break;
00158         case 2:
00159           cnt[blocknr]++;
00160           break;
00161         case 3:
00162           (*table)[blocknr][cnt[blocknr]++] = data;
00163           break;
00164         }
00165     }
00166 
00167 
00168     void Add (int blocknr, IntRange range)
00169     {
00170       switch (mode)
00171         {
00172         case 1:
00173           if (blocknr+1 > nd) nd = blocknr+1; 
00174           break;
00175         case 2:
00176           cnt[blocknr]+=range.Size();
00177           break;
00178         case 3:
00179           for (int j = 0; j < range.Size(); j++)
00180             (*table)[blocknr][cnt[blocknr]+j] = range.First()+j;
00181           cnt[blocknr]+=range.Size();
00182           break;
00183         }
00184     }
00185 
00186   };
00187 
00188   class BitArray;
00189   
00190   class NGS_DLL_HEADER FilteredTableCreator : public TableCreator<int>
00191   {
00192   protected:
00193     const BitArray* takedofs;  
00194   public:
00195     FilteredTableCreator(const BitArray* atakedofs) 
00196       : TableCreator<int>(), takedofs(atakedofs) { };
00197     FilteredTableCreator(int acnt, const BitArray* atakedofs)
00198       : TableCreator<int>(acnt),takedofs(atakedofs) { };
00199     void Add (int blocknr, const int & data);
00200     void Add (int blocknr, IntRange range);
00201   };
00202 
00203 
00204 
00205 
00206 
00207 
00208 
00209 
00211 class BaseDynamicTable
00212 {
00213 protected:
00214   
00216   struct linestruct
00217   {
00219     int size;
00221     int maxsize;
00223     void * col;
00224   };
00225   
00227   Array<linestruct> data;
00229   char * oneblock;
00230 
00231 public:
00233   NGS_DLL_HEADER BaseDynamicTable (int size);
00235   NGS_DLL_HEADER BaseDynamicTable (const Array<int> & entrysizes, int elemsize);
00237   NGS_DLL_HEADER ~BaseDynamicTable ();
00238 
00240   NGS_DLL_HEADER void SetSize (int size);
00242   NGS_DLL_HEADER void IncSize (int i, int elsize);
00243 
00244   NGS_DLL_HEADER void DecSize (int i);
00245 };
00246 
00247 
00248 
00255 template <class T>
00256 class DynamicTable : public BaseDynamicTable
00257 {
00258 public:
00260   DynamicTable (int size = 0)
00261     : BaseDynamicTable (size) { ; }
00262 
00264   DynamicTable (const Array<int> & entrysizes)
00265     : BaseDynamicTable (entrysizes, sizeof(T)) { ; }
00266 
00268   void Add (int i, const T & acont)
00269   {
00270     if (data[i].size == data[i].maxsize)
00271       IncSize (i, sizeof (T));
00272     else
00273       data[i].size++;
00274     static_cast<T*> (data[i].col) [data[i].size-1] = acont;
00275   }
00276 
00278   void AddUnique (int i, const T & cont)
00279   {
00280     int es = EntrySize (i);
00281     int * line = const_cast<int*> (GetLine (i));
00282     for (int j = 0; j < es; j++)
00283       if (line[j] == cont)
00284         return;
00285     Add (i, cont);
00286   }
00287 
00288 
00290   void AddEmpty (int i)
00291   {
00292     IncSize (i, sizeof (T));
00293   }
00294 
00297   void Set (int i, int nr, const T & acont)
00298   { static_cast<T*> (data[i].col)[nr] = acont; }
00299   
00300 
00303   const T & Get (int i, int nr) const
00304   { return static_cast<T*> (data[i].col)[nr]; }
00305 
00306 
00308   const T * GetLine (int i) const
00309   { return static_cast<T*> (data[i].col); }
00310   
00311   
00313   int Size () const
00314   { return data.Size(); }
00315 
00317   int EntrySize (int i) const
00318   { return data[i].size; }
00319   
00321   void DecEntrySize (int i)
00322   { DecSize(i); }
00323 
00325   FlatArray<T> operator[] (int i) 
00326   { return FlatArray<T> (data[i].size, static_cast<T*> (data[i].col)); }
00327 
00328   typedef const FlatArray<T> ConstFlatArray;
00330   ConstFlatArray operator[] (int i) const
00331   { return FlatArray<T> (data[i].size, static_cast<T*> (data[i].col)); }
00332 };
00333 
00334 
00335 
00336 
00338 template <class T>
00339 inline ostream & operator<< (ostream & s, const DynamicTable<T> & table)
00340 {
00341   for (int i = 0; i < table.Size(); i++)
00342     {
00343       s << i << ":";
00344       for (int j = 0; j < table[i].Size(); j++)
00345         s << " " << table[i][j];
00346       s << "\n";
00347     }
00348   s << flush;
00349   return s;
00350 }
00351 
00352 typedef DynamicTable<int> IntTable;
00353 
00354 }
00355 
00356 #endif
00357