18#include <glm/gtc/integer.hpp>
21#include "AException.h"
24#define AUI_ASSERT_MY_ITERATOR(it) AUI_ASSERTX((this->begin() <= it && it <= this->end()), "foreign iterator")
35template<
typename StoredType>
40 using iterator = StoredType*;
41 using const_iterator =
const StoredType*;
42 using reference = StoredType&;
43 using const_reference =
const StoredType&;
44 using value = StoredType;
48 insert(mBegin, rhs.begin(), rhs.end());
51 operator=(std::move(rhs));
58 constexpr StoredType* data()
noexcept {
62 constexpr const StoredType* data()
const noexcept {
67 constexpr iterator begin()
noexcept {
71 constexpr const_iterator begin()
const noexcept {
76 constexpr iterator end()
noexcept {
81 constexpr const_iterator end()
const noexcept {
86 constexpr StoredType& front()
noexcept {
91 constexpr StoredType& back()
noexcept {
92 return *std::prev(end());
96 constexpr const StoredType& front()
const noexcept {
101 constexpr const StoredType& back()
const noexcept {
102 return *std::prev(end());
105 constexpr void push_back(StoredType value)
noexcept {
106 insert(end(), std::move(value));
109 constexpr void push_front(StoredType value)
noexcept {
110 insert(begin(), std::move(value));
113 constexpr void pop_back()
noexcept {
114 AUI_ASSERTX(size() > 0,
"ADynamicVector is empty");
115 erase(std::prev(end()));
117 constexpr void pop_front()
noexcept {
118 AUI_ASSERTX(size() > 0,
"ADynamicVector is empty");
123 constexpr StoredType& operator[](std::size_t index)
noexcept {
125 return *(data() + index);
129 constexpr StoredType& operator[](std::size_t index)
const noexcept {
134 constexpr bool empty()
const noexcept {
135 return begin() == end();
138 constexpr void clear()
noexcept {
140 mBegin = mEnd = mBufferEnd =
nullptr;
144 for (
auto& v : *
this) {
147 operator delete[](mBegin);
151 constexpr std::size_t size()
const noexcept {
152 return mEnd - mBegin;
156 constexpr std::size_t reserved()
const noexcept {
157 return mBufferEnd - mBegin;
160 template<
typename OtherIterator>
161 constexpr iterator insert(iterator at, OtherIterator begin, OtherIterator end) {
162 AUI_ASSERT_MY_ITERATOR(at);
163 auto distance = std::distance(begin, end);
165 if (size() + distance <= reserved()) {
166 return aui::container::vector_impl::insert_no_growth(mEnd, at, begin, end);
169 temp.reserve(aui::container::vector_impl::ceilPower2(distance + size()));
170 aui::container::vector_impl::insert_no_growth(temp.mEnd, temp.mEnd,
171 std::make_move_iterator(mBegin), std::make_move_iterator(at));
173 auto result = aui::container::vector_impl::insert_no_growth(temp.mEnd, temp.mEnd,
176 aui::container::vector_impl::insert_no_growth(temp.mEnd, temp.mEnd,
177 std::make_move_iterator(at), std::make_move_iterator(mEnd));
178 operator=(std::move(temp));
184 constexpr iterator insert(iterator at, StoredType value) {
185 AUI_ASSERT_MY_ITERATOR(at);
186 return insert(at, std::make_move_iterator(&value), std::make_move_iterator(&value + 1));
189 constexpr iterator erase(iterator at) {
190 return erase(at, std::next(at));
193 constexpr iterator erase(iterator begin, iterator end) {
194 AUI_ASSERT_MY_ITERATOR(begin);
195 AUI_ASSERT_MY_ITERATOR(end);
197 return aui::container::vector_impl::erase(mBegin, mEnd, begin, end);
200 void reserve(std::size_t newSize) {
201 if (reserved() == newSize) {
205 auto newBuffer =
static_cast<StoredType*
>(
operator new[](newSize *
sizeof(StoredType)));
206 auto newBufferEnd = newBuffer;
208 auto elementsToMove = std::min(newSize, size());
209 auto moveFrom = begin();
210 for (std::size_t i = 0; i < elementsToMove; ++newBufferEnd, ++moveFrom) {
211 new (newBufferEnd) StoredType(std::move(*moveFrom));
216 mBufferEnd = newBuffer + newSize;
227 mBufferEnd = rhs.mBufferEnd;
229 rhs.mBegin =
nullptr;
231 rhs.mBufferEnd =
nullptr;
245 template<
typename OtherContainer>
257 template<
typename OtherContainer>
259 return insertAll(super::end(), std::forward<OtherContainer>(c));
270 template<
typename OtherContainer>
271 iterator
insertAll(iterator at,
const OtherContainer& c)
noexcept {
272 return super::insert(at, c.begin(), c.end());
283 template<
typename OtherContainer>
284 iterator
insertAll(iterator at, OtherContainer&& c)
noexcept {
285 return super::insert(at, std::make_move_iterator(c.begin()), std::make_move_iterator(c.end()));
311 template<
typename OtherContainer>
320 bool contains(
const StoredType& value)
const noexcept {
325 std::size_t sizeInBytes() const noexcept {
326 return super::size() *
sizeof(StoredType);
330 StoredType& at(std::size_t index) {
331 if (index >= super::size()) {
332 aui::impl::outOfBoundsException();
334 return super::operator[](index);
338 const StoredType& at(std::size_t index)
const {
339 if (index >= super::size()) {
340 aui::impl::outOfBoundsException();
342 return super::operator[](index);
353 super::push_back(rhs);
364 super::push_back(std::forward<StoredType>(rhs));
373 template<
typename OtherContainer, std::enable_if_t<!std::is_convertible_v<OtherContainer, StoredType>,
bool> = true>
385 template<
typename OtherContainer, std::enable_if_t<!std::is_convertible_v<OtherContainer, StoredType>,
bool> = true>
388 insertAll(std::forward<OtherContainer>(c));
402 AUI_ASSERTX(!super::empty(),
"empty container could not have the first element");
403 return super::front();
413 const StoredType&
first() const noexcept
415 AUI_ASSERTX(!super::empty(),
"empty container could not have the first element");
416 return super::front();
428 AUI_ASSERTX(!super::empty(),
"empty container could not have the last element");
429 return super::back();
439 const StoredType&
last() const noexcept
441 AUI_ASSERTX(!super::empty(),
"empty container could not have the last element");
442 return super::back();
450 size_t indexOf(
const StoredType& value)
const noexcept
456 void sort() noexcept {
457 std::sort(super::begin(), super::end());
460 template<
typename Comparator>
461 void sort(Comparator&& comparator)
noexcept {
462 std::sort(super::begin(), super::end(), std::forward<Comparator>(comparator));
482 template<
typename Predicate>
485 super::erase(std::remove_if(super::begin(), super::end(), std::forward<Predicate>(predicate)), super::end());
498 template<aui::incrementable Iterator, aui::invocable<decltype(*std::declval<Iterator>())> UnaryOperation>
500 AVector<
decltype(transformer(range.first()))> result;
501 result.reserve(range.size());
502 std::transform(range.begin(), range.end(), std::back_inserter(result), std::forward<UnaryOperation>(transformer));
506 template<aui::invocable<const StoredType&> UnaryOperation>
509 result.reserve(super::size());
510 std::transform(super::begin(), super::end(), std::back_inserter(result), std::forward<UnaryOperation>(transformer));
514 template<aui::invocable<const StoredType&> UnaryOperation>
517 decltype(transformer(std::declval<StoredType>()).second)> {
521 template<aui::invocable<StoredType&> UnaryOperation>
524 decltype(transformer(std::declval<StoredType>()).second)> {
528 template<aui::predicate<const StoredType&> Predicate>
529 self filter(Predicate&& predicate) {
531 result.reserve(super::size());
532 for (
const auto& element : *
this) {
533 if (predicate(element)) {
534 result.push_back(element);
541 iterator mBegin =
nullptr;
542 iterator mEnd =
nullptr;
543 iterator mBufferEnd =
nullptr;
Vector implementation for ASmallVector.
Definition: ADynamicVector.h:36
bool isSubsetOf(const OtherContainer &c) const noexcept
Definition: ADynamicVector.h:312
StoredType & first() noexcept
Definition: ADynamicVector.h:400
const StoredType & last() const noexcept
Definition: ADynamicVector.h:439
self & operator<<(OtherContainer &&c) noexcept
Definition: ADynamicVector.h:386
void removeIf(Predicate &&predicate) noexcept
Definition: ADynamicVector.h:483
iterator insertAll(iterator at, const OtherContainer &c) noexcept
Definition: ADynamicVector.h:271
self & operator<<(StoredType &&rhs) noexcept
Definition: ADynamicVector.h:362
iterator insertAll(OtherContainer &&c) noexcept
Definition: ADynamicVector.h:258
size_t indexOf(const StoredType &value) const noexcept
Definition: ADynamicVector.h:450
const StoredType & first() const noexcept
Definition: ADynamicVector.h:413
iterator insertAll(iterator at, OtherContainer &&c) noexcept
Definition: ADynamicVector.h:284
void removeFirst(const StoredType &item) noexcept
Definition: ADynamicVector.h:302
self & operator<<(const OtherContainer &c) noexcept
Definition: ADynamicVector.h:374
void removeAll(const StoredType &item) noexcept
Definition: ADynamicVector.h:293
void removeAt(size_t index) noexcept
Definition: ADynamicVector.h:473
bool contains(const StoredType &value) const noexcept
Definition: ADynamicVector.h:320
StoredType & last() noexcept
Definition: ADynamicVector.h:426
iterator insertAll(const OtherContainer &c) noexcept
Definition: ADynamicVector.h:246
static auto fromRange(aui::range< Iterator > range, UnaryOperation &&transformer) -> AVector< decltype(transformer(range.first()))>
Constructs a new vector of transformed items of the range.
Definition: ADynamicVector.h:499
self & operator<<(const StoredType &rhs) noexcept
Definition: ADynamicVector.h:351
A std::map with AUI extensions.
Definition: AMap.h:218
A std::set with AUI extensions.
Definition: ASet.h:25
A std::vector with AUI extensions.
Definition: AVector.h:38
bool is_subset(LContainer &l, RContainer &r) noexcept
Definition: containers.h:233
bool contains(const Container &c, const typename Container::const_reference value) noexcept
Definition: containers.h:153
AOptional< std::size_t > remove_first(Container &container, typename Container::const_reference value) noexcept
Removes first occurrence of value.
Definition: containers.h:200
auto to_map(Iterator begin, Iterator end, UnaryOperation &&transformer)
Transforms sequence to map.
Definition: AMap.h:237
size_t index_of(const Container &c, const typename Container::const_reference value) noexcept
Finds the index of the first occurrence of the value.
Definition: containers.h:141
void remove_at(Container &c, size_t index) noexcept
Removes element at the specified index.
Definition: containers.h:128
void remove_all(Container &container, typename Container::const_reference value) noexcept
Removes all occurrences of value.
Definition: containers.h:172
#define AUI_ASSERTX(condition, what)
Asserts that the passed condition evaluates to true. Adds extra message string.
Definition: Assert.h:74
Definition: iterators.h:50