CppCommon  1.0.4.1
C++ Common Library
bintree_splay.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_CONTAINERS_BINTREE_SPLAY_H
10 #define CPPCOMMON_CONTAINERS_BINTREE_SPLAY_H
11 
12 #include "bintree.h"
13 
14 namespace CppCommon {
15 
17 
196 template <typename T, typename TCompare = std::less<T>>
198 {
199 public:
200  // Standard container type definitions
201  typedef T value_type;
202  typedef TCompare value_compare;
204  typedef const value_type& const_reference;
205  typedef value_type* pointer;
206  typedef const value_type* const_pointer;
207  typedef ptrdiff_t difference_type;
208  typedef size_t size_type;
213 
215  struct Node
216  {
217  T* parent;
218  T* left;
219  T* right;
220 
221  Node() : parent(nullptr), left(nullptr), right(nullptr) {}
222  };
223 
224  explicit BinTreeSplay(const TCompare& compare = TCompare()) noexcept
225  : _compare(compare),
226  _size(0),
227  _root(nullptr)
228  {}
229  template <class InputIterator>
230  BinTreeSplay(InputIterator first, InputIterator last, const TCompare& compare = TCompare()) noexcept;
231  BinTreeSplay(const BinTreeSplay&) noexcept = default;
232  BinTreeSplay(BinTreeSplay&&) noexcept = default;
233  ~BinTreeSplay() noexcept = default;
234 
235  BinTreeSplay& operator=(const BinTreeSplay&) noexcept = default;
236  BinTreeSplay& operator=(BinTreeSplay&&) noexcept = default;
237 
239  explicit operator bool() const noexcept { return !empty(); }
240 
242  bool empty() const noexcept { return _root == nullptr; }
243 
245  size_t size() const noexcept { return _size; }
246 
248  T* root() noexcept { return _root; }
249  const T* root() const noexcept { return _root; }
251  T* lowest() noexcept;
252  const T* lowest() const noexcept;
254  T* highest() noexcept;
255  const T* highest() const noexcept;
256 
258  bool compare(const T& item1, const T& item2) const noexcept { return _compare(item1, item2); }
259 
261  iterator begin() noexcept;
262  const_iterator begin() const noexcept;
263  const_iterator cbegin() const noexcept;
265  iterator end() noexcept;
266  const_iterator end() const noexcept;
267  const_iterator cend() const noexcept;
268 
270  reverse_iterator rbegin() noexcept;
271  const_reverse_iterator rbegin() const noexcept;
272  const_reverse_iterator crbegin() const noexcept;
274  reverse_iterator rend() noexcept;
275  const_reverse_iterator rend() const noexcept;
276  const_reverse_iterator crend() const noexcept;
277 
279  iterator find(const T& item) noexcept;
280  const_iterator find(const T& item) const noexcept;
281 
283  iterator lower_bound(const T& item) noexcept;
284  const_iterator lower_bound(const T& item) const noexcept;
286  iterator upper_bound(const T& item) noexcept;
287  const_iterator upper_bound(const T& item) const noexcept;
288 
290 
294  std::pair<iterator, bool> insert(T& item) noexcept;
296 
301  std::pair<iterator, bool> insert(const const_iterator& position, T& item) noexcept;
302 
304 
308  T* erase(const T& item) noexcept;
310 
314  iterator erase(const iterator& it) noexcept;
315 
317  void clear() noexcept;
318 
320  void swap(BinTreeSplay& bintree) noexcept;
321  template <typename U, typename UCompare>
322  friend void swap(BinTreeSplay<U, UCompare>& bintree1, BinTreeSplay<U, UCompare>& bintree2) noexcept;
323 
324 private:
325  TCompare _compare; // Binary tree compare
326  size_t _size; // Binary tree size
327  T* _root; // Binary tree root node
328 
329  const T* InternalLowest() const noexcept;
330  const T* InternalHighest() const noexcept;
331  const T* InternalFind(const T& item) const noexcept;
332  const T* InternalLowerBound(const T& item) const noexcept;
333  const T* InternalUpperBound(const T& item) const noexcept;
334 
335  void Splay(T* x) const;
336  void Zig(T* x) const;
337  void ZigZig(T* x) const;
338  void ZigZag(T* x) const;
339 };
340 
341 } // namespace CppCommon
342 
343 #include "bintree_splay.inl"
344 
345 #endif // CPPCOMMON_CONTAINERS_BINTREE_SPLAY_H
Intrusive non balanced binary tree container definition.
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
Intrusive balanced Splay binary tree container.
reverse_iterator rend() noexcept
Get the reverse end binary tree iterator.
const T * root() const noexcept
BinTreeSplay(const TCompare &compare=TCompare()) noexcept
const_iterator cbegin() const noexcept
iterator end() noexcept
Get the end binary tree iterator.
const value_type * const_pointer
size_t size() const noexcept
Get the binary tree size.
bool empty() const noexcept
Is the binary tree empty?
BinTreeConstReverseIterator< BinTreeSplay< T, TCompare >, T > const_reverse_iterator
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 ...
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_reverse_iterator crbegin() const noexcept
void clear() noexcept
Clear the binary tree.
T * highest() noexcept
Get the highest binary tree item.
T * root() noexcept
Get the root binary tree item.
iterator find(const T &item) noexcept
Find the iterator which points to the first equal item in the binary tree or return end iterator.
BinTreeConstIterator< BinTreeSplay< T, TCompare >, T > const_iterator
const_iterator cend() const noexcept
std::pair< iterator, bool > insert(T &item) noexcept
Insert a new item into the binary tree.
BinTreeReverseIterator< BinTreeSplay< T, TCompare >, T > reverse_iterator
reverse_iterator rbegin() noexcept
Get the reverse begin binary tree iterator.
T * lowest() noexcept
Get the lowest binary tree item.
iterator begin() noexcept
Get the begin binary tree iterator.
const_reverse_iterator crend() const noexcept
T * erase(const T &item) noexcept
Erase the given item from the binary tree.
const value_type & const_reference
void swap(BinTreeSplay &bintree) noexcept
Swap two instances.
bool compare(const T &item1, const T &item2) const noexcept
Compare two items: if the first item is less than the second one?
BinTreeIterator< BinTreeSplay< T, TCompare >, T > iterator
C++ Common project definitions.
Definition: token_bucket.h:15
Splay binary tree node.
T * parent
Pointer to the parent binary tree node.
T * right
Pointer to the right child binary tree node.
T * left
Pointer to the left child binary tree node.