43 template <
class n_value>
class tnode
80 bool DoReparentTo(
self & p,
self * s,
bool behind )
83 if ( &p ==
this || p.IsDescendantOf(
this ) )
92 if ( !s || s->parent != parent )
93 s = behind ? parent->lchild : parent->fchild;
98 parent->fchild = parent->lchild =
this;
105 nsibling = s->nsibling;
109 nsibling->psibling =
this;
111 parent->lchild =
this;
115 psibling = s->psibling;
120 psibling->nsibling =
this;
122 parent->fchild =
this;
133 virtual void PreDisconnect() {}
135 virtual void PostDisconnect() {}
137 virtual void PreReparent() {}
139 virtual void PostReparent() {}
145 tnode( n_value v,
self * p = 0,
bool behind =
true )
154 DoReparentTo( *p, 0, behind );
159 tnode( n_value v,
self & p,
bool behind =
true )
167 DoReparentTo( p, 0, behind );
173 tnode( n_value v,
self & p,
self & s,
bool behind =
true )
181 DoReparentTo( p, &s, behind );
202 psibling->nsibling = nsibling;
204 parent->fchild = nsibling;
207 nsibling->psibling = psibling;
209 parent->lchild = psibling;
211 parent = psibling = nsibling = 0;
224 return DoReparentTo( p, 0, behind );
241 return DoReparentTo( p, &s, behind );
245 n_value & Value()
const {
return val; }
250 self * Parent() {
return parent; }
252 const self * Parent()
const {
return parent; }
270 const self *
Fchild()
const {
return fchild; }
276 const self *
Lchild()
const {
return lchild; }
278 bool HasParent()
const {
return parent; }
280 bool HasSiblings()
const {
return psibling || nsibling; }
282 bool HasChildren()
const {
return fchild; }
284 bool IsParentOf(
const self & c )
const {
return c.parent ==
this; }
286 bool IsSiblingOf(
const self & s )
const {
return parent && s.parent == parent; }
288 bool IsChildOf(
const self & p )
const {
return parent == &p; }
293 self * l =
const_cast<self *
>( this );
307 bool IsDescendantOf(
const self & n )
const
309 for (
const self * l = parent; l; l = l->parent )
318 bool IsDescendantOf(
const self * n )
const
320 return( n && IsDescendantOf( *n ) );
337 self *
Next(
bool restart =
false )
344 while ( !l->nsibling )
349 return restart ? l : 0;
355 self * Prev(
bool restart =
false )
357 if ( !psibling && parent )
360 if ( !psibling && !restart )
364 self * l = psibling ? psibling :
this;
373 self *
Next(
self *& c,
bool restart =
false )
375 return c =
Next( restart );
379 self *
Prev(
self *& c,
bool restart =
false )
381 return c = Prev( restart );
386 const self &
Top()
const
388 return const_cast<self *
>( this )->
Top();
391 const self *
Next(
bool restart =
false )
const
393 return const_cast<self *
>( this )->
Next( restart );
396 const self * Prev(
bool restart =
false )
const
398 return const_cast<self *
>( this )->Prev( restart );
401 const self *
Next(
const self *& c,
bool restart =
false )
const
403 return c =
const_cast<self *
>( this )->
Next( restart );
406 const self * Prev(
const self *& c,
bool restart =
false )
const
408 return c =
const_cast<self *
>( this )->Prev( restart );
self * Next(bool restart=false)
Next node: depth first, pre-order.
n_value & operator()() const
Alias for Value.
self * Fchild()
First child.
self & Top()
Root of the tree.
const self * Lchild() const
Last child.
void Disconnect()
Disconnect from the parent and siblings, but keep children.
self * Prev(self *&c, bool restart=false)
Return Prev and assign it to c.
tnode(n_value v, self *p=0, bool behind=true)
New node, added as the last child by default (which is natural).
bool ReparentTo(self &p, self &s, bool behind=true)
Disconnect from old parent, connect to new parent p and sibling s.
tnode(n_value v, self &p, self &s, bool behind=true)
New node under p, just after s (or before s if behind==false)
unsigned Depth() const
Depth: zero if no parent, otherwise 1 + parent's depth.
self * Nsibling()
Next sibling.
self * Lchild()
Last child.
const self * Nsibling() const
Next sibling.
self * Next(self *&c, bool restart=false)
Return Next and assign it to c.
const self * Fchild() const
First child.
tnode(n_value v, self &p, bool behind=true)
New node, added as the last child by default (which is natural).
bool ReparentTo(self &p, bool behind=true)
Disconnect from old parent, connect to new parent p.
self * Psibling()
Previous sibling.
const self * Psibling() const
Previous sibling.