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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* This file is part of the class library */
/* SoPlex --- the Sequential object-oriented simPlex. */
/* */
/* Copyright 1996-2022 Zuse Institute Berlin */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* */
/* You should have received a copy of the Apache-2.0 license */
/* along with SoPlex; see the file LICENSE. If not email to soplex@zib.de. */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**@file classarray.h
* @brief Save arrays of data objects.
*/
#ifndef _CLASSARRAY_H_
#define _CLASSARRAY_H_
#include <assert.h>
#include <stddef.h>
#include <string.h>
#include <iostream>
#include "soplex/spxdefines.h"
#include "soplex/spxalloc.h"
namespace soplex
{
/**@brief Safe arrays of class objects.
* @ingroup Elementary
*
* Class ClassArray provides safe arrays of general C++ objects (in contrast to data objects). The elements of an
* instance of ClassArray can be accessed just like ordinary C++ array elements by means of the index
* operator[](). Safety is provided by
*
* - automatic memory management in constructor and destructor preventing memory leaks
* - checking of array bounds when accessing elements with the indexing operator[]() when compiled without \c -DNDEBUG
*
* Moreover, #ClassArray%s may easily be extended by #insert%ing or #append%ing elements to the ClassArray or shrunken
* by \ref remove() "removing" elements. Method reSize(int n) resets the ClassArray%s length to \p n thereby possibly
* appending elements or truncating the ClassArray to the required size.
*
* A ClassArray may be used as arguments for standard C functions requiring pointers through the use of get_ptr() and
* get_const_ptr().
*
* Internally, a ClassArray object allocates a block of memory that fits up to max() elements, only size() of them are
* used. This makes extension and shrinking methods perform better.
*
* @see Array, \ref DataObjects "Data Objects"
*/
template < class T >
class ClassArray
{
protected:
int thesize; ///< number of used elements in array data
int themax; ///< the length of array data and
T* data; ///< the array of elements
protected:
/** When a ClassArray is reSize()%d to more than max() elements, the new value for max() is not just set to the new
* size but rather to \p memFactor * \p size. This makes #reSize%ing perform better in codes where a ClassArray is
* extended often by a small number of elements only.
*/
double memFactor; ///< memory extension factor.
public:
/// Reference to \p n 'th element.
T& operator[](int n)
{
assert(n >= 0);
assert(n < thesize);
return data[n];
}
/// Reference to \p n 'th const element.
const T& operator[](int n) const
{
assert(n >= 0);
assert(n < thesize);
return data[n];
}
/// Reference to last element.
T& last()
{
assert(thesize > 0);
return data[thesize - 1];
}
/// Reference to last const element.
const T& last() const
{
assert(thesize > 0);
return data[thesize - 1];
}
/// Gets a C pointer to the data.
T* get_ptr()
{
return data;
}
/// Gets a const C pointer to the data.
const T* get_const_ptr() const
{
return data;
}
/// Appends element \p t.
void append(const T& t)
{
insert(thesize, 1, &t);
}
/// Appends \p n elements from \p t.
void append(int n, const T t[])
{
insert(thesize, n, t);
}
/// Appends all elements from \p t.
void append(const ClassArray<T>& t)
{
insert(thesize, t);
}
/// Inserts \p n uninitialized elements before \p i 'th element.
void insert(int i, int n)
{
assert(n >= 0);
assert(i >= 0);
assert(i <= thesize);
if(n > 0)
{
int j = thesize;
reSize(thesize + n);
assert(thesize == j + n);
/// move \p n elements in memory from insert position \p i to the back
while(j > i)
{
j--;
data[j + n] = data[j];
}
}
}
/// Inserts \p n elements from \p t before \p i 'the element.
void insert(int i, int n, const T t[])
{
if(n > 0)
{
insert(i, n);
for(int j = 0; j < n; j++)
data[i + j] = t[j];
}
}
/// Inserts all elements from \p t before \p i 'th element.
void insert(int i, const ClassArray<T>& t)
{
if(t.size())
{
insert(i, t.size());
for(int j = 0; j < t.size(); j++)
data[i + j] = t[j];
}
}
/// Removes \p m elements starting at \p n.
void remove(int n = 0, int m = 1)
{
assert(n >= 0);
assert(n < size());
assert(m >= 0);
assert(n + m <= size());
for(int j = n + m; j < size(); j++)
data[j - m] = data[j];
thesize -= m;
}
/// Removes \p m last elements.
void removeLast(int m = 1)
{
assert(m >= 0);
assert(m <= size());
thesize -= m;
}
/// Removes all elements.
void clear()
{
thesize = 0;
}
/// Returns number of elements.
int size() const
{
return thesize;
}
/// Resets size to \p newsize.
/** Resizing a ClassArray to less than the previous size, involves discarding its last elements. Resizing to a larger
* value involves adding uninitialized elements (similar to append()). If neccessary, also memory will be
* reallocated.
*/
void reSize(int newsize)
{
assert(memFactor >= 1);
if(newsize > themax)
reMax(int(memFactor * newsize), newsize);
else if(newsize < 0)
thesize = 0;
else
thesize = newsize;
}
/// Returns maximum number of elements.
/** Even though the ClassArray currently holds no more than size() elements, up to max() elements could be added
* without need to reallocated free store.
*/
int max() const
{
return themax;
}
/// Resets maximum number of elements.
/** The value of max() is reset to \p newMax thereby setting size() to \p newSize. However, if \p newSize has a value
* \c < \c 0 (as the default argument does) size() remains unchanged and max() is set to MIN(size(), newMax). Hence,
* calling reMax() without the default arguments, will reduce the memory consumption to a minimum. In no instance
* max() will be set to a value less than 1 (even if specified).
*
* @return reMax returns the difference in bytes of the new and the old memory block, which can be used to update
* pointers pointing to elements of the memory block.
*/
ptrdiff_t reMax(int newMax = 1, int newSize = -1)
{
/* process input */
if(newSize < 0)
newSize = size();
if(newMax < 1)
newMax = 1;
if(newMax < newSize)
newMax = newSize;
/* nothing to reallocate */
if(newMax == themax)
{
thesize = newSize;
return 0;
}
/* allocate new memory */
T* newMem = 0;
spx_alloc(newMem, newMax);
/* call copy constructor for first elements */
int i;
for(i = 0; i < size() && i < newSize; i++)
new(&(newMem[i])) T(data[i]);
/* call default constructor for remaining elements */
for(; i < newMax; i++)
new(&(newMem[i])) T();
/* compute pointer difference */
ptrdiff_t pshift = reinterpret_cast<char*>(newMem) - reinterpret_cast<char*>(data);
/* free old memory */
for(i = themax - 1; i >= 0; i--)
data[i].~T();
spx_free(data);
/* assign new memory */
data = newMem;
themax = newMax;
thesize = newSize;
return pshift;
}
/// Assignment operator.
ClassArray& operator=(const ClassArray& rhs)
{
if(this != &rhs)
{
reSize(rhs.size());
for(int i = 0; i < size(); i++)
data[i] = rhs.data[i];
assert(isConsistent());
}
return *this;
}
/// Consistency check.
bool isConsistent() const
{
#ifdef ENABLE_CONSISTENCY_CHECKS
if((data == 0)
|| (themax < 1)
|| (themax < thesize)
|| (thesize < 0)
|| (memFactor < 1.0))
return MSGinconsistent("ClassArray");
#endif
return true;
}
/// Copy constructor.
ClassArray(const ClassArray& old)
: thesize(old.thesize)
, themax(old.themax)
, data(0)
, memFactor(old.memFactor)
{
/* allocate memory */
spx_alloc(data, max());
/* call copy constructor for first elements */
int i;
for(i = 0; i < size(); i++)
new(&(data[i])) T(old.data[i]);
/* call default constructor for remaining elements */
for(; i < max(); i++)
new(&(data[i])) T();
assert(isConsistent());
}
/// Default constructor.
/** The constructor allocates a ClassArray containing \p size uninitialized elements. The internal array is allocated
* to have \p max nonzeros, and the memory extension factor is set to \p fac.
*
* @param p_size number of unitialised elements.
* @param p_max maximum number of elements the array can hold.
* @param p_fac value for memFactor.
*/
explicit ClassArray(int p_size = 0, int p_max = 0, double p_fac = 1.2)
: data(0)
, memFactor(p_fac)
{
thesize = (p_size < 0) ? 0 : p_size;
if(p_max > thesize)
themax = p_max;
else
themax = (thesize == 0) ? 1 : thesize;
spx_alloc(data, max());
/* call default constructor for each element */
for(int i = 0; i < max(); i++)
new(&(data[i])) T();
assert(isConsistent());
}
/// Destructor.
virtual ~ClassArray()
{
if(data)
{
for(int i = themax - 1; i >= 0; i--)
data[i].~T();
spx_free(data);
}
}
};
} // namespace soplex
#endif // _CLASSARRAY_H_