libUTL++
BinTree.h
1 #pragma once
2 
4 
5 #include <libutl/BinTreeNode.h>
6 #include <libutl/MaxObject.h>
7 #include <libutl/SortedCollection.h>
8 
10 
11 UTL_NS_BEGIN;
12 
14 
15 class BinTreeBfsIt;
16 class BinTreeIt;
17 
19 
31 
33 class BinTree : public SortedCollection
34 {
36 
37 public:
38  typedef BinTreeIt iterator;
39 
40 public:
47  BinTree(bool owner, bool multiSet = false, Ordering* ordering = nullptr);
48 
49  virtual void steal(Object& rhs);
50 
52 
53 
54  void
55  dumpTree(Stream& os) const
56  {
57  _root->dump(os, 0);
58  }
60 
62 
63 
67  void
69  {
70  serializeFast(nullptr, stream, io, mode);
71  }
72 
78  void
79  serializeFast(const RunTimeClass* rtc, Stream& stream, uint_t io, uint_t mode = ser_default);
81 
83 
84  virtual Object* find(const Object& key) const;
85 
91  iterator findIt(const Object& key) const;
92 
98  iterator findIt(const Object& key);
99 
100  void
101  findIt(const Object& key, BidIt& it) const
102  {
103  const_cast_this->findIt(key, it);
104  IFDEBUG(it.setConst(true));
105  }
106 
107  virtual void findIt(const Object& key, BidIt& it);
108 
109  void
110  findFirstIt(const Object& key, BidIt& it, bool insert = false) const
111  {
112  const_cast_this->findFirstIt(key, it, insert);
113  IFDEBUG(it.setConst(true));
114  }
115 
116  virtual void findFirstIt(const Object& key, BidIt& it, bool insert = false);
117 
124  iterator findFirstIt(const Object& key, bool insert = false) const;
125 
132  iterator findFirstIt(const Object& key, bool insert = false);
133 
134  void
135  findLastIt(const Object& key, BidIt& it) const
136  {
137  const_cast_this->findLastIt(key, it);
138  IFDEBUG(it.setConst(true));
139  }
140 
141  virtual void findLastIt(const Object& key, BidIt& it);
142 
148  iterator findLastIt(const Object& key) const;
149 
155  iterator findLastIt(const Object& key);
157 
159 
160  bool
161  add(const Object& object)
162  {
163  return super::add(object);
164  }
165 
166  virtual bool add(const Object* object);
167 
168  void
169  add(const Collection& collection)
170  {
171  super::add(collection);
172  }
173 
174  void
175  insert(const Object& object, const BidIt& it)
176  {
177  return super::insert(object, it);
178  }
179 
180  virtual void insert(const Object* object, const BidIt& it);
182 
184 
185  virtual void clear();
186 
187  bool
188  remove(const Object* key)
189  {
190  ASSERTD(key != nullptr);
191  return remove(*key);
192  }
193 
194  virtual bool remove(const Object& key);
195 
196  virtual void removeIt(BidIt& it);
197 
198  virtual void
199  removeIt(BidIt& begin, BidIt& end)
200  {
201  super::removeIt(begin, end);
202  }
204 
206 
207 
208  inline iterator begin() const;
209 
211  inline iterator begin();
212 
214  inline BinTreeBfsIt beginBFS() const;
215 
217  inline BinTreeBfsIt beginBFS();
218 
219  inline virtual BidIt* beginNew() const;
220 
221  inline virtual BidIt* beginNew();
222 
224  inline BinTreeBfsIt* beginBFSNew() const;
225 
227  inline BinTreeBfsIt* beginBFSNew();
228 
230  inline iterator end() const;
231 
233  inline iterator end();
234 
239  inline BinTreeBfsIt endBFS() const;
240 
242  inline BinTreeBfsIt endBFS();
243 
244  inline virtual BidIt* endNew() const;
245 
246  inline virtual BidIt* endNew();
247 
252  inline BinTreeBfsIt* endBFSNew() const;
253 
255  inline BinTreeBfsIt* endBFSNew();
256 
258  inline iterator root() const;
259 
261  inline iterator root();
263 
264 #ifdef DEBUG
265  iterator begin(bool notifyOwner);
266 
267  virtual void sanityCheck() const;
268 #endif
269 protected:
270  inline int
271  compareObjects(const Object* lhs, const Object* rhs) const
272  {
273  return (lhs == &maxObject) ? 1 : super::compareObjects(lhs, rhs);
274  }
275  void clear(BinTreeNode* node);
276  virtual BinTreeNode* createNode(const Object* object) = 0;
277  BinTreeNode* findFirstMatch(const Object& key, BinTreeNode* node) const;
278  BinTreeNode* findInsertionPoint(const Object& key, bool* insertLeft = nullptr) const;
279  iterator findIt(const Object& key, uint_t findType) const;
280  iterator findIt(const Object& key, uint_t findType);
281  BinTreeNode* findLastMatch(const Object& key, BinTreeNode* node) const;
282  BinTreeNode* findNode(const Object& key, uint_t findType) const;
283  BinTreeNode* removeNode(BinTreeNode* node);
284  virtual void resetRoot();
285 
286 protected:
287  BinTreeNode* _root;
288 
289 private:
290  void init(bool owner = true, bool multiSet = false, Ordering* ordering = nullptr);
291  void deInit();
292 };
293 
295 
296 UTL_NS_END;
297 
299 
300 #include <libutl/BinTreeIt.h>
301 #include <libutl/BinTreeBfsIt.h>
Binary tree abstraction.
Definition: BinTree.h:33
#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
const MaxObject maxObject
Global instance of MaxObject.
default representation (via getSerializeMode())
Definition: util.h:75
Store information about a class.
Definition: RunTimeClass.h:23
#define IFDEBUG(x)
Do x in DEBUG mode only.
Abstraction for a Collection whose objects may be sorted.
In-order bi-directional BinTree iterator.
Definition: BinTreeIt.h:22
void setConst(bool p_const)
Set the const flag.
Definition: FwdIt.h:68
#define UTL_CLASS_DECL_ABC(DC, BC)
Declaration of standard UTL++ functionality for an abstract base class (ABC).
Definition: macros.h:650
Binary tree node.
Definition: BinTreeNode.h:31
Breadth-first BinTree iterator.
Definition: BinTreeBfsIt.h:34
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
Stream I/O abstraction.
Definition: Stream.h:68
Bi-directional iterator abstraction.
Definition: BidIt.h:25
void serializeFast(Stream &stream, uint_t io, uint_t mode=ser_default)
Serialize to/from the given stream.
Definition: BinTree.h:68
void dumpTree(Stream &os) const
Dump the tree to the given stream (for debugging).
Definition: BinTree.h:55
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