File: Array.h

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 (373 lines) | stat: -rw-r--r-- 8,645 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
#pragma once
#include "Object.h"
#include "Handle.h"
#include "GcArray.h"
#include "StrBuf.h"
#include "Fn.h"

namespace storm {
	STORM_PKG(core);

	/**
	 * Base class for arrays. Implements a slightly inconvenient interface that is reusable
	 * regardless of the contained type. Use the derived class Array<> for C++.
	 */
	class ArrayBase : public Object {
		STORM_CLASS;
	public:
		// Empty array.
		ArrayBase(const Handle &type);

		// Create an array with 'n' copies of 'data'.
		ArrayBase(const Handle &type, Nat n, const void *data);

		// Copy another array.
		ArrayBase(const ArrayBase &other);

		// Deep copy.
		virtual void STORM_FN deepCopy(CloneEnv *env);

		// Get size.
		inline Nat STORM_FN count() const { return data ? Nat(data->filled) : 0; }

		// Reserve size.
		void STORM_FN reserve(Nat count);

		// Clear.
		void STORM_FN clear();

		// Any elements?
		inline Bool STORM_FN any() const { return count() > 0; }

		// Empty?
		inline Bool STORM_FN empty() const { return count() == 0; }

		// Remove an element.
		void STORM_FN remove(Nat id);

		// Remove the last element.
		void STORM_FN pop();

		// Insert an element, giving it the id 'id'. 'id' <= 'count()'.
		void CODECALL insertRaw(Nat id, const void *item);

		// Append the entire contents of another array.
		ArrayBase *CODECALL appendRaw(ArrayBase *from);

		// Reverse the array.
		void STORM_FN reverse();

		// Get a random element. Throws if array is empty.
		void *CODECALL randomRaw() const;

		// Sort the array. Assumes the handle contains a '<' function.
		void CODECALL sortRaw();

		// Sort using a predicate.
		void CODECALL sortRawPred(FnBase *compare);

		// Remove duplicates, assumes sorted beforehand.
		void CODECALL removeDuplicatesRaw();

		// Remove duplicates, assumes sorted beforehand, custom predicate.
		void CODECALL removeDuplicatesRawPred(FnBase *compare);

		// Remove duplicates, return the new version.
		ArrayBase *CODECALL withoutDuplicatesRaw() const;

		// Remove duplicates, return the new version, custom predicate.
		ArrayBase *CODECALL withoutDuplicatesRawPred(FnBase *compare) const;

		// Find first element that is greater than or equal to 'test', using binary search. Returns
		// 'count' if no element is found.
		Nat CODECALL lowerBoundRaw(const void *find) const;
		Nat CODECALL lowerBoundRawPred(const void *find, FnBase *compare) const;

		// Find first element that is greater than 'test', using binary search.
		Nat CODECALL upperBoundRaw(const void *find) const;
		Nat CODECALL upperBoundRawPred(const void *find, FnBase *compare) const;

		// Compare for equality.
		Bool CODECALL equalRaw(const ArrayBase *other) const;

		// Compare lexiographically less than.
		Bool CODECALL lessRaw(const ArrayBase *other) const;

		// To string.
		virtual void STORM_FN toS(StrBuf *to) const;

		// Handle of the contained type.
		const Handle &handle;

		// First element access.
		inline void *CODECALL firstRaw() const {
			return getRaw(0);
		}

		// Last element access.
		inline void *CODECALL lastRaw() const {
			return getRaw(count() - 1);
		}

		// Raw element access.
		inline void *CODECALL getRaw(Nat id) const {
			// We're calling a function to throw the exception to make it more likely that the
			// compiler inlines this function. Throwing an exception makes it far too large for
			// inlining.
			if (id < count()) {
				return ptr(id);
			} else {
				outOfBounds(id);
				return null;
			}
		}

		// Push an element.
		void CODECALL pushRaw(const void *element);


		/**
		 * Base class for the iterator.
		 * TODO: How does this fit with deepCopy?
		 */
		class Iter {
			STORM_VALUE;
		public:
			// Pointing to the end.
			Iter();

			// Pointing to a specific element in 'owner'.
			Iter(ArrayBase *owner);
			Iter(ArrayBase *owner, Nat index);

			// Compare.
			bool operator ==(const Iter &o) const;
			bool operator !=(const Iter &o) const;

			// Increase.
			Iter &operator ++();
			Iter operator ++(int z);

			// Raw get function.
			void *CODECALL getRaw() const;

			// Get the current index.
			inline Nat getIndex() const { return index; }

			// Raw pre- and post increment.
			Iter &CODECALL preIncRaw();
			Iter CODECALL postIncRaw();

		private:
			// Array we're pointing to.
			ArrayBase *owner;

			// Index.
			Nat index;

			// At end?
			bool atEnd() const;
		};

		// Raw iterator access.
		Iter CODECALL beginRaw();
		Iter CODECALL endRaw();

