22 #ifndef COMMON_ARRAY_H 23 #define COMMON_ARRAY_H 25 #include "common/scummsys.h" 26 #include "common/algorithm.h" 27 #include "common/textconsole.h" 28 #include "common/memory.h" 67 constexpr
Array() : _capacity(0), _size(0), _storage(nullptr) {}
73 explicit Array(size_type count) : _size(count) {
78 for (size_type i = 0; i < count; ++i)
79 new ((
void *)&storage[i]) T();
85 Array(size_type count,
const T &value) : _size(count) {
93 Array(
const Array<T> &array) : _capacity(array._size), _size(array._size), _storage(nullptr) {
103 Array(
Array<T> &&old) : _capacity(old._capacity), _size(old._size), _storage(old._storage) {
104 old._storage =
nullptr;
119 Array(std::initializer_list<T> list) : _size(list.
size()) {
129 Array(
const T2 *array, size_type n) {
138 _capacity = _size = 0;
142 template<
class... TArgs>
143 void emplace(const_iterator pos, TArgs&&... args) {
144 assert(pos >= _storage && pos <= _storage + _size);
146 const size_type index =
static_cast<size_type
>(pos -
_storage);
148 if (_size != _capacity && index == _size) {
150 new (_storage + index) T(Common::forward<TArgs>(args)...);
161 new (_storage + index) T(Common::forward<TArgs>(args)...);
174 template<
class... TArgs>
176 emplace(
begin() + _size, Common::forward<TArgs>(args)...);
193 _size += array.
size();
203 _storage[
_size].~T();
231 return _storage[_size-1];
237 return _storage[_size-1];
242 assert(idx <= _size);
243 insert_aux(_storage + idx, &element, &element + 1);
248 assert(idx <= _size);
255 void insert(iterator pos,
const T &element) {
263 move(_storage + idx + 1, _storage + _size, _storage + idx);
266 _storage[
_size].~T();
275 return _storage[idx];
281 return _storage[idx];
303 _capacity = old._capacity;
305 _storage = old._storage;
307 old._storage =
nullptr;
329 move(pos + 1, _storage + _size, pos);
332 _storage[
_size].~T();
337 iterator
erase(iterator first, iterator last) {
338 move(last, _storage + _size, first);
340 int count = (last - first);
344 for (uint idx = _size; idx < (_size + count); ++idx)
359 if (_size != other.
_size)
361 for (size_type i = 0; i <
_size; ++i) {
362 if (_storage[i] != other.
_storage[i])
370 return !(*
this == other);
380 return _storage +
_size;
389 const_iterator
end()
const {
390 return _storage +
_size;
397 if (newCapacity <= _capacity)
416 for (size_type i = newSize; i <
_size; ++i)
418 for (size_type i = _size; i < newSize; ++i)
419 new ((
void *)&storage[i]) T();
426 void resize(size_type newSize,
const T value) {
431 for (size_type i = newSize; i <
_size; ++i)
442 void assign(const_iterator first, const_iterator last) {
443 resize(distance(first, last));
445 while (first != last)
449 void swap(
Array &arr) {
461 while (capa < capacity)
468 _capacity = capacity;
470 _storage = (T *)malloc(
sizeof(T) * capacity);
472 ::error(
"Common::Array: failure to allocate %u bytes", capacity * (size_type)
sizeof(T));
480 for (size_type i = 0; i < elements; ++i)
498 iterator
insert_aux(iterator pos, const_iterator first, const_iterator last) {
499 assert(_storage <= pos && pos <= _storage + _size);
500 assert(first <= last);
501 const size_type n = last - first;
503 const size_type idx = pos -
_storage;
504 if (_size + n > _capacity || (_storage <= first && first <= _storage + _size)) {
522 }
else if (idx + n <= _size) {
531 copy(first, last, pos);
539 copy(first, first + (_size - idx), pos);
557 template<
class T,
typename CompareArgType = const
void *>
560 typedef int (*Comparator)(CompareArgType, CompareArgType);
565 _comparator = comparator;
577 T *where = bsearchMin(element);
588 void insert_at(size_type idx,
const T &element);
592 void insert(iterator pos,
const T &element);
601 T *bsearchMin(CompareArgType key) {
602 uint start_ = 0, end_ = this->
_size;
605 while (start_ < end_) {
606 uint mid = start_ + (end_ - start_) / 2;
608 result = this->_comparator(key, this->
_storage[mid]);
618 Comparator _comparator;
void push_back(const Array< T > &array)
Definition: array.h:190
bool operator==(const Array< T > &other) const
Definition: array.h:356
T * _storage
Definition: array.h:64
bool operator!=(const Array< T > &other) const
Definition: array.h:369
void insert_at(size_type idx, const T &element)
Definition: array.h:241
const_iterator begin() const
Definition: array.h:384
iterator insert_aux(iterator pos, const_iterator first, const_iterator last)
Definition: array.h:498
T * data()
Definition: array.h:212
const T * data() const
Definition: array.h:207
void insert_at(size_type idx, const Array< T > &array)
Definition: array.h:247
const T & operator[](size_type idx) const
Definition: array.h:279
void clear()
Definition: array.h:320
iterator end()
Definition: array.h:379
iterator begin()
Definition: array.h:374
T & operator[](size_type idx)
Definition: array.h:273
T * iterator
Definition: array.h:54
Array< T > & operator=(const Array< T > &array)
Definition: array.h:285
void SWAP(T &a, T &b)
Definition: util.h:82
T & front()
Definition: array.h:217
Array(size_type count)
Definition: array.h:73
Array & operator=(Array< T > &&old)
Definition: array.h:298
static size_type roundUpCapacity(size_type capacity)
Definition: array.h:459
Type * uninitialized_move(In first, In last, Type *dst)
Definition: memory.h:71
const T & front() const
Definition: array.h:223
const_iterator end() const
Definition: array.h:389
Out copy(In first, In last, Out dst)
Definition: algorithm.h:52
bool empty() const
Definition: array.h:351
void push_back(const T &element)
Definition: array.h:180
size_type _size
Definition: array.h:63
const T * const_iterator
Definition: array.h:55
void reserve(size_type newCapacity)
Definition: array.h:396
Type * uninitialized_copy(In first, In last, Type *dst)
Definition: memory.h:59
Definition: algorithm.h:29
void pop_back()
Definition: array.h:199
Array(size_type count, const T &value)
Definition: array.h:85
void resize(size_type newSize, const T value)
Definition: array.h:426
void freeStorage(T *storage, const size_type elements)
Definition: array.h:479
size_type size() const
Definition: array.h:315
Out move(In first, In last, Out dst)
Definition: algorithm.h:109
Out move_backward(In first, In last, Out dst)
Definition: algorithm.h:124
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
void push_back(T &&element)
Definition: array.h:185
iterator erase(iterator first, iterator last)
Definition: array.h:337
void resize(size_type newSize)
Definition: array.h:411
T remove_at(size_type idx)
Definition: array.h:260
Array(const Array< T > &array)
Definition: array.h:93
size_type _capacity
Definition: array.h:62
uint size_type
Definition: array.h:59
void allocCapacity(size_type capacity)
Definition: array.h:467
void assign(const_iterator first, const_iterator last)
Definition: array.h:442
void emplace_back(TArgs &&...args)
Definition: array.h:175
T value_type
Definition: array.h:57
const T & back() const
Definition: array.h:235
Array(const T2 *array, size_type n)
Definition: array.h:129
void insert(const T &element)
Definition: array.h:571
void insert(iterator pos, const T &element)
Definition: array.h:255
Array(std::initializer_list< T > list)
Definition: array.h:119
Array(Array< T > &&old)
Definition: array.h:103
void emplace(const_iterator pos, TArgs &&... args)
Definition: array.h:143
iterator erase(iterator pos)
Definition: array.h:328
T & back()
Definition: array.h:229
void uninitialized_fill_n(Type *dst, size_t n, const Value &x)
Definition: memory.h:94