CppCommon  1.0.4.1
C++ Common Library
bintree.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_CONTAINERS_BINTREE_H
10 #define CPPCOMMON_CONTAINERS_BINTREE_H
11 
12 #include <cassert>
13 #include <cstddef>
14 #include <functional>
15 #include <iterator>
16 #include <utility>
17 
18 namespace CppCommon {
19 
20 template <class TContainer, typename T>
21 class BinTreeIterator;
22 template <class TContainer, typename T>
23 class BinTreeConstIterator;
24 template <class TContainer, typename T>
25 class BinTreeReverseIterator;
26 template <class TContainer, typename T>
27 class BinTreeConstReverseIterator;
28 
30 
137 template <typename T, typename TCompare = std::less<T>>
138 class BinTree
139 {
140 public:
141  // Standard container type definitions
142  typedef T value_type;
143  typedef TCompare value_compare;
145  typedef const value_type& const_reference;
146  typedef value_type* pointer;
147  typedef const value_type* const_pointer;
148  typedef ptrdiff_t difference_type;
149  typedef size_t size_type;
154 
156  struct Node
157  {
158  T* parent;
159  T* left;
160  T* right;
161 
162  Node() : parent(nullptr), left(nullptr), right(nullptr) {}
163  };
164 
165  explicit BinTree(const TCompare& compare = TCompare()) noexcept : _compare(compare), _size(0), _root(nullptr) {}
166  template <class InputIterator>
167  BinTree(InputIterator first, InputIterator last, const TCompare& compare = TCompare()) noexcept;
168  BinTree(const BinTree&) noexcept = default;
169  BinTree(BinTree&&) noexcept = default;
170  ~BinTree() noexcept = default;
171 
172  BinTree& operator=(const BinTree&) noexcept = default;
173  BinTree& operator=(BinTree&&) noexcept = default;
174 
176  explicit operator bool() const noexcept { return !empty(); }
177 
179  bool empty() const noexcept { return _root == nullptr; }
180 
182  size_t size() const noexcept { return _size; }
183 
185  T* root() noexcept { return _root; }
186  const T* root() const noexcept { return _root; }
188  T* lowest() noexcept;
189  const T* lowest() const noexcept;
191  T* highest() noexcept;
192  const T* highest() const noexcept;
193 
195  bool compare(const T& item1, const T& item2) const noexcept { return _compare(item1, item2); }
196 
198  iterator begin() noexcept;
199  const_iterator begin() const noexcept;
200  const_iterator cbegin() const noexcept;
202  iterator end() noexcept;
203  const_iterator end() const noexcept;
204  const_iterator cend() const noexcept;
205 
207  reverse_iterator rbegin() noexcept;
208  const_reverse_iterator rbegin() const noexcept;
209  const_reverse_iterator crbegin() const noexcept;
211  reverse_iterator rend() noexcept;
212  const_reverse_iterator rend() const noexcept;
213  const_reverse_iterator crend() const noexcept;
214 
216  iterator find(const T& item) noexcept;
217  const_iterator find(const T& item) const noexcept;
218 
220  iterator lower_bound(const T& item) noexcept;
221  const_iterator lower_bound(const T& item) const noexcept;
223  iterator upper_bound(const T& item) noexcept;
224  const_iterator upper_bound(const T& item) const noexcept;
225 
227 
231  std::pair<iterator, bool> insert(T& item) noexcept;
233 
238  std::pair<iterator, bool> insert(const const_iterator& position, T& item) noexcept;
239 
241 
245  T* erase(const T& item) noexcept;
247 
251  iterator erase(const iterator& it) noexcept;
252 
254  void clear() noexcept;
255 
257  void swap(BinTree& bintree) noexcept;
258  template <typename U, typename UCompare>
259  friend void swap(BinTree<U, UCompare>& bintree1, BinTree<U, UCompare>& bintree2) noexcept;
260 
261 private:
262  TCompare _compare; // Binary tree node comparator
263  size_t _size; // Binary tree size
264  T* _root; // Binary tree root node
265 
266  const T* InternalLowest() const noexcept;
267  const T* InternalHighest() const noexcept;
268  const T* InternalFind(const T& item) const noexcept;
269  const T* InternalLowerBound(const T& item) const noexcept;
270  const T* InternalUpperBound(const T& item) const noexcept;
271 };
272 
274 
277 template <class TContainer, typename T>
279 {
281 
282 public:
283  // Standard iterator type definitions
284  typedef T value_type;
286  typedef const value_type& const_reference;
287  typedef value_type* pointer;
288  typedef const value_type* const_pointer;
289  typedef ptrdiff_t difference_type;
290  typedef size_t size_type;
291  typedef std::bidirectional_iterator_tag iterator_category;
292 
293  BinTreeIterator() noexcept : _container(nullptr), _node(nullptr) {}
294  explicit BinTreeIterator(TContainer* container, T* node) noexcept : _container(container), _node(node) {}
295  BinTreeIterator(const BinTreeIterator& it) noexcept = default;
296  BinTreeIterator(BinTreeIterator&& it) noexcept = default;
297  ~BinTreeIterator() noexcept = default;
298 
299  BinTreeIterator& operator=(const BinTreeIterator& it) noexcept = default;
300  BinTreeIterator& operator=(BinTreeIterator&& it) noexcept = default;
301 
302  friend bool operator==(const BinTreeIterator& it1, const BinTreeIterator& it2) noexcept
303  { return (it1._container == it2._container) && (it1._node == it2._node); }
304  friend bool operator!=(const BinTreeIterator& it1, const BinTreeIterator& it2) noexcept
305  { return (it1._container != it2._container) || (it1._node != it2._node); }
306 
307  BinTreeIterator& operator++() noexcept;
308  BinTreeIterator operator++(int) noexcept;
309 
310  reference operator*() noexcept;
311  pointer operator->() noexcept;
312 
314  explicit operator bool() const noexcept { return (_container != nullptr) && (_node != nullptr); }
315 
317  bool compare(const T& item1, const T& item2) const noexcept { return (_container != nullptr) ? _container->compare(item1, item2) : false; }
318 
320  void swap(BinTreeIterator& it) noexcept;
321  template <class UContainer, typename U>
323 
324 private:
325  TContainer* _container;
326  T* _node;
327 };
328 
330 
333 template <class TContainer, typename T>
335 {
336 public:
337  // Standard iterator type definitions
338  typedef T value_type;
340  typedef const value_type& const_reference;
341  typedef value_type* pointer;
342  typedef const value_type* const_pointer;
343  typedef ptrdiff_t difference_type;
344  typedef size_t size_type;
345  typedef std::bidirectional_iterator_tag iterator_category;
346 
347  BinTreeConstIterator() noexcept : _container(nullptr), _node(nullptr) {}
348  explicit BinTreeConstIterator(const TContainer* container, const T* node) noexcept : _container(container), _node(node) {}
349  BinTreeConstIterator(const BinTreeIterator<TContainer, T>& it) noexcept : _container(it._container), _node(it._node) {}
350  BinTreeConstIterator(const BinTreeConstIterator& it) noexcept = default;
351  BinTreeConstIterator(BinTreeConstIterator&& it) noexcept = default;
352  ~BinTreeConstIterator() noexcept = default;
353 
354  BinTreeConstIterator& operator=(const BinTreeIterator<TContainer, T>& it) noexcept
355  { _container = it._container; _node = it._node; return *this; }
356  BinTreeConstIterator& operator=(const BinTreeConstIterator& it) noexcept = default;
358 
359  friend bool operator==(const BinTreeConstIterator& it1, const BinTreeConstIterator& it2) noexcept
360  { return (it1._container == it2._container) && (it1._node == it2._node); }
361  friend bool operator!=(const BinTreeConstIterator& it1, const BinTreeConstIterator& it2) noexcept
362  { return (it1._container != it2._container) || (it1._node != it2._node); }
363 
364  BinTreeConstIterator& operator++() noexcept;
365  BinTreeConstIterator operator++(int) noexcept;
366 
367  const_reference operator*() const noexcept;
368  const_pointer operator->() const noexcept;
369 
371  explicit operator bool() const noexcept { return (_container != nullptr) && (_node != nullptr); }
372 
374  bool compare(const T& item1, const T& item2) const noexcept { return (_container != nullptr) ? _container->compare(item1, item2) : false; }
375 
377  void swap(BinTreeConstIterator& it) noexcept;
378  template <class UContainer, typename U>
380 
381 private:
382  const TContainer* _container;
383  const T* _node;
384 };
385 
387 
390 template <class TContainer, typename T>
392 {
394 
395 public:
396  // Standard iterator type definitions
397  typedef T value_type;
399  typedef const value_type& const_reference;
400  typedef value_type* pointer;
401  typedef const value_type* const_pointer;
402  typedef ptrdiff_t difference_type;
403  typedef size_t size_type;
404  typedef std::bidirectional_iterator_tag iterator_category;
405 
406  BinTreeReverseIterator() noexcept : _container(nullptr), _node(nullptr) {}
407  explicit BinTreeReverseIterator(TContainer* container, T* node) noexcept : _container(container), _node(node) {}
408  BinTreeReverseIterator(const BinTreeReverseIterator& it) noexcept = default;
410  ~BinTreeReverseIterator() noexcept = default;
411 
412  BinTreeReverseIterator& operator=(const BinTreeReverseIterator& it) noexcept = default;
413  BinTreeReverseIterator& operator=(BinTreeReverseIterator&& it) noexcept = default;
414 
415  friend bool operator==(const BinTreeReverseIterator& it1, const BinTreeReverseIterator& it2) noexcept
416  { return (it1._container == it2._container) && (it1._node == it2._node); }
417  friend bool operator!=(const BinTreeReverseIterator& it1, const BinTreeReverseIterator& it2) noexcept
418  { return (it1._container != it2._container) || (it1._node != it2._node); }
419 
420  BinTreeReverseIterator& operator++() noexcept;
421  BinTreeReverseIterator operator++(int) noexcept;
422 
423  reference operator*() noexcept;
424  pointer operator->() noexcept;
425 
427  explicit operator bool() const noexcept { return (_container != nullptr) && (_node != nullptr); }
428 
430  bool compare(const T& item1, const T& item2) const noexcept { return (_container != nullptr) ? _container->compare(item1, item2) : false; }
431 
433  void swap(BinTreeReverseIterator& it) noexcept;
434  template <class UContainer, typename U>
436 
437 private:
438  TContainer* _container;
439  T* _node;
440 };
441 
443 
446 template <class TContainer, typename T>
448 {
449 public:
450  // Standard iterator type definitions
451  typedef T value_type;
453  typedef const value_type& const_reference;
454  typedef value_type* pointer;
455  typedef const value_type* const_pointer;
456  typedef ptrdiff_t difference_type;
457  typedef size_t size_type;
458  typedef std::bidirectional_iterator_tag iterator_category;
459 
460  BinTreeConstReverseIterator() noexcept : _container(nullptr), _node(nullptr) {}
461  explicit BinTreeConstReverseIterator(const TContainer* container, const T* node) noexcept : _container(container), _node(node) {}
462  BinTreeConstReverseIterator(const BinTreeReverseIterator<TContainer, T>& it) noexcept : _container(it._container), _node(it._node) {}
465  ~BinTreeConstReverseIterator() noexcept = default;
466 
467  BinTreeConstReverseIterator& operator=(const BinTreeReverseIterator<TContainer, T>& it) noexcept
468  { _container = it._container; _node = it._node; return *this; }
471 
472  friend bool operator==(const BinTreeConstReverseIterator& it1, const BinTreeConstReverseIterator& it2) noexcept
473  { return (it1._container == it2._container) && (it1._node == it2._node); }
474  friend bool operator!=(const BinTreeConstReverseIterator& it1, const BinTreeConstReverseIterator& it2) noexcept
475  { return (it1._container != it2._container) || (it1._node != it2._node); }
476 
477  BinTreeConstReverseIterator& operator++() noexcept;
478  BinTreeConstReverseIterator operator++(int) noexcept;
479 
480  const_reference operator*() const noexcept;
481  const_pointer operator->() const noexcept;
482 
484  explicit operator bool() const noexcept { return (_container != nullptr) && (_node != nullptr); }
485 
487  bool compare(const T& item1, const T& item2) const noexcept { return (_container != nullptr) ? _container->compare(item1, item2) : false; }
488 
490  void swap(BinTreeConstReverseIterator& it) noexcept;
491  template <class UContainer, typename U>
493 
494 private:
495  const TContainer* _container;
496  const T* _node;
497 };
498 
501 } // namespace CppCommon
502 
503 #include "bintree.inl"
504 
505 #endif // CPPCOMMON_CONTAINERS_BINTREE_H
Intrusive non balanced binary tree container inline implementation.
Intrusive binary tree constant iterator.
Definition: bintree.h:335
BinTreeConstIterator(const BinTreeConstIterator &it) noexcept=default
BinTreeConstIterator & operator=(BinTreeConstIterator &&it) noexcept=default
~BinTreeConstIterator() noexcept=default
friend bool operator!=(const BinTreeConstIterator &it1, const BinTreeConstIterator &it2) noexcept
Definition: bintree.h:361
const value_type & const_reference
Definition: bintree.h:340
friend bool operator==(const BinTreeConstIterator &it1, const BinTreeConstIterator &it2) noexcept
Definition: bintree.h:359
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.h:374
std::bidirectional_iterator_tag iterator_category
Definition: bintree.h:345
friend void swap(BinTreeConstIterator< UContainer, U > &it1, BinTreeConstIterator< UContainer, U > &it2) noexcept
const value_type * const_pointer
Definition: bintree.h:342
BinTreeConstIterator(const BinTreeIterator< TContainer, T > &it) noexcept
Definition: bintree.h:349
BinTreeConstIterator(BinTreeConstIterator &&it) noexcept=default
BinTreeConstIterator(const TContainer *container, const T *node) noexcept
Definition: bintree.h:348
BinTreeConstIterator & operator=(const BinTreeConstIterator &it) noexcept=default
Intrusive binary tree constant reverse iterator.
Definition: bintree.h:448
~BinTreeConstReverseIterator() noexcept=default
BinTreeConstReverseIterator & operator=(const BinTreeConstReverseIterator &it) noexcept=default
BinTreeConstReverseIterator(const TContainer *container, const T *node) noexcept
Definition: bintree.h:461
const value_type * const_pointer
Definition: bintree.h:455
BinTreeConstReverseIterator(BinTreeConstReverseIterator &&it) noexcept=default
BinTreeConstReverseIterator & operator=(BinTreeConstReverseIterator &&it) noexcept=default
BinTreeConstReverseIterator(const BinTreeConstReverseIterator &it) noexcept=default
std::bidirectional_iterator_tag iterator_category
Definition: bintree.h:458
friend void swap(BinTreeConstReverseIterator< UContainer, U > &it1, BinTreeConstReverseIterator< UContainer, U > &it2) noexcept
friend bool operator==(const BinTreeConstReverseIterator &it1, const BinTreeConstReverseIterator &it2) noexcept
Definition: bintree.h:472
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.h:487
const value_type & const_reference
Definition: bintree.h:453
BinTreeConstReverseIterator(const BinTreeReverseIterator< TContainer, T > &it) noexcept
Definition: bintree.h:462
friend bool operator!=(const BinTreeConstReverseIterator &it1, const BinTreeConstReverseIterator &it2) noexcept
Definition: bintree.h:474
Intrusive non balanced binary tree container.
Definition: bintree.h:139
std::pair< iterator, bool > insert(T &item) noexcept
Insert a new item into the binary tree.
Definition: bintree.inl:260
bool empty() const noexcept
Is the binary tree empty?
Definition: bintree.h:179
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.inl:223
void clear() noexcept
Clear the binary tree.
Definition: bintree.inl:399
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.inl:179
const value_type * const_pointer
Definition: bintree.h:147
const value_type & const_reference
Definition: bintree.h:145
reverse_iterator rend() noexcept
Get the reverse end binary tree iterator.
Definition: bintree.inl:119
BinTreeConstReverseIterator< BinTree< T, TCompare >, T > const_reverse_iterator
Definition: bintree.h:153
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.inl:137
BinTreeIterator< BinTree< T, TCompare >, T > iterator
Definition: bintree.h:150
value_type * pointer
Definition: bintree.h:146
const T * root() const noexcept
Definition: bintree.h:186
size_t size_type
Definition: bintree.h:149
TCompare value_compare
Definition: bintree.h:143
iterator end() noexcept
Get the end binary tree iterator.
Definition: bintree.inl:83
BinTree(const TCompare &compare=TCompare()) noexcept
Definition: bintree.h:165
BinTreeReverseIterator< BinTree< T, TCompare >, T > reverse_iterator
Definition: bintree.h:152
value_type & reference
Definition: bintree.h:144
iterator begin() noexcept
Get the begin binary tree iterator.
Definition: bintree.inl:65
ptrdiff_t difference_type
Definition: bintree.h:148
BinTreeConstIterator< BinTree< T, TCompare >, T > const_iterator
Definition: bintree.h:151
T * highest() noexcept
Get the highest binary tree item.
Definition: bintree.inl:43
const_reverse_iterator crend() const noexcept
Definition: bintree.inl:131
T * lowest() noexcept
Get the lowest binary tree item.
Definition: bintree.inl:21
T * erase(const T &item) noexcept
Erase the given item from the binary tree.
Definition: bintree.inl:320
size_t size() const noexcept
Get the binary tree size.
Definition: bintree.h:182
const_iterator cbegin() const noexcept
Definition: bintree.inl:77
const_iterator cend() const noexcept
Definition: bintree.inl:95
reverse_iterator rbegin() noexcept
Get the reverse begin binary tree iterator.
Definition: bintree.inl:101
void swap(BinTree &bintree) noexcept
Swap two instances.
Definition: bintree.inl:406
const_reverse_iterator crbegin() const noexcept
Definition: bintree.inl:113
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.h:195
T * root() noexcept
Get the root binary tree item.
Definition: bintree.h:185
Intrusive binary tree iterator.
Definition: bintree.h:279
friend bool operator!=(const BinTreeIterator &it1, const BinTreeIterator &it2) noexcept
Definition: bintree.h:304
BinTreeIterator() noexcept
Definition: bintree.h:293
const value_type * const_pointer
Definition: bintree.h:288
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.h:317
value_type & reference
Definition: bintree.h:285
~BinTreeIterator() noexcept=default
value_type * pointer
Definition: bintree.h:287
const value_type & const_reference
Definition: bintree.h:286
std::bidirectional_iterator_tag iterator_category
Definition: bintree.h:291
friend void swap(BinTreeIterator< UContainer, U > &it1, BinTreeIterator< UContainer, U > &it2) noexcept
BinTreeIterator(const BinTreeIterator &it) noexcept=default
BinTreeIterator(BinTreeIterator &&it) noexcept=default
BinTreeIterator(TContainer *container, T *node) noexcept
Definition: bintree.h:294
Intrusive binary tree reverse iterator.
Definition: bintree.h:392
BinTreeReverseIterator(TContainer *container, T *node) noexcept
Definition: bintree.h:407
const value_type & const_reference
Definition: bintree.h:399
~BinTreeReverseIterator() noexcept=default
std::bidirectional_iterator_tag iterator_category
Definition: bintree.h:404
BinTreeReverseIterator(BinTreeReverseIterator &&it) noexcept=default
BinTreeReverseIterator(const BinTreeReverseIterator &it) noexcept=default
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.h:430
friend void swap(BinTreeReverseIterator< UContainer, U > &it1, BinTreeReverseIterator< UContainer, U > &it2) noexcept
friend bool operator!=(const BinTreeReverseIterator &it1, const BinTreeReverseIterator &it2) noexcept
Definition: bintree.h:417
const value_type * const_pointer
Definition: bintree.h:401
C++ Common project definitions.
Definition: token_bucket.h:15
Binary tree node.
Definition: bintree.h:157
T * parent
Pointer to the parent binary tree node.
Definition: bintree.h:158
T * left
Pointer to the left child binary tree node.
Definition: bintree.h:159
T * right
Pointer to the right child binary tree node.
Definition: bintree.h:160