COMMON: Changed Array::resize to not shrink the internal storage if we shrink the array
svn-id: r40262
This commit is contained in:
parent
c379927d3a
commit
4d57c1f9a9
1 changed files with 7 additions and 12 deletions
|
@ -30,6 +30,12 @@
|
||||||
|
|
||||||
namespace Common {
|
namespace Common {
|
||||||
|
|
||||||
|
// TODO: Improve the storage management of this class.
|
||||||
|
// In particular, don't use new[] and delete[], but rather
|
||||||
|
// construct/destruct objects manually. This way, we can
|
||||||
|
// ensure that storage which is not currently used does not
|
||||||
|
// correspond to a live active object.
|
||||||
|
// (This is only of interest for array of non-POD objects).
|
||||||
template<class T>
|
template<class T>
|
||||||
class Array {
|
class Array {
|
||||||
protected:
|
protected:
|
||||||
|
@ -196,18 +202,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
void resize(uint newSize) {
|
void resize(uint newSize) {
|
||||||
if (newSize == _size)
|
reserve(newSize);
|
||||||
return;
|
|
||||||
|
|
||||||
T *old_storage = _storage;
|
|
||||||
_capacity = newSize;
|
|
||||||
_storage = new T[newSize];
|
|
||||||
if (old_storage) {
|
|
||||||
// Copy old data
|
|
||||||
int cnt = (_size < newSize ? _size : newSize);
|
|
||||||
copy(old_storage, old_storage + cnt, _storage);
|
|
||||||
delete[] old_storage;
|
|
||||||
}
|
|
||||||
_size = newSize;
|
_size = newSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue