[Scummvm-cvs-logs] SF.net SVN: scummvm: [30982] scummvm/trunk/common/array.h
fingolfin at users.sourceforge.net
fingolfin at users.sourceforge.net
Wed Feb 27 14:35:30 CET 2008
Revision: 30982
http://scummvm.svn.sourceforge.net/scummvm/?rev=30982&view=rev
Author: fingolfin
Date: 2008-02-27 05:35:29 -0800 (Wed, 27 Feb 2008)
Log Message:
-----------
Added Array::resize() method, matching vector::resize() from the std C++ lib
Modified Paths:
--------------
scummvm/trunk/common/array.h
Modified: scummvm/trunk/common/array.h
===================================================================
--- scummvm/trunk/common/array.h 2008-02-27 11:03:09 UTC (rev 30981)
+++ scummvm/trunk/common/array.h 2008-02-27 13:35:29 UTC (rev 30982)
@@ -33,8 +33,8 @@
template <class T>
class Array {
protected:
- int _capacity;
- int _size;
+ uint _capacity;
+ uint _size;
T *_data;
public:
@@ -68,7 +68,7 @@
}
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 @@
}
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 @@
// 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 @@
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 @@
delete [] old_data;
}
}
+
+protected:
+ void ensureCapacity(uint len) {
+ if (len >= _capacity)
+ reserve(len + 32);
+ }
};
} // End of namespace Common
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
More information about the Scummvm-git-logs
mailing list