16#include "pw_containers/internal/aa_tree.h"
17#include "pw_containers/internal/aa_tree_item.h"
67template <
typename Key,
typename T>
70 using GenericIterator = containers::internal::GenericAATree::iterator;
72 using Compare =
typename Tree::Compare;
73 using GetKey =
typename Tree::GetKey;
80 using Item =
typename Tree::Item;
81 using Pair =
typename Tree::Pair;
84 using mapped_type = std::remove_cv_t<T>;
85 using value_type =
Item;
86 using size_type = std::size_t;
87 using difference_type = std::ptrdiff_t;
88 using key_compare = Compare;
89 using reference = value_type&;
90 using const_reference =
const value_type&;
91 using pointer = value_type*;
92 using const_pointer =
const value_type*;
95 class iterator :
public containers::internal::AATreeIterator<T> {
101 constexpr explicit iterator(GenericIterator iter)
102 : containers::internal::AATreeIterator<T>(iter) {}
106 :
public containers::internal::AATreeIterator<std::add_const_t<T>> {
113 : containers::internal::AATreeIterator<std::add_const_t<T>>(iter) {}
116 using reverse_iterator = std::reverse_iterator<iterator>;
117 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
129 template <
typename Comparator>
131 :
IntrusiveMap(std::move(compare), [](const T& t) {
return t.key(); }) {}
139 template <
typename Comparator,
typename KeyRetriever>
142 std::forward<Comparator>(compare),
143 std::forward<KeyRetriever>(get_key)) {
151 template <
typename Iterator,
typename... Functors>
154 tree_.
insert(first, last);
159 template <
typename... Functors>
160 explicit IntrusiveMap(std::initializer_list<T*> items, Functors&&... functors)
162 items.begin(), items.end(), std::forward<Functors>(functors)...) {}
169 T&
at(
const key_type& key) {
170 auto iter = tree_.
find(key);
171 PW_DASSERT(iter != tree_.
end());
172 return static_cast<T&
>(*iter);
175 const T&
at(
const key_type& key)
const {
176 auto iter = tree_.
find(key);
177 PW_DASSERT(iter != tree_.
end());
178 return static_cast<const T&
>(*iter);
183 iterator begin() noexcept {
return iterator(tree_.
begin()); }
184 const_iterator begin() const noexcept {
185 return const_iterator(tree_.
begin());
187 const_iterator cbegin() const noexcept {
return begin(); }
189 iterator end() noexcept {
return iterator(tree_.
end()); }
190 const_iterator end() const noexcept {
return const_iterator(tree_.
end()); }
191 const_iterator cend() const noexcept {
return end(); }
193 reverse_iterator rbegin() noexcept {
return reverse_iterator(end()); }
194 const_reverse_iterator rbegin() const noexcept {
195 return const_reverse_iterator(end());
197 const_reverse_iterator crbegin() const noexcept {
return rbegin(); }
199 reverse_iterator rend() noexcept {
return reverse_iterator(begin()); }
200 const_reverse_iterator rend() const noexcept {
201 return const_reverse_iterator(begin());
203 const_reverse_iterator crend() const noexcept {
return rend(); }
208 [[nodiscard]]
bool empty() const noexcept {
return tree_.
empty(); }
232 std::pair<iterator, bool>
insert(T& item) {
233 auto result = tree_.
insert(item);
234 return std::make_pair(
iterator(result.first), result.second);
237 iterator
insert(iterator, T& item) {
239 return insert(item).first;
242 template <
class Iterator>
243 void insert(Iterator first, Iterator last) {
244 tree_.
insert(first, last);
247 void insert(std::initializer_list<T*> ilist) {
248 tree_.
insert(ilist.begin(), ilist.end());
257 iterator
erase(iterator pos) {
return iterator(tree_.
erase_one(*pos)); }
259 iterator
erase(iterator first, iterator last) {
263 size_t erase(
const key_type& key) {
return tree_.
erase_all(key); }
269 template <
typename MapType>
271 tree_.
merge(other.tree_);
277 size_t count(
const key_type& key)
const {
return tree_.
count(key); }
283 const_iterator
find(
const key_type& key)
const {
284 return const_iterator(tree_.
find(key));
294 std::pair<const_iterator, const_iterator>
equal_range(
295 const key_type& key)
const {
297 return std::make_pair(const_iterator(result.first),
298 const_iterator(result.second));
306 const_iterator
lower_bound(
const key_type& key)
const {
315 const_iterator
upper_bound(
const key_type& key)
const {
322 static constexpr void CheckItemType() {
323 using ItemBase = containers::internal::AATreeItem;
324 using IntrusiveItemType =
325 typename containers::internal::IntrusiveItem<ItemBase, T>::Type;
327 std::is_base_of<IntrusiveItemType, T>(),
328 "IntrusiveMap items must be derived from IntrusiveMap<Key, T>::Item, "
329 "where T is the item or one of its bases.");
333 template <
typename,
typename>
334 friend class IntrusiveMultiMap;
Definition: intrusive_map.h:106
Definition: intrusive_map.h:95
Definition: intrusive_map.h:68
iterator erase_range(AATreeItem &first, AATreeItem &last)
iterator erase_one(AATreeItem &item)
void swap(IntrusiveMap< Key, T > &other)
Exchanges this map's items with the other map's items.
Definition: intrusive_map.h:266
T & at(const key_type &key)
Definition: intrusive_map.h:169
std::pair< iterator, iterator > equal_range(const key_type &key)
Definition: intrusive_map.h:290
IntrusiveMap(Iterator first, Iterator last, Functors &&... functors)
Definition: intrusive_map.h:152
iterator lower_bound(const key_type &key)
Definition: intrusive_map.h:303
size_t count(const key_type &key) const
Definition: intrusive_map.h:277
constexpr IntrusiveMap(Comparator &&compare, KeyRetriever &&get_key)
Definition: intrusive_map.h:140
iterator upper_bound(const key_type &key)
Definition: intrusive_map.h:312
constexpr IntrusiveMap(Comparator compare)
Definition: intrusive_map.h:130
typename Tree::Item Item
Definition: intrusive_map.h:80
std::pair< iterator, bool > insert(T &item)
Definition: intrusive_map.h:232
iterator erase(T &item)
Definition: intrusive_map.h:255
size_t size() const
Returns the number of items in the map.
Definition: intrusive_map.h:211
iterator find(const key_type &key)
Definition: intrusive_map.h:281
constexpr size_t max_size() const noexcept
Definition: intrusive_map.h:216
IntrusiveMap(std::initializer_list< T * > items, Functors &&... functors)
Definition: intrusive_map.h:160
bool empty() const noexcept
Returns whether the map has zero items or not.
Definition: intrusive_map.h:208
void clear()
Definition: intrusive_map.h:223
constexpr IntrusiveMap()
Constructs an empty map of items.
Definition: intrusive_map.h:120
void merge(MapType &other)
Splices items from the other map into this one.
Definition: intrusive_map.h:270
iterator lower_bound(Key key)
Definition: aa_tree.h:400
constexpr iterator begin() noexcept
Returns a pointer to the first item, if any.
Definition: aa_tree.h:62
constexpr size_t max_size() const noexcept
Definition: aa_tree.h:83
std::pair< iterator, iterator > equal_range(Key key)
Definition: aa_tree.h:395
iterator upper_bound(Key key)
Definition: aa_tree.h:424
void merge(KeyedAATree< K > &other)
Definition: aa_tree.h:370
iterator find(Key key)
Definition: aa_tree.h:385
std::pair< iterator, bool > insert(AATreeItem &item)
Definition: aa_tree.h:306
size_t erase_all(Key key)
Definition: aa_tree.h:359
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:75
size_t count(Key key)
Definition: aa_tree.h:380
constexpr iterator end() noexcept
Returns a pointer to the last item, if any.
Definition: aa_tree.h:67
The Pigweed namespace.
Definition: alignment.h:27