File: Array.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (524 lines) | stat: -rw-r--r-- 11,635 bytes parent folder | download | duplicates (2)
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#include "stdafx.h"
#include "Array.h"
#include "StrBuf.h"
#include "GcType.h"
#include "Random.h"
#include "Sort.h"
#include "Exception.h"

namespace storm {


	ArrayBase::ArrayBase(const Handle &type) : handle(type), data(null) {}

	ArrayBase::ArrayBase(const Handle &type, Nat n, const void *src) : handle(type), data(null) {
		ensure(n);

		for (nat i = 0; i < n; i++) {
			handle.safeCopy(ptr(i), src);
			data->filled = i + 1;
		}
	}

	ArrayBase::ArrayBase(const ArrayBase &other) : handle(other.handle), data(null) {
		nat count = other.count();
		if (handle.copyFn) {
			ensure(count);

			for (nat i = 0; i < count; i++) {
				(*handle.copyFn)(ptr(i), other.ptr(i));
				// Remember the element was created, if we get an exception during copying.
				data->filled = i + 1;
			}
		} else if (count > 0) {
			// Memcpy will do!
			ensure(count);
			memcpy(ptr(0), other.ptr(0), count * handle.size);
			data->filled = count;
		}
	}

	void ArrayBase::deepCopy(CloneEnv *env) {
		if (handle.deepCopyFn) {
			for (nat i = 0; i < count(); i++)
				(*handle.deepCopyFn)(ptr(i), env);
		}
	}

	void ArrayBase::ensure(Nat n) {
		if (n == 0)
			return;

		Nat oldCap = data ? Nat(data->count) : 0;
		if (oldCap >= n)
			return;

		Nat oldCount = count();

		// We need to grow 'data'. How much?
		Nat newCap = max(Nat(16), oldCap * 2);
		if (newCap < n)
			newCap = n;
		GcArray<byte> *newData = runtime::allocArray<byte>(engine(), handle.gcArrayType, newCap);

		// Move data.
		if (data) {
			memcpy(ptr(newData, 0), ptr(0), handle.size*oldCount);
			data->filled = 0;
			newData->filled = oldCount;
		}

		// Swap contents.
		data = newData;
	}

	void ArrayBase::reserve(Nat n) {
		ensure(n);
	}

	void ArrayBase::clear() {
		data = null;
	}

	void ArrayBase::remove(Nat id) {
		if (id >= count())
			throw new (this) ArrayError(id, count());

		handle.safeDestroy(ptr(id));
		memmove(ptr(id), ptr(id + 1), (count() - id - 1)*handle.size);
		data->filled--;
	}

	void ArrayBase::pop() {
		if (empty())
			throw new (this) ArrayError(0, 0, new (this) Str(S("pop")));

		Nat id = count() - 1;
		handle.safeDestroy(ptr(id));
		data->filled--;
	}

	void ArrayBase::insertRaw(Nat to, const void *item) {
		if (to > count())
			throw new (this) ArrayError(to, count(), new (this) Str(S("insert")));

		ensure(count() + 1);

		// Move the last few elements away.
		memmove(ptr(to + 1), ptr(to), (count() - to)*handle.size);
		// Insert the new one.
		handle.safeCopy(ptr(to), item);

		data->filled++;
	}

	ArrayBase *ArrayBase::appendRaw(ArrayBase *from) {
		Nat here = count();
		Nat copy = from->count();
		if (here + copy == 0)
			return this;

		ensure(here + copy);

		// Copy elements.
		if (handle.copyFn) {
			// Use the copy-ctor.
			for (Nat i = 0; i < copy; i++) {
				(*handle.copyFn)(ptr(here + i), ptr(from->data, i));
			}
		} else {
			// Memcpy is enough.
			memcpy(ptr(here), ptr(from->data, 0), handle.size * copy);
		}

		data->filled = here + copy;

		return this;
	}

	static void arraySwap(void *a, void *b, size_t size) {
		// Note: we can always move an object (the GC does it all the time), which is why this works
		// without issues. We just need to make sure we don't copy byte by byte, as the GC may pause
		// the world partly through and see an invalid pointer.

		nat copied = 0;
		{
			size_t *u = (size_t *)a;
			size_t *v = (size_t *)b;

			for (; copied + sizeof(size_t) <= size; copied += sizeof(size_t))
				std::swap(*u++, *v++);
		}

		// Any remaining bytes?
		{
			byte *u = (byte *)a;
			byte *v = (byte *)b;

			for (; copied < size; copied++)
				std::swap(u[copied], v[copied]);
		}
	}

	void ArrayBase::reverse() {
		if (empty())
			return;

		nat first = 0;
		nat last = count();

		while ((first != last) && (first != --last)) {
			arraySwap(ptr(first), ptr(last), handle.size);
			first++;
		}
	}

