diff --git a/BAN/include/BAN/Queue.h b/BAN/include/BAN/Queue.h index fd6e94d57f..488f091afe 100644 --- a/BAN/include/BAN/Queue.h +++ b/BAN/include/BAN/Queue.h @@ -37,7 +37,8 @@ namespace BAN private: [[nodiscard]] ErrorOr EnsureCapacity(size_type size); - T* Address(size_type, uint8_t* = nullptr) const; + const T* AddressOf(size_type, uint8_t* = nullptr) const; + T* AddressOf(size_type, uint8_t* = nullptr); private: uint8_t* m_data = nullptr; @@ -62,7 +63,7 @@ namespace BAN { MUST(EnsureCapacity(other.Size())); for (size_type i = 0; i < other.Size(); i++) - new (Address(i)) T(*Address(i, other.m_data)); + new (AddressOf(i)) T(*AddressOf(i, other.m_data)); m_size = other.m_size; } @@ -94,7 +95,7 @@ namespace BAN Clear(); MUST(EnsureCapacity(other.Size())); for (size_type i = 0; i < other.Size(); i++) - new (Address(i)) T(*Address(i, other.m_data)); + new (AddressOf(i)) T(*AddressOf(i, other.m_data)); m_size = other.m_size; return *this; } @@ -103,7 +104,7 @@ namespace BAN ErrorOr Queue::Push(T&& value) { TRY(EnsureCapacity(m_size + 1)); - new (Address(m_size)) T(Move(value)); + new (AddressOf(m_size)) T(Move(value)); m_size++; return {}; } @@ -119,8 +120,8 @@ namespace BAN { ASSERT(m_size > 0); for (size_type i = 0; i < m_size - 1; i++) - *Address(i) = Move(*Address(i + 1)); - Address(m_size - 1)->~T(); + *AddressOf(i) = Move(*AddressOf(i + 1)); + AddressOf(m_size - 1)->~T(); m_size--; } @@ -128,7 +129,7 @@ namespace BAN void Queue::Clear() { for (size_type i = 0; i < m_size; i++) - Address(i)->~T(); + AddressOf(i)->~T(); BAN::deallocator(m_data); m_data = nullptr; m_capacity = 0; @@ -151,14 +152,14 @@ namespace BAN const T& Queue::Front() const { ASSERT(m_size > 0); - return *Address(0); + return *AddressOf(0); } template T& Queue::Front() { ASSERT(m_size > 0); - return *Address(0); + return *AddressOf(0); } template @@ -172,8 +173,8 @@ namespace BAN return Error::FromString("Queue: Could not allocate memory"); for (size_type i = 0; i < m_size; i++) { - new (Address(i, new_data)) T(Move(*Address(i))); - Address(i)->~T(); + new (AddressOf(i, new_data)) T(Move(*AddressOf(i))); + AddressOf(i)->~T(); } BAN::deallocator(m_data); m_data = new_data; @@ -182,7 +183,15 @@ namespace BAN } template - T* Queue::Address(size_type index, uint8_t* base) const + const T* Queue::AddressOf(size_type index, uint8_t* base) const + { + if (base == nullptr) + base = m_data; + return (T*)(base + index * sizeof(T)); + } + + template + T* Queue::AddressOf(size_type index, uint8_t* base) { if (base == nullptr) base = m_data; diff --git a/BAN/include/BAN/Vector.h b/BAN/include/BAN/Vector.h index d4d698bf03..c3182c71ed 100644 --- a/BAN/include/BAN/Vector.h +++ b/BAN/include/BAN/Vector.h @@ -101,7 +101,8 @@ namespace BAN size_type Capacity() const; private: - T* Address(size_type, uint8_t* = nullptr) const; + const T* AddressOf(size_type, uint8_t* = nullptr) const; + T* AddressOf(size_type, uint8_t* = nullptr); [[nodiscard]] ErrorOr EnsureCapasity(size_type); private: @@ -127,7 +128,7 @@ namespace BAN { MUST(EnsureCapasity(other.m_size)); for (size_type i = 0; i < other.m_size; i++) - new (Address(i)) T(other[i]); + new (AddressOf(i)) T(other[i]); m_size = other.m_size; } @@ -157,7 +158,7 @@ namespace BAN Clear(); MUST(EnsureCapasity(other.Size())); for (size_type i = 0; i < other.Size(); i++) - new (Address(i)) T(other[i]); + new (AddressOf(i)) T(other[i]); m_size = other.m_size; return *this; } @@ -166,7 +167,7 @@ namespace BAN ErrorOr Vector::PushBack(T&& value) { TRY(EnsureCapasity(m_size + 1)); - new (Address(m_size)) T(Move(value)); + new (AddressOf(m_size)) T(Move(value)); m_size++; return {}; } @@ -184,11 +185,11 @@ namespace BAN TRY(EnsureCapasity(m_size + 1)); if (index < m_size) { - new (Address(m_size)) T(Move(*Address(m_size - 1))); + new (AddressOf(m_size)) T(Move(*AddressOf(m_size - 1))); for (size_type i = m_size - 1; i > index; i--) - *Address(i) = Move(*Address(i - 1)); + *AddressOf(i) = Move(*AddressOf(i - 1)); } - *Address(index) = Move(value); + *AddressOf(index) = Move(value); m_size++; return {}; } @@ -202,32 +203,32 @@ namespace BAN template typename Vector::iterator Vector::begin() { - return VectorIterator(Address(0)); + return VectorIterator(AddressOf(0)); } template typename Vector::iterator Vector::end() { - return VectorIterator(Address(m_size)); + return VectorIterator(AddressOf(m_size)); } template typename Vector::const_iterator Vector::begin() const { - return VectorConstIterator(Address(0)); + return VectorConstIterator(AddressOf(0)); } template typename Vector::const_iterator Vector::end() const { - return VectorConstIterator(Address(m_size)); + return VectorConstIterator(AddressOf(m_size)); } template void Vector::PopBack() { ASSERT(m_size > 0); - Address(m_size - 1)->~T(); + AddressOf(m_size - 1)->~T(); m_size--; } @@ -236,8 +237,8 @@ namespace BAN { ASSERT(index < m_size); for (size_type i = index; i < m_size - 1; i++) - *Address(i) = Move(*Address(i + 1)); - Address(m_size - 1)->~T(); + *AddressOf(i) = Move(*AddressOf(i + 1)); + AddressOf(m_size - 1)->~T(); m_size--; } @@ -245,7 +246,7 @@ namespace BAN void Vector::Clear() { for (size_type i = 0; i < m_size; i++) - Address(i)->~T(); + AddressOf(i)->~T(); BAN::deallocator(m_data); m_data = nullptr; m_capacity = 0; @@ -256,7 +257,7 @@ namespace BAN bool Vector::Has(const T& other) const { for (size_type i = 0; i < m_size; i++) - if (*Address(i) == other) + if (*AddressOf(i) == other) return true; return false; } @@ -265,41 +266,41 @@ namespace BAN const T& Vector::operator[](size_type index) const { ASSERT(index < m_size); - return *Address(index); + return *AddressOf(index); } template T& Vector::operator[](size_type index) { ASSERT(index < m_size); - return *Address(index); + return *AddressOf(index); } template const T& Vector::Back() const { ASSERT(m_size > 0); - return *Address(m_size - 1); + return *AddressOf(m_size - 1); } template T& Vector::Back() { ASSERT(m_size > 0); - return *Address(m_size - 1); + return *AddressOf(m_size - 1); } template const T& Vector::Front() const { ASSERT(m_size > 0); - return *Address(0); + return *AddressOf(0); } template T& Vector::Front() { ASSERT(m_size > 0); - return *Address(0); + return *AddressOf(0); } template @@ -308,10 +309,10 @@ namespace BAN TRY(EnsureCapasity(size)); if (size < m_size) for (size_type i = size; i < m_size; i++) - Address(i)->~T(); + AddressOf(i)->~T(); if (size > m_size) for (size_type i = m_size; i < size; i++) - new (Address(i)) T(); + new (AddressOf(i)) T(); m_size = size; return {}; } @@ -352,8 +353,8 @@ namespace BAN return Error::FromString("Vector: Could not allocate memory"); for (size_type i = 0; i < m_size; i++) { - new (Address(i, new_data)) T(Move(*Address(i))); - Address(i)->~T(); + new (AddressOf(i, new_data)) T(Move(*AddressOf(i))); + AddressOf(i)->~T(); } BAN::deallocator(m_data); m_data = new_data; @@ -362,7 +363,15 @@ namespace BAN } template - T* Vector::Address(size_type index, uint8_t* base) const + const T* Vector::AddressOf(size_type index, uint8_t* base) const + { + if (base == nullptr) + base = m_data; + return (T*)(base + index * sizeof(T)); + } + + template + T* Vector::AddressOf(size_type index, uint8_t* base) { if (base == nullptr) base = m_data;