unique_tree.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "ordered_iterator.h"
  24. #include <set>
  25. namespace tcl
  26. {
  27. // deref less for ordered children set
  28. template<typename tree_type, typename node_order_compare_type>
  29. struct deref_ordered_compare
  30. {
  31. bool operator() (const tree_type* lhs, const tree_type* rhs) const { return node_order_compare_type() (*lhs->get(), *rhs->get()); }
  32. };
  33. // forward declaration for deref comparison functor
  34. template<typename stored_type, typename node_compare_type, typename node_order_compare_type >
  35. class unique_tree;
  36. // deref comparison functor, derive from binary function per Scott Meyer
  37. template<typename stored_type, typename node_compare_type, typename node_order_compare_type >
  38. struct unique_tree_deref_less : public std::binary_function<const unique_tree<stored_type, node_compare_type, node_order_compare_type>*, const unique_tree<stored_type, node_compare_type, node_order_compare_type>*, bool>
  39. {
  40. bool operator () (const unique_tree<stored_type, node_compare_type, node_order_compare_type>* lhs, const unique_tree<stored_type, node_compare_type, node_order_compare_type>* rhs) const
  41. {
  42. // call < on actual object
  43. return node_compare_type()(*lhs->get(), *rhs->get());
  44. }
  45. };
  46. }
  47. // instanciates base_tree_type with type of container (set of unique_tree ptrs) to use for node and key comparisons
  48. template<typename stored_type, typename node_compare_type = std::less<stored_type>, typename node_order_compare_type = node_compare_type >
  49. class tcl::unique_tree : public tcl::associative_tree<stored_type, tcl::unique_tree<stored_type, node_compare_type, node_order_compare_type>, std::set<tcl::unique_tree<stored_type, node_compare_type, node_order_compare_type>*, tcl::unique_tree_deref_less<stored_type, node_compare_type, node_order_compare_type> > >
  50. {
  51. public:
  52. // typedefs
  53. typedef unique_tree<stored_type, node_compare_type, node_order_compare_type> tree_type;
  54. typedef unique_tree_deref_less<stored_type, node_compare_type, node_order_compare_type> key_compare;
  55. typedef unique_tree_deref_less<stored_type, node_compare_type, node_order_compare_type> value_compare;
  56. typedef std::set<tree_type*, key_compare> container_type;
  57. typedef basic_tree<stored_type, tree_type, container_type> basic_tree_type;
  58. typedef associative_tree<stored_type, tree_type, container_type> associative_tree_type;
  59. typedef typename associative_tree_type::iterator associative_iterator_type;
  60. typedef typename associative_tree_type::const_iterator associative_const_iterator_type;
  61. typedef const_unique_tree_ordered_iterator<stored_type, node_compare_type, node_order_compare_type> const_ordered_iterator;
  62. typedef unique_tree_ordered_iterator<stored_type, node_compare_type, node_order_compare_type> ordered_iterator;
  63. // needed for inl function definitions in VC8
  64. typedef typename associative_tree_type::iterator child_iterator;
  65. typedef typename associative_tree_type::const_iterator const_child_iterator;
  66. // constructors/destructor
  67. explicit unique_tree( const stored_type& value = stored_type() ) : associative_tree_type(value), pOrphans(0), allowing_orphans(false) {}
  68. unique_tree( const tree_type& rhs ); // copy constructor
  69. template<typename iterator_type> unique_tree(iterator_type it_beg, iterator_type it_end, const stored_type& value = stored_type()) : associative_tree_type(value), pOrphans(0), allowing_orphans(false) { while (it_beg != it_end) { insert(*it_beg); ++it_beg; } }
  70. ~unique_tree() { clear(); if ( pOrphans ) basic_tree_type::deallocate_tree_type(pOrphans); }
  71. // public interface
  72. public:
  73. tree_type& operator = (const tree_type& rhs); // assignment operator
  74. typename associative_tree_type::iterator insert(const stored_type& value);
  75. 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); }
  76. typename associative_tree_type::iterator insert(const tree_type& tree_obj );
  77. 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); }
  78. void swap(tree_type& rhs);
  79. typename associative_tree_type::iterator insert( const stored_type& parent_obj, const stored_type& value);
  80. #if !defined(_MSC_VER) || _MSC_VER >= 1300 // insert range not available for VC6
  81. template<typename iterator_type> void insert(iterator_type it_beg, iterator_type it_end) { while ( it_beg != it_end ) insert(*it_beg++); }
  82. #endif
  83. typename associative_tree_type::iterator find_deep(const stored_type& value);
  84. typename associative_tree_type::const_iterator find_deep(const stored_type& value) const;
  85. const_ordered_iterator ordered_begin() const { return const_ordered_iterator(ordered_children.begin()); }
  86. const_ordered_iterator ordered_end() const { return const_ordered_iterator(ordered_children.end()); }
  87. ordered_iterator ordered_begin() { return ordered_iterator(ordered_children.begin()); }
  88. ordered_iterator ordered_end() { return ordered_iterator(ordered_children.end()); }
  89. ordered_iterator find_ordered(const stored_type& value);
  90. const_ordered_iterator find_ordered(const stored_type& value) const;
  91. bool erase(const stored_type& value);
  92. void erase(associative_iterator_type it);
  93. void erase(associative_iterator_type it_beg, associative_iterator_type it_end);
  94. void clear();
  95. bool allow_orphans() const { return get_root()->allowing_orphans; }
  96. void allow_orphans(const bool allow) const { get_root()->allowing_orphans = allow; }
  97. const tree_type* get_orphans() const { return get_root()->pOrphans; }
  98. bool is_orphan() const { const tree_type* const root = get_root(); return (!root->empty() && root->ordered_children.empty()); }
  99. // descendant element iterator accessors
  100. typedef typename associative_tree_type::post_order_iterator post_order_iterator_type;
  101. typedef typename associative_tree_type::const_post_order_iterator const_post_order_iterator_type;
  102. typedef typename associative_tree_type::pre_order_iterator pre_order_iterator_type;
  103. typedef typename associative_tree_type::const_pre_order_iterator const_pre_order_iterator_type;
  104. typedef typename associative_tree_type::level_order_iterator level_order_iterator_type;
  105. typedef typename associative_tree_type::const_level_order_iterator const_level_order_iterator_type;
  106. pre_order_iterator_type pre_order_begin() { return pre_order_iterator_type(this, true); }
  107. pre_order_iterator_type pre_order_end() { return pre_order_iterator_type(this, false); }
  108. const_pre_order_iterator_type pre_order_begin() const { return const_pre_order_iterator_type(this, true); }
  109. const_pre_order_iterator_type pre_order_end() const { return const_pre_order_iterator_type(this, false); }
  110. post_order_iterator_type post_order_begin() { return post_order_iterator_type(this, true); }
  111. post_order_iterator_type post_order_end() { return post_order_iterator_type(this, false); }
  112. const_post_order_iterator_type post_order_begin() const { return const_post_order_iterator_type(this, true); }
  113. const_post_order_iterator_type post_order_end() const { return const_post_order_iterator_type(this, false); }
  114. level_order_iterator_type level_order_begin() { return level_order_iterator_type(this, true); }
  115. level_order_iterator_type level_order_end() { return level_order_iterator_type(this, false); }
  116. const_level_order_iterator_type level_order_begin() const { return const_level_order_iterator_type(this, true); }
  117. const_level_order_iterator_type level_order_end() const { return const_level_order_iterator_type(this, false); }
  118. // descendant node iterator accessors
  119. typedef typename associative_tree_type::pre_order_node_iterator pre_order_node_iterator_type;
  120. typedef typename associative_tree_type::const_pre_order_node_iterator const_pre_order_node_iterator_type;
  121. typedef typename associative_tree_type::post_order_node_iterator post_order_node_iterator_type;
  122. typedef typename associative_tree_type::const_post_order_node_iterator const_post_order_node_iterator_type;
  123. typedef typename associative_tree_type::level_order_node_iterator level_order_node_iterator_type;
  124. typedef typename associative_tree_type::const_level_order_node_iterator const_level_order_node_iterator_type;
  125. pre_order_node_iterator_type pre_order_node_begin() { return pre_order_node_iterator_type(this, true); }
  126. pre_order_node_iterator_type pre_order_node_end() { return pre_order_node_iterator_type(this, false); }
  127. const_pre_order_node_iterator_type pre_order_node_begin() const { return const_pre_order_node_iterator_type(this, true); }
  128. const_pre_order_node_iterator_type pre_order_node_end() const { return const_pre_order_node_iterator_type(this, false); }
  129. post_order_node_iterator_type post_order_node_begin() { return post_order_node_iterator_type(this, true); }
  130. post_order_node_iterator_type post_order_node_end() { return post_order_node_iterator_type(this, false); }
  131. const_post_order_node_iterator_type post_order_node_begin() const { return const_post_order_node_iterator_type(this, true); }
  132. const_post_order_node_iterator_type post_order_node_end() const { return const_post_order_node_iterator_type(this, false); }
  133. level_order_node_iterator_type level_order_node_begin() { return level_order_node_iterator_type(this, true); }
  134. level_order_node_iterator_type level_order_node_end() { return level_order_node_iterator_type(this, false); }
  135. const_level_order_node_iterator_type level_order_node_begin() const { return const_level_order_node_iterator_type(this, true); }
  136. const_level_order_node_iterator_type level_order_node_end() const { return const_level_order_node_iterator_type(this, false); }
  137. private:
  138. void set(const stored_type& value) { basic_tree_type::set(value); }
  139. void set(const tree_type& tree_obj);
  140. void inform_grandparents( tree_type* pNew_child, tree_type* pParent );
  141. bool check_for_duplicate(const stored_type& value, const tree_type* pParent) const;
  142. const tree_type* get_root() const;
  143. private:
  144. // data
  145. mutable std::set<tree_type*, key_compare > descendents;
  146. std::multiset<tree_type*, deref_ordered_compare<tree_type, node_order_compare_type> > ordered_children;
  147. mutable tree_type* pOrphans;
  148. mutable bool allowing_orphans;
  149. // friends
  150. #if defined(_MSC_VER) && _MSC_VER < 1300
  151. friend class basic_tree<stored_type, tree_type, container_type>;
  152. #else
  153. template<typename T, typename U, typename V> friend class basic_tree;
  154. #endif
  155. };
  156. #include "unique_tree.inl"