Implemented move semantics and initializer list for Array class.

git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@3046 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
stephena 2014-11-06 23:27:01 +00:00
parent 5bc3737a2b
commit 08649b2069
1 changed files with 66 additions and 29 deletions

View File

@ -42,26 +42,76 @@ class Array
typedef const T* const_iterator; typedef const T* const_iterator;
public: public:
Array<T>() : _capacity(0), _size(0), _data(0) {} // Standard c'tor
Array<T>(const Array<T>& array) : _capacity(0), _size(0), _data(0) Array<T>() : _capacity(0), _size(0), _data(nullptr) { }
// Copy c'tor
Array<T>(const Array<T>& array)
: _capacity(array._size+128), _size(array._size), _data(nullptr)
{ {
_data = new T[_capacity];
for(uInt32 i = 0; i < _size; i++)
_data[i] = array._data[i];
}
// Copy assignment
Array<T>& operator =(const Array<T>& array)
{
delete [] _data;
_size = array._size; _size = array._size;
_capacity = _size + 128; _capacity = _size + 128;
_data = new T[_capacity]; _data = new T[_capacity];
for(uInt32 i = 0; i < _size; i++) for(uInt32 i = 0; i < _size; i++)
_data[i] = array._data[i]; _data[i] = array._data[i];
return *this;
} }
// Move c'tor
Array<T>(Array<T>&& array)
: _capacity(array._capacity), _size(array._size), _data(array._data)
{
array._size = 0;
array._capacity = 0;
array._data = nullptr;
}
// Move assignment
Array<T>& operator =(Array<T>&& array)
{
if(this != &array)
{
delete[] _data;
_capacity = array._size;
_size = array._size;
_data = array._data;
array._capacity = 0;
array._size = 0;
array._data = nullptr;
}
return *this;
}
// Initializer list c'tor
Array<T>(std::initializer_list<T> il) : _capacity(0), _size(0), _data(nullptr)
{
ensureCapacity(il.size());
for(int e: il)
_data[_size++] = e;
}
// D'tor
~Array<T>() ~Array<T>()
{ {
if(_data) if(_data)
{
delete[] _data; delete[] _data;
_data = nullptr;
}
} }
void reserve(uInt32 capacity) void reserve(uInt32 capacity)
{ {
if(capacity <= _capacity) if(capacity > _capacity)
return;
ensureCapacity(capacity - 128); ensureCapacity(capacity - 128);
} }
@ -116,19 +166,6 @@ class Array
return _data[idx]; return _data[idx];
} }
Array<T>& operator =(const Array<T>& array)
{
if (_data)
delete [] _data;
_size = array._size;
_capacity = _size + 128;
_data = new T[_capacity];
for(uInt32 i = 0; i < _size; i++)
_data[i] = array._data[i];
return *this;
}
uInt32 size() const { return _size; } uInt32 size() const { return _size; }
uInt32 capacity() const { return _capacity; } uInt32 capacity() const { return _capacity; }
@ -139,7 +176,7 @@ class Array
if(_data) if(_data)
{ {
delete [] _data; delete [] _data;
_data = 0; _data = nullptr;
} }
_capacity = 0; _capacity = 0;
} }
@ -193,7 +230,7 @@ class Array
} // Namespace Common } // Namespace Common
typedef Common::Array<int> IntArray; typedef Common::Array<Int32> IntArray;
typedef Common::Array<bool> BoolArray; typedef Common::Array<bool> BoolArray;
typedef Common::Array<uInt8> ByteArray; typedef Common::Array<uInt8> ByteArray;