	void *ArrayBase::randomRaw() const {
		if (empty())
			throw new (this) ArrayError(0, 0, new (this) Str(S("random")));

		Nat id = rand(Nat(0), count());
		return getRaw(id);
	}

	void ArrayBase::sortRaw() {
		assert(handle.lessFn, L"The operator < is required when sorting an array.");

		if (empty())
			return;

		// We need one temporary element.
		ensure(count() + 1);

		SortData d(data, handle);
		sort(d);
	}

	void ArrayBase::sortRawPred(FnBase *compare) {
		if (empty())
			return;

		// We need one temporary element.
		ensure(count() + 1);

		SortData d(data, handle, compare);
		sort(d);
	}

	void ArrayBase::removeDuplicatesRaw() {
		if (count() == 0)
			return;

		Bool isEqual = true;
		Bool (*compare)(const void *, const void *) = handle.equalFn;
		if (!compare) {
			isEqual = false;
			compare = handle.lessFn;
		}

		Nat out = 0;
		for (Nat i = 1; i < count(); i++) {
			if (compare(ptr(out), ptr(i)) != isEqual) {
				++out;
				if (out != i)
					arraySwap(ptr(out), ptr(i), handle.size);
			}
		}

		while (count() > out + 1)
			pop();
	}

	void ArrayBase::removeDuplicatesRawPred(FnBase *compare) {
		if (count() == 0)
			return;

		RawFn compareFn = compare->rawCall();

		// Support *both* compare being < and == by checking:
		Bool isEqual = false;
		{
			const void *params[2] = { ptr(0), ptr(0) };
			compareFn.call(compare, &isEqual, params);
		}

		Nat out = 0;
		for (Nat i = 1; i < count(); i++) {
			// Compare:
			Bool equal = false;
			const void *params[2] = { ptr(out), ptr(i) };
			compareFn.call(compare, &equal, params);

			if (equal != isEqual) {
				++out;
				if (out != i)
					arraySwap(ptr(out), ptr(i), handle.size);
			}
		}

		while (count() > out + 1)
			pop();
	}

	ArrayBase *ArrayBase::withoutDuplicatesRaw() const {
		// Create a copy:
		void *copyMem = runtime::allocObject(sizeof(ArrayBase), runtime::typeOf(this));
		ArrayBase *copy = new (Place(copyMem)) ArrayBase(handle);
		runtime::setVTable(copy);

		// Copy unique elements:
		if (count() == 0)
			return copy;

		Bool isEqual = true;
		Bool (*compare)(const void *, const void *) = handle.equalFn;
		if (!compare) {
			isEqual = false;
			compare = handle.lessFn;
		}

		copy->pushRaw(ptr(0));
		for (Nat i = 1; i < count(); i++) {
			if (compare(copy->ptr(copy->count() - 1), ptr(i)) != isEqual)
				copy->pushRaw(ptr(i));
		}

		return copy;
	}

	ArrayBase *ArrayBase::withoutDuplicatesRawPred(FnBase *compare) const {
		// Create a copy:
		void *copyMem = runtime::allocObject(sizeof(ArrayBase), runtime::typeOf(this));
		ArrayBase *copy = new (Place(copyMem)) ArrayBase(handle);
		runtime::setVTable(copy);

		// Copy unique elements:
		if (count() == 0)
			return copy;

		RawFn compareFn = compare->rawCall();

		Bool isEqual = false;
		{
			const void *params[2] = { ptr(0), ptr(0) };
			compareFn.call(compare, &isEqual, params);
		}

		copy->pushRaw(ptr(0));
		for (Nat i = 1; i < count(); i++) {
			// Compare:
			Bool equal = false;
			const void *params[2] = { copy->ptr(copy->count() - 1), ptr(i) };
			compareFn.call(compare, &equal, params);

			if (equal != isEqual)
				copy->pushRaw(ptr(i));
		}

		return copy;
	}

	Nat ArrayBase::lowerBoundRaw(const void *find) const {
		// Adapted from the example implementation at:
		// https://en.cppreference.com/w/cpp/algorithm/lower_bound
		Nat first = 0;
		Nat count = this->count();
		while (count > 0) {
			Nat step = count / 2;
			Nat middle = first + step;
			if ((*handle.lessFn)(ptr(middle), find)) {
				first = middle + 1;
				count -= step + 1;
			} else {
				count = step;
			}
		}
		return first;
	}

	Nat ArrayBase::lowerBoundRawPred(const void *find, FnBase *compare) const {
		// Adapted from the example implementation at:
		// https://en.cppreference.com/w/cpp/algorithm/lower_bound
		RawFn compareFn = compare->rawCall();

		Nat first = 0;
		Nat count = this->count();
		while (count > 0) {
			Nat step = count / 2;
			Nat middle = first + step;

			Bool isLess = false;
			const void *params[2] = { ptr(middle), find };
			compareFn.call(compare, &isLess, params);

			if (isLess) {
				first = middle + 1;
				count -= step + 1;
			} else {
				count = step;
			}
		}
		return first;
	}

	Nat ArrayBase::upperBoundRaw(const void *find) const {
		// Adapted from the example implementation at:
		// https://en.cppreference.com/w/cpp/algorithm/upper_bound
		Nat first = 0;
		Nat count = this->count();
		while (count > 0) {
			Nat step = count / 2;
			Nat middle = first + step;
			if (!(*handle.lessFn)(find, ptr(middle))) {
				first = middle + 1;
				count -= step + 1;
			} else {
				count = step;
			}
		}
		return first;
	}

	Nat ArrayBase::upperBoundRawPred(const void *find, FnBase *compare) const {
		// Adapted from the example implementation at:
		// https://en.cppreference.com/w/cpp/algorithm/upper_bound
		RawFn compareFn = compare->rawCall();

		Nat first = 0;
		Nat count = this->count();
		while (count > 0) {
			Nat step = count / 2;
			Nat middle = first + step;

			Bool isLess = false;
			const void *params[2] = { find, ptr(middle) };
			compareFn.call(compare, &isLess, params);

			if (!isLess) {
				first = middle + 1;
				count -= step + 1;
			} else {
				count = step;
			}
		}
		return first;
	}

	Bool ArrayBase::equalRaw(const ArrayBase *other) const {
		if (count() != other->count())
			return false;

		for (Nat i = 0; i < count(); i++)
			if (!handle.equal(ptr(i), other->ptr(i)))
				return false;

		return true;
	}

	Bool ArrayBase::lessRaw(const ArrayBase *other) const {
		Nat smallest = min(count(), other->count());

		if (handle.equalFn) {
			for (Nat i = 0; i < smallest; i++) {
				if (!(*handle.equalFn)(ptr(i), other->ptr(i)))
					return (*handle.lessFn)(ptr(i), other->ptr(i));
			}
		} else {
			for (Nat i = 0; i < smallest; i++) {
				if ((*handle.lessFn)(ptr(i), other->ptr(i)))
					return true;
				if ((*handle.lessFn)(other->ptr(i), ptr(i)))
					return false;
			}
		}

		return count() < other->count();
	}

	void ArrayBase::toS(StrBuf *to) const {
		*to << S("[");
		if (count() > 0)
			(*handle.toSFn)(ptr(0), to);

		for (nat i = 1; i < count(); i++) {
			*to << S(", ");
			(*handle.toSFn)(ptr(i), to);
		}
		*to << S("]");
	}

	void ArrayBase::pushRaw(const void *element) {
		Nat c = count();
		ensure(c + 1);

		if (handle.copyFn) {
			(*handle.copyFn)(ptr(c), element);
		} else {
			memcpy(ptr(c), element, handle.size);
		}
		data->filled = c + 1;
	}

	void ArrayBase::outOfBounds(Nat n) const {
		throw new (this) ArrayError(n, count());
	}

	ArrayBase::Iter ArrayBase::beginRaw() {
		return Iter(this, 0);
	}

	ArrayBase::Iter ArrayBase::endRaw() {
		return Iter(this, count());
	}

	ArrayBase::Iter::Iter() : owner(0), index(0) {}

	ArrayBase::Iter::Iter(ArrayBase *owner) : owner(owner), index(0) {}

	ArrayBase::Iter::Iter(ArrayBase *owner, Nat index) : owner(owner), index(index) {}

	bool ArrayBase::Iter::operator ==(const Iter &o) const {
		if (atEnd() || o.atEnd())
			return atEnd() == o.atEnd();
		else
			return index == o.index && owner == o.owner;
	}

	bool ArrayBase::Iter::atEnd() const {
		return owner == null
			|| owner->count() <= index;
	}

	bool ArrayBase::Iter::operator !=(const Iter &o) const {
		return !(*this == o);
	}

	ArrayBase::Iter &ArrayBase::Iter::operator ++() {
		if (!atEnd())
			index++;
		return *this;
	}

	ArrayBase::Iter ArrayBase::Iter::operator ++(int) {
		Iter c = *this;
		++(*this);
		return c;
	}

	void *ArrayBase::Iter::getRaw() const {
		if (atEnd()) {
			Engine &e = runtime::someEngine();
			throw new (e) ArrayError(index, owner->count(), new (e) Str(S("iterator")));
		}
		return owner->getRaw(index);
	}

	ArrayBase::Iter &ArrayBase::Iter::preIncRaw() {
		return operator ++();
	}

	ArrayBase::Iter ArrayBase::Iter::postIncRaw() {
		return operator ++(0);
	}

}