	protected:
		// Array contents (of bytes, to make addressing easier).
		GcArray<byte> *data;

		// Get the pointer to an element.
		inline void *ptr(GcArray<byte> *data, Nat id) const { return data->v + (id * handle.size); }
		inline void *ptr(Nat id) const { return ptr(data, id); }

		// Ensure 'data' can hold at least 'n' objects.
		void ensure(Nat n);

		// Throw out of bounds exception.
		void outOfBounds(Nat n) const;
	};

	// Declare the array's template in Storm.
	STORM_TEMPLATE(Array, createArray);

	/**
	 * Class used from C++.
	 */
	template <class T>
	class Array : public ArrayBase {
		STORM_SPECIAL;
	public:
		// Get the Storm type for this object.
		static Type *stormType(Engine &e) {
			return runtime::cppTemplate(e, ArrayId, 1, StormInfo<T>::id());
		}

		// Empty array.
		Array() : ArrayBase(StormInfo<T>::handle(engine())) {
			runtime::setVTable(this);
		}

		// 'n' elements.
		Array(Nat n, const T &item = T()) : ArrayBase(StormInfo<T>::handle(engine()), n, &item) {
			runtime::setVTable(this);
		}

		// Copy array.
		Array(const Array<T> &o) : ArrayBase(o) {
			runtime::setVTable(this);
		}

		// Create an array with one element in it.
		Array(const T &item) : ArrayBase(StormInfo<T>::handle(engine())) {
			runtime::setVTable(this);
			push(item);
		}

		// Element access.
		T &at(Nat i) {
			return *(T *)getRaw(i);
		}

		const T &at(Nat i) const {
			return *(const T *)getRaw(i);
		}

		// Get the last element (if any).
		T &last() {
			return *(T *)lastRaw();
		}

		const T &first() const {
			return *(const T *)firstRaw();
		}

		const T &last() const {
			return *(const T *)lastRaw();
		}

		// Insert an element.
		void push(const T &item) {
			pushRaw(&item);
		}

		// Insert at a specific location.
		void insert(Nat pos, const T &item) {
			insertRaw(pos, &item);
		}

		// Append another array.
		Array<T> *append(Array<T> *from) {
			appendRaw(from);
			return this;
		}

		// Random element.
		const T &random() const {
			return *(const T *)randomRaw();
		}

		// Sort. Assumes we have a '<' comparison in the handle.
		void sort() {
			sortRaw();
		}

		Array<T> *sorted() const {
			Array<T> *copy = new (this) Array<T>(*this);
			copy->sortRaw();
			return copy;
		}

		// Sort with custom predicate.
		void sort(Fn<Bool, T, T> *compare) {
			sortRawPred(compare);
		}

		Array<T> *sorted(Fn<Bool, T, T> *compare) {
			Array<T> *copy = new (this) Array<T>(*this);
			copy->sortRawPred(compare);
			return copy;
		}

		// Remove duplicates, assuming sorted beforehand.
		// Assumes we have a '==' or '<' in the handle.
		void removeDuplicates() {
			removeDuplicatesRaw();
		}

		// Removed duplicates.
		Array<T> *withoutDuplicates() const {
			return (Array<T> *)withoutDuplicatesRaw();
		}

		// Remove duplicates with custom predicate.
		void removeDuplicates(Fn<Bool, T, T> *compare) {
			removeDuplicatesRawPred(compare);
		}

		Array<T> *withoutDuplicates(Fn<Bool, T, T> *compare) const {
			return (Array<T> *)withoutDuplicatesRawPred(compare);
		}

		// Find lower bound. Assumes '<' is in the handle.
		Nat lowerBound(const T &find) const {
			return lowerBoundRaw(&find);
		}

		// Lower bound with custom predicate.
		template <class U>
		Nat lowerBound(const U &find, Fn<Bool, T, U> *compare) const {
			return lowerBoundRawPred(&find, compare);
		}

		// Find upper bound. Assumes '<' is in the handle.
		Nat upperBound(const T &find) const {
			return upperBoundRaw(&find);
		}

		// Upper bound with custom predicate.
		template <class U>
		Nat upperBound(const T &find, Fn<Bool, U, T> *compare) const {
			return upperBoundRawPred(&find, compare);
		}

		// Append elements.
		Array<T> &operator <<(const T &item) {
			push(item);
			return *this;
		}

		/**
		 * Iterator.
		 */
		class Iter : public ArrayBase::Iter {
		public:
			Iter() : ArrayBase::Iter() {}

			Iter(Array<T> *owner, Nat index = 0) : ArrayBase::Iter(owner, index) {}

			T &operator *() const {
				return *(T *)getRaw();
			}

			T &v() const {
				return *(T *)getRaw();
			}

			T *operator ->() const {
				return (T *)getRaw();
			}

		};

		// Create iterators.
		Iter begin() {
			return Iter(this, 0);
		}

		Iter end() {
			return Iter(this, count());
		}
	};

}