31 template <
class n_value>
class tnode
52 bool DoReparentTo(
self & p,
self * s,
const bool behind )
55 if ( &p ==
this || p.IsDescendantOf(
this ) )
64 if ( !s || s->parent != parent )
65 s = behind ? parent->lchild : parent->fchild;
70 parent->fchild = parent->lchild =
this;
77 nsibling = s->nsibling;
81 nsibling->psibling =
this;
83 parent->lchild =
this;
87 psibling = s->psibling;
92 psibling->nsibling =
this;
94 parent->fchild =
this;
105 virtual void PreDisconnect() {}
107 virtual void PostDisconnect() {}
109 virtual void PreReparent() {}
111 virtual void PostReparent() {}
115 tnode( n_value v,
self * p = 0,
const bool behind =
true )
124 DoReparentTo( *p, 0, behind );
127 tnode( n_value v,
self & p,
const bool behind =
true )
135 DoReparentTo( p, 0, behind );
138 tnode( n_value v,
self & p,
self & s,
const bool behind =
true )
146 DoReparentTo( p, &s, behind );
153 fchild->Disconnect();
167 psibling->nsibling = nsibling;
169 parent->fchild = nsibling;
172 nsibling->psibling = psibling;
174 parent->lchild = psibling;
176 parent = psibling = nsibling = 0;
181 bool ReparentTo(
self & p,
const bool behind =
true )
183 return DoReparentTo( p, 0, behind );
186 bool ReparentTo(
self & p,
self & s,
const bool behind =
true )
188 return DoReparentTo( p, &s, behind );
192 n_value & Value()
const {
return val; }
194 n_value & operator()()
const {
return Value(); }
196 self * Parent() {
return parent; }
198 const self * Parent()
const {
return parent; }
200 self * Psibling() {
return psibling; }
202 const self * Psibling()
const {
return psibling; }
204 self * Nsibling() {
return nsibling; }
206 const self * Nsibling()
const {
return nsibling; }
208 self * Fchild() {
return fchild; }
210 const self * Fchild()
const {
return fchild; }
212 self * Lchild() {
return lchild; }
214 const self * Lchild()
const {
return lchild; }
216 bool HasParent()
const {
return parent; }
218 bool HasSiblings()
const {
return psibling || nsibling; }
220 bool HasChildren()
const {
return fchild; }
222 bool IsParentOf(
const self & c )
const {
return c.parent ==
this; }
224 bool IsSiblingOf(
const self & s )
const {
return parent && s.parent == parent; }
226 bool IsChildOf(
const self & p )
const {
return parent == &p; }
228 unsigned Depth()
const
230 self * l =
const_cast<self *
>( this );
244 bool IsDescendantOf(
const self & n )
const
246 for (
const self * l = parent; l; l = l->parent )
255 bool IsDescendantOf(
const self * n )
const
257 return( n && IsDescendantOf( *n ) );
270 self * Next(
const bool restart =
false )
277 while ( !l->nsibling )
282 return restart ? l : 0;
288 self * Prev(
const bool restart =
false )
290 if ( !psibling && parent )
293 if ( !psibling && !restart )
297 self * l = psibling ? psibling :
this;
305 self * Next(
self *& c,
const bool restart =
false )
307 return c = Next( restart );
310 self * Prev(
self *& c,
const bool restart =
false )
312 return c = Prev( restart );
317 const self & Top()
const
319 return const_cast<self *
>( this )->Top();
322 const self * Next(
const bool restart =
false )
const
324 return const_cast<self *
>( this )->Next( restart );
327 const self * Prev(
const bool restart =
false )
const
329 return const_cast<self *
>( this )->Prev( restart );
332 const self * Next(
const self *& c,
const bool restart =
false )
const
334 return c =
const_cast<self *
>( this )->Next( restart );
337 const self * Prev(
const self *& c,
const bool restart =
false )
const
339 return c =
const_cast<self *
>( this )->Prev( restart );