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>
126 :
IntrusiveMap(std::move(compare), [](const T& t) {
return t.key(); }) {}
134 template <
typename Comparator,
typename KeyRetriever>
137 std::forward<Comparator>(compare),
138 std::forward<KeyRetriever>(get_key)) {
146 template <
typename Iterator,
typename... Functors>
149 tree_.
insert(first, last);
154 template <
typename... Functors>
155 explicit IntrusiveMap(std::initializer_list<T*> items, Functors&&... functors)
157 items.begin(), items.end(), std::forward<Functors>(functors)...) {}
164 T&
at(
const key_type& key) {
165 auto iter = tree_.
find(key);
166 PW_DASSERT(iter != tree_.
end());
167 return static_cast<T&
>(*iter);
170 const T&
at(
const key_type& key)
const {
171 auto iter = tree_.
find(key);
172 PW_DASSERT(iter != tree_.
end());
173 return static_cast<const T&
>(*iter);
178 iterator begin() noexcept {
return iterator(tree_.
begin()); }
179 const_iterator begin() const noexcept {
180 return const_iterator(tree_.
begin());
182 const_iterator cbegin() const noexcept {
return begin(); }
184 iterator end() noexcept {
return iterator(tree_.
end()); }
185 const_iterator end() const noexcept {
return const_iterator(tree_.
end()); }
186 const_iterator cend() const noexcept {
return end(); }
188 reverse_iterator rbegin() noexcept {
return reverse_iterator(end()); }
189 const_reverse_iterator rbegin() const noexcept {
190 return const_reverse_iterator(end());
192 const_reverse_iterator crbegin() const noexcept {
return rbegin(); }
194 reverse_iterator rend() noexcept {
return reverse_iterator(begin()); }
195 const_reverse_iterator rend() const noexcept {
196 return const_reverse_iterator(begin());
198 const_reverse_iterator crend() const noexcept {
return rend(); }
203 [[nodiscard]]
bool empty() const noexcept {
return tree_.
empty(); }
227 std::pair<iterator, bool>
insert(T& item) {
228 auto result = tree_.
insert(item);
229 return std::make_pair(
iterator(result.first), result.second);
232 iterator
insert(iterator, T& item) {
234 return insert(item).first;
237 template <
class Iterator>
238 void insert(Iterator first, Iterator last) {
239 tree_.
insert(first, last);
242 void insert(std::initializer_list<T*> ilist) {
243 tree_.
insert(ilist.begin(), ilist.end());
252 iterator
erase(iterator pos) {
return iterator(tree_.
erase_one(*pos)); }
254 iterator
erase(iterator first, iterator last) {
258 size_t erase(
const key_type& key) {
return tree_.
erase_all(key); }
264 template <
typename MapType>
266 tree_.
merge(other.tree_);
272 size_t count(
const key_type& key)
const {
return tree_.
count(key); }
278 const_iterator
find(
const key_type& key)
const {
279 return const_iterator(tree_.
find(key));
289 std::pair<const_iterator, const_iterator>
equal_range(
290 const key_type& key)
const {
292 return std::make_pair(const_iterator(result.first),
293 const_iterator(result.second));
301 const_iterator
lower_bound(
const key_type& key)
const {
310 const_iterator
upper_bound(
const key_type& key)
const {
317 static constexpr void CheckItemType() {
318 using ItemBase = containers::internal::AATreeItem;
319 using IntrusiveItemType =
320 typename containers::internal::IntrusiveItem<ItemBase, T>::Type;
322 std::is_base_of<IntrusiveItemType, T>(),
323 "IntrusiveMap items must be derived from IntrusiveMap<Key, T>::Item, "
324 "where T is the item or one of its bases.");
328 template <
typename,
typename>
329 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:261
T & at(const key_type &key)
Definition: intrusive_map.h:164
std::pair< iterator, iterator > equal_range(const key_type &key)
Definition: intrusive_map.h:285
IntrusiveMap(Iterator first, Iterator last, Functors &&... functors)
Definition: intrusive_map.h:147
iterator lower_bound(const key_type &key)
Definition: intrusive_map.h:298
size_t count(const key_type &key) const
Definition: intrusive_map.h:272
constexpr IntrusiveMap(Comparator &&compare, KeyRetriever &&get_key)
Definition: intrusive_map.h:135
iterator upper_bound(const key_type &key)
Definition: intrusive_map.h:307
constexpr IntrusiveMap(Comparator compare)
Definition: intrusive_map.h:125
typename Tree::Item Item
Definition: intrusive_map.h:75
std::pair< iterator, bool > insert(T &item)
Definition: intrusive_map.h:227
iterator erase(T &item)
Definition: intrusive_map.h:250
size_t size() const
Returns the number of items in the map.
Definition: intrusive_map.h:206
iterator find(const key_type &key)
Definition: intrusive_map.h:276
constexpr size_t max_size() const noexcept
Definition: intrusive_map.h:211
IntrusiveMap(std::initializer_list< T * > items, Functors &&... functors)
Definition: intrusive_map.h:155
bool empty() const noexcept
Returns whether the map has zero items or not.
Definition: intrusive_map.h:203
void clear()
Definition: intrusive_map.h:218
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:265
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
iterator erase_range(AATreeItem &first, AATreeItem &last)
iterator lower_bound(Key key)
Definition: aa_tree.h:398
std::pair< iterator, iterator > equal_range(Key key)
Definition: aa_tree.h:393
iterator upper_bound(Key key)
Definition: aa_tree.h:422
void merge(KeyedAATree< K > &other)
Definition: aa_tree.h:368
iterator find(Key key)
Definition: aa_tree.h:383
std::pair< iterator, bool > insert(AATreeItem &item)
Definition: aa_tree.h:304
iterator erase_one(AATreeItem &item)
size_t erase_all(Key key)
Definition: aa_tree.h:357
size_t count(Key key)
Definition: aa_tree.h:378
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27