22#include "pw_assert/assert.h"
23#include "pw_containers/internal/aa_tree_item.h"
24#include "pw_containers/internal/aa_tree_iterator.h"
25#include "pw_containers/internal/intrusive_item.h"
26#include "pw_function/function.h"
28namespace pw::containers::internal {
39 using iterator = AATreeIterator<>;
46 : unique_keys_(unique_keys) {}
55 [[nodiscard]]
constexpr bool unique_keys()
const {
return unique_keys_; }
63 constexpr iterator
begin() noexcept {
64 return empty() ? iterator(&root_) : iterator(&root_, root_->GetLeftmost());
68 constexpr iterator
end() noexcept {
69 return empty() ? iterator(&root_)
70 : ++(iterator(&root_, root_->GetRightmost()));
76 [[nodiscard]]
constexpr bool empty()
const {
return root_ ==
nullptr; }
85 return std::numeric_limits<ptrdiff_t>::max();
123 AATreeItem* root_ =
nullptr;
136 const bool unique_keys_;
152 using Compare =
Function<bool(Key, Key)>;
153 using GetKey =
Function<Key(
const AATreeItem&)>;
154 using GenericAATree::iterator;
164 compare_(std::move(compare)),
165 get_key_(std::move(get_key)) {}
169 void SetCompare(Compare&& compare) { compare_ = std::move(compare); }
170 void SetGetKey(GetKey&& get_key) { get_key_ = std::move(get_key); }
181 std::pair<iterator, bool>
insert(AATreeItem& item);
186 template <
class Iterator>
187 void insert(Iterator first, Iterator last);
231 AATreeItem* InsertImpl(AATreeItem& parent,
233 AATreeItem*& duplicate);
238 AATreeItem* GetLowerBoundImpl(AATreeItem* item, Key key);
243 AATreeItem* GetUpperBoundImpl(AATreeItem* item, Key key);
260template <
typename K,
typename V>
264 using Key =
typename Base::Key;
265 using Compare =
typename Base::Compare;
266 using GetKey =
Function<Key(
const V&)>;
272 class Item :
public AATreeItem {
274 constexpr explicit Item() =
default;
279 template <
typename,
typename,
bool>
280 friend struct IntrusiveItem;
287 constexpr explicit Pair(Key key) : key_(key) {}
288 constexpr Key key()
const {
return key_; }
290 constexpr bool operator<(
const Pair& rhs) {
return key_ < rhs.key(); }
300 constexpr AATree(
bool unique_keys, Compare&& compare, GetKey&& get_key)
303 std::move([this](const AATreeItem& item) -> Key {
304 return get_key_(static_cast<const V&>(item));
306 get_key_(std::move(get_key)) {}
316 CheckIntrusiveItemIsUncontained(!item.IsMapped());
320 return std::make_pair(iterator(&root_, &item),
true);
322 AATreeItem* duplicate =
nullptr;
323 SetRoot(InsertImpl(*root_, item, duplicate));
324 if (duplicate ==
nullptr) {
325 return std::make_pair(iterator(&root_, &item),
true);
328 return std::make_pair(iterator(&root_, duplicate),
false);
334 AATreeItem*& duplicate) {
335 if (compare_(get_key_(child), get_key_(parent))) {
336 AATreeItem* left = parent.left_.get();
337 left = left ==
nullptr ? &child : InsertImpl(*left, child, duplicate);
338 parent.SetLeft(left);
340 }
else if (compare_(get_key_(parent), get_key_(child)) || !unique_keys()) {
341 AATreeItem* right = parent.right_.get();
342 right = right ==
nullptr ? &child : InsertImpl(*right, child, duplicate);
343 parent.SetRight(right);
350 return parent.Skew()->Split();
354template <
class Iterator>
356 for (Iterator iter = first; iter != last; ++iter) {
357 if constexpr (std::is_pointer_v<
358 typename std::iterator_traits<Iterator>::value_type>) {
369 iterator iter = lower_bound(key);
370 while (iter != end() && !compare_(key, get_key_(*iter))) {
371 iter = erase_one(*iter);
379 auto iter = other.
begin();
380 while (!other.
empty()) {
381 AATreeItem& item = *iter;
389 return static_cast<size_t>(std::distance(lower_bound(key), upper_bound(key)));
394 iterator iter = lower_bound(key);
395 if (iter == end() || compare_(key, get_key_(*iter))) {
402std::pair<GenericAATree::iterator, GenericAATree::iterator>
404 return std::make_pair(lower_bound(key), upper_bound(key));
409 AATreeItem* item = GetLowerBoundImpl(root_, key);
410 return item ==
nullptr ? end() : iterator(&root_, item);
415 if (item ==
nullptr) {
419 if (compare_(get_key_(*item), key)) {
420 return GetLowerBoundImpl(item->right_.get(), key);
423 AATreeItem* left = item->left_.get();
424 if (left ==
nullptr) {
427 AATreeItem* next = GetLowerBoundImpl(left, key);
428 return next ==
nullptr ? item : next;
433 AATreeItem* item = GetUpperBoundImpl(root_, key);
434 return item ==
nullptr ? end() : iterator(&root_, item);
439 if (item ==
nullptr) {
443 if (!compare_(key, get_key_(*item))) {
444 return GetUpperBoundImpl(item->right_.get(), key);
447 AATreeItem* left = item->left_.get();
448 if (left ==
nullptr) {
451 AATreeItem* next = GetUpperBoundImpl(left, key);
452 return next ==
nullptr ? item : next;
Definition: aa_tree.h:272
An extension of Item that includes storage for a key.
Definition: aa_tree.h:285
Definition: aa_tree.h:261
Definition: aa_tree.h:149
iterator erase_one(AATreeItem &item)
iterator erase_range(AATreeItem &first, AATreeItem &last)
iterator lower_bound(Key key)
Definition: aa_tree.h:408
constexpr iterator begin() noexcept
Returns a pointer to the first item, if any.
Definition: aa_tree.h:63
constexpr size_t max_size() const noexcept
Definition: aa_tree.h:84
std::pair< iterator, iterator > equal_range(Key key)
Definition: aa_tree.h:403
iterator upper_bound(Key key)
Definition: aa_tree.h:432
void insert(Iterator first, Iterator last)
Definition: aa_tree.h:355
void merge(KeyedAATree< K > &other)
Definition: aa_tree.h:378
iterator find(Key key)
Definition: aa_tree.h:393
std::pair< iterator, bool > insert(AATreeItem &item)
Definition: aa_tree.h:314
iterator erase_one(AATreeItem &item)
iterator erase_range(iterator first, iterator last)
size_t erase_all(Key key)
Definition: aa_tree.h:367
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:49
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:76
constexpr GenericAATree(bool unique_keys)
Definition: aa_tree.h:45
constexpr KeyedAATree(bool unique_keys, Compare &&compare, GetKey &&get_key)
Definition: aa_tree.h:160
size_t count(Key key)
Definition: aa_tree.h:388
constexpr AATree(bool unique_keys, Compare &&compare, GetKey &&get_key)
Definition: aa_tree.h:300
constexpr iterator end() noexcept
Returns a pointer to the last item, if any.
Definition: aa_tree.h:68
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:73