Tree.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #pragma once
  2. #include "Array.h"
  3. namespace Framework
  4. {
  5. template< class T >
  6. class TreeIterator;
  7. template< class T >
  8. class Tree
  9. {
  10. private:
  11. T *value;
  12. RCArray< Tree > *subtrees;
  13. Tree *zParent;
  14. int ref;
  15. public:
  16. Tree()
  17. {
  18. value = 0;
  19. zParent = 0;
  20. subtrees = new RCArray< Tree >();
  21. ref = 1;
  22. }
  23. Tree( T *value )
  24. {
  25. zParent = 0;
  26. this->value = value;
  27. subtrees = new RCArray< Tree >();
  28. ref = 1;
  29. }
  30. ~Tree()
  31. {
  32. if( value )
  33. value->release();
  34. subtrees->release();
  35. }
  36. TreeIterator<T> getIterator()
  37. {
  38. return TreeIterator( this );
  39. }
  40. void leeren()
  41. {
  42. if( value )
  43. value->release();
  44. value = 0;
  45. subtrees->leeren();
  46. }
  47. void addSubtree( Tree *t )
  48. {
  49. t->zParent = this;
  50. subtrees->add( t );
  51. }
  52. void setValue( T *v )
  53. {
  54. if( value )
  55. value->release();
  56. value = v;
  57. }
  58. T *getValue()
  59. {
  60. return value ? value->getThis() : 0;
  61. }
  62. T *zValue()
  63. {
  64. return value;
  65. }
  66. Tree *getParent()
  67. {
  68. return zParent ? zParent->getThis() : 0;
  69. }
  70. Tree *zParent()
  71. {
  72. return zParent;
  73. }
  74. Tree *getThis()
  75. {
  76. ref++;
  77. return this;
  78. }
  79. Tree *release()
  80. {
  81. if( !--ref )
  82. delete this;
  83. return 0;
  84. }
  85. friend TreeIterator;
  86. };
  87. template< class T >
  88. class TreeIterator
  89. {
  90. private:
  91. Tree *current;
  92. int nextSub;
  93. public:
  94. TreeIterator( Tree *tree )
  95. {
  96. current = tree;
  97. nextSub = 0;
  98. }
  99. TreeIterator( const TreeIterator &it )
  100. {
  101. current = it.current;
  102. nextSub = it.nextSub;
  103. }
  104. TreeIterator< T > &operator=( TreeIterator< T > &r )
  105. {
  106. current = r.current;
  107. nextSub = it.nextSub;
  108. return *this;
  109. }
  110. bool hasNext()
  111. {
  112. if( current->subtrees->getEintragAnzahl() >= nextSub )
  113. return 1;
  114. Tree<T> *c = current;
  115. while( true )
  116. {
  117. Tree<T> *p = c->zParent();
  118. if( !p )
  119. {
  120. return 0;
  121. }
  122. for( auto i = p->subtrees->getIterator(); i; i++ )
  123. {
  124. if( i._ == c )
  125. {
  126. i++;
  127. if( i )
  128. return 1;
  129. else
  130. {
  131. c = p;
  132. break;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. TreeIterator< T > next()
  139. {
  140. if( current->subtrees->getEintragAnzahl() >= nextSub )
  141. return TreeIterator( current->subtrees->z( nextSub++ ) );
  142. Tree<T> *c = current;
  143. while( true )
  144. {
  145. Tree<T> *p = c->zParent();
  146. if( !p )
  147. return TreeIterator<T>( 0 );
  148. for( auto i = p->subtrees->getIterator(); i; i++ )
  149. {
  150. if( i._ == c )
  151. {
  152. i++;
  153. if( i )
  154. return TreeIterator<T>( i._ );
  155. else
  156. {
  157. c = p;
  158. break;
  159. }
  160. }
  161. }
  162. }
  163. }
  164. operator bool()
  165. {
  166. return current != 0;
  167. }
  168. operator T*()
  169. {
  170. return current->zValue();
  171. }
  172. TreeIterator< T > &operator++() // prefix
  173. {
  174. TreeIterator< T > temp( *this );
  175. *this = next();
  176. return temp;
  177. }
  178. TreeIterator< T > &operator++( int ) // postfix
  179. {
  180. *this = next();
  181. return *this;
  182. }
  183. T *operator->()
  184. {
  185. return current->zValue();
  186. }
  187. T val()
  188. {
  189. return current->zValue();
  190. }
  191. };
  192. }