xref: /aosp_15_r20/external/pigweed/pw_containers/aa_tree.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_containers/internal/aa_tree.h"
16 
17 namespace pw::containers::internal {
18 
SetRoot(AATreeItem * item)19 void GenericAATree::SetRoot(AATreeItem* item) {
20   if (item != nullptr) {
21     item->parent_.set(nullptr);
22   }
23   root_ = item;
24 }
25 
size() const26 size_t GenericAATree::size() const {
27   return empty() ? 0 : root_->GetTreeSize();
28 }
29 
clear()30 void GenericAATree::clear() {
31   if (root_ != nullptr) {
32     root_->Clear();
33     SetRoot(nullptr);
34   }
35 }
36 
erase_one(AATreeItem & item)37 GenericAATree::iterator GenericAATree::erase_one(AATreeItem& item) {
38   if (item.GetRoot() != root_) {
39     return iterator(&root_);
40   }
41   AATreeItem* next = item.GetSuccessor();
42   SetRoot(item.Unmap());
43   return iterator(&root_, next);
44 }
45 
erase_range(AATreeItem & first,AATreeItem & last)46 GenericAATree::iterator GenericAATree::erase_range(AATreeItem& first,
47                                                    AATreeItem& last) {
48   iterator iter(&root_, &first);
49   while (&(*iter) != &last) {
50     iter = erase_one(*iter);
51   }
52   return iter;
53 }
54 
swap(GenericAATree & other)55 void GenericAATree::swap(GenericAATree& other) {
56   std::swap(root_, other.root_);
57 }
58 
59 }  // namespace pw::containers::internal
60