-
Notifications
You must be signed in to change notification settings - Fork 3
/
array.h
474 lines (399 loc) · 12.4 KB
/
array.h
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//-< ARRAY.H >-------------------------------------------------------*--------*
// POST++ Version 1.0 (c) 1998 GARRET * ? *
// (Persistent Object Storage) * /\| *
// * / \ *
// Created: 17-Mar-98 K.A. Knizhnik * / [] \ *
// Last update: 17-Mar-98 K.A. Knizhnik * GARRET *
//-------------------------------------------------------------------*--------*
// Arrays: example of POST++ class definition
//-------------------------------------------------------------------*--------*
// P. Shaffer version
#ifndef __ARRAY_H__
#define __ARRAY_H__
#include "object.h"
BEGIN_POST_NAMESPACE
//--------------------------------------------------------------------------//
template<class T>
class array_of_scalar : public object
{
public:
size_t get_size() const { return size; }
T& operator[](size_t i) {
assert(i < size);
return buf[i];
}
T get_at(size_t i) const {
assert(i < size);
return buf[i];
}
void put_at(size_t i, T val) {
assert(i < size);
buf[i] = val;
}
static array_of_scalar* create(storage& store, size_t size)
{
return new (self_class,store,(size-1)*sizeof(T)) array_of_scalar(size);
}
array_of_scalar* reallocate(size_t new_size)
{
array_of_scalar* a = create(*get_storage(), new_size);
memcpy(a->buf, buf, sizeof(T)*(new_size > size ? size : new_size));
delete this;
return a;
}
T* body() { return buf; }
CLASSINFO(array_of_scalar, NO_REFS);
protected:
size_t size;
T buf[1];
array_of_scalar(size_t len) : size(len) {}
};
//--------------------------------------------------------------------------//
typedef array_of_scalar<char> ArrayOfChar;
typedef array_of_scalar<unsigned char> ArrayOfByte;
typedef array_of_scalar<unsigned short> ArrayOfWord;
typedef array_of_scalar<int> ArrayOfInt;
typedef array_of_scalar<unsigned> ArrayOfNat;
typedef array_of_scalar<float> ArrayOfFloat;
typedef array_of_scalar<double> ArrayOfDouble;
//--------------------------------------------------------------------------//
template<class T> class array_of_ptr : public object
{
public:
size_t get_size() const { return size; }
T*& operator[](size_t i)
{
assert(i < size);
return buf[i];
}
T* get_at(size_t i) const
{
assert(i < size);
return buf[i];
}
void put_at(size_t i, T* val)
{
assert(i < size);
buf[i] = val;
}
static array_of_ptr* create(storage& store, size_t size)
{
return new (self_class, store, (size-1)*sizeof(T*)) array_of_ptr(size);
}
array_of_ptr* reallocate(size_t new_size)
{
array_of_ptr* a = create(*get_storage(), new_size);
memcpy(a->buf, buf, sizeof(T*)*(new_size > size ? size : new_size));
delete this;
return a;
}
T** body() { return buf; }
CLASSINFO(array_of_ptr, VREFS(buf));
protected:
size_t size;
T* buf[1];
array_of_ptr(size_t len) : size(len) {}
};
//--------------------------------------------------------------------------//
typedef array_of_ptr<object> ArrayOfObject;
//--------------------------------------------------------------------------//
class POST_DLL_ENTRY String : public ArrayOfChar
{
public:
size_t get_length() const { return size-1; }
int compare(const char* str) const { return strcmp(buf, str); }
int compare(String const& str) const { return -str.compare(buf); }
int index(const char* str) const
{
char* p = strstr((char*)buf, (char*)str);
return p ? p - buf : -1;
}
bool operator == (const char* ptr) const
{
return compare(ptr) == 0;
}
bool operator != (const char* ptr) const
{
return compare(ptr) != 0;
}
static String* create(storage& store, size_t size)
{
return new (self_class, store, size-1) String(size);
}
static String* create(storage& store, const char* str)
{
size_t len = strlen(str);
String* s = new (self_class, store, len) String(len+1);
memcpy(s->buf, str, len+1);
return s;
}
CLASSINFO(String, NO_REFS);
protected:
String(size_t len) : ArrayOfChar(len) {}
};
//--------------------------------------------------------------------------//
template<class T> class matrix : public object
{
public:
size_t get_number_of_rows() const { return rows; }
size_t get_number_of_colons() const { return colons; }
T& operator()(size_t i, size_t j) {
assert(i < rows && j < colons);
return buf[i*colons + j];
}
T get_at(size_t i, size_t j) const {
assert(i < rows && j < colons);
return buf[i*colons + j];
}
void put_at(size_t i, size_t j, T val) {
assert(i < rows && j < colons);
buf[i*colons + j] = val;
}
static matrix* create(storage& store, size_t rows, size_t colons)
{
return new (self_class, store, (rows*colons-1)*sizeof(T))
matrix(rows, colons);
}
CLASSINFO(matrix, NO_REFS);
protected:
size_t rows, colons;
T buf[1];
matrix(size_t n_rows, size_t n_colons) : rows(n_rows), colons(n_colons) {}
};
//--------------------------------------------------------------------------//
typedef matrix<char> MatrixOfChar;
typedef matrix<unsigned char> MatrixOfByte;
typedef matrix<unsigned short> MatrixOfWord;
typedef matrix<int> MatrixOfInt;
typedef matrix<unsigned> MatrixOfNat;
typedef matrix<float> MatrixOfFloat;
typedef matrix<double> MatrixOfDouble;
//--------------------------------------------------------------------------//
template<class T> class dyn_array_of_scalar : public object
{
public:
size_t get_size() const { return used; }
T& operator[](size_t i)
{
assert(i < used);
return (*arr)[i];
}
T get_at(size_t i) const
{
assert(i < used);
return arr->get_at(i);
}
void put_at(size_t i, T val)
{
assert(i < used);
arr->put_at(i, val);
}
void set_size(size_t new_size)
{
if (new_size > allocated_size)
{
allocated_size = (new_size < allocated_size*2)
? allocated_size*2 : new_size;
arr = arr->reallocate(allocated_size);
}
used = new_size;
}
void push(T val)
{
if (used == allocated_size)
{
allocated_size = (allocated_size+1)*2;
arr = arr->reallocate(allocated_size);
}
arr->put_at(used++, val);
}
void ins(size_t pos, size_t count, T val)
{
size_t size = used;
assert(pos <= size);
set_size(size + count);
T* p = arr->body();
size_t dst, src;
for (dst = size+count, src = size; src > pos; p[--dst] = p[--src]);
while (dst > pos) p[--dst] = val;
}
void del(size_t pos, size_t count)
{
size_t size = used;
assert(pos + count <= size);
size_t dst, src;
T* p = arr->body();
for (dst = pos, src = dst+count; src < size; p[dst++] = p[src++]);
used -= count;
}
T pop() {
assert(used != 0);
return arr->get_at(--used);
}
T* body() { return arr->body(); }
dyn_array_of_scalar(size_t init_allocated_len, size_t init_used_len = 0)
{
used = init_used_len;
allocated_size = init_used_len > init_allocated_len
? init_used_len : init_allocated_len;
arr = array_of_scalar<T>::create(*get_storage(), allocated_size);
}
~dyn_array_of_scalar() { delete arr; }
CLASSINFO(dyn_array_of_scalar, REF(arr));
protected:
size_t allocated_size;
size_t used;
array_of_scalar<T>* arr;
};
//--------------------------------------------------------------------------//
template<class T> class dyn_array_of_ptr : public object
{
public:
size_t get_size() const
{
return used;
}
T*& operator[](size_t i)
{
assert(i < used);
return (*arr)[i];
}
T* get_at(size_t i) const
{
assert(i < used);
return arr->get_at(i);
}
void put_at(size_t i, T* val)
{
assert(i < used);
arr->put_at(i, val);
}
void set_size(size_t new_size)
{
if (new_size > allocated_size)
{
allocated_size = (new_size < allocated_size*2)
? allocated_size*2 : new_size;
arr = arr->reallocate(allocated_size);
} else {
T** p = arr->body();
for (size_t i = used; i > new_size; p[--i] = NULL);
}
used = new_size;
}
void push(T* val)
{
if (used == allocated_size)
{
allocated_size = (allocated_size+1)*2;
arr = arr->reallocate( allocated_size );
}
arr->put_at(used++, val);
}
void ins(size_t pos, size_t count, T* val)
{
size_t size = used;
assert(pos <= size);
set_size(size + count);
T** p = arr->body();
size_t dst, src;
for (dst = size+count, src = size; src > pos; p[--dst] = p[--src]);
while (dst > pos) p[--dst] = val;
}
void del(size_t pos, size_t count)
{
size_t size = used;
assert(pos + count <= size);
size_t dst, src;
T** p = arr->body();
for (dst = pos, src = dst+count; src < size; p[dst++] = p[src++]);
used -= count;
while (count != 0) {
p[dst++] = NULL;
count -= 1;
}
}
T* pop()
{
assert(used != 0);
T* top = arr->get_at(--used);
arr->put_at(used, NULL);
return top;
}
T** body() { return arr->body(); }
dyn_array_of_ptr(size_t init_allocated_len, size_t init_used_len = 0)
{
used = init_used_len;
allocated_size = init_used_len > init_allocated_len
? init_used_len : init_allocated_len;
arr = array_of_ptr<T>::create(*get_storage(), allocated_size);
}
~dyn_array_of_ptr() { delete arr; }
CLASSINFO(dyn_array_of_ptr, REF(arr));
protected:
size_t allocated_size;
size_t used;
array_of_ptr<T>* arr;
};
//--------------------------------------------------------------------------//
typedef POST_DLL_ENTRY dyn_array_of_scalar<char> DynArrayOfChar;
typedef dyn_array_of_scalar<unsigned char> DynArrayOfByte;
typedef dyn_array_of_scalar<unsigned short> DynArrayOfWord;
typedef dyn_array_of_scalar<int> DynArrayOfInt;
typedef dyn_array_of_scalar<unsigned> DynArrayOfNat;
typedef dyn_array_of_scalar<float> DynArrayOfFloat;
typedef dyn_array_of_scalar<double> DynArrayOfDouble;
typedef dyn_array_of_ptr<object> DynArrayOfObject;
//------------------------------------------------------------------------//
class POST_DLL_ENTRY DynString : public DynArrayOfChar
{
public:
size_t get_length() const
{
return used-1;
}
int compare(const char* str) const
{
return strcmp(arr->body(), str);
}
int compare(DynString const& str) const
{
return -str.compare(arr->body());
}
int index(const char* str) const
{
char* p = strstr((char*)arr->body(), (char*)str);
return p ? p - arr->body() : -1;
}
bool operator == (const char* ptr) const
{
return compare(ptr) == 0;
}
bool operator != (const char* ptr) const
{
return compare(ptr) != 0;
}
void operator = (const char* str)
{
assert(this);
size_t len = strlen(str);
set_size(len+1);
memcpy(arr->body(), str, len+1);
}
static DynString* create(storage& store, size_t size)
{
return new (self_class, store, size-1) DynString(size);
}
static DynString* create(storage& store, const char* str)
{
size_t len = strlen(str);
DynString* s = new (self_class, store) DynString(len+1);
memcpy(s->arr->body(), str, len+1);
return s;
}
CLASSINFO(DynString, NO_REFS);
protected:
DynString(size_t len) : DynArrayOfChar(len) {}
};
END_POST_NAMESPACE
#endif