21#include "pw_assert/assert.h"
22#include "pw_containers/internal/aa_tree_item.h"
23#include "pw_containers/internal/aa_tree_iterator.h"
24#include "pw_containers/internal/intrusive_item.h"
25#include "pw_function/function.h"
27namespace pw::containers::internal {
38 using iterator = AATreeIterator<>;
45 : unique_keys_(unique_keys) {}
54 [[nodiscard]]
constexpr bool unique_keys()
const {
return unique_keys_; }
62 constexpr iterator
begin() noexcept {
63 return empty() ? iterator(&root_) : iterator(&root_, root_->GetLeftmost());
67 constexpr iterator
end() noexcept {
68 return empty() ? iterator(&root_)
69 : ++(iterator(&root_, root_->GetRightmost()));
75 [[nodiscard]]
constexpr bool empty()
const {
return root_ ==
nullptr; }
84 return std::numeric_limits<ptrdiff_t>::max();
122 AATreeItem* root_ =
nullptr;
135 const bool unique_keys_;
151 using Compare =
Function<bool(Key, Key)>;
152 using GetKey =
Function<Key(
const AATreeItem&)>;
153 using GenericAATree::iterator;
163 compare_(std::move(compare)),
164 get_key_(std::move(get_key)) {}
168 void SetCompare(Compare&& compare) { compare_ = std::move(compare); }
169 void SetGetKey(GetKey&& get_key) { get_key_ = std::move(get_key); }
180 std::pair<iterator, bool>
insert(AATreeItem& item);
185 template <
class Iterator>
186 void insert(Iterator first, Iterator last);
230 AATreeItem* InsertImpl(AATreeItem& parent,
232 AATreeItem*& duplicate);
237 AATreeItem* GetLowerBoundImpl(AATreeItem* item, Key key);
242 AATreeItem* GetUpperBoundImpl(AATreeItem* item, Key key);
259template <
typename K,
typename V>
263 using Key =
typename Base::Key;
264 using Compare =
typename Base::Compare;
265 using GetKey =
Function<Key(
const V&)>;
271 class Item :
public AATreeItem {
273 constexpr explicit Item() =
default;
278 template <
typename,
typename,
bool>
279 friend struct IntrusiveItem;
286 constexpr explicit Pair(Key key) : key_(key) {}
287 constexpr Key key()
const {
return key_; }
289 constexpr bool operator<(
const Pair& rhs) {
return key_ < rhs.key(); }
299 constexpr AATree(
bool unique_keys, Compare&& compare, GetKey&& get_key)
302 std::move([this](const AATreeItem& item) -> Key {
303 return get_key_(static_cast<const V&>(item));
305 get_key_(std::move(get_key)) {}
315 CheckIntrusiveItemIsUncontained(!item.IsMapped());
319 return std::make_pair(iterator(&root_, &item),
true);
321 AATreeItem* duplicate =
nullptr;
322 SetRoot(InsertImpl(*root_, item, duplicate));
323 if (duplicate ==
nullptr) {
324 return std::make_pair(iterator(&root_, &item),
true);
327 return std::make_pair(iterator(&root_, duplicate),
false);
333 AATreeItem*& duplicate) {
334 if (compare_(get_key_(child), get_key_(parent))) {
335 AATreeItem* left = parent.left_.get();
336 left = left ==
nullptr ? &child : InsertImpl(*left, child, duplicate);
337 parent.SetLeft(left);
339 }
else if (compare_(get_key_(parent), get_key_(child)) || !unique_keys()) {
340 AATreeItem* right = parent.right_.get();
341 right = right ==
nullptr ? &child : InsertImpl(*right, child, duplicate);
342 parent.SetRight(right);
349 return parent.Skew()->Split();
353template <
class Iterator>
355 for (Iterator iter = first; iter != last; ++iter) {
356 if constexpr (std::is_pointer_v<
357 typename std::iterator_traits<Iterator>::value_type>) {
368 iterator iter = lower_bound(key);
369 while (iter != end() && !compare_(key, get_key_(*iter))) {
370 iter = erase_one(*iter);
378 auto iter = other.
begin();
379 while (!other.
empty()) {
380 AATreeItem& item = *iter;
388 return std::distance(lower_bound(key), upper_bound(key));
393 iterator iter = lower_bound(key);
394 if (iter == end() || compare_(key, get_key_(*iter))) {
401std::pair<GenericAATree::iterator, GenericAATree::iterator>
403 return std::make_pair(lower_bound(key), upper_bound(key));
408 AATreeItem* item = GetLowerBoundImpl(root_, key);
409 return item ==
nullptr ? end() : iterator(&root_, item);
414 if (item ==
nullptr) {
418 if (compare_(get_key_(*item), key)) {
419 return GetLowerBoundImpl(item->right_.get(), key);
422 AATreeItem* left = item->left_.get();
423 if (left ==
nullptr) {
426 AATreeItem* next = GetLowerBoundImpl(left, key);
427 return next ==
nullptr ? item : next;
432 AATreeItem* item = GetUpperBoundImpl(root_, key);
433 return item ==
nullptr ? end() : iterator(&root_, item);
438 if (item ==
nullptr) {
442 if (!compare_(key, get_key_(*item))) {
443 return GetUpperBoundImpl(item->right_.get(), key);
446 AATreeItem* left = item->left_.get();
447 if (left ==
nullptr) {
450 AATreeItem* next = GetUpperBoundImpl(left, key);
451 return next ==
nullptr ? item : next;
Definition: aa_tree.h:271
An extension of Item that includes storage for a key.
Definition: aa_tree.h:284
Definition: aa_tree.h:260
Definition: aa_tree.h:148
iterator erase_one(AATreeItem &item)
iterator erase_range(AATreeItem &first, AATreeItem &last)
iterator lower_bound(Key key)
Definition: aa_tree.h:407
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:402
iterator upper_bound(Key key)
Definition: aa_tree.h:431
void insert(Iterator first, Iterator last)
Definition: aa_tree.h:354
void merge(KeyedAATree< K > &other)
Definition: aa_tree.h:377
iterator find(Key key)
Definition: aa_tree.h:392
std::pair< iterator, bool > insert(AATreeItem &item)
Definition: aa_tree.h:313
iterator erase_one(AATreeItem &item)
iterator erase_range(iterator first, iterator last)
size_t erase_all(Key key)
Definition: aa_tree.h:366
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.
~GenericAATree()
Destructor.
Definition: aa_tree.h:48
void SetRoot(AATreeItem *item)
Sets the tree's root item.
constexpr bool empty() const
Returns whether the tree has zero items or not.
Definition: aa_tree.h:75
constexpr GenericAATree(bool unique_keys)
Definition: aa_tree.h:44
constexpr KeyedAATree(bool unique_keys, Compare &&compare, GetKey &&get_key)
Definition: aa_tree.h:159
size_t count(Key key)
Definition: aa_tree.h:387
constexpr AATree(bool unique_keys, Compare &&compare, GetKey &&get_key)
Definition: aa_tree.h:299
constexpr iterator end() noexcept
Returns a pointer to the last item, if any.
Definition: aa_tree.h:67
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:73