libUTL++
BinTreeIt.h
1 #pragma once
2 
4 
5 #include <libutl/BidIt.h>
6 
8 
9 UTL_NS_BEGIN;
10 
12 
20 
22 class BinTreeIt : public BidIt
23 {
25 
26 public:
32  BinTreeIt(const BinTree* tree, BinTreeNode* node)
33  : _tree(const_cast<BinTree*>(tree))
34  , _node(node)
35  {
36  IFDEBUG(FwdIt::setOwner(_tree));
37  }
38 
39 #ifdef DEBUG
40  BinTreeIt(const BinTree* tree, BinTreeNode* node, bool notifyOwner)
41  {
42  _tree = const_cast<BinTree*>(tree);
43  _node = node;
44  FwdIt::setOwner(_tree, notifyOwner);
45  }
46 #endif
47 
48  virtual int compare(const Object& rhs) const;
49 
50  virtual void copy(const Object& rhs);
51 
52  virtual void forward(size_t dist = 1);
53 
54  virtual Object* get() const;
55 
58  node() const
59  {
60  return _node;
61  }
62 
64  BinTree*
65  tree() const
66  {
67  return _tree;
68  }
69 
70  virtual void reverse(size_t dist = 1);
71 
72  virtual void set(const Object* object);
73 
79  void
80  set(const BinTree* tree, BinTreeNode* node = nullptr)
81  {
82  _tree = const_cast<BinTree*>(tree);
83  _node = node;
84  IFDEBUG(FwdIt::setOwner(_tree));
85  }
86 
88  void
90  {
91  _node = node;
92  }
93 
94  BinTreeIt& operator++()
95  {
96  forward();
97  return *this;
98  }
99 
100  BinTreeIt operator++(int)
101  {
102  BinTreeIt res = *this;
103  forward();
104  return res;
105  }
106 
107  BinTreeIt& operator--()
108  {
109  reverse();
110  return *this;
111  }
112 
113  BinTreeIt operator--(int)
114  {
115  BinTreeIt res = *this;
116  reverse();
117  return res;
118  }
119 
120 private:
121  void
122  init()
123  {
124  _tree = nullptr;
125  _node = nullptr;
126  }
127  void
128  deInit()
129  {
130  }
131 
132 private:
133  BinTree* _tree;
134  BinTreeNode* _node;
135 };
136 
138 
140 BinTree::begin() const
141 {
142  iterator it(this, _root->leftMost());
143  IFDEBUG(it.setConst(true));
144  return it;
145 }
146 
148 
150 BinTree::begin()
151 {
152  return iterator(this, _root->leftMost());
153 }
154 
156 
157 BidIt*
158 BinTree::beginNew() const
159 {
160  BidIt* it = new iterator(this, _root->leftMost());
161  IFDEBUG(it->setConst(true));
162  return it;
163 }
164 
166 
167 BidIt*
168 BinTree::beginNew()
169 {
170  return new iterator(this, _root->leftMost());
171 }
172 
174 
176 BinTree::end() const
177 {
178  iterator it(this, _root->rightMost());
179  IFDEBUG(it.setConst(true));
180  return it;
181 }
182 
184 
186 BinTree::end()
187 {
188  return iterator(this, _root->rightMost());
189 }
190 
192 
193 BidIt*
194 BinTree::endNew() const
195 {
196  BidIt* it = new iterator(this, _root->rightMost());
197  IFDEBUG(it->setConst(true));
198  return it;
199 }
200 
202 
203 BidIt*
204 BinTree::endNew()
205 {
206  return new iterator(this, _root->rightMost());
207 }
208 
210 
212 BinTree::root() const
213 {
214  iterator it(this, _root);
215  IFDEBUG(it.setConst(true));
216  return it;
217 }
218 
220 
222 BinTree::root()
223 {
224  return iterator(this, _root);
225 }
226 
228 
229 #ifdef DEBUG
230 inline BinTree::iterator
231 BinTree::begin(bool notifyOwner)
232 {
233  return iterator(this, _root->leftMost(), notifyOwner);
234 }
235 #endif
236 
238 
239 UTL_NS_END;
Binary tree abstraction.
Definition: BinTree.h:33
BinTreeNode * node() const
Get the current node.
Definition: BinTreeIt.h:58
void deInit()
De-initialize UTL++.
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
BinTreeIt(const BinTree *tree, BinTreeNode *node)
Constructor.
Definition: BinTreeIt.h:32
void reverse(const BidIt &begin, const BidIt &end)
Reverse a sequence.
#define IFDEBUG(x)
Do x in DEBUG mode only.
void setNode(BinTreeNode *node)
Set the node.
Definition: BinTreeIt.h:89
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
In-order bi-directional BinTree iterator.
Definition: BinTreeIt.h:22
void setConst(bool p_const)
Set the const flag.
Definition: FwdIt.h:68
Binary tree node.
Definition: BinTreeNode.h:31
Bi-directional iterator abstraction.
Definition: BidIt.h:25
Root of UTL++ class hierarchy.
Definition: Object.h:52
int compare(bool lhs, bool rhs)
Compare two boolean values.
void init()
Initialize UTL++.
BinTree * tree() const
Get the associated BinTree.
Definition: BinTreeIt.h:65