diff options
author | Max Horn | 2008-02-27 13:35:29 +0000 |
---|---|---|
committer | Max Horn | 2008-02-27 13:35:29 +0000 |
commit | 0a918b02fb126dad03eed0d9c1e2f0bc43b6aa56 (patch) | |
tree | cecfab7e489eeaa6fe4bd7cbd33deb1978987324 /common | |
parent | 778309ed56d7af86ea10c77555697102558947c4 (diff) | |
download | scummvm-rg350-0a918b02fb126dad03eed0d9c1e2f0bc43b6aa56.tar.gz scummvm-rg350-0a918b02fb126dad03eed0d9c1e2f0bc43b6aa56.tar.bz2 scummvm-rg350-0a918b02fb126dad03eed0d9c1e2f0bc43b6aa56.zip |
Added Array::resize() method, matching vector::resize() from the std C++ lib
svn-id: r30982
Diffstat (limited to 'common')
-rw-r--r-- | common/array.h | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/common/array.h b/common/array.h index 8d7ccb6a60..91f1083338 100644 --- a/common/array.h +++ b/common/array.h @@ -33,8 +33,8 @@ namespace Common { template <class T> class Array { protected: - int _capacity; - int _size; + uint _capacity; + uint _size; T *_data; public: @@ -68,7 +68,7 @@ public: } void insert_at(int idx, const T& element) { - assert(idx >= 0 && idx <= _size); + assert(idx >= 0 && (uint)idx <= _size); ensureCapacity(_size + 1); copy_backward(_data + idx, _data + _size, _data + _size + 1); _data[idx] = element; @@ -76,7 +76,7 @@ public: } T remove_at(int idx) { - assert(idx >= 0 && idx < _size); + assert(idx >= 0 && (uint)idx < _size); T tmp = _data[idx]; copy(_data + idx + 1, _data + _size, _data + idx); _size--; @@ -86,12 +86,12 @@ public: // TODO: insert, remove, ... T& operator [](int idx) { - assert(idx >= 0 && idx < _size); + assert(idx >= 0 && (uint)idx < _size); return _data[idx]; } const T& operator [](int idx) const { - assert(idx >= 0 && idx < _size); + assert(idx >= 0 && (uint)idx < _size); return _data[idx]; } @@ -144,15 +144,13 @@ public: return find(begin(), end(), key) != end(); } - -protected: - void ensureCapacity(int new_len) { - if (new_len <= _capacity) + void reserve(uint newCapacity) { + if (newCapacity <= _capacity) return; T *old_data = _data; - _capacity = new_len + 32; - _data = new T[_capacity]; + _capacity = newCapacity; + _data = new T[newCapacity]; if (old_data) { // Copy old data @@ -160,6 +158,12 @@ protected: delete [] old_data; } } + +protected: + void ensureCapacity(uint len) { + if (len >= _capacity) + reserve(len + 32); + } }; } // End of namespace Common |