CppCommon  1.0.4.1
C++ Common Library
bintree_aa.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_CONTAINERS_BINTREE_AA_H
10 #define CPPCOMMON_CONTAINERS_BINTREE_AA_H
11 
12 #include "bintree.h"
13 
14 namespace CppCommon {
15 
17 
58 template <typename T, typename TCompare = std::less<T>>
59 class BinTreeAA
60 {
61 public:
62  // Standard container type definitions
63  typedef T value_type;
64  typedef TCompare value_compare;
66  typedef const value_type& const_reference;
67  typedef value_type* pointer;
68  typedef const value_type* const_pointer;
69  typedef ptrdiff_t difference_type;
70  typedef size_t size_type;
75 
77  struct Node
78  {
79  T* parent;
80  T* left;
81  T* right;
82  size_t level;
83 
84  Node() : parent(nullptr), left(nullptr), right(nullptr), level(0) {}
85  };
86 
87  explicit BinTreeAA(const TCompare& compare = TCompare()) noexcept
88  : _compare(compare),
89  _size(0),
90  _root(nullptr)
91  {}
92  template <class InputIterator>
93  BinTreeAA(InputIterator first, InputIterator last, const TCompare& compare = TCompare()) noexcept;
94  BinTreeAA(const BinTreeAA&) noexcept = default;
95  BinTreeAA(BinTreeAA&&) noexcept = default;
96  ~BinTreeAA() noexcept = default;
97 
98  BinTreeAA& operator=(const BinTreeAA&) noexcept = default;
99  BinTreeAA& operator=(BinTreeAA&&) noexcept = default;
100 
102  explicit operator bool() const noexcept { return !empty(); }
103 
105  bool empty() const noexcept { return _root == nullptr; }
106 
108  size_t size() const noexcept { return _size; }
109 
111  T* root() noexcept { return _root; }
112  const T* root() const noexcept { return _root; }
114  T* lowest() noexcept;
115  const T* lowest() const noexcept;
117  T* highest() noexcept;
118  const T* highest() const noexcept;
119 
121  bool compare(const T& item1, const T& item2) const noexcept { return _compare(item1, item2); }
122 
124  iterator begin() noexcept;
125  const_iterator begin() const noexcept;
126  const_iterator cbegin() const noexcept;
128  iterator end() noexcept;
129  const_iterator end() const noexcept;
130  const_iterator cend() const noexcept;
131 
133  reverse_iterator rbegin() noexcept;
134  const_reverse_iterator rbegin() const noexcept;
135  const_reverse_iterator crbegin() const noexcept;
137  reverse_iterator rend() noexcept;
138  const_reverse_iterator rend() const noexcept;
139  const_reverse_iterator crend() const noexcept;
140 
142  iterator find(const T& item) noexcept;
143  const_iterator find(const T& item) const noexcept;
144 
146  iterator lower_bound(const T& item) noexcept;
147  const_iterator lower_bound(const T& item) const noexcept;
149  iterator upper_bound(const T& item) noexcept;
150  const_iterator upper_bound(const T& item) const noexcept;
151 
153 
157  std::pair<iterator, bool> insert(T& item) noexcept;
159 
164  std::pair<iterator, bool> insert(const const_iterator& position, T& item) noexcept;
165 
167 
171  T* erase(const T& item) noexcept;
173 
177  iterator erase(const iterator& it) noexcept;
178 
180  void clear() noexcept;
181 
183  void swap(BinTreeAA& bintree) noexcept;
184  template <typename U, typename UCompare>
185  friend void swap(BinTreeAA<U, UCompare>& bintree1, BinTreeAA<U, UCompare>& bintree2) noexcept;
186 
187 private:
188  TCompare _compare; // Binary tree compare
189  size_t _size; // Binary tree size
190  T* _root; // Binary tree root node
191 
192  const T* InternalLowest() const noexcept;
193  const T* InternalHighest() const noexcept;
194  const T* InternalFind(const T& item) const noexcept;
195  const T* InternalLowerBound(const T& item) const noexcept;
196  const T* InternalUpperBound(const T& item) const noexcept;
197 
199 
205  void Skew(T* node);
207 
214  bool Split(T* node);
215 };
216 
217 } // namespace CppCommon
218 
219 #include "bintree_aa.inl"
220 
221 #endif // CPPCOMMON_CONTAINERS_BINTREE_AA_H
Intrusive non balanced binary tree container definition.
Intrusive balanced A.Andersson binary tree container.
Definition: bintree_aa.h:60
void clear() noexcept
Clear the binary tree.
Definition: bintree_aa.inl:494
value_type & reference
Definition: bintree_aa.h:65
const_reverse_iterator crend() const noexcept
Definition: bintree_aa.inl:131
void swap(BinTreeAA &bintree) noexcept
Swap two instances.
Definition: bintree_aa.inl:501
bool empty() const noexcept
Is the binary tree empty?
Definition: bintree_aa.h:105
iterator begin() noexcept
Get the begin binary tree iterator.
Definition: bintree_aa.inl:65
BinTreeIterator< BinTreeAA< T, TCompare >, T > iterator
Definition: bintree_aa.h:71
iterator upper_bound(const T &item) noexcept
Find the iterator which points to the first item that greater than the given item in the binary tree ...
Definition: bintree_aa.inl:223
const value_type * const_pointer
Definition: bintree_aa.h:68
size_t size() const noexcept
Get the binary tree size.
Definition: bintree_aa.h:108
bool compare(const T &item1, const T &item2) const noexcept
Compare two items: if the first item is less than the second one?
Definition: bintree_aa.h:121
value_type * pointer
Definition: bintree_aa.h:67
BinTreeConstReverseIterator< BinTreeAA< T, TCompare >, T > const_reverse_iterator
Definition: bintree_aa.h:74
BinTreeReverseIterator< BinTreeAA< T, TCompare >, T > reverse_iterator
Definition: bintree_aa.h:73
BinTreeAA(const TCompare &compare=TCompare()) noexcept
Definition: bintree_aa.h:87
BinTreeConstIterator< BinTreeAA< T, TCompare >, T > const_iterator
Definition: bintree_aa.h:72
ptrdiff_t difference_type
Definition: bintree_aa.h:69
reverse_iterator rend() noexcept
Get the reverse end binary tree iterator.
Definition: bintree_aa.inl:119
const_reverse_iterator crbegin() const noexcept
Definition: bintree_aa.inl:113
const T * root() const noexcept
Definition: bintree_aa.h:112
iterator end() noexcept
Get the end binary tree iterator.
Definition: bintree_aa.inl:83
const_iterator cbegin() const noexcept
Definition: bintree_aa.inl:77
T * lowest() noexcept
Get the lowest binary tree item.
Definition: bintree_aa.inl:21
const_iterator cend() const noexcept
Definition: bintree_aa.inl:95
T * highest() noexcept
Get the highest binary tree item.
Definition: bintree_aa.inl:43
const value_type & const_reference
Definition: bintree_aa.h:66
iterator find(const T &item) noexcept
Find the iterator which points to the first equal item in the binary tree or return end iterator.
Definition: bintree_aa.inl:137
iterator lower_bound(const T &item) noexcept
Find the iterator which points to the first item that not less than the given item in the binary tree...
Definition: bintree_aa.inl:179
T * erase(const T &item) noexcept
Erase the given item from the binary tree.
Definition: bintree_aa.inl:337
T * root() noexcept
Get the root binary tree item.
Definition: bintree_aa.h:111
std::pair< iterator, bool > insert(T &item) noexcept
Insert a new item into the binary tree.
Definition: bintree_aa.inl:260
TCompare value_compare
Definition: bintree_aa.h:64
reverse_iterator rbegin() noexcept
Get the reverse begin binary tree iterator.
Definition: bintree_aa.inl:101
Intrusive binary tree constant iterator.
Definition: bintree.h:335
Intrusive binary tree constant reverse iterator.
Definition: bintree.h:448
Intrusive binary tree iterator.
Definition: bintree.h:279
Intrusive binary tree reverse iterator.
Definition: bintree.h:392
C++ Common project definitions.
Definition: token_bucket.h:15
A.Andersson binary tree node.
Definition: bintree_aa.h:78
T * right
Pointer to the right child binary tree node.
Definition: bintree_aa.h:81
T * left
Pointer to the left child binary tree node.
Definition: bintree_aa.h:80
T * parent
Pointer to the parent binary tree node.
Definition: bintree_aa.h:79
size_t level
Balance level.
Definition: bintree_aa.h:82