24#include <AUI/Common/AOptional.h>
25#include <AUI/Traits/iterators.h>
28namespace aui::container {
30 namespace vector_impl {
31 template<
typename T,
typename OtherIterator>
32 auto insert_no_growth(T*& vectorEnd, T* at, OtherIterator begin, OtherIterator end) {
33 auto distance = std::distance(begin, end);
35 aui::range insertTargetRange(at, at + distance);
38 aui::range shiftRange(at, vectorEnd);
39 if (!shiftRange.empty()) {
40 auto shiftFrom = std::prev(shiftRange.end());
41 auto shiftTo = shiftFrom + distance;
42 for (; shiftFrom >= shiftRange.begin(); --shiftFrom, --shiftTo) {
43 if (shiftTo >= vectorEnd) {
44 new (shiftTo) T(std::move(*shiftFrom));
46 *shiftTo = std::move(*shiftFrom);
52 for (; it < vectorEnd && begin != end; ++it, ++begin) {
56 for (; begin != end; ++it, ++begin) {
60 vectorEnd += distance;
64 template<
typename T,
typename OtherIterator>
65 auto erase(T* vectorBegin, T*& vectorEnd, OtherIterator begin, OtherIterator end) {
67 return std::prev(vectorEnd);
71 auto remainingValueIt = end;
74 for (; remainingValueIt != vectorEnd; ++eraseIt, ++remainingValueIt) {
75 *eraseIt = std::move(*remainingValueIt);
78 std::size_t destructedValuesCounter = 0;
79 for (; eraseIt != vectorEnd; ++eraseIt, ++destructedValuesCounter) {
82 vectorEnd -= destructedValuesCounter;
97 template<
typename Container>
98 void remove_at(Container& c,
size_t index)
noexcept {
99 AUI_ASSERTX(c.size() > index,
"index out of bounds");
100 c.erase(c.begin() + index);
110 template<
typename Container>
113 auto it = std::find(c.begin(), c.end(), value);
114 if (it == c.end())
return std::nullopt;
115 return it - c.begin();
122 template<
typename Container>
124 bool contains(
const Container& c,
const typename Container::const_reference value)
noexcept {
125 return std::find(c.begin(), c.end(), value) != c.end();
132 template<
typename Iterator>
134 bool contains(Iterator begin, Iterator end,
const typename std::iterator_traits<Iterator>::value_type& value)
noexcept {
135 return std::find(begin, end, value) != end;
142 template<
typename Container>
143 void remove_all(Container& container,
typename Container::const_reference value)
noexcept {
144 container.erase(std::remove_if(container.begin(), container.end(), [&](
typename Container::const_reference probe)
146 return value == probe;
147 }), container.end());
158 template<
typename Container,
typename T,
typename Projection>
159 void remove_all(Container& container,
const T& value,
const Projection& projection)
noexcept {
160 container.erase(std::remove_if(container.begin(), container.end(), [&](
typename Container::const_reference probe)
162 return value == std::invoke(projection, probe);
163 }), container.end());
171 template<
typename Container>
173 for (
auto it = container.begin(); it != container.end(); ++it) {
175 auto index = std::distance(container.begin(), it);
187 template<
typename Iterator,
typename UnaryOperation>
189 auto to_map(Iterator begin, Iterator end, UnaryOperation&& transformer);
195 template<
typename Iterator,
typename UnaryOperation>
197 auto to_unordered_map(Iterator begin, Iterator end, UnaryOperation&& transformer);
203 template<
typename LContainer,
typename RContainer>
225template<
typename Container>
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition AOptional.h:33
bool is_subset(LContainer &l, RContainer &r) noexcept
Definition containers.h:205
AOptional< 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:112
AOptional< size_t > indexOf(const Container &c, const typename Container::const_reference value) noexcept
Finds the index of the first occurrence of the value.
Definition containers.h:227
bool contains(const Container &c, const typename Container::const_reference value) noexcept
Definition containers.h:124
auto to_unordered_map(Iterator begin, Iterator end, UnaryOperation &&transformer)
Transforms sequence to unordered_map.
Definition AMap.h:251
AOptional< std::size_t > remove_first(Container &container, typename Container::const_reference value) noexcept
Removes first occurrence of value.
Definition containers.h:172
auto to_map(Iterator begin, Iterator end, UnaryOperation &&transformer)
Transforms sequence to map.
Definition AMap.h:237
void remove_at(Container &c, size_t index) noexcept
Removes element at the specified index.
Definition containers.h:98
void remove_all(Container &container, typename Container::const_reference value) noexcept
Removes all occurrences of value.
Definition containers.h:143
#define AUI_ASSERTX(condition, what)
Asserts that the passed condition evaluates to true. Adds extra message string.
Definition Assert.h:74