1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
#include <assert.h>
#include <basic/Dynamic_Array.h>
namespace omega {
template<class T, int d> void Dynamic_Array<T,d>::do_constr()
{
// #if ! defined SHUT_UP_ABOUT_STATEMENT_WITH_NO_EFFECT_IN_DYNAMIC_ARRAY_CREATION
// assert(d > 0);
// #endif
bounds = 0;
elements = 0;
partial = false;
}
template<class T> void Dynamic_Array1<T>::do_construct(int d0)
{
this->bounds = new int[1];
this->bounds[0] = d0;
this->elements = new T [d0];
this->partial = false;
}
template<class T> void Dynamic_Array2<T>::do_construct(int d0, int d1)
{
this->bounds = new int[2];
this->bounds[0] = d0;
this->bounds[1] = d1;
this->elements = new T [d0 * d1];
this->partial = false;
}
template<class T> void Dynamic_Array3<T>::do_construct(int d0,int d1,int d2)
{
this->bounds = new int[3];
this->bounds[0] = d0;
this->bounds[1] = d1;
this->bounds[2] = d2;
this->elements = new T [d0 * d1 * d2];
this->partial = false;
}
template<class T> void Dynamic_Array4<T>::do_construct(int d0,int d1,int d2,int d3)
{
this->bounds = new int[4];
this->bounds[0] = d0;
this->bounds[1] = d1;
this->bounds[2] = d2;
this->bounds[3] = d3;
this->elements = new T [d0 * d1 * d2 * d3];
this->partial = false;
}
template<class T, int d> Dynamic_Array<T,d>::Dynamic_Array()
{
do_constr();
}
template<class T> Dynamic_Array1<T>::Dynamic_Array1(const char *)
{
this->do_constr();
}
template<class T> Dynamic_Array2<T>::Dynamic_Array2(const char *,const char *)
{
this->do_constr();
}
template<class T> Dynamic_Array3<T>::Dynamic_Array3(const char *,const char *,const char *)
{
this->do_constr();
}
template<class T> Dynamic_Array4<T>::Dynamic_Array4(const char *,const char *,const char *,const char *)
{
this->do_constr();
}
template<class T> Dynamic_Array1<T>::Dynamic_Array1(int d0)
{
do_construct(d0);
}
template<class T> Dynamic_Array2<T>::Dynamic_Array2(int d0, int d1)
{
do_construct(d0, d1);
}
template<class T> Dynamic_Array3<T>::Dynamic_Array3(int d0,int d1,int d2)
{
do_construct(d0, d1, d2);
}
template<class T> Dynamic_Array4<T>::Dynamic_Array4(int d0,int d1,int d2,int d3)
{
do_construct(d0, d1, d2, d3);
}
template<class T, int d> void Dynamic_Array<T,d>::do_destruct()
{
if (! partial)
{
delete [] bounds;
delete [] elements;
}
}
template<class T, int d> Dynamic_Array<T,d>::~Dynamic_Array()
{
do_destruct();
}
template<class T> void Dynamic_Array1<T>::resize(int d0)
{
assert(!this->partial);
this->do_destruct();
if (d0 == 0)
this->do_constr();
else
do_construct(d0);
}
template<class T> void Dynamic_Array2<T>::resize(int d0, int d1)
{
assert(!this->partial);
this->do_destruct();
if (d0 == 0 && d1 == 0)
this->do_constr();
else
do_construct(d0, d1);
}
template<class T> void Dynamic_Array3<T>::resize(int d0, int d1, int d2)
{
assert(!this->partial);
this->do_destruct();
if (d0 == 0 && d1 == 0 && d2 == 0)
this->do_constr();
else
do_construct(d0, d1, d2);
}
template<class T> void Dynamic_Array4<T>::resize(int d0, int d1, int d2, int d3)
{
assert(!this->partial);
this->do_destruct();
if (d0 == 0 && d1 == 0 && d2 == 0 && d3 == 0)
this->do_constr();
else
do_construct(d0, d1, d2, d3);
}
template<class T> T& Dynamic_Array1<T>::operator[](int d0)
{
#if !defined (NDEBUG)
assert(this->elements != 0 && "Trying to dereference undefined array");
assert(0 <= d0 && d0 < this->bounds[0] && "Array subscript out of bounds");
#endif
return this->elements[d0];
}
template<class T> Dynamic_Array1<T> Dynamic_Array2<T>::operator[](int d0)
{
#if !defined (NDEBUG)
assert(this->elements != 0 && "Trying to dereference undefined array");
assert(0 <= d0 && d0 < this->bounds[0] && "Array subscript out of bounds");
#endif
Dynamic_Array1<T> result;
result.bounds = this->bounds+1;
result.elements = this->elements + this->bounds[1] * d0;
result.partial = true;
return result;
}
template<class T> Dynamic_Array2<T> Dynamic_Array3<T>::operator[](int d0)
{
#if !defined (NDEBUG)
assert(this->elements != 0 && "Trying to dereference undefined array");
assert(0 <= d0 && d0 < this->bounds[0] && "Array subscript out of bounds");
#endif
Dynamic_Array2<T> result;
result.bounds = this->bounds+1;
result.elements = this->elements + this->bounds[1] * this->bounds[2] * d0;
result.partial = true;
return result;
}
template<class T> Dynamic_Array3<T> Dynamic_Array4<T>::operator[](int d0)
{
#if !defined (NDEBUG)
assert(this->elements != 0 && "Trying to dereference undefined array");
assert(0 <= d0 && d0 < this->bounds[0] && "Array subscript out of bounds");
#endif
Dynamic_Array3<T> result;
result.bounds = this->bounds+1;
result.elements = this->elements + this->bounds[1] * this->bounds[2] * this->bounds[3] * d0;
result.partial = true;
return result;
}
template<class T, int d>
Dynamic_Array<T,d>::Dynamic_Array(Dynamic_Array<T,d> &D)
{
assert(D.elements != 0 && "Trying to copy an undefined array");
partial = true;
bounds = D.bounds;
elements = D.elements;
}
} // namespace
|