File: lists32.h

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (368 lines) | stat: -rw-r--r-- 8,070 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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef SCI_GRAPHICS_LISTS32_H
#define SCI_GRAPHICS_LISTS32_H

#include "common/array.h"

namespace Sci {

/**
 * StablePointerArray holds pointers in a fixed-size array that maintains
 * position of erased items until `pack` is called. It is used by RectList
 * and ScreenItemList. StablePointerArray takes ownership of all pointers
 * that are passed to it and deletes them when calling `erase` or when
 * destroying the StablePointerArray.
 *
 * StablePointerArray used to be used for DrawList, until it was discovered
 * that an LSL7 room with many screen items can overflow the fixed array.
 * StablePointerDynamicArray was created below to handle DrawList, while
 * StablePointerArray keeps the performance advantages of fixed arrays on
 * the stack when rendering frames.
 */
template<class T, uint N>
class StablePointerArray {
	uint _size;
	T *_items[N];

public:
	typedef T **iterator;
	typedef T *const *const_iterator;
	typedef T *value_type;
	typedef uint size_type;

	StablePointerArray() : _size(0), _items() {}
	StablePointerArray(const StablePointerArray &other) : _size(other._size) {
		for (size_type i = 0; i < _size; ++i) {
			if (other._items[i] == nullptr) {
				_items[i] = nullptr;
			} else {
				_items[i] = new T(*other._items[i]);
			}
		}
	}
	StablePointerArray(StablePointerArray &&other) : _size(other._size) {
		other._size = 0;
		for (size_type i = 0; i < _size; ++i) {
			_items[i] = other._items[i];
			other._items[i] = nullptr;
		}
	}
	~StablePointerArray() {
		for (size_type i = 0; i < _size; ++i) {
			delete _items[i];
		}
	}

	void operator=(const StablePointerArray &other) {
		clear();
		_size = other._size;
		for (size_type i = 0; i < _size; ++i) {
			if (other._items[i] == nullptr) {
				_items[i] = nullptr;
			} else {
				_items[i] = new T(*other._items[i]);
			}
		}
	}

	void operator=(StablePointerArray &&other) {
		clear();
		_size = other._size;
		other._size = 0;
		for (size_type i = 0; i < _size; ++i) {
			_items[i] = other._items[i];
			other._items[i] = nullptr;
		}
	}

	T *const &operator[](size_type index) const {
		assert(index < _size);
		return _items[index];
	}

	T *&operator[](size_type index) {
		assert(index < _size);
		return _items[index];
	}

	/**
	 * Adds a new pointer to the array.
	 */
	void add(T *item) {
		assert(_size < N);
		_items[_size++] = item;
	}

	iterator begin() {
		return _items;
	}

	const_iterator begin() const {
		return _items;
	}

	void clear() {
		for (size_type i = 0; i < _size; ++i) {
			delete _items[i];
			_items[i] = nullptr;
		}

		_size = 0;
	}

	iterator end() {
		return _items + _size;
	}

	const_iterator end() const {
		return _items + _size;
	}

	/**
	 * Erases the object pointed to by the given iterator.
	 */
	void erase(T *item) {
		for (iterator it = begin(); it != end(); ++it) {
			if (*it == item) {
				delete *it;
				*it = nullptr;
				break;
			}
		}
	}

	/**
	 * Erases the object pointed to by the given iterator.
	 */
	void erase(iterator &it) {
		assert(it >= _items && it < _items + _size);
		delete *it;
		*it = nullptr;
	}

	/**
	 * Erases the object pointed to at the given index.
	 */
	void erase_at(size_type index) {
		assert(index < _size);

		delete _items[index];
		_items[index] = nullptr;
	}

	/**
	 * Removes freed pointers from the pointer list.
	 */
	size_type pack() {
		iterator freePtr = begin();
		size_type newSize = 0;

		for (iterator it = begin(), last = end(); it != last; ++it) {
			if (*it != nullptr) {
				*freePtr = *it;
				++freePtr;
				++newSize;
			}
		}

		_size = newSize;
		return newSize;
	}

