Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/netgen/libsrc/general/table.cpp
3206 views
1
/**************************************************************************/
2
/* File: table.cpp */
3
/* Author: Joachim Schoeberl */
4
/* Date: 01. Jun. 95 */
5
/**************************************************************************/
6
7
/*
8
Abstract data type TABLE
9
*/
10
11
#include <mystdlib.h>
12
#include <myadt.hpp>
13
14
namespace netgen
15
{
16
//using namespace netgen;
17
18
BASE_TABLE :: BASE_TABLE (int size)
19
: data(size)
20
{
21
for (int i = 0; i < size; i++)
22
{
23
data[i].maxsize = 0;
24
data[i].size = 0;
25
data[i].col = NULL;
26
}
27
oneblock = NULL;
28
}
29
30
BASE_TABLE :: BASE_TABLE (const FlatArray<int> & entrysizes, int elemsize)
31
: data(entrysizes.Size())
32
{
33
int i, cnt = 0;
34
int n = entrysizes.Size();
35
36
for (i = 0; i < n; i++)
37
cnt += entrysizes[i];
38
oneblock = new char[elemsize * cnt];
39
// mem_total_alloc_table += elemsize * cnt;
40
41
cnt = 0;
42
for (i = 0; i < n; i++)
43
{
44
data[i].maxsize = entrysizes[i];
45
data[i].size = 0;
46
47
data[i].col = &oneblock[elemsize * cnt];
48
cnt += entrysizes[i];
49
}
50
}
51
52
BASE_TABLE :: ~BASE_TABLE ()
53
{
54
if (oneblock)
55
delete [] oneblock;
56
else
57
{
58
for (int i = 0; i < data.Size(); i++)
59
delete [] (char*)data[i].col;
60
}
61
}
62
63
void BASE_TABLE :: SetSize (int size)
64
{
65
for (int i = 0; i < data.Size(); i++)
66
delete [] (char*)data[i].col;
67
68
data.SetSize(size);
69
for (int i = 0; i < size; i++)
70
{
71
data[i].maxsize = 0;
72
data[i].size = 0;
73
data[i].col = NULL;
74
}
75
}
76
77
void BASE_TABLE :: ChangeSize (int size)
78
{
79
int oldsize = data.Size();
80
if (size == oldsize)
81
return;
82
83
if (size < oldsize)
84
for (int i = size; i < oldsize; i++)
85
delete [] (char*)data[i].col;
86
87
data.SetSize(size);
88
89
for (int i = oldsize; i < size; i++)
90
{
91
data[i].maxsize = 0;
92
data[i].size = 0;
93
data[i].col = NULL;
94
}
95
}
96
97
void BASE_TABLE :: IncSize2 (int i, int elsize)
98
{
99
#ifdef DEBUG
100
if (i < 0 || i >= data.Size())
101
{
102
MyError ("BASE_TABLE::Inc: Out of range");
103
return;
104
}
105
#endif
106
107
linestruct & line = data[i];
108
if (line.size == line.maxsize)
109
{
110
void * p = new char [(line.maxsize+5) * elsize];
111
112
memcpy (p, line.col, line.maxsize * elsize);
113
delete [] (char*)line.col;
114
115
line.col = p;
116
line.maxsize += 5;
117
}
118
119
line.size++;
120
}
121
122
123
124
/*
125
void BASE_TABLE :: DecSize (int i)
126
{
127
#ifdef DEBUG
128
if (i < 0 || i >= data.Size())
129
{
130
MyError ("BASE_TABLE::Dec: Out of range");
131
return;
132
}
133
#endif
134
135
linestruct & line = data[i];
136
137
#ifdef DEBUG
138
if (line.size == 0)
139
{
140
MyError ("BASE_TABLE::Dec: EntrySize < 0");
141
return;
142
}
143
#endif
144
145
line.size--;
146
}
147
*/
148
149
150
151
void BASE_TABLE :: AllocateElementsOneBlock (int elemsize)
152
{
153
int cnt = 0;
154
int n = data.Size();
155
156
for (int i = 0; i < n; i++)
157
cnt += data[i].maxsize;
158
oneblock = new char[elemsize * cnt];
159
160
cnt = 0;
161
for (int i = 0; i < n; i++)
162
{
163
data[i].size = 0;
164
data[i].col = &oneblock[elemsize * cnt];
165
cnt += data[i].maxsize;
166
}
167
}
168
169
170
171
int BASE_TABLE :: AllocatedElements () const
172
{
173
int els = 0;
174
for (int i = 0; i < data.Size(); i++)
175
els += data[i].maxsize;
176
return els;
177
}
178
179
int BASE_TABLE :: UsedElements () const
180
{
181
int els = 0;
182
for (int i = 0; i < data.Size(); i++)
183
els += data[i].size;
184
return els;
185
}
186
187
void BASE_TABLE :: SetElementSizesToMaxSizes ()
188
{
189
for (int i = 0; i < data.Size(); i++)
190
data[i].size = data[i].maxsize;
191
}
192
193
}
194
195