diff --git a/BAN/include/BAN/Queue.h b/BAN/include/BAN/Queue.h index 8f3f9eb0..1a9f95ab 100644 --- a/BAN/include/BAN/Queue.h +++ b/BAN/include/BAN/Queue.h @@ -40,11 +40,9 @@ namespace BAN private: [[nodiscard]] ErrorOr ensure_capacity(size_type size); - const T* address_of(size_type, void* = nullptr) const; - T* address_of(size_type, void* = nullptr); private: - uint8_t* m_data = nullptr; + T* m_data = nullptr; size_type m_capacity = 0; size_type m_size = 0; }; @@ -66,7 +64,7 @@ namespace BAN { MUST(ensure_capacity(other.size())); for (size_type i = 0; i < other.size(); i++) - new (address_of(i)) T(*address_of(i, other.m_data)); + new (m_data + i) T(other.m_data[i]); m_size = other.m_size; } @@ -98,7 +96,7 @@ namespace BAN clear(); MUST(ensure_capacity(other.size())); for (size_type i = 0; i < other.size(); i++) - new (address_of(i)) T(*address_of(i, other.m_data)); + new (m_data + i) T(other.m_data[i]); m_size = other.m_size; return *this; } @@ -107,7 +105,7 @@ namespace BAN ErrorOr Queue::push(T&& value) { TRY(ensure_capacity(m_size + 1)); - new (address_of(m_size)) T(move(value)); + new (m_data + m_size) T(move(value)); m_size++; return {}; } @@ -123,7 +121,7 @@ namespace BAN ErrorOr Queue::emplace(Args&&... args) { TRY(ensure_capacity(m_size + 1)); - new (address_of(m_size)) T(forward(args)...); + new (m_data + m_size) T(forward(args)...); m_size++; return {}; } @@ -133,8 +131,8 @@ namespace BAN { ASSERT(m_size > 0); for (size_type i = 0; i < m_size - 1; i++) - *address_of(i) = move(*address_of(i + 1)); - address_of(m_size - 1)->~T(); + m_data[i] = move(m_data[i + 1]); + m_data[m_size - 1].~T(); m_size--; } @@ -142,7 +140,7 @@ namespace BAN void Queue::clear() { for (size_type i = 0; i < m_size; i++) - address_of(i)->~T(); + m_data[i].~T(); BAN::deallocator(m_data); m_data = nullptr; m_capacity = 0; @@ -165,14 +163,14 @@ namespace BAN const T& Queue::front() const { ASSERT(m_size > 0); - return *address_of(0); + return m_data[0]; } template T& Queue::front() { ASSERT(m_size > 0); - return *address_of(0); + return m_data[0]; } template @@ -181,13 +179,13 @@ namespace BAN if (m_capacity > size) return {}; size_type new_cap = BAN::Math::max(size, m_capacity * 3 / 2); - uint8_t* new_data = (uint8_t*)BAN::allocator(new_cap * sizeof(T)); + T* new_data = (T*)BAN::allocator(new_cap * sizeof(T)); if (new_data == nullptr) return Error::from_string("Queue: Could not allocate memory"); for (size_type i = 0; i < m_size; i++) { - new (address_of(i, new_data)) T(move(*address_of(i))); - address_of(i)->~T(); + new (new_data + i) T(move(m_data[i])); + m_data[i].~T(); } BAN::deallocator(m_data); m_data = new_data; @@ -195,20 +193,4 @@ namespace BAN return {}; } - template - const T* Queue::address_of(size_type index, void* base) const - { - if (base == nullptr) - base = m_data; - return (T*)base + index; - } - - template - T* Queue::address_of(size_type index, void* base) - { - if (base == nullptr) - base = m_data; - return (T*)base + index; - } - } \ No newline at end of file diff --git a/BAN/include/BAN/Vector.h b/BAN/include/BAN/Vector.h index 01ab2e72..ffc2c4a5 100644 --- a/BAN/include/BAN/Vector.h +++ b/BAN/include/BAN/Vector.h @@ -39,10 +39,10 @@ namespace BAN [[nodiscard]] ErrorOr insert(size_type, T&&); [[nodiscard]] ErrorOr insert(size_type, const T&); - iterator begin() { return iterator(address_of(0)); } - const_iterator begin() const { return const_iterator(address_of(0)); } - iterator end() { return iterator(address_of(m_size)); } - const_iterator end() const { return const_iterator(address_of(m_size)); } + iterator begin() { return iterator (m_data); } + const_iterator begin() const { return const_iterator(m_data); } + iterator end() { return iterator (m_data + m_size); } + const_iterator end() const { return const_iterator(m_data + m_size); } void pop_back(); void remove(size_type); @@ -67,11 +67,9 @@ namespace BAN private: [[nodiscard]] ErrorOr ensure_capacity(size_type); - const T* address_of(size_type, void* = nullptr) const; - T* address_of(size_type, void* = nullptr); private: - uint8_t* m_data = nullptr; + T* m_data = nullptr; size_type m_capacity = 0; size_type m_size = 0; }; @@ -136,7 +134,7 @@ namespace BAN { MUST(ensure_capacity(other.m_size)); for (size_type i = 0; i < other.m_size; i++) - new (address_of(i)) T(other[i]); + new (m_data + i) T(other.m_data[i]); m_size = other.m_size; } @@ -168,7 +166,7 @@ namespace BAN clear(); MUST(ensure_capacity(other.size())); for (size_type i = 0; i < other.size(); i++) - new (address_of(i)) T(other[i]); + new (m_data + i) T(other[i]); m_size = other.m_size; return *this; } @@ -177,7 +175,7 @@ namespace BAN ErrorOr Vector::push_back(T&& value) { TRY(ensure_capacity(m_size + 1)); - new (address_of(m_size)) T(move(value)); + new (m_data + m_size) T(move(value)); m_size++; return {}; } @@ -193,7 +191,7 @@ namespace BAN ErrorOr Vector::emplace_back(Args&&... args) { TRY(ensure_capacity(m_size + 1)); - new (address_of(m_size)) T(forward(args)...); + new (m_data + m_size) T(forward(args)...); m_size++; return {}; } @@ -206,14 +204,14 @@ namespace BAN TRY(ensure_capacity(m_size + 1)); if (index < m_size) { - new (address_of(m_size)) T(move(*address_of(m_size - 1))); + new (m_data + m_size) T(move(m_data[m_size - 1])); for (size_type i = m_size - 1; i > index; i--) - *address_of(i) = move(*address_of(i - 1)); - *address_of(index) = move(T(forward(args)...)); + m_data[i] = move(m_data[i - 1]); + m_data[index] = move(T(forward(args)...)); } else { - new (address_of(m_size)) T(forward(args)...); + new (m_data + m_size) T(forward(args)...); } m_size++; return {}; @@ -226,14 +224,14 @@ namespace BAN TRY(ensure_capacity(m_size + 1)); if (index < m_size) { - new (address_of(m_size)) T(move(*address_of(m_size - 1))); + new (m_data + m_size) T(move(m_data[m_size - 1])); for (size_type i = m_size - 1; i > index; i--) - *address_of(i) = move(*address_of(i - 1)); - *address_of(index) = move(value); + m_data[i] = move(m_data[i - 1]); + m_data[index] = move(value); } else { - new (address_of(m_size)) T(move(value)); + new (m_data + m_size) T(move(value)); } m_size++; return {}; @@ -249,7 +247,7 @@ namespace BAN void Vector::pop_back() { ASSERT(m_size > 0); - address_of(m_size - 1)->~T(); + m_data[m_size - 1].~T(); m_size--; } @@ -258,8 +256,8 @@ namespace BAN { ASSERT(index < m_size); for (size_type i = index; i < m_size - 1; i++) - *address_of(i) = move(*address_of(i + 1)); - address_of(m_size - 1)->~T(); + m_data[i] = move(m_data[i + 1]); + m_data[m_size - 1].~T(); m_size--; } @@ -267,7 +265,7 @@ namespace BAN void Vector::clear() { for (size_type i = 0; i < m_size; i++) - address_of(i)->~T(); + m_data[i].~T(); BAN::deallocator(m_data); m_data = nullptr; m_capacity = 0; @@ -278,7 +276,7 @@ namespace BAN bool Vector::contains(const T& other) const { for (size_type i = 0; i < m_size; i++) - if (*address_of(i) == other) + if (m_data[i] == other) return true; return false; } @@ -287,41 +285,41 @@ namespace BAN const T& Vector::operator[](size_type index) const { ASSERT(index < m_size); - return *address_of(index); + return m_data[index]; } template T& Vector::operator[](size_type index) { ASSERT(index < m_size); - return *address_of(index); + return m_data[index]; } template const T& Vector::back() const { ASSERT(m_size > 0); - return *address_of(m_size - 1); + return m_data[m_size - 1]; } template T& Vector::back() { ASSERT(m_size > 0); - return *address_of(m_size - 1); + return m_data[m_size - 1]; } template const T& Vector::front() const { ASSERT(m_size > 0); - return *address_of(0); + return m_data[0]; } template T& Vector::front() { ASSERT(m_size > 0); - return *address_of(0); + return m_data[0]; } template @@ -330,10 +328,10 @@ namespace BAN TRY(ensure_capacity(size)); if (size < m_size) for (size_type i = size; i < m_size; i++) - address_of(i)->~T(); + m_data[i].~T(); if (size > m_size) for (size_type i = m_size; i < size; i++) - new (address_of(i)) T(); + new (m_data + i) T(); m_size = size; return {}; } @@ -369,13 +367,13 @@ namespace BAN if (m_capacity >= size) return {}; size_type new_cap = BAN::Math::max(size, m_capacity * 3 / 2); - uint8_t* new_data = (uint8_t*)BAN::allocator(new_cap * sizeof(T)); + T* new_data = (T*)BAN::allocator(new_cap * sizeof(T)); if (new_data == nullptr) return Error::from_string("Vector: Could not allocate memory"); for (size_type i = 0; i < m_size; i++) { - new (address_of(i, new_data)) T(move(*address_of(i))); - address_of(i)->~T(); + new (new_data + i) T(move(m_data[i])); + m_data[i].~T(); } BAN::deallocator(m_data); m_data = new_data; @@ -383,20 +381,4 @@ namespace BAN return {}; } - template - const T* Vector::address_of(size_type index, void* base) const - { - if (base == nullptr) - base = m_data; - return (T*)base + index; - } - - template - T* Vector::address_of(size_type index, void* base) - { - if (base == nullptr) - base = m_data; - return (T*)base + index; - } - } \ No newline at end of file