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 {
36 using iterator = AATreeIterator<>;
43 : unique_keys_(unique_keys) {}
52 [[nodiscard]]
constexpr bool unique_keys()
const {
return unique_keys_; }
60 constexpr iterator
begin() noexcept {
61 return empty() ? iterator(&root_) : iterator(&root_, root_->GetLeftmost());
65 constexpr iterator
end() noexcept {
66 return empty() ? iterator(&root_)
67 : ++(iterator(&root_, root_->GetRightmost()));
73 [[nodiscard]]
constexpr bool empty()
const {
return root_ ==
nullptr; }
82 return std::numeric_limits<ptrdiff_t>::max();
113 AATreeItem* root_ =
nullptr;
126 const bool unique_keys_;
142 using Compare =
Function<bool(Key, Key)>;
143 using GetKey =
Function<Key(
const AATreeItem&)>;
144 using GenericAATree::iterator;
154 compare_(std::move(compare)),
155 get_key_(std::move(get_key)) {}
159 void SetCompare(Compare&& compare) { compare_ = std::move(compare); }
160 void SetGetKey(GetKey&& get_key) { get_key_ = std::move(get_key); }
171 std::pair<iterator, bool>
insert(AATreeItem& item);
176 template <
class Iterator>
177 void insert(Iterator first, Iterator last);
221 AATreeItem* InsertImpl(AATreeItem& parent,
223 AATreeItem*& duplicate);
228 AATreeItem* GetLowerBoundImpl(AATreeItem* item, Key key);
233 AATreeItem* GetUpperBoundImpl(AATreeItem* item, Key key);
250template <
typename K,
typename V>
254 using Key =
typename Base::Key;
255 using Compare =
typename Base::Compare;
256 using GetKey =
Function<Key(
const V&)>;
262 class Item :
public AATreeItem {
264 constexpr explicit Item() =
default;
269 template <
typename,
typename,
bool>
270 friend struct IntrusiveItem;
277 constexpr explicit Pair(Key key) : key_(key) {}
278 constexpr Key key()
const {
return key_; }
280 constexpr bool operator<(
const Pair& rhs) {
return key_ < rhs.key(); }
290 constexpr AATree(
bool unique_keys, Compare&& compare, GetKey&& get_key)
293 std::move([this](const AATreeItem& item) -> Key {
294 return get_key_(static_cast<const V&>(item));
296 get_key_(std::move(get_key)) {}
306 CheckIntrusiveItemIsUncontained(!item.IsMapped());
310 return std::make_pair(iterator(&root_, &item),
true);
312 AATreeItem* duplicate =
nullptr;
313 SetRoot(InsertImpl(*root_, item, duplicate));
314 if (duplicate ==
nullptr) {
315 return std::make_pair(iterator(&root_, &item),
true);
318 return std::make_pair(iterator(&root_, duplicate),
false);
324 AATreeItem*& duplicate) {
325 if (compare_(get_key_(child), get_key_(parent))) {
326 AATreeItem* left = parent.left_.get();
327 left = left ==
nullptr ? &child : InsertImpl(*left, child, duplicate);
328 parent.SetLeft(left);
330 }
else if (compare_(get_key_(parent), get_key_(child)) || !unique_keys()) {
331 AATreeItem* right = parent.right_.get();
332 right = right ==
nullptr ? &child : InsertImpl(*right, child, duplicate);
333 parent.SetRight(right);
340 return parent.Skew()->Split();
344template <
class Iterator>
346 for (Iterator iter = first; iter != last; ++iter) {
347 if constexpr (std::is_pointer_v<
348 typename std::iterator_traits<Iterator>::value_type>) {
359 iterator iter = lower_bound(key);
360 while (iter != end() && !compare_(key, get_key_(*iter))) {
361 iter = erase_one(*iter);
369 auto iter = other.
begin();
370 while (!other.
empty()) {
371 AATreeItem& item = *iter;
379 return std::distance(lower_bound(key), upper_bound(key));
384 iterator iter = lower_bound(key);
385 if (iter == end() || compare_(key, get_key_(*iter))) {
392std::pair<GenericAATree::iterator, GenericAATree::iterator>
394 return std::make_pair(lower_bound(key), upper_bound(key));
399 AATreeItem* item = GetLowerBoundImpl(root_, key);
400 return item ==
nullptr ? end() : iterator(&root_, item);
405 if (item ==
nullptr) {
409 if (compare_(get_key_(*item), key)) {
410 return GetLowerBoundImpl(item->right_.get(), key);
413 AATreeItem* left = item->left_.get();
414 if (left ==
nullptr) {
417 AATreeItem* next = GetLowerBoundImpl(left, key);
418 return next ==
nullptr ? item : next;
423 AATreeItem* item = GetUpperBoundImpl(root_, key);
424 return item ==
nullptr ? end() : iterator(&root_, item);
429 if (item ==
nullptr) {
433 if (!compare_(key, get_key_(*item))) {
434 return GetUpperBoundImpl(item->right_.get(), key);
437 AATreeItem* left = item->left_.get();
438 if (left ==
nullptr) {
441 AATreeItem* next = GetUpperBoundImpl(left, key);
442 return next ==
nullptr ? item : next;
Definition: aa_tree.h:262
An extension of Item that includes storage for a key.
Definition: aa_tree.h:275
Definition: aa_tree.h:251
constexpr AATree(bool unique_keys, Compare &&compare, GetKey &&get_key)
Definition: aa_tree.h:290
iterator erase_range(AATreeItem &first, AATreeItem &last)
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
iterator erase_one(AATreeItem &item)
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:46
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:73
constexpr GenericAATree(bool unique_keys)
Definition: aa_tree.h:42
constexpr iterator end() noexcept
Returns a pointer to the last item, if any.
Definition: aa_tree.h:65
Definition: aa_tree.h:139
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 insert(Iterator first, Iterator last)
Definition: aa_tree.h:345
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
constexpr KeyedAATree(bool unique_keys, Compare &&compare, GetKey &&get_key)
Definition: aa_tree.h:150
size_t count(Key key)
Definition: aa_tree.h:378
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:74