17#include "SharedPtrTypes.h"
21#include <AUI/Traits/containers.h>
22#include <AUI/Traits/iterators.h>
23#include "AContainerPrototypes.h"
24#include "AUI/Traits/concepts.h"
29 API_AUI_CORE
void outOfBoundsException();
36template <
class StoredType,
class Allocator>
37class AVector:
public std::vector<StoredType, Allocator>
40 using super = std::vector<StoredType, Allocator>;
45 using iterator =
typename super::iterator;
47 template<
typename Iterator>
50 explicit AVector(std::vector<StoredType, Allocator>&& rhs)
noexcept: super(std::move(rhs)) {}
59 template<
typename OtherContainer>
60 iterator
insertAll(
const OtherContainer& c)
noexcept {
71 template<
typename OtherContainer>
73 return insertAll(super::end(), std::forward<OtherContainer>(c));
84 template<
typename OtherContainer>
85 iterator
insertAll(iterator at,
const OtherContainer& c)
noexcept {
86 return super::insert(at, c.begin(), c.end());
97 template<
typename OtherContainer>
98 iterator
insertAll(iterator at, OtherContainer&& c)
noexcept {
99 return super::insert(at, std::make_move_iterator(c.begin()), std::make_move_iterator(c.end()));
118 template<
typename T, aui::mapper<const StoredType&, const T&> Projection>
119 void removeAll(
const T& item, Projection projection)
noexcept
138 template<
typename OtherContainer>
147 bool contains(
const StoredType& value)
const noexcept {
152 std::size_t sizeInBytes() const noexcept {
153 return super::size() *
sizeof(StoredType);
157 StoredType& at(std::size_t index) {
158 if (index >= super::size()) {
159 aui::impl::outOfBoundsException();
161 return super::operator[](index);
165 const StoredType& at(std::size_t index)
const {
166 if (index >= super::size()) {
167 aui::impl::outOfBoundsException();
169 return super::operator[](index);
180 super::push_back(rhs);
191 super::push_back(std::move(rhs));
200 template<
typename OtherContainer, std::enable_if_t<!std::is_convertible_v<OtherContainer, StoredType>,
bool> = true>
212 template<
typename OtherContainer, std::enable_if_t<!std::is_convertible_v<OtherContainer, StoredType>,
bool> = true>
215 insertAll(std::forward<OtherContainer>(c));
230 AUI_ASSERTX(!super::empty(),
"empty container could not have the first element");
231 return super::front();
242 const StoredType&
first() const noexcept
244 AUI_ASSERTX(!super::empty(),
"empty container could not have the first element");
245 return super::front();
258 AUI_ASSERTX(!super::empty(),
"empty container could not have the last element");
259 return super::back();
270 const StoredType&
last() const noexcept
272 AUI_ASSERTX(!super::empty(),
"empty container could not have the last element");
273 return super::back();
282 size_t indexOf(
const StoredType& value)
const noexcept
289 std::sort(super::begin(), super::end());
293 template<
typename Comparator>
295 std::sort(super::begin(), super::end(), std::forward<Comparator>(comparator));
304 template<aui::predicate<StoredType> Predicate>
306 StoredType*
findIf(Predicate&& predicate)
noexcept
308 if (
auto i = std::find_if(super::begin(), super::end(), std::forward<Predicate>(predicate)); i != super::end()) {
332 template<aui::predicate<StoredType> Predicate>
335 super::erase(std::remove_if(super::begin(), super::end(), std::forward<Predicate>(predicate)), super::end());
342 template<aui::predicate<StoredType> Predicate>
345 auto i = std::find_if(super::begin(), super::end(), std::forward<Predicate>(predicate));
346 if (i == super::end()) {
352 template<aui::mapper<std::
size_t, StoredType> Callable>
357 for (
size_t i = 0; i < size; ++i) {
374 template<aui::incrementable Iterator, aui::invocable<decltype(*std::declval<Iterator>())> UnaryOperation>
377 AVector<
decltype(transformer(range.first()))> result;
378 result.reserve(range.size());
379 std::transform(range.begin(), range.end(), std::back_inserter(result), std::forward<UnaryOperation>(transformer));
383 template<aui::invocable<StoredType&> UnaryOperation>
387 result.reserve(super::size());
388 std::transform(super::begin(), super::end(), std::back_inserter(result), std::forward<UnaryOperation>(transformer));
392 template<aui::invocable<const StoredType&> UnaryOperation>
396 result.reserve(super::size());
397 std::transform(super::begin(), super::end(), std::back_inserter(result), std::forward<UnaryOperation>(transformer));
401 template<aui::invocable<const StoredType&> UnaryOperation>
404 decltype(transformer(std::declval<StoredType>()).second)> {
408 template<aui::invocable<StoredType&> UnaryOperation>
411 decltype(transformer(std::declval<StoredType>()).second)> {
415 template<aui::predicate<const StoredType&> Predicate>
417 self filter(Predicate&& predicate) {
419 result.reserve(super::size());
420 for (
const auto& element : *
this) {
421 if (predicate(element)) {
422 result.push_back(element);
431inline std::ostream& operator<<(std::ostream& o,
const AVector<T>& v) {
435 o <<
"[ " << v.
first();
436 for (
auto it = v.begin() + 1; it != v.end(); ++it) {
A std::map with AUI extensions.
Definition: AMap.h:218
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition: AOptional.h:32
A std::set with AUI extensions.
Definition: ASet.h:25
A std::vector with AUI extensions.
Definition: AVector.h:38
const StoredType & first() const noexcept
Definition: AVector.h:242
self & operator<<(StoredType &&rhs) noexcept
Definition: AVector.h:189
bool contains(const StoredType &value) const noexcept
Definition: AVector.h:147
iterator insertAll(iterator at, const OtherContainer &c) noexcept
Definition: AVector.h:85
self & operator<<(OtherContainer &&c) noexcept
Definition: AVector.h:213
void removeIf(Predicate &&predicate) noexcept
Definition: AVector.h:333
StoredType * findIf(Predicate &&predicate) noexcept
Finds element by predicate.
Definition: AVector.h:306
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: AVector.h:376
self & operator<<(const StoredType &rhs) noexcept
Definition: AVector.h:178
iterator insertAll(iterator at, OtherContainer &&c) noexcept
Definition: AVector.h:98
void removeAll(const T &item, Projection projection) noexcept
Definition: AVector.h:119
const StoredType & last() const noexcept
Definition: AVector.h:270
StoredType & first() noexcept
Definition: AVector.h:228
StoredType & last() noexcept
Definition: AVector.h:256
bool isSubsetOf(const OtherContainer &c) const noexcept
Definition: AVector.h:139
AOptional< std::size_t > removeFirst(const StoredType &item) noexcept
Definition: AVector.h:129
void removeIfFirst(Predicate &&predicate) noexcept
Definition: AVector.h:343
self & operator<<(const OtherContainer &c) noexcept
Definition: AVector.h:201
void removeAt(size_t index) noexcept
Definition: AVector.h:323
iterator insertAll(const OtherContainer &c) noexcept
Definition: AVector.h:60
iterator insertAll(OtherContainer &&c) noexcept
Definition: AVector.h:72
void removeAll(const StoredType &item) noexcept
Definition: AVector.h:107
size_t indexOf(const StoredType &value) const noexcept
Definition: AVector.h:282
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