sequential_tree.inl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*******************************************************************************
  2. Tree Container Library: Generic container library to store data in tree-like structures.
  3. Copyright (c) 2006 Mitchel Haas
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the author be held liable for any damages arising from
  6. the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it freely,
  9. subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented;
  11. you must not claim that you wrote the original software.
  12. If you use this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such,
  15. and must not be misrepresented as being the original software.
  16. 3. The above copyright notice and this permission notice may not be removed
  17. or altered from any source distribution.
  18. For complete documentation on this library, see http://www.datasoftsolutions.net
  19. Email questions, comments or suggestions to mhaas@datasoftsolutions.net
  20. *******************************************************************************/
  21. // copy constructor
  22. template<typename stored_type>
  23. tcl::sequential_tree<stored_type>::sequential_tree(const tree_type& rhs) : basic_tree_type(rhs)
  24. {
  25. const_iterator it = rhs.begin();
  26. const const_iterator it_end = rhs.end();
  27. for ( ; it != it_end; ++it ) // do a deep copy by inserting children (and descendants)
  28. {
  29. insert(*it.node());
  30. }
  31. }
  32. // assignment operator
  33. template<typename stored_type>
  34. tcl::sequential_tree<stored_type>& tcl::sequential_tree<stored_type>::operator = (const tree_type& rhs)
  35. {
  36. if ( this == &rhs ) // check for self assignment
  37. return *this;
  38. clear();
  39. basic_tree_type::operator =(rhs); // call base class operation
  40. const_iterator it = rhs.begin(), it_end = rhs.end();
  41. for ( ; it != it_end; ++it ) // insert children and descendants
  42. {
  43. insert(*it.node());
  44. }
  45. return *this;
  46. }
  47. // swap
  48. template<typename stored_type>
  49. void tcl::sequential_tree<stored_type>::swap(tree_type& rhs)
  50. {
  51. tree_type temp(*this);
  52. clear();
  53. *this = rhs;
  54. rhs.clear();
  55. rhs = temp;
  56. }
  57. // insert(const stored_type&)
  58. template< typename stored_type>
  59. typename tcl::sequential_tree<stored_type>::iterator
  60. tcl::sequential_tree<stored_type>::insert( const stored_type& value)
  61. {
  62. // create a new tree_type object to hold the node object
  63. tree_type* pNew_node;
  64. basic_tree_type::allocate_tree_type(pNew_node, tree_type(value));
  65. pNew_node->set_parent(this);
  66. const typename basic_tree_type::size_type sz = basic_tree_type::children.size();
  67. // insert the tree node into the children container
  68. const typename container_type::iterator it = basic_tree_type::children.insert(basic_tree_type::children.end(), pNew_node);
  69. if ( sz == basic_tree_type::children.size() ) { // check for successful insertion
  70. basic_tree_type::deallocate_tree_type(pNew_node); // not successful. delete new node and return end()
  71. return iterator(basic_tree_type::children.end(), this);
  72. }
  73. return iterator(it, this);
  74. }
  75. // insert(const tree_type&)
  76. template< typename stored_type>
  77. typename tcl::sequential_tree<stored_type>::iterator
  78. tcl::sequential_tree<stored_type>::insert(const tree_type& tree_obj)
  79. {
  80. // insert current node
  81. const iterator base_it = insert(*tree_obj.get());
  82. if ( base_it != end() ) {
  83. const_iterator it = tree_obj.begin();
  84. const const_iterator it_end = tree_obj.end();
  85. // call this function recursively thru derived tree for children
  86. for ( ; it != it_end; ++it )
  87. base_it.node()->insert(*it.node());
  88. }
  89. return base_it;
  90. }
  91. // push_back(const stored_type&)
  92. template< typename stored_type>
  93. void tcl::sequential_tree<stored_type>::push_back(const stored_type& value)
  94. {
  95. // create a new tree_type object to hold the node object
  96. tree_type* pNew_node;
  97. basic_tree_type::allocate_tree_type(pNew_node, tree_type(value));
  98. pNew_node->set_parent(this);
  99. basic_tree_type::children.push_back(pNew_node);
  100. }
  101. // insert(const_iterator, const stored_type&)
  102. template<typename stored_type>
  103. typename tcl::sequential_tree<stored_type>::iterator
  104. tcl::sequential_tree<stored_type>::insert(const_iterator pos, const stored_type& value)
  105. {
  106. // create a new tree_type object to hold the node object
  107. tree_type* pNew_node = 0;
  108. basic_tree_type::allocate_tree_type(pNew_node, tree_type(value));
  109. pNew_node->set_parent(this);
  110. const typename std::vector<stored_type>::size_type sz = basic_tree_type::children.size();
  111. // calculate the insertion point
  112. const const_iterator beg_it = begin();
  113. typename container_type::iterator pos_it = basic_tree_type::children.begin();
  114. for ( ; pos != beg_it; --pos, ++pos_it) ;
  115. // insert the tree node into the children container
  116. const typename container_type::iterator it = basic_tree_type::children.insert(pos_it, pNew_node);
  117. if ( sz == basic_tree_type::children.size() ) { // check for successful insertion
  118. basic_tree_type::deallocate_tree_type(pNew_node); // not successful. delete new node and return end()
  119. iterator end_it(basic_tree_type::children.end(), this);
  120. return end_it;
  121. }
  122. iterator node_it(it, this);
  123. return node_it;
  124. }
  125. // insert(const_iterator, size_type, const stored_type&)
  126. template<typename stored_type>
  127. void
  128. tcl::sequential_tree<stored_type>::insert(const_iterator pos, const typename basic_tree_type::size_type num, const stored_type& value)
  129. {
  130. for (typename basic_tree_type::size_type i = 0; i < num; ++i) {
  131. pos = insert(pos, value);
  132. ++pos;
  133. }
  134. }
  135. // insert(const_iterator, const tree_type&)
  136. template<typename stored_type>
  137. typename tcl::sequential_tree<stored_type>::iterator
  138. tcl::sequential_tree<stored_type>::insert(const const_iterator& pos, const tree_type& tree_obj)
  139. {
  140. // insert current node
  141. const iterator base_it = insert(pos, *tree_obj.get());
  142. if ( base_it != end() ) {
  143. const_iterator it = tree_obj.begin();
  144. const const_iterator it_end = tree_obj.end();
  145. // call this function recursively thru derived tree for children
  146. for ( ; it != it_end; ++it )
  147. base_it.node()->insert(*it.node());
  148. }
  149. return base_it;
  150. }
  151. // set(const tree_type&)
  152. template< typename stored_type >
  153. void tcl::sequential_tree<stored_type>::set(const sequential_tree<stored_type>& tree_obj)
  154. {
  155. set(*tree_obj.get()); // set data for this node
  156. const_iterator it = tree_obj.begin();
  157. const const_iterator it_end = tree_obj.end();
  158. for ( ; it != it_end; ++it ) { // and insert all descendants of passed tree
  159. insert(*it.node());
  160. }
  161. }
  162. // clear()
  163. template< typename stored_type >
  164. void tcl::sequential_tree<stored_type>::clear()
  165. {
  166. iterator it = begin();
  167. const iterator it_end = end();
  168. for ( ; it != it_end; ++it )
  169. {
  170. basic_tree_type::deallocate_tree_type(it.node()); // delete all child nodes
  171. }
  172. basic_tree_type::children.clear(); // and remove them from set
  173. }
  174. // erase(iterator)
  175. template<typename stored_type>
  176. typename tcl::sequential_tree<stored_type>::iterator
  177. tcl::sequential_tree<stored_type>::erase(iterator it)
  178. {
  179. // check for node presence
  180. if (it.pParent != this)
  181. return end();
  182. // clear children
  183. it.node()->clear();
  184. deallocate_tree_type(it.node());
  185. const iterator beg_it = begin();
  186. typename container_type::iterator pos_it = basic_tree_type::children.begin();
  187. for ( ; it != beg_it; --it, ++pos_it) ; // get child iterator position
  188. return iterator(basic_tree_type::children.erase(pos_it), this);
  189. }
  190. // erase(iterator, iterator)
  191. template<typename stored_type>
  192. typename tcl::sequential_tree<stored_type>::iterator
  193. tcl::sequential_tree<stored_type>::erase(iterator beg_it, iterator end_it)
  194. {
  195. int delete_count = 0;
  196. for (; beg_it != end_it; --end_it)
  197. ++delete_count;
  198. for (int i = 0; i < delete_count; ++i) {
  199. beg_it = erase(beg_it);
  200. }
  201. return beg_it;
  202. }
  203. // operator [](size_type)
  204. template<typename stored_type>
  205. tcl::sequential_tree<stored_type>& tcl::sequential_tree<stored_type>::operator [](basic_size_type index)
  206. {
  207. if (index >= basic_tree_type::size())
  208. throw std::out_of_range("sequential_tree index out of range");
  209. return *(begin() + index).node();
  210. }
  211. // operator [](size_type) const
  212. template<typename stored_type>
  213. const tcl::sequential_tree<stored_type>& tcl::sequential_tree<stored_type>::operator [](basic_size_type index) const
  214. {
  215. if (index >= basic_tree_type::size())
  216. throw std::out_of_range("sequential_tree index out of range");
  217. return *(begin() + index).node();
  218. }
  219. // operator ==
  220. template<typename stored_type>
  221. bool tcl::operator == (const sequential_tree<stored_type>& lhs, const sequential_tree<stored_type>& rhs)
  222. {
  223. // check this node
  224. if (!(*lhs.get() == *rhs.get()))
  225. return false;
  226. typename sequential_tree<stored_type>::const_iterator lhs_it = lhs.begin();
  227. const typename sequential_tree<stored_type>::const_iterator lhs_end = lhs.end();
  228. typename sequential_tree<stored_type>::const_iterator rhs_it = rhs.begin();
  229. const typename sequential_tree<stored_type>::const_iterator rhs_end = rhs.end();
  230. for ( ; lhs_it != lhs_end && rhs_it != rhs_end; ++lhs_it, ++rhs_it ) {
  231. if (!(*lhs_it.node() == *rhs_it.node())) {
  232. return false;
  233. }
  234. }
  235. if (lhs_it != lhs.end() || rhs_it != rhs.end())
  236. return false;
  237. return true;
  238. }
  239. // operator <
  240. template<typename stored_type>
  241. bool tcl::operator < (const sequential_tree<stored_type>& lhs, const sequential_tree<stored_type>& rhs)
  242. {
  243. // check this node
  244. if (*lhs.get() < *rhs.get())
  245. return true;
  246. typename sequential_tree<stored_type>::const_iterator lhs_it = lhs.begin();
  247. const typename sequential_tree<stored_type>::const_iterator lhs_end = lhs.end();
  248. typename sequential_tree<stored_type>::const_iterator rhs_it = rhs.begin();
  249. const typename sequential_tree<stored_type>::const_iterator rhs_end = rhs.end();
  250. for ( ; lhs_it != lhs_end && rhs_it != rhs_end; ++lhs_it, ++rhs_it ) {
  251. if (*lhs_it.node() < *rhs_it.node()) {
  252. return true;
  253. }
  254. }
  255. if (lhs.size() != rhs.size()) {
  256. return lhs.size() < rhs.size();
  257. }
  258. return false;
  259. }
  260. // sort_descendants()
  261. template<typename stored_type>
  262. void tcl::sequential_tree<stored_type>::sort_descendants()
  263. {
  264. post_order_iterator it = post_order_begin();
  265. const post_order_iterator it_end = post_order_end();
  266. for ( ; it != it_end; ++it )
  267. {
  268. it.node()->sort();
  269. }
  270. }