1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Olaf Krzikalla 2004-2006.
4 // (C) Copyright Ion Gaztanaga 2006-2013.
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)
10 // See http://www.boost.org/libs/intrusive for documentation.
12 /////////////////////////////////////////////////////////////////////////////
13 #ifndef BOOST_INTRUSIVE_DETAIL_ITESTVALUE_HPP
14 #define BOOST_INTRUSIVE_DETAIL_ITESTVALUE_HPP
17 #include <boost/intrusive/options.hpp>
18 #include <boost/functional/hash.hpp>
19 #include <boost/intrusive/pointer_traits.hpp>
20 #include "nonhook_node.hpp"
21 #include "int_holder.hpp"
22 #include <boost/intrusive/detail/get_value_traits.hpp>
23 #include <boost/container/vector.hpp>
28 struct testvalue_filler
36 , Hooks::base_hook_type
37 , Hooks::auto_base_hook_type
39 typename Hooks::member_hook_type node_;
40 typename Hooks::auto_member_hook_type auto_node_;
41 typename Hooks::nonhook_node_member_type nhn_member_;
48 explicit testvalue(int i)
52 testvalue (const testvalue& src)
56 // testvalue is used in boost::container::vector and thus prev and next
57 // have to be handled appropriately when copied:
58 testvalue & operator= (const testvalue& src)
60 Hooks::base_hook_type::operator=(static_cast<const typename Hooks::base_hook_type&>(src));
61 Hooks::auto_base_hook_type::operator=(static_cast<const typename Hooks::auto_base_hook_type&>(src));
62 this->node_ = src.node_;
63 this->auto_node_ = src.auto_node_;
64 this->nhn_member_ = src.nhn_member_;
69 void swap_nodes(testvalue &other)
71 Hooks::base_hook_type::swap_nodes(static_cast<typename Hooks::base_hook_type&>(other));
72 Hooks::auto_base_hook_type::swap_nodes(static_cast<typename Hooks::auto_base_hook_type&>(other));
73 node_.swap_nodes(other.node_);
74 auto_node_.swap_nodes(other.auto_node_);
75 nhn_member_.swap_nodes(other.nhn_member_);
78 bool is_linked() const
80 return Hooks::base_hook_type::is_linked() ||
81 Hooks::auto_base_hook_type::is_linked() ||
83 auto_node_.is_linked() ||
84 nhn_member_.is_linked();
87 const int_holder &get_int_holder() const
91 { return value_.int_value(); }
96 bool operator< (const testvalue &other) const
97 { return value_ < other.value_; }
99 bool operator> (const testvalue &other) const
100 { return value_ > other.value_; }
102 bool operator==(const testvalue &other) const
103 { return value_ == other.value_; }
105 bool operator!=(const testvalue &other) const
106 { return value_ != other.value_; }
108 friend bool operator< (int other1, const testvalue &other2)
109 { return other1 < other2.value_.int_; }
111 friend bool operator> (int other1, const testvalue &other2)
112 { return other1 > other2.value_.int_; }
114 friend bool operator< (const testvalue &other1, int other2)
115 { return other1.value_.int_ < other2; }
117 friend bool operator> (const testvalue &other1, int other2)
118 { return other1.value_.int_ > other2; }
120 friend bool operator== (int other1, const testvalue &other2)
121 { return other1 == other2.value_.int_; }
123 friend bool operator== (const testvalue &other1, int other2)
124 { return other1.value_.int_ == other2; }
126 friend bool operator!= (int other1, const testvalue &other2)
127 { return other1 != other2.value_.int_; }
129 friend bool operator!= (const testvalue &other1, int other2)
130 { return other1.value_.int_ != other2; }
132 friend std::size_t hash_value(const testvalue&t)
134 boost::hash<int> hasher;
135 return hasher((&t)->int_value());
140 std::size_t priority_hash(const T &t)
141 { return boost::hash<int>()((&t)->int_value()); }
143 std::size_t priority_hash(int i)
144 { return boost::hash<int>()(i); }
146 template <class T, class U>
147 bool priority_order(const T& t1, const U& t2)
149 std::size_t hash1 = (priority_hash)(t1);
150 boost::hash_combine(hash1, -hash1);
151 std::size_t hash2 = (priority_hash)(t2);
152 boost::hash_combine(hash2, -hash2);
153 return hash1 < hash2;
156 template < typename Node_Algorithms, class Hooks>
157 void swap_nodes(testvalue<Hooks>& lhs, testvalue<Hooks>& rhs)
162 template<class Hooks>
163 std::ostream& operator<<
164 (std::ostream& s, const testvalue<Hooks>& t)
165 { return s << t.value_.int_value(); }
169 template < typename key_type_1, typename key_type_2 >
171 (const key_type_1& v1
172 ,const key_type_2& v2) const
174 if (((&v1)->int_value() & 1) == ((&v2)->int_value() & 1))
175 return (&v1)->int_value() < (&v2)->int_value();
177 return (&v2)->int_value() & 1;
183 template <typename key_type>
185 (const key_type& v1) const
186 { return ((&v1)->int_value() & 1) == 0; }
191 template <typename key_type>
193 (const key_type& v1) const
194 { return ((&v1)->int_value() & 1) != 0; }
198 struct ValueContainer;
200 template < class Hooks>
201 struct ValueContainer< testvalue<Hooks> >
203 typedef boost::container::vector< testvalue<Hooks> > type;
206 template < typename Pointer >
207 class heap_node_holder
209 typedef typename pointer_traits<Pointer>::element_type element_type;
210 typedef Pointer pointer;
211 typedef typename pointer_rebind<pointer, const element_type>::type const_pointer;
215 : m_ptr(pointer_traits<Pointer>::pointer_to(*new element_type))
221 const_pointer get_node() const
231 template<class Hooks>
232 struct testvalue_traits
235 typedef testvalue< Hooks > value_type;
238 typedef typename detail::get_base_value_traits
240 , typename Hooks::base_hook_type
241 >::type base_value_traits;
243 typedef typename detail::get_base_value_traits
245 , typename Hooks::auto_base_hook_type
246 >::type auto_base_value_traits;
248 typedef typename detail::get_member_value_traits
251 , typename Hooks::member_hook_type
254 >::type member_value_traits;
256 typedef typename detail::get_member_value_traits
259 , typename Hooks::auto_member_hook_type
260 , &value_type::auto_node_
262 >::type auto_member_value_traits;
264 typedef nonhook_node_member_value_traits
266 , typename Hooks::nonhook_node_member_type
267 , &value_type::nhn_member_
269 > nonhook_value_traits;
272 } //namespace intrusive{
275 bool priority_order(int t1, int t2)
277 std::size_t hash1 = boost::hash<int>()(t1);
278 boost::hash_combine(hash1, &t1);
279 std::size_t hash2 = boost::hash<int>()(t2);
280 boost::hash_combine(hash2, &t2);
281 return hash1 < hash2;