1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Olaf Krzikalla 2004-2006.
// (C) Copyright Ion Gaztanaga 2006-2013.
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/intrusive for documentation.
//
/////////////////////////////////////////////////////////////////////////////
#ifndef BOOST_INTRUSIVE_DETAIL_ITESTVALUE_HPP
#define BOOST_INTRUSIVE_DETAIL_ITESTVALUE_HPP
#include <iostream>
#include <boost/intrusive/options.hpp>
#include <boost/functional/hash.hpp>
#include <boost/intrusive/pointer_traits.hpp>
#include "nonhook_node.hpp"
#include "int_holder.hpp"
#include <boost/intrusive/detail/get_value_traits.hpp>
#include <boost/container/vector.hpp>
namespace boost{
namespace intrusive{
struct testvalue_filler
{
void *dummy_[3];
};
template<class Hooks>
struct testvalue
: testvalue_filler
, Hooks::base_hook_type
, Hooks::auto_base_hook_type
{
typename Hooks::member_hook_type node_;
typename Hooks::auto_member_hook_type auto_node_;
typename Hooks::nonhook_node_member_type nhn_member_;
int_holder value_;
testvalue()
{}
explicit testvalue(int i)
: value_(i)
{}
testvalue (const testvalue& src)
: value_(src.value_)
{}
// testvalue is used in boost::container::vector and thus prev and next
// have to be handled appropriately when copied:
testvalue & operator= (const testvalue& src)
{
Hooks::base_hook_type::operator=(static_cast<const typename Hooks::base_hook_type&>(src));
Hooks::auto_base_hook_type::operator=(static_cast<const typename Hooks::auto_base_hook_type&>(src));
this->node_ = src.node_;
this->auto_node_ = src.auto_node_;
this->nhn_member_ = src.nhn_member_;
value_ = src.value_;
return *this;
}
void swap_nodes(testvalue &other)
{
Hooks::base_hook_type::swap_nodes(static_cast<typename Hooks::base_hook_type&>(other));
Hooks::auto_base_hook_type::swap_nodes(static_cast<typename Hooks::auto_base_hook_type&>(other));
node_.swap_nodes(other.node_);
auto_node_.swap_nodes(other.auto_node_);
nhn_member_.swap_nodes(other.nhn_member_);
}
bool is_linked() const
{
return Hooks::base_hook_type::is_linked() ||
Hooks::auto_base_hook_type::is_linked() ||
node_.is_linked() ||
auto_node_.is_linked() ||
nhn_member_.is_linked();
}
const int_holder &get_int_holder() const
{ return value_; }
int int_value() const
{ return value_.int_value(); }
~testvalue()
{}
bool operator< (const testvalue &other) const
{ return value_ < other.value_; }
bool operator> (const testvalue &other) const
{ return value_ > other.value_; }
bool operator==(const testvalue &other) const
{ return value_ == other.value_; }
bool operator!=(const testvalue &other) const
{ return value_ != other.value_; }
friend bool operator< (int other1, const testvalue &other2)
{ return other1 < other2.value_.int_; }
friend bool operator> (int other1, const testvalue &other2)
{ return other1 > other2.value_.int_; }
friend bool operator< (const testvalue &other1, int other2)
{ return other1.value_.int_ < other2; }
friend bool operator> (const testvalue &other1, int other2)
{ return other1.value_.int_ > other2; }
friend bool operator== (int other1, const testvalue &other2)
{ return other1 == other2.value_.int_; }
friend bool operator== (const testvalue &other1, int other2)
{ return other1.value_.int_ == other2; }
friend bool operator!= (int other1, const testvalue &other2)
{ return other1 != other2.value_.int_; }
friend bool operator!= (const testvalue &other1, int other2)
{ return other1.value_.int_ != other2; }
friend std::size_t hash_value(const testvalue&t)
{
boost::hash<int> hasher;
return hasher((&t)->int_value());
}
};
template<class T>
std::size_t priority_hash(const T &t)
{ return boost::hash<int>()((&t)->int_value()); }
std::size_t priority_hash(int i)
{ return boost::hash<int>()(i); }
template <class T, class U>
bool priority_order(const T& t1, const U& t2)
{
std::size_t hash1 = (priority_hash)(t1);
boost::hash_combine(hash1, -hash1);
std::size_t hash2 = (priority_hash)(t2);
boost::hash_combine(hash2, -hash2);
return hash1 < hash2;
}
template < typename Node_Algorithms, class Hooks>
void swap_nodes(testvalue<Hooks>& lhs, testvalue<Hooks>& rhs)
{
lhs.swap_nodes(rhs);
}
template<class Hooks>
std::ostream& operator<<
(std::ostream& s, const testvalue<Hooks>& t)
{ return s << t.value_.int_value(); }
struct even_odd
{
template < typename key_type_1, typename key_type_2 >
bool operator()
(const key_type_1& v1
,const key_type_2& v2) const
{
if (((&v1)->int_value() & 1) == ((&v2)->int_value() & 1))
return (&v1)->int_value() < (&v2)->int_value();
else
return (&v2)->int_value() & 1;
}
};
struct is_even
{
template <typename key_type>
bool operator()
(const key_type& v1) const
{ return ((&v1)->int_value() & 1) == 0; }
};
struct is_odd
{
template <typename key_type>
bool operator()
(const key_type& v1) const
{ return ((&v1)->int_value() & 1) != 0; }
};
template <typename>
struct ValueContainer;
template < class Hooks>
struct ValueContainer< testvalue<Hooks> >
{
typedef boost::container::vector< testvalue<Hooks> > type;
};
template < typename Pointer >
class heap_node_holder
{
typedef typename pointer_traits<Pointer>::element_type element_type;
typedef Pointer pointer;
typedef typename pointer_rebind<pointer, const element_type>::type const_pointer;
public:
heap_node_holder()
: m_ptr(pointer_traits<Pointer>::pointer_to(*new element_type))
{}
~heap_node_holder()
{ delete &*m_ptr; }
const_pointer get_node() const
{ return m_ptr; }
pointer get_node()
{ return m_ptr; }
private:
pointer m_ptr;
};
template<class Hooks>
struct testvalue_traits
: public Hooks
{
typedef testvalue< Hooks > value_type;
//base
typedef typename detail::get_base_value_traits
< value_type
, typename Hooks::base_hook_type
>::type base_value_traits;
//auto-base
typedef typename detail::get_base_value_traits
< value_type
, typename Hooks::auto_base_hook_type
>::type auto_base_value_traits;
//member
typedef typename detail::get_member_value_traits
< member_hook
< value_type
, typename Hooks::member_hook_type
, &value_type::node_
>
>::type member_value_traits;
//auto-member
typedef typename detail::get_member_value_traits
< member_hook
< value_type
, typename Hooks::auto_member_hook_type
, &value_type::auto_node_
>
>::type auto_member_value_traits;
//nonmember
typedef nonhook_node_member_value_traits
< value_type
, typename Hooks::nonhook_node_member_type
, &value_type::nhn_member_
, safe_link
> nonhook_value_traits;
};
} //namespace intrusive{
} //namespace boost{
bool priority_order(int t1, int t2)
{
std::size_t hash1 = boost::hash<int>()(t1);
boost::hash_combine(hash1, &t1);
std::size_t hash2 = boost::hash<int>()(t2);
boost::hash_combine(hash2, &t2);
return hash1 < hash2;
}
#endif
|