14#include <unordered_map>
16#include "AException.h"
17#include <AUI/Common/AVector.h>
18#include <AUI/Traits/containers.h>
19#include <AUI/Reflect/AClass.h>
20#include "AContainerPrototypes.h"
26template <
class KeyType,
class ValueType,
class Parent>
30 using iterator =
typename Parent::iterator;
31 using const_iterator =
typename Parent::const_iterator;
35 ValueType& operator[](KeyType&& k)
37 return Parent::operator[](std::move(k));
40 ValueType& operator[](
const KeyType& k)
42 return Parent::operator[](k);
45 const ValueType& operator[](KeyType&& k)
const
47 return Parent::at(std::move(k));
50 const ValueType& operator[](
const KeyType& k)
const
71 mIterator(c.mIterator),
77 operator bool()
const noexcept {
81 iterator operator->()
const noexcept
85 iterator operator*()
const noexcept
93 const_iterator mIterator;
104 mIterator(c.mIterator),
110 operator bool()
const noexcept {
114 const_iterator operator->()
const noexcept
118 const_iterator operator*()
const noexcept
124 ValueType& at(
const KeyType& key) {
125 auto it = Parent::find(key);
126 if (it == Parent::end())
130 const ValueType& at(
const KeyType& key)
const {
131 auto it = Parent::find(key);
132 if (it == Parent::end())
138 const_contains_iterator contains(
const KeyType& key)
const noexcept
140 auto it = Parent::find(key);
141 return const_contains_iterator(it, it != Parent::end());
145 contains_iterator contains(
const KeyType& key)
noexcept
147 auto it = Parent::find(key);
148 return contains_iterator(it, it != Parent::end());
153 auto it = Parent::find(key);
154 if (it == Parent::end()) {
162 r.reserve(Parent::size());
163 for (
auto& p : *
this) {
170 r.reserve(Parent::size());
171 for (
auto& p : *
this) {
184 template<
typename Factory>
186 static_assert(std::is_constructible_v<ValueType>,
"ValueType is expected to be default-constructible");
187 auto[it, isElementCreated] = Parent::insert(
typename Parent::value_type(keyType, ValueType{}));
188 static_assert(std::is_same_v<
decltype(it),
typename Parent::iterator>,
"govno");
189 if (isElementCreated) {
190 it->second = factory();
195 template<
typename BinaryOperation>
198 result.reserve(Parent::size());
199 std::transform(Parent::begin(), Parent::end(), std::back_inserter(result), [transformer = std::forward<BinaryOperation>(transformer)](
const typename Parent::value_type& p){
200 return transformer(p.first, p.second);
206 return toVector([](
const KeyType& k,
const ValueType& v) {
207 return std::make_tuple(k, v);
216template <
class KeyType,
class ValueType,
class Predicate,
class Allocator>
217class AMap:
public ABaseMap<KeyType, ValueType, std::map<KeyType, ValueType, Predicate, Allocator>>
221 using parent::parent;
228template <
class KeyType,
class ValueType,
class Hasher,
class Comparer,
class Allocator>
229class AUnorderedMap:
public ABaseMap<KeyType, ValueType, std::unordered_map<KeyType, ValueType, Hasher, Comparer, Allocator>>
233 using parent::parent;
236template<
typename Iterator,
typename UnaryOperation>
239 UnaryOperation&& transformer) {
240 AMap<
decltype(transformer(*begin).first),
241 decltype(transformer(*begin).second)> result;
243 for (
auto it = begin; it != end; ++it) {
244 auto[key, value] = transformer(*it);
245 result[std::move(key)] = std::move(value);
250template<
typename Iterator,
typename UnaryOperation>
253 UnaryOperation&& transformer) {
255 decltype(transformer(*begin).second)> result;
257 for (
auto it = begin; it != end; ++it) {
258 auto[key, value] = transformer(*it);
259 result[std::move(key)] = std::move(value);
Base class for maps with AUI extensions.
Definition: AMap.h:28
ValueType & getOrInsert(const KeyType &keyType, Factory &&factory) noexcept(noexcept(factory()))
Definition: AMap.h:185
Abstract AUI exception.
Definition: AException.h:29
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::unordered_map with AUI extensions.
Definition: AMap.h:230
A std::vector with AUI extensions.
Definition: AVector.h:38
StoredType & first() noexcept
Definition: AVector.h:228
auto to_unordered_map(Iterator begin, Iterator end, UnaryOperation &&transformer)
Transforms sequence to unordered_map.
Definition: AMap.h:251
auto to_map(Iterator begin, Iterator end, UnaryOperation &&transformer)
Transforms sequence to map.
Definition: AMap.h:237