	/**
	 * The maximum number of elements the container is able to hold.
	 */
	size_type max_size() const {
		return N;
	}

	/**
	 * The number of populated slots in the array. The size
	 * of the array will only go down once `pack` is called.
	 */
	size_type size() const {
		return _size;
	}
};

/**
 * StablePointerDynamicArray is like StablePointerArray above, except that
 * it uses a Common::Array for storage instead of a fixed array.
 * It is only used by DrawList, and was created upon discovering that LSL7
 * room 301 can overflow DrawList when displaying a large menu. Bug #14632
 */
template<class T, uint N>
class StablePointerDynamicArray {
	Common::Array<T *> _items;

public:
	typedef T **iterator;
	typedef T *const *const_iterator;
	typedef T *value_type;
	typedef uint size_type;

	StablePointerDynamicArray() {
		_items.reserve(N);
	}
	StablePointerDynamicArray(const StablePointerDynamicArray &other) {
		_items.reserve(MAX(N, other.size()));
		for (size_type i = 0; i < other.size(); ++i) {
			if (other._items[i] == nullptr) {
				_items.push_back(nullptr);
			} else {
				_items.push_back(new T(*other._items[i]));
			}
		}
	}
	StablePointerDynamicArray(StablePointerDynamicArray &&other) {
		_items = Common::move(other._items);
	}
	~StablePointerDynamicArray() {
		for (size_type i = 0; i < _items.size(); ++i) {
			delete _items[i];
		}
	}

	void operator=(StablePointerDynamicArray &other) {
		clear();
		for (size_type i = 0; i < other.size(); ++i) {
			if (other._items[i] == nullptr) {
				_items.push_back(nullptr);
			} else {
				_items.push_back(new T(*other._items[i]));
			}
		}
	}
	void operator=(StablePointerDynamicArray &&other) {
		clear();
		_items = Common::move(other._items);
	}

	T *const &operator[](size_type index) const {
		return _items[index];
	}

	T *&operator[](size_type index) {
		return _items[index];
	}

	/**
	 * Adds a new pointer to the array.
	 */
	void add(T *item) {
		_items.push_back(item);
	}

	iterator begin() {
		return _items.begin();
	}

	const_iterator begin() const {
		return _items.begin();
	}

	void clear() {
		for (size_type i = 0; i < _items.size(); ++i) {
			delete _items[i];
		}
		_items.resize(0);
	}

	iterator end() {
		return _items.end();
	}

	const_iterator end() const {
		return _items.end();
	}

	/**
	 * Erases the object pointed to by the given iterator.
	 */
	void erase(T *item) {
		for (iterator it = begin(); it != end(); ++it) {
			if (*it == item) {
				delete *it;
				*it = nullptr;
				break;
			}
		}
	}

	/**
	 * Erases the object pointed to by the given iterator.
	 */
	void erase(iterator &it) {
		assert(it >= begin() && it < end());
		delete *it;
		*it = nullptr;
	}

	/**
	 * Erases the object pointed to at the given index.
	 */
	void erase_at(size_type index) {
		delete _items[index];
		_items[index] = nullptr;
	}

	/**
	 * Removes freed pointers from the pointer list.
	 */
	size_type pack() {
		iterator freePtr = begin();
		size_type newSize = 0;

		for (iterator it = begin(), last = end(); it != last; ++it) {
			if (*it != nullptr) {
				*freePtr = *it;
				++freePtr;
				++newSize;
			}
		}
		_items.resize(newSize);
		return newSize;
	}

	/**
	 * The number of populated slots in the array. The size
	 * of the array will only go down once `pack` is called.
	 */
	size_type size() const {
		return _items.size();
	}
};

template<typename T>
class FindByObject {
	const reg_t &_object;
public:
	FindByObject(const reg_t &object) : _object(object) {}
	bool operator()(const T entry) const {
		return entry && entry->_object == _object;
	}
};

} // End of namespace Sci

#endif // SCI_GRAPHICS_LISTS32_H