Path: blob/devel/ElmerGUI/netgen/libsrc/general/symbolta.hpp
3206 views
#ifndef FILE_SYMBOLTA1#define FILE_SYMBOLTA234/**************************************************************************/5/* File: symbolta.hh */6/* Author: Joachim Schoeberl */7/* Date: 01. Jun. 95 */8/**************************************************************************/910/**11Base class for the generic SYMBOLTABLE.12An array of identifiers is maintained.13*/14class BASE_SYMBOLTABLE15{16protected:17/// identifiers18ARRAY <char*> names;1920public:21/// Constructor22BASE_SYMBOLTABLE ();23///24~BASE_SYMBOLTABLE ();25///26void DelNames ();27/// Index of symbol name, returns 0 if not used.28int Index (const char * name) const;29};303132/**33Abstract data type Symbol Table.3435To a string an value of the generic type T is associated.36The string is not copied into the symbol table class!37*/38template <class T>39class SYMBOLTABLE : public BASE_SYMBOLTABLE40{41private:42/// Associated data43ARRAY <T> data;4445public:46/// Creates a symboltable47inline SYMBOLTABLE ();48/// Returns size of symboltable49inline INDEX Size() const;50/// Returns reference to element, error if not used51inline T & Elem (const char * name);52/// Returns reference to i-th element53inline T & Elem (int i)54{ return data.Elem(i); }55/// Returns element, error if not used56inline const T & Get (const char * name) const;57/// Returns i-th element58inline const T & Get (int i) const;59/// Returns name of i-th element60inline const char* GetName (int i) const;61/// Associates el to the string name, overrides if name is used62inline void Set (const char * name, const T & el);63/// Checks whether name is used64inline bool Used (const char * name) const;65/// Deletes symboltable66inline void DeleteAll ();6768inline T & operator[] (int i)69{ return data[i]; }70inline const T & operator[] (int i) const71{ return data[i]; }7273private:74/// Prevents from copying symboltable by pointer assignment75SYMBOLTABLE<T> & operator= (SYMBOLTABLE<T> &);76};7778798081template <class T>82inline SYMBOLTABLE<T> :: SYMBOLTABLE ()83{84;85}868788template <class T>89inline INDEX SYMBOLTABLE<T> :: Size() const90{91return data.Size();92}9394template <class T>95inline T & SYMBOLTABLE<T> :: Elem (const char * name)96{97int i = Index (name);98if (i)99return data.Elem (i);100else101return data.Elem(1);102}103104template <class T>105inline const T & SYMBOLTABLE<T> :: Get (const char * name) const106{107int i;108i = Index (name);109if (i)110return data.Get(i);111else112return data.Get(1);113}114115template <class T>116inline const T & SYMBOLTABLE<T> :: Get (int i) const117{118return data.Get(i);119}120121template <class T>122inline const char* SYMBOLTABLE<T> :: GetName (int i) const123{124return names.Get(i);125}126127template <class T>128inline void SYMBOLTABLE<T> :: Set (const char * name, const T & el)129{130int i;131i = Index (name);132if (i)133data.Set(i, el);134else135{136data.Append (el);137char * hname = new char [strlen (name) + 1];138strcpy (hname, name);139names.Append (hname);140}141}142143template <class T>144inline bool SYMBOLTABLE<T> :: Used (const char * name) const145{146return (Index(name)) ? true : false;147}148149template <class T>150inline void SYMBOLTABLE<T> :: DeleteAll ()151{152DelNames();153data.DeleteAll();154}155156157#endif158159160