15#include "AException.h"
20#define AUI_ASSERT_MY_ITERATOR(it) AUI_ASSERTX((this->begin() <= it && it <= this->end()), "foreign iterator")
32template<
typename StoredType, std::
size_t MaxSize>
37 using iterator = StoredType*;
38 using const_iterator =
const StoredType*;
39 using reference = StoredType&;
40 using const_reference =
const StoredType&;
41 using value = StoredType;
43 constexpr AStaticVector()
noexcept: mBegin(
reinterpret_cast<StoredType*
>(&mStorage)), mEnd(mBegin) {}
45 insert(mBegin, rhs.begin(), rhs.end());
48 insert(mBegin, std::make_move_iterator(rhs.begin()), std::make_move_iterator(rhs.end()));
52 insert(mBegin, std::make_move_iterator(rhs.begin()), std::make_move_iterator(rhs.end()));
54 template<
typename Iterator>
56 for (
auto it = begin; it != end; ++it) {
61 for (
auto& v : *
this) {
67 static constexpr size_t capacity()
noexcept {
76 insert(mBegin, rhs.begin(), rhs.end());
85 insert(mBegin, std::make_move_iterator(rhs.begin()), std::make_move_iterator(rhs.end()));
91 constexpr bool full()
const noexcept {
92 return size() >= MaxSize;
96 constexpr StoredType* data()
noexcept {
100 constexpr const StoredType* data()
const noexcept {
105 constexpr iterator begin()
noexcept {
109 constexpr const_iterator begin()
const noexcept {
114 constexpr iterator end()
noexcept {
119 constexpr const_iterator end()
const noexcept {
124 constexpr StoredType& front()
noexcept {
129 constexpr StoredType& back()
noexcept {
130 return *std::prev(end());
134 constexpr const StoredType& front()
const noexcept {
139 constexpr const StoredType& back()
const noexcept {
140 return *std::prev(end());
143 template<
typename... Args>
144 constexpr void emplace_back(Args&&...
args)
noexcept {
145 AUI_ASSERTX(!full(),
"insufficient size in AStaticVector");
146 new (mEnd++) StoredType(std::forward<Args>(
args)...);
150 constexpr void push_back(StoredType value)
noexcept {
151 AUI_ASSERTX(!full(),
"insufficient size in AStaticVector");
152 new (mEnd++) StoredType(std::move(value));
155 constexpr void push_front(StoredType value)
noexcept {
156 AUI_ASSERTX(!full(),
"insufficient size in AStaticVector");
157 insert(begin(), std::move(value));
160 constexpr void pop_back()
noexcept {
162 erase(std::prev(end()));
164 constexpr void pop_front()
noexcept {
170 constexpr StoredType& operator[](std::size_t index)
noexcept {
172 return *(data() + index);
176 constexpr StoredType& operator[](std::size_t index)
const noexcept {
181 constexpr bool empty()
const noexcept {
182 return begin() == end();
185 constexpr void clear()
noexcept {
186 for (
auto& v : *
this) {
193 constexpr std::size_t size()
const noexcept {
194 return mEnd - mBegin;
197 template<
typename OtherIterator>
198 constexpr iterator insert(iterator at, OtherIterator begin, OtherIterator end) {
199 AUI_ASSERT_MY_ITERATOR(at);
200 auto distance = std::distance(begin, end);
201 AUI_ASSERTX(size() + distance <= MaxSize,
"out of bounds");
203 return aui::container::vector_impl::insert_no_growth(mEnd, at, begin, end);
206 constexpr iterator insert(iterator at, StoredType value) {
207 AUI_ASSERT_MY_ITERATOR(at);
208 return insert(at, std::make_move_iterator(&value), std::make_move_iterator(&value + 1));
211 constexpr iterator erase(iterator at) {
212 return erase(at, std::next(at));
215 constexpr iterator erase(iterator begin, iterator end) {
216 AUI_ASSERT_MY_ITERATOR(begin);
217 AUI_ASSERT_MY_ITERATOR(end);
219 return aui::container::vector_impl::erase(mBegin, mEnd, begin, end);
222 void reserve(std::size_t) {
235 template<
typename OtherContainer>
247 template<
typename OtherContainer>
249 return insertAll(super::end(), std::forward<OtherContainer>(c));
260 template<
typename OtherContainer>
261 iterator
insertAll(iterator at,
const OtherContainer& c)
noexcept {
262 return super::insert(at, c.begin(), c.end());
273 template<
typename OtherContainer>
274 iterator
insertAll(iterator at, OtherContainer&& c)
noexcept {
275 return super::insert(at, std::make_move_iterator(c.begin()), std::make_move_iterator(c.end()));
301 template<
typename OtherContainer>
310 bool contains(
const StoredType& value)
const noexcept {
315 std::size_t sizeInBytes() const noexcept {
316 return super::size() *
sizeof(StoredType);
320 StoredType& at(std::size_t index) {
321 if (index >= super::size()) {
322 aui::impl::outOfBoundsException();
324 return super::operator[](index);
328 const StoredType& at(std::size_t index)
const {
329 if (index >= super::size()) {
330 aui::impl::outOfBoundsException();
332 return super::operator[](index);
343 super::push_back(rhs);
354 super::push_back(std::forward<StoredType>(rhs));
363 template<
typename OtherContainer, std::enable_if_t<!std::is_convertible_v<OtherContainer, StoredType>,
bool> = true>
375 template<
typename OtherContainer, std::enable_if_t<!std::is_convertible_v<OtherContainer, StoredType>,
bool> = true>
378 insertAll(std::forward<OtherContainer>(c));
392 AUI_ASSERTX(!super::empty(),
"empty container could not have the first element");
393 return super::front();
403 const StoredType&
first() const noexcept
405 AUI_ASSERTX(!super::empty(),
"empty container could not have the first element");
406 return super::front();
418 AUI_ASSERTX(!super::empty(),
"empty container could not have the last element");
419 return super::back();
429 const StoredType&
last() const noexcept
431 AUI_ASSERTX(!super::empty(),
"empty container could not have the last element");
432 return super::back();
440 size_t indexOf(
const StoredType& value)
const noexcept
446 void sort() noexcept {
447 std::sort(super::begin(), super::end());
450 template<
typename Comparator>
451 void sort(Comparator&& comparator)
noexcept {
452 std::sort(super::begin(), super::end(), std::forward<Comparator>(comparator));
472 template<
typename Predicate>
475 super::erase(std::remove_if(super::begin(), super::end(), std::forward<Predicate>(predicate)), super::end());
488 template<aui::incrementable Iterator, aui::invocable<decltype(*std::declval<Iterator>())> UnaryOperation>
490 AVector<
decltype(transformer(range.first()))> result;
491 result.reserve(range.size());
492 std::transform(range.begin(), range.end(), std::back_inserter(result), std::forward<UnaryOperation>(transformer));
496 template<aui::invocable<const StoredType&> UnaryOperation>
499 result.reserve(super::size());
500 std::transform(super::begin(), super::end(), std::back_inserter(result), std::forward<UnaryOperation>(transformer));
504 template<aui::invocable<const StoredType&> UnaryOperation>
507 decltype(transformer(std::declval<StoredType>()).second)> {
511 template<aui::invocable<StoredType&> UnaryOperation>
514 decltype(transformer(std::declval<StoredType>()).second)> {
518 template<aui::predicate<const StoredType&> Predicate>
519 self filter(Predicate&& predicate) {
521 result.reserve(super::size());
522 for (
const auto& element : *
this) {
523 if (predicate(element)) {
524 result.push_back(element);
531 operator std::span<StoredType>() {
532 return std::span(data(), size());
536 operator std::span<const StoredType>()
const {
537 return std::span(data(), size());
543 std::aligned_storage_t<
sizeof(StoredType) * MaxSize,
alignof(StoredType)> mStorage;
A std::map with AUI extensions.
Definition: AMap.h:218
A std::set with AUI extensions.
Definition: ASet.h:25
Vector-like container up to maxSize elements inplace.
Definition: AStaticVector.h:33
StoredType & first() noexcept
Definition: AStaticVector.h:390
iterator insertAll(iterator at, const OtherContainer &c) noexcept
Definition: AStaticVector.h:261
void removeAt(size_t index) noexcept
Definition: AStaticVector.h:463
self & operator<<(const StoredType &rhs) noexcept
Definition: AStaticVector.h:341
void removeFirst(const StoredType &item) noexcept
Definition: AStaticVector.h:292
void removeAll(const StoredType &item) noexcept
Definition: AStaticVector.h:283
self & operator<<(StoredType &&rhs) noexcept
Definition: AStaticVector.h:352
const StoredType & first() const noexcept
Definition: AStaticVector.h:403
bool contains(const StoredType &value) const noexcept
Definition: AStaticVector.h:310
iterator insertAll(OtherContainer &&c) noexcept
Definition: AStaticVector.h:248
iterator insertAll(const OtherContainer &c) noexcept
Definition: AStaticVector.h:236
void removeIf(Predicate &&predicate) noexcept
Definition: AStaticVector.h:473
self & operator<<(OtherContainer &&c) noexcept
Definition: AStaticVector.h:376
const StoredType & last() const noexcept
Definition: AStaticVector.h:429
self & operator<<(const OtherContainer &c) noexcept
Definition: AStaticVector.h:364
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: AStaticVector.h:489
StoredType & last() noexcept
Definition: AStaticVector.h:416
bool isSubsetOf(const OtherContainer &c) const noexcept
Definition: AStaticVector.h:302
size_t indexOf(const StoredType &value) const noexcept
Definition: AStaticVector.h:440
iterator insertAll(iterator at, OtherContainer &&c) noexcept
Definition: AStaticVector.h:274
A std::vector with AUI extensions.
Definition: AVector.h:38
bool is_subset(LContainer &l, RContainer &r) noexcept
Definition: containers.h:233
API_AUI_CORE const ACommandLineArgs & args() noexcept
Definition: OSAndroid.cpp:29
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