Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/netgen/libsrc/general/table.hpp
3206 views
1
#ifndef FILE_TABLE
2
#define FILE_TABLE
3
4
/**************************************************************************/
5
/* File: table.hpp */
6
/* Author: Joachim Schoeberl */
7
/* Date: 01. Jun. 95 */
8
/**************************************************************************/
9
10
/// Base class to generic class TABLE.
11
class BASE_TABLE
12
{
13
protected:
14
15
///
16
class linestruct
17
{
18
public:
19
///
20
int size;
21
///
22
int maxsize;
23
///
24
void * col;
25
};
26
27
///
28
ARRAY<linestruct> data;
29
char * oneblock;
30
31
public:
32
///
33
BASE_TABLE (int size);
34
///
35
BASE_TABLE (const FlatArray<int> & entrysizes, int elemsize);
36
///
37
~BASE_TABLE ();
38
///
39
void SetSize (int size);
40
///
41
void ChangeSize (int size);
42
43
/// increment size of entry i by one, i is 0-based
44
void IncSize (int i, int elsize)
45
{
46
if (data[i].size < data[i].maxsize)
47
data[i].size++;
48
else
49
IncSize2 (i, elsize);
50
}
51
///
52
void IncSize2 (int i, int elsize);
53
54
// void DecSize (int i);
55
56
///
57
void AllocateElementsOneBlock (int elemsize);
58
59
int AllocatedElements () const;
60
int UsedElements () const;
61
62
void SetElementSizesToMaxSizes ();
63
};
64
65
66
67
68
69
70
71
/**
72
Abstract data type TABLE.
73
74
To an integer i in the range from 1 to size a set of elements of the
75
generic type T is associated.
76
*/
77
template <class T, int BASE = 0>
78
class TABLE : public BASE_TABLE
79
{
80
public:
81
/// Creates table.
82
inline TABLE () : BASE_TABLE(0) { ; }
83
84
/// Creates table of size size
85
inline TABLE (int size) : BASE_TABLE (size) { ; }
86
87
/// Creates fixed maximal element size table
88
inline TABLE (const FlatArray<int,BASE> & entrysizes)
89
: BASE_TABLE (FlatArray<int> (entrysizes.Size(), const_cast<int*>(&entrysizes[BASE])),
90
sizeof(T))
91
{ ; }
92
93
/// Changes Size of table to size, deletes data
94
inline void SetSize (int size)
95
{
96
BASE_TABLE::SetSize (size);
97
}
98
99
/// Changes Size of table to size, keep data
100
inline void ChangeSize (int size)
101
{
102
BASE_TABLE::ChangeSize (size);
103
}
104
105
106
/// Inserts element acont into row i, BASE-based. Does not test if already used.
107
inline void Add (int i, const T & acont)
108
{
109
IncSize (i-BASE, sizeof (T));
110
((T*)data[i-BASE].col)[data[i-BASE].size-1] = acont;
111
}
112
113
114
/// Inserts element acont into row i, 1-based. Does not test if already used.
115
inline void Add1 (int i, const T & acont)
116
{
117
IncSize (i-1, sizeof (T));
118
((T*)data.Elem(i).col)[data.Elem(i).size-1] = acont;
119
}
120
121
///
122
void IncSizePrepare (int i)
123
{
124
data[i-BASE].maxsize++;
125
}
126
127
128
/// Inserts element acont into row i. BASE-based. Does not test if already used, assumes to have enough memory
129
inline void AddSave (int i, const T & acont)
130
{
131
((T*)data[i-BASE].col)[data[i-BASE].size] = acont;
132
data[i-BASE].size++;
133
}
134
135
/// Inserts element acont into row i. 1-based. Does not test if already used, assumes to have mem
136
inline void AddSave1 (int i, const T & acont)
137
{
138
((T*)data.Elem(i).col)[data.Elem(i).size] = acont;
139
data.Elem(i).size++;
140
}
141
142
/// Inserts element acont into row i. Does not test if already used.
143
inline void AddEmpty (int i)
144
{
145
IncSize (i-BASE, sizeof (T));
146
}
147
148
/** Set the nr-th element in the i-th row to acont.
149
Does not check for overflow. */
150
inline void Set (int i, int nr, const T & acont)
151
{ ((T*)data.Get(i).col)[nr-1] = acont; }
152
/** Returns the nr-th element in the i-th row.
153
Does not check for overflow. */
154
inline const T & Get (int i, int nr) const
155
{ return ((T*)data.Get(i).col)[nr-1]; }
156
157
158
/** Returns pointer to the first element in row i. */
159
inline const T * GetLine (int i) const
160
{
161
return ((const T*)data.Get(i).col);
162
}
163
164
165
/// Returns size of the table.
166
inline int Size () const
167
{
168
return data.Size();
169
}
170
171
/// Returns size of the i-th row.
172
inline int EntrySize (int i) const
173
{ return data.Get(i).size; }
174
175
/*
176
inline void DecEntrySize (int i)
177
{ DecSize(i); }
178
*/
179
void AllocateElementsOneBlock ()
180
{ BASE_TABLE::AllocateElementsOneBlock (sizeof(T)); }
181
182
183
inline void PrintMemInfo (ostream & ost) const
184
{
185
int els = AllocatedElements();
186
ost << "table: allocaed " << els
187
<< " a " << sizeof(T) << " Byts = "
188
<< els * sizeof(T)
189
<< " bytes in " << Size() << " bags."
190
<< " used: " << UsedElements()
191
<< endl;
192
}
193
194
/// Access entry.
195
FlatArray<T> operator[] (int i) const
196
{
197
#ifdef DEBUG
198
if (i-BASE < 0 || i-BASE >= data.Size())
199
cout << "table out of range, i = " << i << ", s = " << data.Size() << endl;
200
#endif
201
202
return FlatArray<T> (data[i-BASE].size, (T*)data[i-BASE].col);
203
}
204
};
205
206
207
template <class T, int BASE>
208
inline ostream & operator<< (ostream & ost, const TABLE<T,BASE> & table)
209
{
210
for (int i = BASE; i < table.Size()+BASE; i++)
211
{
212
ost << i << ": ";
213
FlatArray<T> row = table[i];
214
ost << "(" << row.Size() << ") ";
215
for (int j = 0; j < row.Size(); j++)
216
ost << row[j] << " ";
217
ost << endl;
218
}
219
return ost;
220
}
221
222
#endif
223
224
225