libUTL++
SkipList.h
1 #pragma once
2 
4 
5 #include <libutl/SortedCollection.h>
6 #include <libutl/SkipListNode.h>
7 
8 // comment out this #define to try the regular/deterministic method of setting node level
9 //#define UTL_SKIPLIST_RNG
10 #ifdef UTL_SKIPLIST_RNG
11 #include <libutl/RandUtils.h>
12 #endif
13 
15 
16 UTL_NS_BEGIN;
17 
19 
20 class SkipListIt;
21 
23 
49 
51 class SkipList : public SortedCollection
52 {
54  friend class SkipListIt;
55 
56 public:
57  typedef SkipListIt iterator;
58 
59 public:
67  SkipList(bool owner, bool multiSet = false, Ordering* ordering = nullptr, uint_t maxLevel = 19)
68  {
69  init(owner, multiSet, ordering, maxLevel);
70  }
71 
72  virtual void steal(Object& rhs);
73 
74  virtual size_t innerAllocatedSize() const;
75 
77 
78  virtual Object* find(const Object& key) const;
79 
80  iterator findIt(const Object& key) const;
81 
82  iterator findIt(const Object& key);
83 
84  void
85  findIt(const Object& key, BidIt& it) const
86  {
87  const_cast_this->findIt(key, it);
88  IFDEBUG(it.setConst(true));
89  }
90 
91  virtual void findIt(const Object& key, BidIt& it);
92 
93  void
94  findFirstIt(const Object& key, BidIt& it, bool insert = false) const
95  {
96  const_cast_this->findFirstIt(key, it, insert);
97  IFDEBUG(it.setConst(true));
98  }
99 
100  virtual void findFirstIt(const Object& key, BidIt& it, bool insert = false);
101 
108  iterator findFirstIt(const Object& key, bool insert = false) const;
109 
116  iterator findFirstIt(const Object& key, bool insert = false);
117 
118  void
119  findLastIt(const Object& key, BidIt& it) const
120  {
121  const_cast_this->findLastIt(key, it);
122  IFDEBUG(it.setConst(true));
123  }
124 
125  virtual void findLastIt(const Object& key, BidIt& it);
126 
132  iterator findLastIt(const Object& key) const;
133 
139  iterator findLastIt(const Object& key);
141 
143 
144  bool
145  add(const Object& object)
146  {
147  return super::add(object);
148  }
149 
150  virtual bool add(const Object* object);
151 
152  void
153  add(const Collection& collection)
154  {
155  super::add(collection);
156  }
157 
158  void
159  insert(const Object& object, const BidIt& it)
160  {
161  return super::insert(object, it);
162  }
163 
164  virtual void insert(const Object* object, const BidIt& it);
166 
168 
169  virtual void clear();
170 
171  bool
172  remove(const Object* key)
173  {
174  ASSERTD(key != nullptr);
175  return remove(*key);
176  }
177 
178  virtual bool remove(const Object& key);
179 
180  virtual void removeIt(BidIt& it);
182 
184 
185  inline iterator begin() const;
186 
187  inline iterator begin();
188 
189  inline virtual BidIt* beginNew() const;
190 
191  inline virtual BidIt* beginNew();
192 
193  inline iterator end() const;
194 
195  inline iterator end();
196 
197  inline virtual BidIt* endNew() const;
198 
199  inline virtual BidIt* endNew();
201 
202  static void utl_init();
203 
204  static void utl_deInit();
205 
206 private:
207  void init(bool owner = true,
208  bool multiSet = false,
209  Ordering* ordering = nullptr,
210  uint_t maxLevel = 19);
211  void deInit();
212  iterator findIt(const Object& key, uint_t findType) const;
213  iterator findIt(const Object& key, uint_t findType);
214  SkipListNode* findNode(const Object& key,
215  uint_t findType = find_first,
216  SkipListNode** update = nullptr) const;
217  void removeNode(SkipListNode* node, SkipListNode** update);
218 
219 private:
220  SkipListNode *_head, *_tail;
221  uint_t _level;
222  uint_t _maxLevel;
223 #ifdef UTL_SKIPLIST_RNG
224  randutils::default_rng* _rng;
225 #else
226  uint32_t _counter;
227  uint32_t _counterLim;
228 #endif
229 };
230 
232 
233 UTL_NS_END;
234 
236 
237 #include <libutl/TSkipList.h>
#define const_cast_this
Pointer to the object the method was invoked on (casting away const).
Definition: macros.h:41
void deInit()
De-initialize UTL++.
Object comparison abstraction.
Definition: Ordering.h:30
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
Templated proxy for BidIt.
Definition: TBidIt.h:19
#define IFDEBUG(x)
Do x in DEBUG mode only.
unsigned int uint32_t
Unsigned 32-bit integer.
Definition: types.h:115
Abstraction for a Collection whose objects may be sorted.
SkipList node.
Definition: SkipListNode.h:30
Skip list.
Definition: SkipList.h:51
void setConst(bool p_const)
Set the const flag.
Definition: FwdIt.h:68
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
SkipList(bool owner, bool multiSet=false, Ordering *ordering=nullptr, uint_t maxLevel=19)
Constructor.
Definition: SkipList.h:67
Bi-directional iterator abstraction.
Definition: BidIt.h:25
find first matching object
Definition: util.h:44
Bi-directional SkipList iterator.
Definition: SkipListIt.h:22
Root of UTL++ class hierarchy.
Definition: Object.h:52
void init()
Initialize UTL++.
#define ASSERTD
Do an assertion in DEBUG mode only.
A collection of objects.
Definition: Collection.h:62