NGSolve  4.9
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 <char*> names;
00022   
00023 public:
00025   NGS_DLL_HEADER BaseSymbolTable ();
00027   NGS_DLL_HEADER ~BaseSymbolTable ();
00029   NGS_DLL_HEADER void DelNames ();
00030 
00032   NGS_DLL_HEADER void AppendName (const char * name);
00033 
00035   NGS_DLL_HEADER int Index (const char * name) const;
00036 
00038   NGS_DLL_HEADER int CheckIndex (const char * name) const;
00039 };
00040 
00041 
00042 
00043 
00044 
00045 
00052 template <class T>
00053 class SymbolTable : public BaseSymbolTable
00054 {
00056   Array <T> data;
00057 public:
00059   SymbolTable ()
00060   { ; }
00061 
00063   int Size() const
00064   { 
00065     return data.Size(); 
00066   }
00067 
00068 
00070   T & operator[] (const char * name)
00071   {
00072     return data[Index (name)]; 
00073   }
00074 
00076   T & operator[] (const string & name)
00077   {
00078     return data[Index (name.c_str())]; 
00079   }
00080 
00082   const T & operator[] (const char * name) const
00083   {
00084     return data[Index (name)]; 
00085   }
00086 
00087   const T & operator[] (const string & name) const
00088   {
00089     return data[Index (name.c_str())]; 
00090   }
00091 
00093   T & operator[] (int i)
00094   { return data[i]; } 
00095 
00097   const T & operator[] (int i) const
00098   { return data[i]; } 
00099 
00101   const char* GetName (int i) const
00102   { return names[i]; }
00103 
00104 
00106   void Set (const char * name, const T & el)
00107   {
00108     int i = CheckIndex (name);
00109     if (i >= 0) 
00110       data[i] = el;
00111     else
00112       {
00113         data.Append (el);
00114         AppendName (name);
00115       }
00116   }
00117 
00118   void Set (const string & name, const T & el)
00119   {
00120     Set (name.c_str(), el);
00121   }
00122 
00124   bool Used (const char * name) const
00125   {
00126     return (CheckIndex(name) >= 0) ? 1 : 0;
00127   }
00128 
00129   bool Used (const string & name) const
00130   {
00131     return Used (name.c_str());
00132   }
00133 
00135   inline void DeleteAll ()
00136   {
00137     DelNames();
00138     data.DeleteAll();
00139   }
00140 
00141 
00142   SymbolTable<T> & operator= (const SymbolTable<T> & tab2)
00143   {
00144     for (int i = 0; i < tab2.Size(); i++)
00145       Set (tab2.GetName(i), tab2[i]);
00146     return *this;
00147   }
00148 };
00149 
00150 template <typename T>
00151 ostream & operator<< (ostream & ost, SymbolTable<T> & st)
00152 {
00153   for (int i = 0; i < st.Size(); i++)
00154     ost << st.GetName(i) << " : " << st[i] << endl;
00155   return ost;
00156 }
00157 
00158 }
00159 
00160 #endif