| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- $#include "Vector.h"
- $#include "Vector3.h"
- /// %Vector template class.
- class Vector : public VectorBase
- {
- TOLUA_TEMPLATE_BIND(T, String)
- public:
- /// Construct empty.
- Vector()
- {
- }
-
- /// Construct with initial size.
- explicit Vector(unsigned size)
- {
- Resize(size, 0);
- }
-
- /// Construct with initial data.
- Vector(const T* data, unsigned size)
- {
- Resize(size, data);
- }
-
- /// Construct from another vector.
- Vector(const Vector<T>& vector)
- {
- *this = vector;
- }
-
- /// Destruct.
- ~Vector()
- {
- Clear();
- delete[] buffer_;
- }
-
- /// Add an element.
- Vector<T> operator + (const T& rhs) const
- {
- Vector<T> ret(*this);
- ret.Push(rhs);
- return ret;
- }
-
- /// Add another vector.
- Vector<T> operator + (const Vector<T>& rhs) const
- {
- Vector<T> ret(*this);
- ret.Push(rhs);
- return ret;
- }
-
- /// Test for equality with another vector.
- bool operator == (const Vector<T>& rhs) const
- {
- if (rhs.size_ != size_)
- return false;
-
- T* buffer = Buffer();
- T* rhsBuffer = rhs.Buffer();
- for (unsigned i = 0; i < size_; ++i)
- {
- if (buffer[i] != rhsBuffer[i])
- return false;
- }
-
- return true;
- }
-
- /// Return element at index.
- T& operator [] (unsigned index) { assert(index < size_); return Buffer()[index]; }
- /// Return const element at index.
- const T& operator [] (unsigned index) const { assert(index < size_); return Buffer()[index]; }
- /// Return element at index.
- T& At(unsigned index) { assert(index < size_); return Buffer()[index]; }
- /// Return const element at index.
- const T& At(unsigned index) const { assert(index < size_); return Buffer()[index]; }
- /// Add an element at the end.
- void Push(const T& value) { Resize(size_ + 1, &value); }
- /// Add another vector at the end.
- void Push(const Vector<T>& vector) { Resize(size_ + vector.size_, vector.Buffer()); }
-
- /// Remove the last element.
- void Pop()
- {
- if (size_)
- Resize(size_ - 1, 0);
- }
-
- /// Insert an element at position.
- void Insert(unsigned pos, const T& value)
- {
- if (pos > size_)
- pos = size_;
-
- unsigned oldSize = size_;
- Resize(size_ + 1, 0);
- MoveRange(pos + 1, pos, oldSize - pos);
- Buffer()[pos] = value;
- }
-
- /// Insert another vector at position.
- void Insert(unsigned pos, const Vector<T>& vector)
- {
- if (pos > size_)
- pos = size_;
-
- unsigned oldSize = size_;
- Resize(size_ + vector.size_, 0);
- MoveRange(pos + vector.size_, pos, oldSize - pos);
- CopyElements(Buffer() + pos, vector.Buffer(), vector.size_);
- }
- /// Erase a range of elements.
- void Erase(unsigned pos, unsigned length = 1)
- {
- // Return if the range is illegal
- if (pos + length > size_ || !length)
- return;
-
- MoveRange(pos, pos + length, size_ - pos - length);
- Resize(size_ - length, 0);
- }
-
- /// Erase an element if found.
- bool Remove(const T& value)
- {
- Iterator i = Find(value);
- if (i != End())
- {
- Erase(i);
- return true;
- }
- else
- return false;
- }
-
- /// Clear the vector.
- void Clear() { Resize(0); }
- /// Resize the vector.
- void Resize(unsigned newSize) { Resize(newSize, 0); }
-
- /// Set new capacity.
- void Reserve(unsigned newCapacity)
- {
- if (newCapacity < size_)
- newCapacity = size_;
-
- if (newCapacity != capacity_)
- {
- T* newBuffer = 0;
- capacity_ = newCapacity;
-
- if (capacity_)
- {
- newBuffer = reinterpret_cast<T*>(AllocateBuffer(capacity_ * sizeof(T)));
- // Move the data into the new buffer
- ConstructElements(newBuffer, Buffer(), size_);
- }
-
- // Delete the old buffer
- DestructElements(Buffer(), size_);
- delete[] buffer_;
- buffer_ = reinterpret_cast<unsigned char*>(newBuffer);
- }
- }
-
- /// Reallocate so that no extra memory is used.
- void Compact() { Reserve(size_); }
-
- /// Return whether contains a specific value.
- bool Contains(const T& value) const { return Find(value) != End(); }
-
- /// Return first element.
- T& Front() { assert(size_); return Buffer()[0]; }
- /// Return const first element.
- const T& Front() const { assert(size_); return Buffer()[0]; }
- /// Return last element.
- T& Back() { assert(size_); return Buffer()[size_ - 1]; }
- /// Return const last element.
- const T& Back() const { assert(size_); return Buffer()[size_ - 1]; }
- /// Return size of vector.
- unsigned Size() const { return size_; }
- /// Return capacity of vector.
- unsigned Capacity() const { return capacity_; }
- /// Return whether vector is empty.
- bool Empty() const { return size_ == 0; }
- };
- /// %Vector template class for POD types. Does not call constructors or destructors and uses block move.
- class PODVector
- {
- TOLUA_TEMPLATE_BIND(T, Vector3)
- public:
- /// Construct empty.
- PODVector()
- {
- }
-
- /// Construct with initial size.
- explicit PODVector(unsigned size)
- {
- Resize(size);
- }
-
- /// Construct with initial data.
- PODVector(const T* data, unsigned size)
- {
- Resize(size);
- CopyElements(Buffer(), data, size);
- }
-
- /// Destruct.
- ~PODVector()
- {
- delete[] buffer_;
- }
-
- /// Add an element.
- PODVector<T> operator + (const T& rhs) const
- {
- PODVector<T> ret(*this);
- ret.Push(rhs);
- return ret;
- }
-
- /// Add another vector.
- PODVector<T> operator + (const PODVector<T>& rhs) const
- {
- PODVector<T> ret(*this);
- ret.Push(rhs);
- return ret;
- }
-
- /// Test for equality with another vector.
- bool operator == (const PODVector<T>& rhs) const
- {
- if (rhs.size_ != size_)
- return false;
-
- T* buffer = Buffer();
- T* rhsBuffer = rhs.Buffer();
- for (unsigned i = 0; i < size_; ++i)
- {
- if (buffer[i] != rhsBuffer[i])
- return false;
- }
-
- return true;
- }
-
- /// Return element at index.
- T& operator [] (unsigned index) { assert(index < size_); return Buffer()[index]; }
- /// Return const element at index.
- const T& operator [] (unsigned index) const { assert(index < size_); return Buffer()[index]; }
- /// Return element at index.
- T& At(unsigned index) { assert(index < size_); return Buffer()[index]; }
- /// Return const element at index.
- const T& At(unsigned index) const { assert(index < size_); return Buffer()[index]; }
-
- /// Add an element at the end.
- void Push(const T& value)
- {
- if (size_ < capacity_)
- ++size_;
- else
- Resize(size_ + 1);
- Back() = value;
- }
-
- /// Add another vector at the end.
- void Push(const PODVector<T>& vector)
- {
- unsigned oldSize = size_;
- Resize(size_ + vector.size_);
- CopyElements(Buffer() + oldSize, vector.Buffer(), vector.size_);
- }
-
- /// Remove the last element.
- void Pop()
- {
- if (size_)
- Resize(size_ - 1);
- }
-
- /// Insert an element at position.
- void Insert(unsigned pos, const T& value)
- {
- if (pos > size_)
- pos = size_;
-
- unsigned oldSize = size_;
- Resize(size_ + 1);
- MoveRange(pos + 1, pos, oldSize - pos);
- Buffer()[pos] = value;
- }
-
- /// Insert another vector at position.
- void Insert(unsigned pos, const PODVector<T>& vector)
- {
- if (pos > size_)
- pos = size_;
-
- unsigned oldSize = size_;
- Resize(size_ + vector.size_);
- MoveRange(pos + vector.size_, pos, oldSize - pos);
- CopyElements(Buffer() + pos, vector.Buffer(), vector.size_);
- }
-
- /// Erase a range of elements.
- void Erase(unsigned pos, unsigned length = 1)
- {
- // Return if the range is illegal
- if (!length || pos + length > size_)
- return;
-
- MoveRange(pos, pos + length, size_ - pos - length);
- Resize(size_ - length);
- }
-
- /// Erase an element if found.
- bool Remove(const T& value)
- {
- Iterator i = Find(value);
- if (i != End())
- {
- Erase(i);
- return true;
- }
- else
- return false;
- }
-
- /// Clear the vector.
- void Clear() { Resize(0); }
-
- /// Resize the vector.
- void Resize(unsigned newSize)
- {
- if (newSize > capacity_)
- {
- if (!capacity_)
- capacity_ = newSize;
- else
- {
- while (capacity_ < newSize)
- capacity_ += (capacity_ + 1) >> 1;
- }
-
- unsigned char* newBuffer = AllocateBuffer(capacity_ * sizeof(T));
- // Move the data into the new buffer and delete the old
- if (buffer_)
- {
- CopyElements(reinterpret_cast<T*>(newBuffer), Buffer(), size_);
- delete[] buffer_;
- }
- buffer_ = newBuffer;
- }
-
- size_ = newSize;
- }
-
- /// Set new capacity.
- void Reserve(unsigned newCapacity)
- {
- if (newCapacity < size_)
- newCapacity = size_;
-
- if (newCapacity != capacity_)
- {
- unsigned char* newBuffer = 0;
- capacity_ = newCapacity;
-
- if (capacity_)
- {
- newBuffer = AllocateBuffer(capacity_ * sizeof(T));
- // Move the data into the new buffer
- CopyElements(reinterpret_cast<T*>(newBuffer), Buffer(), size_);
- }
-
- // Delete the old buffer
- delete[] buffer_;
- buffer_ = newBuffer;
- }
- }
-
- /// Reallocate so that no extra memory is used.
- void Compact() { Reserve(size_); }
-
- /// Return whether contains a specific value.
- bool Contains(const T& value) const { return Find(value) != End(); }
- /// Return first element.
- T& Front() { return Buffer()[0]; }
- /// Return const first element.
- const T& Front() const { return Buffer()[0]; }
- /// Return last element.
- T& Back() { assert(size_); return Buffer()[size_ - 1]; }
- /// Return const last element.
- const T& Back() const { assert(size_); return Buffer()[size_ - 1]; }
- /// Return number of elements.
- unsigned Size() const { return size_; }
- /// Return capacity of vector.
- unsigned Capacity() const { return capacity_; }
- /// Return whether vector is empty.
- bool Empty() const { return size_ == 0; }
- };
|