NGSolve  5.3
ngstd/symboltable.hpp
00001 #ifndef FILE_NGS_SYMBOLTABLE
00002 #define FILE_NGS_SYMBOLTABLE
00003 
00004 /**************************************************************************/
00005 /* File:   symboltable.hpp                                                */
00006 /* Author: Joachim Schoeberl                                              */
00007 /* Date:   01. Jun. 95                                                    */
00008 /**************************************************************************/
00009 
00010 namespace ngstd
00011 {
00012 
00017 class BaseSymbolTable
00018 {
00019 protected:
00021   Array <string> names;
00022   
00023 public:
00025   NGS_DLL_HEADER BaseSymbolTable () = default;
00027   NGS_DLL_HEADER BaseSymbolTable (const BaseSymbolTable & tab2);
00028   NGS_DLL_HEADER BaseSymbolTable (BaseSymbolTable && tab2) = default;
00029 
00031   NGS_DLL_HEADER ~BaseSymbolTable ();
00033   NGS_DLL_HEADER void DelNames ();
00034 
00036   NGS_DLL_HEADER void AppendName (const string & name);
00037 
00039   NGS_DLL_HEADER int Index (const string & name) const;
00040 
00042   NGS_DLL_HEADER int CheckIndex (const string & name) const;
00043 };
00044 
00045 
00046 
00047 
00048 
00049 
00056 template <class T>
00057 class SymbolTable : public BaseSymbolTable
00058 {
00060   Array <T> data;
00061 public:
00063   SymbolTable () = default;
00064   SymbolTable (const SymbolTable & tab2) = default;
00065   SymbolTable (SymbolTable && tab2) = default;
00066 
00068   int Size() const
00069   { 
00070     return data.Size(); 
00071   }
00072 
00073   /*
00075   T & operator[] (const char * name)
00076   {
00077     return data[Index (name)]; 
00078   }
00079   */
00080 
00082   T & operator[] (const string & name)
00083   {
00084     return data[Index (name)]; 
00085   }
00086 
00087   /*
00089   const T & operator[] (const char * name) const
00090   {
00091     return data[Index (name)]; 
00092   }
00093   */
00094   const T & operator[] (const string & name) const
00095   {
00096     return data[Index (name)];
00097   }
00098 
00100   T & operator[] (int i)
00101   { return data[i]; } 
00102 
00104   const T & operator[] (int i) const
00105   { return data[i]; } 
00106 
00108   const string & GetName (int i) const
00109   { return names[i]; }
00110 
00111 
00113   void Set (const string & name, const T & el)
00114   {
00115     int i = CheckIndex (name);
00116     if (i >= 0) 
00117       data[i] = el;
00118     else
00119       {
00120         data.Append (el);
00121         AppendName (name);
00122       }
00123   }
00124 
00125   bool Used (const string & name) const
00126   {
00127     return (CheckIndex(name) >= 0) ? 1 : 0;
00128   }
00129 
00131   inline void DeleteAll ()
00132   {
00133     DelNames();
00134     data.DeleteAll();
00135   }
00136 
00137   SymbolTable<T> & operator= (const SymbolTable<T> & tab2)
00138   {
00139     for (int i = 0; i < tab2.Size(); i++)
00140       Set (tab2.GetName(i), tab2[i]);
00141     return *this;
00142   }
00143 };
00144 
00145   template <typename T>
00146   ostream & operator<< (ostream & ost, const SymbolTable<T> & st)
00147   {
00148     for (int i = 0; i < st.Size(); i++)
00149       ost << st.GetName(i) << " : " << st[i] << endl;
00150     return ost;
00151   }
00152 
00153 
00154   template <typename T> 
00155   Archive & operator & (Archive & ar, SymbolTable<T> & table)
00156   {
00157     if (ar.Output())
00158       {
00159         ar << table.Size();
00160         for (int i = 0; i < table.Size(); i++)
00161           {
00162             ar << string (table.GetName(i));
00163             ar & table[i];
00164           }
00165       }
00166     else
00167       {
00168         int s;
00169         ar & s;
00170         for (int i = 0; i < s; i++)
00171           {
00172             string name;
00173             T entry;
00174             ar & name & entry;
00175             table.Set (name, entry);
00176           }
00177       }
00178     return ar;
00179   }
00180 
00181 
00182 }
00183 
00184 #endif