Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/netgen/libsrc/general/symbolta.hpp
3206 views
1
#ifndef FILE_SYMBOLTA
2
#define FILE_SYMBOLTA
3
4
5
/**************************************************************************/
6
/* File: symbolta.hh */
7
/* Author: Joachim Schoeberl */
8
/* Date: 01. Jun. 95 */
9
/**************************************************************************/
10
11
/**
12
Base class for the generic SYMBOLTABLE.
13
An array of identifiers is maintained.
14
*/
15
class BASE_SYMBOLTABLE
16
{
17
protected:
18
/// identifiers
19
ARRAY <char*> names;
20
21
public:
22
/// Constructor
23
BASE_SYMBOLTABLE ();
24
///
25
~BASE_SYMBOLTABLE ();
26
///
27
void DelNames ();
28
/// Index of symbol name, returns 0 if not used.
29
int Index (const char * name) const;
30
};
31
32
33
/**
34
Abstract data type Symbol Table.
35
36
To a string an value of the generic type T is associated.
37
The string is not copied into the symbol table class!
38
*/
39
template <class T>
40
class SYMBOLTABLE : public BASE_SYMBOLTABLE
41
{
42
private:
43
/// Associated data
44
ARRAY <T> data;
45
46
public:
47
/// Creates a symboltable
48
inline SYMBOLTABLE ();
49
/// Returns size of symboltable
50
inline INDEX Size() const;
51
/// Returns reference to element, error if not used
52
inline T & Elem (const char * name);
53
/// Returns reference to i-th element
54
inline T & Elem (int i)
55
{ return data.Elem(i); }
56
/// Returns element, error if not used
57
inline const T & Get (const char * name) const;
58
/// Returns i-th element
59
inline const T & Get (int i) const;
60
/// Returns name of i-th element
61
inline const char* GetName (int i) const;
62
/// Associates el to the string name, overrides if name is used
63
inline void Set (const char * name, const T & el);
64
/// Checks whether name is used
65
inline bool Used (const char * name) const;
66
/// Deletes symboltable
67
inline void DeleteAll ();
68
69
inline T & operator[] (int i)
70
{ return data[i]; }
71
inline const T & operator[] (int i) const
72
{ return data[i]; }
73
74
private:
75
/// Prevents from copying symboltable by pointer assignment
76
SYMBOLTABLE<T> & operator= (SYMBOLTABLE<T> &);
77
};
78
79
80
81
82
template <class T>
83
inline SYMBOLTABLE<T> :: SYMBOLTABLE ()
84
{
85
;
86
}
87
88
89
template <class T>
90
inline INDEX SYMBOLTABLE<T> :: Size() const
91
{
92
return data.Size();
93
}
94
95
template <class T>
96
inline T & SYMBOLTABLE<T> :: Elem (const char * name)
97
{
98
int i = Index (name);
99
if (i)
100
return data.Elem (i);
101
else
102
return data.Elem(1);
103
}
104
105
template <class T>
106
inline const T & SYMBOLTABLE<T> :: Get (const char * name) const
107
{
108
int i;
109
i = Index (name);
110
if (i)
111
return data.Get(i);
112
else
113
return data.Get(1);
114
}
115
116
template <class T>
117
inline const T & SYMBOLTABLE<T> :: Get (int i) const
118
{
119
return data.Get(i);
120
}
121
122
template <class T>
123
inline const char* SYMBOLTABLE<T> :: GetName (int i) const
124
{
125
return names.Get(i);
126
}
127
128
template <class T>
129
inline void SYMBOLTABLE<T> :: Set (const char * name, const T & el)
130
{
131
int i;
132
i = Index (name);
133
if (i)
134
data.Set(i, el);
135
else
136
{
137
data.Append (el);
138
char * hname = new char [strlen (name) + 1];
139
strcpy (hname, name);
140
names.Append (hname);
141
}
142
}
143
144
template <class T>
145
inline bool SYMBOLTABLE<T> :: Used (const char * name) const
146
{
147
return (Index(name)) ? true : false;
148
}
149
150
template <class T>
151
inline void SYMBOLTABLE<T> :: DeleteAll ()
152
{
153
DelNames();
154
data.DeleteAll();
155
}
156
157
158
#endif
159
160