bptr_value.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Matei David 2014
  4. // (C) Copyright Ion Gaztanaga 2014
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // See http://www.boost.org/libs/intrusive for documentation.
  11. //
  12. /////////////////////////////////////////////////////////////////////////////
  13. #ifndef BOOST_INTRUSIVE_BPTR_VALUE_HPP
  14. #define BOOST_INTRUSIVE_BPTR_VALUE_HPP
  15. #include <cassert>
  16. #include <iostream>
  17. #include "bounded_pointer.hpp"
  18. #include "common_functors.hpp"
  19. #include "int_holder.hpp"
  20. #include <boost/intrusive/link_mode.hpp>
  21. namespace boost {
  22. namespace intrusive {
  23. struct BPtr_Value
  24. {
  25. static const bool constant_time_size = true;
  26. explicit BPtr_Value(int value = 42)
  27. : value_(value)
  28. {}
  29. BPtr_Value(const BPtr_Value& rhs)
  30. : value_(rhs.value_)
  31. {}
  32. ~BPtr_Value()
  33. {
  34. if (is_linked())
  35. {
  36. assert(false);
  37. }
  38. }
  39. // testvalue is used in boost::container::vector and thus prev and next
  40. // have to be handled appropriately when copied:
  41. BPtr_Value& operator = (const BPtr_Value& src)
  42. {
  43. if (is_linked())
  44. {
  45. assert(false);
  46. }
  47. value_ = src.value_;
  48. return *this;
  49. }
  50. // value
  51. int_holder value_;
  52. // list node hooks
  53. bounded_pointer< BPtr_Value > m_previous;
  54. bounded_pointer< BPtr_Value > m_next;
  55. // tree node hooks
  56. bounded_pointer< BPtr_Value > m_parent;
  57. bounded_pointer< BPtr_Value > m_l_child;
  58. bounded_pointer< BPtr_Value > m_r_child;
  59. signed char m_extra;
  60. const int_holder &get_int_holder() const
  61. { return value_; }
  62. int int_value() const
  63. { return value_.int_value(); }
  64. bool is_linked() const
  65. { return m_previous || m_next || m_parent || m_l_child || m_r_child; }
  66. friend bool operator< (const BPtr_Value &other1, const BPtr_Value &other2)
  67. { return other1.value_ < other2.value_; }
  68. friend bool operator< (int other1, const BPtr_Value &other2)
  69. { return other1 < other2.value_; }
  70. friend bool operator< (const BPtr_Value &other1, int other2)
  71. { return other1.value_ < other2; }
  72. friend bool operator> (const BPtr_Value &other1, const BPtr_Value &other2)
  73. { return other1.value_ > other2.value_; }
  74. friend bool operator> (int other1, const BPtr_Value &other2)
  75. { return other1 > other2.value_; }
  76. friend bool operator> (const BPtr_Value &other1, int other2)
  77. { return other1.value_ > other2; }
  78. friend bool operator== (const BPtr_Value &other1, const BPtr_Value &other2)
  79. { return other1.value_ == other2.value_; }
  80. friend bool operator== (int other1, const BPtr_Value &other2)
  81. { return other1 == other2.value_; }
  82. friend bool operator== (const BPtr_Value &other1, int other2)
  83. { return other1.value_ == other2; }
  84. friend bool operator!= (const BPtr_Value &other1, const BPtr_Value &other2)
  85. { return !(other1 == other2); }
  86. friend bool operator!= (int other1, const BPtr_Value &other2)
  87. { return !(other1 == other2.value_); }
  88. friend bool operator!= (const BPtr_Value &other1, int other2)
  89. { return !(other1.value_ == other2); }
  90. }; // class BPtr_Value
  91. std::ostream& operator<<(std::ostream& s, const BPtr_Value& t)
  92. { return s << t.value_.int_value(); }
  93. template < typename Node_Algorithms >
  94. void swap_nodes(bounded_reference< BPtr_Value > lhs, bounded_reference< BPtr_Value > rhs)
  95. {
  96. Node_Algorithms::swap_nodes(
  97. boost::intrusive::pointer_traits< bounded_pointer< BPtr_Value > >::pointer_to(lhs),
  98. boost::intrusive::pointer_traits< bounded_pointer< BPtr_Value > >::pointer_to(rhs));
  99. }
  100. struct List_BPtr_Node_Traits
  101. {
  102. typedef BPtr_Value val_t;
  103. typedef val_t node;
  104. typedef bounded_pointer< val_t > node_ptr;
  105. typedef bounded_pointer< const val_t > const_node_ptr;
  106. static node_ptr get_previous(const_node_ptr p) { return p->m_previous; }
  107. static void set_previous(node_ptr p, node_ptr prev) { p->m_previous = prev; }
  108. static node_ptr get_next(const_node_ptr p) { return p->m_next; }
  109. static void set_next(node_ptr p, node_ptr next) { p->m_next = next; }
  110. };
  111. struct Tree_BPtr_Node_Traits
  112. {
  113. typedef BPtr_Value val_t;
  114. typedef val_t node;
  115. typedef bounded_pointer< val_t > node_ptr;
  116. typedef bounded_pointer< const val_t > const_node_ptr;
  117. static node_ptr get_parent(const_node_ptr p) { return p->m_parent; }
  118. static void set_parent(node_ptr p, node_ptr parent) { p->m_parent = parent; }
  119. static node_ptr get_left(const_node_ptr p) { return p->m_l_child; }
  120. static void set_left(node_ptr p, node_ptr l_child) { p->m_l_child = l_child; }
  121. static node_ptr get_right(const_node_ptr p) { return p->m_r_child; }
  122. static void set_right(node_ptr p, node_ptr r_child) { p->m_r_child = r_child; }
  123. };
  124. struct RBTree_BPtr_Node_Traits
  125. : public Tree_BPtr_Node_Traits
  126. {
  127. typedef signed char color;
  128. typedef Tree_BPtr_Node_Traits::node_ptr node_ptr;
  129. typedef Tree_BPtr_Node_Traits::const_node_ptr const_node_ptr;
  130. static color get_color(const_node_ptr p) { return p->m_extra; }
  131. static void set_color(node_ptr p, color c) { p->m_extra = c; }
  132. static color black() { return 0; }
  133. static color red() { return 1; }
  134. };
  135. struct AVLTree_BPtr_Node_Traits
  136. : public Tree_BPtr_Node_Traits
  137. {
  138. typedef signed char balance;
  139. typedef Tree_BPtr_Node_Traits::node_ptr node_ptr;
  140. typedef Tree_BPtr_Node_Traits::const_node_ptr const_node_ptr;
  141. static balance get_balance(const_node_ptr p) { return p->m_extra; }
  142. static void set_balance(node_ptr p, balance b) { p->m_extra = b; }
  143. static balance negative() { return -1; }
  144. static balance zero() { return 0; }
  145. static balance positive() { return 1; }
  146. };
  147. template < typename NodeTraits >
  148. struct BPtr_Value_Traits
  149. {
  150. typedef NodeTraits node_traits;
  151. typedef typename node_traits::val_t value_type;
  152. typedef typename node_traits::node_ptr node_ptr;
  153. typedef typename node_traits::const_node_ptr const_node_ptr;
  154. typedef node_ptr pointer;
  155. typedef const_node_ptr const_pointer;
  156. typedef bounded_reference< value_type > reference;
  157. typedef bounded_reference< const value_type > const_reference;
  158. typedef BPtr_Value_Traits<NodeTraits> * value_traits_ptr;
  159. static const boost::intrusive::link_mode_type link_mode = boost::intrusive::safe_link;
  160. static const_node_ptr to_node_ptr(const_reference v) { return &v; }
  161. static node_ptr to_node_ptr(reference v) { return &v; }
  162. static const_pointer to_value_ptr(const_node_ptr p) { return p; }
  163. static pointer to_value_ptr(node_ptr p) { return p; }
  164. };
  165. template < typename >
  166. struct ValueContainer;
  167. template <>
  168. struct ValueContainer< BPtr_Value >
  169. {
  170. typedef bounded_reference_cont< BPtr_Value > type;
  171. };
  172. namespace test{
  173. template <>
  174. class new_cloner< BPtr_Value >
  175. {
  176. public:
  177. typedef BPtr_Value value_type;
  178. typedef bounded_pointer< value_type > pointer;
  179. typedef bounded_reference< const value_type > const_reference;
  180. typedef bounded_allocator< value_type > allocator_type;
  181. pointer operator () (const_reference r)
  182. {
  183. pointer p = allocator_type().allocate(1);
  184. new (p.raw()) value_type(r);
  185. return p;
  186. }
  187. };
  188. template <>
  189. class new_nonconst_cloner< BPtr_Value >
  190. {
  191. public:
  192. typedef BPtr_Value value_type;
  193. typedef bounded_pointer< value_type > pointer;
  194. typedef bounded_reference< value_type > reference;
  195. typedef bounded_allocator< value_type > allocator_type;
  196. pointer operator () (reference r)
  197. {
  198. pointer p = allocator_type().allocate(1);
  199. new (p.raw()) value_type(r);
  200. return p;
  201. }
  202. };
  203. template <>
  204. class delete_disposer< BPtr_Value >
  205. {
  206. public:
  207. typedef BPtr_Value value_type;
  208. typedef bounded_pointer< value_type > pointer;
  209. typedef bounded_allocator< value_type > allocator_type;
  210. void operator () (pointer p)
  211. {
  212. p->~value_type();
  213. allocator_type().deallocate(p, 1);
  214. }
  215. };
  216. } // namespace test
  217. } // namespace intrusive
  218. } // namespace boost
  219. #endif