9 #ifndef CPPCOMMON_CONTAINERS_BINTREE_H
10 #define CPPCOMMON_CONTAINERS_BINTREE_H
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;
137 template <
typename T,
typename TCompare = std::less<T>>
166 template <
class InputIterator>
167 BinTree(InputIterator first, InputIterator last,
const TCompare&
compare = TCompare()) noexcept;
176 explicit operator
bool() const noexcept {
return !
empty(); }
179 bool empty() const noexcept {
return _root ==
nullptr; }
182 size_t size() const noexcept {
return _size; }
185 T*
root() noexcept {
return _root; }
186 const T*
root() const noexcept {
return _root; }
189 const T*
lowest() const noexcept;
192 const T*
highest() const noexcept;
195 bool compare(const T& item1, const T& item2) const noexcept {
return _compare(item1, item2); }
245 T*
erase(const T& item) noexcept;
254 void clear() noexcept;
258 template <typename U, typename UCompare>
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;
277 template <class TContainer, typename T>
294 explicit BinTreeIterator(TContainer* container, T* node) noexcept : _container(container), _node(node) {}
303 {
return (it1._container == it2._container) && (it1._node == it2._node); }
305 {
return (it1._container != it2._container) || (it1._node != it2._node); }
314 explicit operator
bool() const noexcept {
return (_container !=
nullptr) && (_node !=
nullptr); }
317 bool compare(
const T& item1,
const T& item2)
const noexcept {
return (_container !=
nullptr) ? _container->compare(item1, item2) :
false; }
321 template <
class UContainer,
typename U>
325 TContainer* _container;
333 template <
class TContainer,
typename T>
348 explicit BinTreeConstIterator(
const TContainer* container,
const T* node) noexcept : _container(container), _node(node) {}
355 { _container = it._container; _node = it._node;
return *
this; }
360 {
return (it1._container == it2._container) && (it1._node == it2._node); }
362 {
return (it1._container != it2._container) || (it1._node != it2._node); }
371 explicit operator
bool() const noexcept {
return (_container !=
nullptr) && (_node !=
nullptr); }
374 bool compare(
const T& item1,
const T& item2)
const noexcept {
return (_container !=
nullptr) ? _container->
compare(item1, item2) :
false; }
378 template <
class UContainer,
typename U>
382 const TContainer* _container;
390 template <
class TContainer,
typename T>
416 {
return (it1._container == it2._container) && (it1._node == it2._node); }
418 {
return (it1._container != it2._container) || (it1._node != it2._node); }
427 explicit operator
bool() const noexcept {
return (_container !=
nullptr) && (_node !=
nullptr); }
430 bool compare(
const T& item1,
const T& item2)
const noexcept {
return (_container !=
nullptr) ? _container->compare(item1, item2) :
false; }
434 template <
class UContainer,
typename U>
438 TContainer* _container;
446 template <
class TContainer,
typename T>
468 { _container = it._container; _node = it._node;
return *
this; }
473 {
return (it1._container == it2._container) && (it1._node == it2._node); }
475 {
return (it1._container != it2._container) || (it1._node != it2._node); }
484 explicit operator
bool() const noexcept {
return (_container !=
nullptr) && (_node !=
nullptr); }
487 bool compare(
const T& item1,
const T& item2)
const noexcept {
return (_container !=
nullptr) ? _container->
compare(item1, item2) :
false; }
491 template <
class UContainer,
typename U>
495 const TContainer* _container;
Intrusive non balanced binary tree container inline implementation.
Intrusive binary tree constant iterator.
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
const value_type & const_reference
friend bool operator==(const BinTreeConstIterator &it1, const BinTreeConstIterator &it2) noexcept
bool compare(const T &item1, const T &item2) const noexcept
Compare two items: if the first item is less than the second one?
std::bidirectional_iterator_tag iterator_category
friend void swap(BinTreeConstIterator< UContainer, U > &it1, BinTreeConstIterator< UContainer, U > &it2) noexcept
const value_type * const_pointer
ptrdiff_t difference_type
BinTreeConstIterator(const BinTreeIterator< TContainer, T > &it) noexcept
BinTreeConstIterator(BinTreeConstIterator &&it) noexcept=default
BinTreeConstIterator(const TContainer *container, const T *node) noexcept
BinTreeConstIterator & operator=(const BinTreeConstIterator &it) noexcept=default
BinTreeConstIterator() noexcept
Intrusive binary tree constant reverse iterator.
~BinTreeConstReverseIterator() noexcept=default
BinTreeConstReverseIterator & operator=(const BinTreeConstReverseIterator &it) noexcept=default
BinTreeConstReverseIterator(const TContainer *container, const T *node) noexcept
const value_type * const_pointer
BinTreeConstReverseIterator(BinTreeConstReverseIterator &&it) noexcept=default
ptrdiff_t difference_type
BinTreeConstReverseIterator & operator=(BinTreeConstReverseIterator &&it) noexcept=default
BinTreeConstReverseIterator(const BinTreeConstReverseIterator &it) noexcept=default
std::bidirectional_iterator_tag iterator_category
friend void swap(BinTreeConstReverseIterator< UContainer, U > &it1, BinTreeConstReverseIterator< UContainer, U > &it2) noexcept
friend bool operator==(const BinTreeConstReverseIterator &it1, const BinTreeConstReverseIterator &it2) noexcept
bool compare(const T &item1, const T &item2) const noexcept
Compare two items: if the first item is less than the second one?
const value_type & const_reference
BinTreeConstReverseIterator(const BinTreeReverseIterator< TContainer, T > &it) noexcept
friend bool operator!=(const BinTreeConstReverseIterator &it1, const BinTreeConstReverseIterator &it2) noexcept
BinTreeConstReverseIterator() noexcept
Intrusive non balanced binary tree container.
std::pair< iterator, bool > insert(T &item) noexcept
Insert a new item into the binary tree.
bool empty() const noexcept
Is the binary tree empty?
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 ...
void clear() noexcept
Clear the binary tree.
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...
const value_type * const_pointer
const value_type & const_reference
reverse_iterator rend() noexcept
Get the reverse end binary tree iterator.
BinTreeConstReverseIterator< BinTree< T, TCompare >, T > const_reverse_iterator
iterator find(const T &item) noexcept
Find the iterator which points to the first equal item in the binary tree or return end iterator.
BinTreeIterator< BinTree< T, TCompare >, T > iterator
const T * root() const noexcept
iterator end() noexcept
Get the end binary tree iterator.
BinTree(const TCompare &compare=TCompare()) noexcept
BinTreeReverseIterator< BinTree< T, TCompare >, T > reverse_iterator
iterator begin() noexcept
Get the begin binary tree iterator.
ptrdiff_t difference_type
BinTreeConstIterator< BinTree< T, TCompare >, T > const_iterator
T * highest() noexcept
Get the highest binary tree item.
const_reverse_iterator crend() const noexcept
T * lowest() noexcept
Get the lowest binary tree item.
T * erase(const T &item) noexcept
Erase the given item from the binary tree.
size_t size() const noexcept
Get the binary tree size.
const_iterator cbegin() const noexcept
const_iterator cend() const noexcept
reverse_iterator rbegin() noexcept
Get the reverse begin binary tree iterator.
void swap(BinTree &bintree) noexcept
Swap two instances.
const_reverse_iterator crbegin() const noexcept
bool compare(const T &item1, const T &item2) const noexcept
Compare two items: if the first item is less than the second one?
T * root() noexcept
Get the root binary tree item.
Intrusive binary tree iterator.
friend bool operator!=(const BinTreeIterator &it1, const BinTreeIterator &it2) noexcept
BinTreeIterator() noexcept
const value_type * const_pointer
bool compare(const T &item1, const T &item2) const noexcept
Compare two items: if the first item is less than the second one?
~BinTreeIterator() noexcept=default
const value_type & const_reference
std::bidirectional_iterator_tag iterator_category
friend void swap(BinTreeIterator< UContainer, U > &it1, BinTreeIterator< UContainer, U > &it2) noexcept
BinTreeIterator(const BinTreeIterator &it) noexcept=default
BinTreeIterator(BinTreeIterator &&it) noexcept=default
ptrdiff_t difference_type
BinTreeIterator(TContainer *container, T *node) noexcept
Intrusive binary tree reverse iterator.
BinTreeReverseIterator(TContainer *container, T *node) noexcept
BinTreeReverseIterator() noexcept
const value_type & const_reference
~BinTreeReverseIterator() noexcept=default
std::bidirectional_iterator_tag iterator_category
BinTreeReverseIterator(BinTreeReverseIterator &&it) noexcept=default
BinTreeReverseIterator(const BinTreeReverseIterator &it) noexcept=default
ptrdiff_t difference_type
bool compare(const T &item1, const T &item2) const noexcept
Compare two items: if the first item is less than the second one?
friend void swap(BinTreeReverseIterator< UContainer, U > &it1, BinTreeReverseIterator< UContainer, U > &it2) noexcept
friend bool operator!=(const BinTreeReverseIterator &it1, const BinTreeReverseIterator &it2) noexcept
const value_type * const_pointer
C++ Common project definitions.
T * parent
Pointer to the parent binary tree node.
T * left
Pointer to the left child binary tree node.
T * right
Pointer to the right child binary tree node.