tree.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #pragma once
  22. #include "associative_tree.h"
  23. #include <set>
  24. namespace tcl
  25. {
  26. // forward declaration for deref comparison functor
  27. template<typename stored_type, typename node_compare_type > class tree;
  28. // deref comparison functor, derive from binary function per Scott Meyer
  29. template<typename stored_type, typename node_compare_type >
  30. struct tree_deref_less
  31. {
  32. bool operator () (const tree<stored_type, node_compare_type>* lhs, const tree<stored_type, node_compare_type>* rhs) const
  33. {
  34. // call < on actual object
  35. return node_compare_type()(*lhs->get(), *rhs->get());
  36. }
  37. };
  38. }
  39. // node object type. forwards most operations to base_tree_type,
  40. // instanciates base_tree_type with type of container (set of unique_tree ptrs) to use for node and key comparisons
  41. template<typename stored_type, typename node_compare_type = std::less<stored_type> >
  42. class tcl::tree : public tcl::associative_tree<stored_type, tcl::tree<stored_type, node_compare_type>, std::set<tcl::tree<stored_type, node_compare_type>*, tcl::tree_deref_less<stored_type, node_compare_type> > >
  43. {
  44. public:
  45. // typedefs
  46. typedef tree<stored_type, node_compare_type> tree_type;
  47. typedef tree_deref_less<stored_type, node_compare_type> key_compare;
  48. typedef tree_deref_less<stored_type, node_compare_type> value_compare;
  49. typedef std::set<tree_type*, key_compare> container_type;
  50. typedef basic_tree<stored_type, tree_type, container_type> basic_tree_type;
  51. typedef associative_tree<stored_type, tree_type, container_type> associative_tree_type;
  52. // constructors/destructor
  53. explicit tree( const stored_type& value = stored_type() ) : associative_tree_type(value) {}
  54. template<typename iterator_type> tree(iterator_type it_beg, iterator_type it_end, const stored_type& value = stored_type()) : associative_tree_type(value) { while (it_beg != it_end) { insert(*it_beg); ++it_beg; } }
  55. tree( const tree_type& rhs ); // copy constructor
  56. ~tree() { associative_tree_type::clear(); }
  57. // assignment operator
  58. tree_type& operator = (const tree_type& rhs);
  59. // public interface
  60. public:
  61. typename associative_tree_type::iterator insert(const stored_type& value) { return associative_tree_type::insert(value, this); }
  62. typename associative_tree_type::iterator insert(const typename associative_tree_type::const_iterator pos, const stored_type& value) { return associative_tree_type::insert(pos, value, this); }
  63. typename associative_tree_type::iterator insert(const tree_type& tree_obj ) { return associative_tree_type::insert(tree_obj, this); }
  64. typename associative_tree_type::iterator insert(const typename associative_tree_type::const_iterator pos, const tree_type& tree_obj) { return associative_tree_type::insert(pos, tree_obj, this); }
  65. #if !defined(_MSC_VER) || _MSC_VER >= 1300 // insert range not available for VC6
  66. template<typename iterator_type> void insert(iterator_type it_beg, iterator_type it_end) { while ( it_beg != it_end ) insert(*it_beg++); }
  67. #endif
  68. void swap(tree_type& rhs);
  69. // descendant element iterator accessors
  70. typedef typename associative_tree_type::post_order_iterator post_order_iterator_type;
  71. typedef typename associative_tree_type::const_post_order_iterator const_post_order_iterator_type;
  72. typedef typename associative_tree_type::pre_order_iterator pre_order_iterator_type;
  73. typedef typename associative_tree_type::const_pre_order_iterator const_pre_order_iterator_type;
  74. typedef typename associative_tree_type::level_order_iterator level_order_iterator_type;
  75. typedef typename associative_tree_type::const_level_order_iterator const_level_order_iterator_type;
  76. pre_order_iterator_type pre_order_begin() { return pre_order_iterator_type(this, true); }
  77. pre_order_iterator_type pre_order_end() { return pre_order_iterator_type(this, false); }
  78. const_pre_order_iterator_type pre_order_begin() const { return const_pre_order_iterator_type(this, true); }
  79. const_pre_order_iterator_type pre_order_end() const { return const_pre_order_iterator_type(this, false); }
  80. post_order_iterator_type post_order_begin() { return post_order_iterator_type(this, true); }
  81. post_order_iterator_type post_order_end() { return post_order_iterator_type(this, false); }
  82. const_post_order_iterator_type post_order_begin() const { return const_post_order_iterator_type(this, true); }
  83. const_post_order_iterator_type post_order_end() const { return const_post_order_iterator_type(this, false); }
  84. level_order_iterator_type level_order_begin() { return level_order_iterator_type(this, true); }
  85. level_order_iterator_type level_order_end() { return level_order_iterator_type(this, false); }
  86. const_level_order_iterator_type level_order_begin() const { return const_level_order_iterator_type(this, true); }
  87. const_level_order_iterator_type level_order_end() const { return const_level_order_iterator_type(this, false); }
  88. // descendant node iterator accessors
  89. typedef typename associative_tree_type::pre_order_node_iterator pre_order_node_iterator_type;
  90. typedef typename associative_tree_type::const_pre_order_node_iterator const_pre_order_node_iterator_type;
  91. typedef typename associative_tree_type::post_order_node_iterator post_order_node_iterator_type;
  92. typedef typename associative_tree_type::const_post_order_node_iterator const_post_order_node_iterator_type;
  93. typedef typename associative_tree_type::level_order_node_iterator level_order_node_iterator_type;
  94. typedef typename associative_tree_type::const_level_order_node_iterator const_level_order_node_iterator_type;
  95. pre_order_node_iterator_type pre_order_node_begin() { return pre_order_node_iterator_type(this, true); }
  96. pre_order_node_iterator_type pre_order_node_end() { return pre_order_node_iterator_type(this, false); }
  97. const_pre_order_node_iterator_type pre_order_node_begin() const { return const_pre_order_node_iterator_type(this, true); }
  98. const_pre_order_node_iterator_type pre_order_node_end() const { return const_pre_order_node_iterator_type(this, false); }
  99. post_order_node_iterator_type post_order_node_begin() { return post_order_node_iterator_type(this, true); }
  100. post_order_node_iterator_type post_order_node_end() { return post_order_node_iterator_type(this, false); }
  101. const_post_order_node_iterator_type post_order_node_begin() const { return const_post_order_node_iterator_type(this, true); }
  102. const_post_order_node_iterator_type post_order_node_end() const { return const_post_order_node_iterator_type(this, false); }
  103. level_order_node_iterator_type level_order_node_begin() { return level_order_node_iterator_type(this, true); }
  104. level_order_node_iterator_type level_order_node_end() { return level_order_node_iterator_type(this, false); }
  105. const_level_order_node_iterator_type level_order_node_begin() const { return const_level_order_node_iterator_type(this, true); }
  106. const_level_order_node_iterator_type level_order_node_end() const { return const_level_order_node_iterator_type(this, false); }
  107. // friends
  108. #if defined(_MSC_VER) && _MSC_VER < 1300
  109. friend class basic_tree<stored_type, tree_type, container_type>;
  110. #else
  111. template<typename T, typename U, typename V> friend class basic_tree;
  112. #endif
  113. };
  114. #include "tree.inl"