ScummVM API documentation
lists32.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef SCI_GRAPHICS_LISTS32_H
23 #define SCI_GRAPHICS_LISTS32_H
24 
25 #include "common/array.h"
26 
27 namespace Sci {
28 
42 template<class T, uint N>
44  uint _size;
45  T *_items[N];
46 
47 public:
48  typedef T **iterator;
49  typedef T *const *const_iterator;
50  typedef T *value_type;
51  typedef uint size_type;
52 
53  StablePointerArray() : _size(0), _items() {}
54  StablePointerArray(const StablePointerArray &other) : _size(other._size) {
55  for (size_type i = 0; i < _size; ++i) {
56  if (other._items[i] == nullptr) {
57  _items[i] = nullptr;
58  } else {
59  _items[i] = new T(*other._items[i]);
60  }
61  }
62  }
63  StablePointerArray(StablePointerArray &&other) : _size(other._size) {
64  other._size = 0;
65  for (size_type i = 0; i < _size; ++i) {
66  _items[i] = other._items[i];
67  other._items[i] = nullptr;
68  }
69  }
71  for (size_type i = 0; i < _size; ++i) {
72  delete _items[i];
73  }
74  }
75 
76  void operator=(const StablePointerArray &other) {
77  clear();
78  _size = other._size;
79  for (size_type i = 0; i < _size; ++i) {
80  if (other._items[i] == nullptr) {
81  _items[i] = nullptr;
82  } else {
83  _items[i] = new T(*other._items[i]);
84  }
85  }
86  }
87 
88  void operator=(StablePointerArray &&other) {
89  clear();
90  _size = other._size;
91  other._size = 0;
92  for (size_type i = 0; i < _size; ++i) {
93  _items[i] = other._items[i];
94  other._items[i] = nullptr;
95  }
96  }
97 
98  T *const &operator[](size_type index) const {
99  assert(index < _size);
100  return _items[index];
101  }
102 
103  T *&operator[](size_type index) {
104  assert(index < _size);
105  return _items[index];
106  }
107 
111  void add(T *item) {
112  assert(_size < N);
113  _items[_size++] = item;
114  }
115 
116  iterator begin() {
117  return _items;
118  }
119 
120  const_iterator begin() const {
121  return _items;
122  }
123 
124  void clear() {
125  for (size_type i = 0; i < _size; ++i) {
126  delete _items[i];
127  _items[i] = nullptr;
128  }
129 
130  _size = 0;
131  }
132 
133  iterator end() {
134  return _items + _size;
135  }
136 
137  const_iterator end() const {
138  return _items + _size;
139  }
140 
144  void erase(T *item) {
145  for (iterator it = begin(); it != end(); ++it) {
146  if (*it == item) {
147  delete *it;
148  *it = nullptr;
149  break;
150  }
151  }
152  }
153 
157  void erase(iterator &it) {
158  assert(it >= _items && it < _items + _size);
159  delete *it;
160  *it = nullptr;
161  }
162 
166  void erase_at(size_type index) {
167  assert(index < _size);
168 
169  delete _items[index];
170  _items[index] = nullptr;
171  }
172 
176  size_type pack() {
177  iterator freePtr = begin();
178  size_type newSize = 0;
179 
180  for (iterator it = begin(), last = end(); it != last; ++it) {
181  if (*it != nullptr) {
182  *freePtr = *it;
183  ++freePtr;
184  ++newSize;
185  }
186  }
187 
188  _size = newSize;
189  return newSize;
190  }
191 
195  size_type max_size() const {
196  return N;
197  }
198 
203  size_type size() const {
204  return _size;
205  }
206 };
207 
214 template<class T, uint N>
216  Common::Array<T *> _items;
217 
218 public:
219  typedef T **iterator;
220  typedef T *const *const_iterator;
221  typedef T *value_type;
222  typedef uint size_type;
223 
225  _items.reserve(N);
226  }
228  _items.reserve(MAX(N, other.size()));
229  for (size_type i = 0; i < other.size(); ++i) {
230  if (other._items[i] == nullptr) {
231  _items.push_back(nullptr);
232  } else {
233  _items.push_back(new T(*other._items[i]));
234  }
235  }
236  }
238  _items = Common::move(other._items);
239  }
241  for (size_type i = 0; i < _items.size(); ++i) {
242  delete _items[i];
243  }
244  }
245 
246  void operator=(StablePointerDynamicArray &other) {
247  clear();
248  for (size_type i = 0; i < other.size(); ++i) {
249  if (other._items[i] == nullptr) {
250  _items.push_back(nullptr);
251  } else {
252  _items.push_back(new T(*other._items[i]));
253  }
254  }
255  }
256  void operator=(StablePointerDynamicArray &&other) {
257  clear();
258  _items = Common::move(other._items);
259  }
260 
261  T *const &operator[](size_type index) const {
262  return _items[index];
263  }
264 
265  T *&operator[](size_type index) {
266  return _items[index];
267  }
268 
272  void add(T *item) {
273  _items.push_back(item);
274  }
275 
276  iterator begin() {
277  return _items.begin();
278  }
279 
280  const_iterator begin() const {
281  return _items.begin();
282  }
283 
284  void clear() {
285  for (size_type i = 0; i < _items.size(); ++i) {
286  delete _items[i];
287  }
288  _items.resize(0);
289  }
290 
291  iterator end() {
292  return _items.end();
293  }
294 
295  const_iterator end() const {
296  return _items.end();
297  }
298 
302  void erase(T *item) {
303  for (iterator it = begin(); it != end(); ++it) {
304  if (*it == item) {
305  delete *it;
306  *it = nullptr;
307  break;
308  }
309  }
310  }
311 
315  void erase(iterator &it) {
316  assert(it >= begin() && it < end());
317  delete *it;
318  *it = nullptr;
319  }
320 
324  void erase_at(size_type index) {
325  delete _items[index];
326  _items[index] = nullptr;
327  }
328 
332  size_type pack() {
333  iterator freePtr = begin();
334  size_type newSize = 0;
335 
336  for (iterator it = begin(), last = end(); it != last; ++it) {
337  if (*it != nullptr) {
338  *freePtr = *it;
339  ++freePtr;
340  ++newSize;
341  }
342  }
343  _items.resize(newSize);
344  return newSize;
345  }
346 
351  size_type size() const {
352  return _items.size();
353  }
354 };
355 
356 template<typename T>
358  const reg_t &_object;
359 public:
360  FindByObject(const reg_t &object) : _object(object) {}
361  bool operator()(const T entry) const {
362  return entry && entry->_object == _object;
363  }
364 };
365 
366 } // End of namespace Sci
367 
368 #endif // SCI_GRAPHICS_LISTS32_H
size_type pack()
Definition: lists32.h:176
void add(T *item)
Definition: lists32.h:272
Definition: array.h:52
iterator end()
Definition: array.h:379
iterator begin()
Definition: array.h:374
size_type size() const
Definition: lists32.h:351
void add(T *item)
Definition: lists32.h:111
size_type pack()
Definition: lists32.h:332
Definition: lists32.h:43
void push_back(const T &element)
Definition: array.h:180
void erase(T *item)
Definition: lists32.h:302
void reserve(size_type newCapacity)
Definition: array.h:396
void erase(iterator &it)
Definition: lists32.h:157
void erase_at(size_type index)
Definition: lists32.h:166
Definition: lists32.h:215
Definition: console.h:28
size_type size() const
Definition: array.h:315
Out move(In first, In last, Out dst)
Definition: algorithm.h:109
size_type max_size() const
Definition: lists32.h:195
void resize(size_type newSize)
Definition: array.h:411
Definition: lists32.h:357
void erase_at(size_type index)
Definition: lists32.h:324
void erase(iterator &it)
Definition: lists32.h:315
T MAX(T a, T b)
Definition: util.h:62
Definition: vm_types.h:39
void erase(T *item)
Definition: lists32.h:144
size_type size() const
Definition: lists32.h:203