16#include "pw_containers/internal/aa_tree.h"
17#include "pw_containers/internal/aa_tree_item.h"
62template <
typename Key,
typename T>
65 using GenericIterator = containers::internal::GenericAATree::iterator;
67 using Compare =
typename Tree::Compare;
68 using GetKey =
typename Tree::GetKey;
75 using Item =
typename Tree::Item;
76 using Pair =
typename Tree::Pair;
79 using mapped_type = std::remove_cv_t<T>;
80 using value_type =
Item;
81 using size_type = std::size_t;
82 using difference_type = std::ptrdiff_t;
83 using key_compare = Compare;
84 using reference = value_type&;
85 using const_reference =
const value_type&;
86 using pointer = value_type*;
87 using const_pointer =
const value_type*;
90 class iterator :
public containers::internal::AATreeIterator<T> {
96 constexpr explicit iterator(GenericIterator iter)
97 : containers::internal::AATreeIterator<T>(iter) {}
101 :
public containers::internal::AATreeIterator<std::add_const_t<T>> {
108 : containers::internal::AATreeIterator<std::add_const_t<T>>(iter) {}
111 using reverse_iterator = std::reverse_iterator<iterator>;
112 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
124 template <
typename Comparator>
127 [](const T& t) {
return t.key(); }) {}
135 template <
typename Comparator,
typename KeyRetriever>
138 std::forward<Comparator>(compare),
139 std::forward<KeyRetriever>(get_key)) {
147 template <
typename Iterator,
typename... Functors>
150 tree_.
insert(first, last);
155 template <
typename... Functors>
156 explicit IntrusiveMap(std::initializer_list<T*> items, Functors&&... functors)
158 items.begin(), items.end(), std::forward<Functors>(functors)...) {}
165 T&
at(
const key_type& key) {
166 auto iter = tree_.
find(key);
167 PW_DASSERT(iter != tree_.
end());
168 return static_cast<T&
>(*iter);
171 const T&
at(
const key_type& key)
const {
172 auto iter = tree_.
find(key);
173 PW_DASSERT(iter != tree_.
end());
174 return static_cast<const T&
>(*iter);
179 iterator begin() noexcept {
return iterator(tree_.
begin()); }
180 const_iterator begin() const noexcept {
181 return const_iterator(tree_.
begin());
183 const_iterator cbegin() const noexcept {
return begin(); }
185 iterator end() noexcept {
return iterator(tree_.
end()); }
186 const_iterator end() const noexcept {
return const_iterator(tree_.
end()); }
187 const_iterator cend() const noexcept {
return end(); }
189 reverse_iterator rbegin() noexcept {
return reverse_iterator(end()); }
190 const_reverse_iterator rbegin() const noexcept {
191 return const_reverse_iterator(end());
193 const_reverse_iterator crbegin() const noexcept {
return rbegin(); }
195 reverse_iterator rend() noexcept {
return reverse_iterator(begin()); }
196 const_reverse_iterator rend() const noexcept {
197 return const_reverse_iterator(begin());
199 const_reverse_iterator crend() const noexcept {
return rend(); }
204 [[nodiscard]]
bool empty() const noexcept {
return tree_.
empty(); }
228 std::pair<iterator, bool>
insert(T& item) {
229 auto result = tree_.
insert(item);
230 return std::make_pair(
iterator(result.first), result.second);
233 iterator
insert(iterator, T& item) {
235 return insert(item).first;
238 template <
class Iterator>
239 void insert(Iterator first, Iterator last) {
240 tree_.
insert(first, last);
243 void insert(std::initializer_list<T*> ilist) {
244 tree_.
insert(ilist.begin(), ilist.end());
253 iterator
erase(iterator pos) {
return iterator(tree_.
erase_one(*pos)); }
255 iterator
erase(iterator first, iterator last) {
259 size_t erase(
const key_type& key) {
return tree_.
erase_all(key); }
265 template <
typename MapType>
267 tree_.
merge(other.tree_);
273 size_t count(
const key_type& key)
const {
return tree_.
count(key); }
279 const_iterator
find(
const key_type& key)
const {
280 return const_iterator(tree_.
find(key));
290 std::pair<const_iterator, const_iterator>
equal_range(
291 const key_type& key)
const {
293 return std::make_pair(const_iterator(result.first),
294 const_iterator(result.second));
302 const_iterator
lower_bound(
const key_type& key)
const {
311 const_iterator
upper_bound(
const key_type& key)
const {
318 static constexpr void CheckItemType() {
319 using ItemBase = containers::internal::AATreeItem;
320 using IntrusiveItemType =
321 typename containers::internal::IntrusiveItem<ItemBase, T>::Type;
323 std::is_base_of<IntrusiveItemType, T>(),
324 "IntrusiveMap items must be derived from IntrusiveMap<Key, T>::Item, "
325 "where T is the item or one of its bases.");
329 template <
typename,
typename>
330 friend class IntrusiveMultiMap;
Definition: intrusive_map.h:101
Definition: intrusive_map.h:90
Definition: intrusive_map.h:63
void swap(IntrusiveMap< Key, T > &other)
Exchanges this map's items with the other map's items.
Definition: intrusive_map.h:262
T & at(const key_type &key)
Definition: intrusive_map.h:165
std::pair< iterator, iterator > equal_range(const key_type &key)
Definition: intrusive_map.h:286
IntrusiveMap(Iterator first, Iterator last, Functors &&... functors)
Definition: intrusive_map.h:148
iterator lower_bound(const key_type &key)
Definition: intrusive_map.h:299
size_t count(const key_type &key) const
Definition: intrusive_map.h:273
constexpr IntrusiveMap(Comparator &&compare, KeyRetriever &&get_key)
Definition: intrusive_map.h:136
iterator upper_bound(const key_type &key)
Definition: intrusive_map.h:308
typename Tree::Item Item
Definition: intrusive_map.h:75
std::pair< iterator, bool > insert(T &item)
Definition: intrusive_map.h:228
iterator erase(T &item)
Definition: intrusive_map.h:251
size_t size() const
Returns the number of items in the map.
Definition: intrusive_map.h:207
iterator find(const key_type &key)
Definition: intrusive_map.h:277
constexpr size_t max_size() const noexcept
Definition: intrusive_map.h:212
constexpr IntrusiveMap(Comparator &&compare)
Definition: intrusive_map.h:125
IntrusiveMap(std::initializer_list< T * > items, Functors &&... functors)
Definition: intrusive_map.h:156
bool empty() const noexcept
Returns whether the map has zero items or not.
Definition: intrusive_map.h:204
void clear()
Definition: intrusive_map.h:219
constexpr IntrusiveMap()
Constructs an empty map of items.
Definition: intrusive_map.h:115
void merge(MapType &other)
Splices items from the other map into this one.
Definition: intrusive_map.h:266
iterator erase_range(AATreeItem &first, AATreeItem &last)
iterator find(Key key)
Definition: aa_tree.h:351
std::pair< iterator, bool > insert(AATreeItem &item)
Definition: aa_tree.h:272
size_t erase_all(Key key)
Definition: aa_tree.h:325
iterator upper_bound(Key key)
Definition: aa_tree.h:390
iterator erase_one(AATreeItem &item)
std::pair< iterator, iterator > equal_range(Key key)
Definition: aa_tree.h:361
iterator lower_bound(Key key)
Definition: aa_tree.h:366
void merge(AATree< K, V > &other)
Definition: aa_tree.h:336
size_t count(Key key)
Definition: aa_tree.h:346
constexpr iterator begin() noexcept
Returns a pointer to the first item, if any.
Definition: aa_tree.h:60
constexpr size_t max_size() const noexcept
Definition: aa_tree.h:81
void swap(GenericAATree &other)
Exchanges this tree's items with the other tree's items.
size_t size() const
Returns the number of items in the tree.
constexpr bool empty() const
Returns whether the tree has zero items or not.
Definition: aa_tree.h:73
constexpr iterator end() noexcept
Returns a pointer to the last item, if any.
Definition: aa_tree.h:65
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27