///////////////////////////////////////////////////////////////////////////// // // (C) Copyright Ion Gaztanaga 2006-2009 // // 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_UTILITIES_HPP #define BOOST_INTRUSIVE_DETAIL_UTILITIES_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace intrusive { namespace detail { template struct internal_member_value_traits { template static detail::one test(...); template static detail::two test(typename U::member_value_traits* = 0); static const bool value = sizeof(test(0)) == sizeof(detail::two); }; template struct internal_base_hook_bool { template struct two_or_three {one _[2 + Add];}; template static one test(...); template static two_or_three test (int); static const std::size_t value = sizeof(test(0)); }; template struct internal_base_hook_bool_is_true { static const bool value = internal_base_hook_bool::value > sizeof(one)*2; }; template struct internal_any_hook_bool { template struct two_or_three {one _[2 + Add];}; template static one test(...); template static two_or_three test (int); static const std::size_t value = sizeof(test(0)); }; template struct internal_any_hook_bool_is_true { static const bool value = internal_any_hook_bool::value > sizeof(one)*2; }; template struct external_value_traits_bool { template struct two_or_three {one _[2 + Add];}; template static one test(...); template static two_or_three test (int); static const std::size_t value = sizeof(test(0)); }; template struct external_bucket_traits_bool { template struct two_or_three {one _[2 + Add];}; template static one test(...); template static two_or_three test (int); static const std::size_t value = sizeof(test(0)); }; template struct external_value_traits_is_true { static const bool value = external_value_traits_bool::value > sizeof(one)*2; }; template struct node_holder : public Node {}; template struct smart_ptr_type { typedef typename SmartPtr::value_type value_type; typedef value_type *pointer; static pointer get (const SmartPtr &smartptr) { return smartptr.get();} }; template struct smart_ptr_type { typedef T value_type; typedef value_type *pointer; static pointer get (pointer ptr) { return ptr;} }; //!Overload for smart pointers to avoid ADL problems with get_pointer template inline typename smart_ptr_type::pointer get_pointer(const Ptr &ptr) { return smart_ptr_type::get(ptr); } //This functor compares a stored value //and the one passed as an argument template class equal_to_value { ConstReference t_; public: equal_to_value(ConstReference t) : t_(t) {} bool operator()(ConstReference t)const { return t_ == t; } }; class null_disposer { public: template void operator()(Pointer) {} }; template class init_disposer { typedef typename NodeAlgorithms::node_ptr node_ptr; public: void operator()(node_ptr p) { NodeAlgorithms::init(p); } }; template struct size_holder { static const bool constant_time_size = ConstantSize; typedef SizeType size_type; SizeType get_size() const { return size_; } void set_size(SizeType size) { size_ = size; } void decrement() { --size_; } void increment() { ++size_; } SizeType size_; }; template struct size_holder { static const bool constant_time_size = false; typedef SizeType size_type; size_type get_size() const { return 0; } void set_size(size_type) {} void decrement() {} void increment() {} }; template struct key_nodeptr_comp : private detail::ebo_functor_holder { typedef typename Container::real_value_traits real_value_traits; typedef typename real_value_traits::node_ptr node_ptr; typedef typename real_value_traits::const_node_ptr const_node_ptr; typedef detail::ebo_functor_holder base_t; key_nodeptr_comp(KeyValueCompare kcomp, const Container *cont) : base_t(kcomp), cont_(cont) {} template bool operator()( const_node_ptr node, const KeyType &key , typename enable_if_c ::value>::type * = 0) const { return base_t::get()(*cont_->get_real_value_traits().to_value_ptr(node), key); } template bool operator()(const KeyType &key, const_node_ptr node , typename enable_if_c ::value>::type * = 0) const { return base_t::get()(key, *cont_->get_real_value_traits().to_value_ptr(node)); } bool operator()(const_node_ptr node1, const_node_ptr node2) const { return base_t::get() ( *cont_->get_real_value_traits().to_value_ptr(node1) , *cont_->get_real_value_traits().to_value_ptr(node2) ); } const Container *cont_; }; template struct node_cloner : private detail::ebo_functor_holder { typedef typename Container::real_value_traits real_value_traits; typedef typename Container::node_algorithms node_algorithms; typedef typename real_value_traits::value_type value_type; typedef typename real_value_traits::pointer pointer; typedef typename real_value_traits::node_traits::node node; typedef typename real_value_traits::node_ptr node_ptr; typedef typename real_value_traits::const_node_ptr const_node_ptr; typedef detail::ebo_functor_holder base_t; enum { safemode_or_autounlink = (int)real_value_traits::link_mode == (int)auto_unlink || (int)real_value_traits::link_mode == (int)safe_link }; node_cloner(F f, const Container *cont) : base_t(f), cont_(cont) {} node_ptr operator()(node_ptr p) { return this->operator()(*p); } node_ptr operator()(const node &to_clone) { const value_type &v = *cont_->get_real_value_traits().to_value_ptr(const_node_ptr(&to_clone)); node_ptr n = cont_->get_real_value_traits().to_node_ptr(*base_t::get()(v)); //Cloned node must be in default mode if the linking mode requires it if(safemode_or_autounlink) BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(n)); return n; } const Container *cont_; }; template struct node_disposer : private detail::ebo_functor_holder { typedef typename Container::real_value_traits real_value_traits; typedef typename real_value_traits::node_ptr node_ptr; typedef detail::ebo_functor_holder base_t; typedef typename Container::node_algorithms node_algorithms; enum { safemode_or_autounlink = (int)real_value_traits::link_mode == (int)auto_unlink || (int)real_value_traits::link_mode == (int)safe_link }; node_disposer(F f, const Container *cont) : base_t(f), cont_(cont) {} void operator()(node_ptr p) { if(safemode_or_autounlink) node_algorithms::init(p); base_t::get()(cont_->get_real_value_traits().to_value_ptr(p)); } const Container *cont_; }; struct dummy_constptr { dummy_constptr(const void *) {} const void *get_ptr() const { return 0; } }; template struct constptr { typedef typename boost::pointer_to_other ::type ConstVoidPtr; constptr(const void *ptr) : const_void_ptr_(ptr) {} const void *get_ptr() const { return detail::get_pointer(const_void_ptr_); } ConstVoidPtr const_void_ptr_; }; template struct select_constptr { typedef typename detail::if_c < store_ptr , constptr , dummy_constptr >::type type; }; template struct add_const_if_c { typedef typename detail::if_c < Add , typename detail::add_const::type , T >::type type; }; template struct link_dispatch {}; template void destructor_impl(Hook &hook, detail::link_dispatch) { //If this assertion raises, you might have destroyed an object //while it was still inserted in a container that is alive. //If so, remove the object from the container before destroying it. (void)hook; BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT(!hook.is_linked()); } template void destructor_impl(Hook &hook, detail::link_dispatch) { hook.unlink(); } template void destructor_impl(Hook &, detail::link_dispatch) {} template struct base_hook_traits { public: typedef detail::node_holder node_holder; typedef NodeTraits node_traits; typedef T value_type; typedef typename node_traits::node_ptr node_ptr; typedef typename node_traits::const_node_ptr const_node_ptr; typedef typename boost::pointer_to_other::type pointer; typedef typename boost::pointer_to_other::type const_pointer; typedef typename std::iterator_traits::reference reference; typedef typename std::iterator_traits::reference const_reference; static const link_mode_type link_mode = LinkMode; static node_ptr to_node_ptr(reference value) { return static_cast(&value); } static const_node_ptr to_node_ptr(const_reference value) { return static_cast(&value); } static pointer to_value_ptr(node_ptr n) { return static_cast(static_cast(&*n)); } static const_pointer to_value_ptr(const_node_ptr n) { return static_cast(static_cast(&*n)); } }; template struct member_hook_traits { public: typedef Hook hook_type; typedef typename hook_type::boost_intrusive_tags::node_traits node_traits; typedef typename node_traits::node node; typedef T value_type; typedef typename node_traits::node_ptr node_ptr; typedef typename node_traits::const_node_ptr const_node_ptr; typedef typename boost::pointer_to_other::type pointer; typedef typename boost::pointer_to_other::type const_pointer; typedef typename std::iterator_traits::reference reference; typedef typename std::iterator_traits::reference const_reference; static const link_mode_type link_mode = Hook::boost_intrusive_tags::link_mode; static node_ptr to_node_ptr(reference value) { return reinterpret_cast(&(value.*P)); } static const_node_ptr to_node_ptr(const_reference value) { return static_cast(&(value.*P)); } static pointer to_value_ptr(node_ptr n) { return detail::parent_from_member (static_cast(detail::get_pointer(n)), P); } static const_pointer to_value_ptr(const_node_ptr n) { return detail::parent_from_member (static_cast(detail::get_pointer(n)), P); } }; //This function uses binary search to discover the //highest set bit of the integer inline std::size_t floor_log2 (std::size_t x) { const std::size_t Bits = sizeof(std::size_t)*CHAR_BIT; const bool Size_t_Bits_Power_2= !(Bits & (Bits-1)); BOOST_STATIC_ASSERT(Size_t_Bits_Power_2); std::size_t n = x; std::size_t log2 = 0; for(std::size_t shift = Bits >> 1; shift; shift >>= 1){ std::size_t tmp = n >> shift; if (tmp) log2 += shift, n = tmp; } return log2; } inline float fast_log2 (float val) { boost::uint32_t * exp_ptr = static_cast(static_cast(&val)); boost::uint32_t x = *exp_ptr; const int log_2 = (int)(((x >> 23) & 255) - 128); x &= ~(255 << 23); x += 127 << 23; *exp_ptr = x; val = ((-1.0f/3) * val + 2) * val - 2.0f/3; return (val + log_2); } inline std::size_t ceil_log2 (std::size_t x) { return ((x & (x-1))!= 0) + floor_log2(x); } template struct numbits_eq { static const bool value = sizeof(SizeType)*CHAR_BIT == N; }; template struct sqrt2_pow_max; template struct sqrt2_pow_max >::type> { static const boost::uint32_t value = 0xb504f334; static const std::size_t pow = 31; }; template struct sqrt2_pow_max >::type> { static const boost::uint64_t value = 0xb504f333f9de6484ull; static const std::size_t pow = 63; }; // Returns floor(pow(sqrt(2), x * 2 + 1)). // Defined for X from 0 up to the number of bits in size_t minus 1. inline std::size_t sqrt2_pow_2xplus1 (std::size_t x) { const std::size_t value = (std::size_t)sqrt2_pow_max::value; const std::size_t pow = (std::size_t)sqrt2_pow_max::pow; return (value >> (pow - x)) + 1; } template class exception_disposer { Container *cont_; Disposer &disp_; exception_disposer(const exception_disposer&); exception_disposer &operator=(const exception_disposer&); public: exception_disposer(Container &cont, Disposer &disp) : cont_(&cont), disp_(disp) {} void release() { cont_ = 0; } ~exception_disposer() { if(cont_){ cont_->clear_and_dispose(disp_); } } }; template class exception_array_disposer { Container *cont_; Disposer &disp_; typename Container::size_type &constructed_; exception_array_disposer(const exception_array_disposer&); exception_array_disposer &operator=(const exception_array_disposer&); public: typedef typename Container::size_type size_type; exception_array_disposer (Container &cont, Disposer &disp, size_type &constructed) : cont_(&cont), disp_(disp), constructed_(constructed) {} void release() { cont_ = 0; } ~exception_array_disposer() { size_type n = constructed_; if(cont_){ while(n--){ cont_[n].clear_and_dispose(disp_); } } } }; template struct store_cont_ptr_on_it_impl { static const bool value = is_stateful_value_traits::value; }; template struct store_cont_ptr_on_it_impl { static const bool value = true; }; template struct store_cont_ptr_on_it { typedef typename Container::value_traits value_traits; static const bool value = store_cont_ptr_on_it_impl ::value>::value; }; template struct node_to_value : public detail::select_constptr < typename boost::pointer_to_other ::type , detail::store_cont_ptr_on_it::value >::type { static const bool store_container_ptr = detail::store_cont_ptr_on_it::value; typedef typename Container::real_value_traits real_value_traits; typedef typename real_value_traits::value_type value_type; typedef typename detail::select_constptr < typename boost::pointer_to_other ::type , store_container_ptr >::type Base; typedef typename real_value_traits::node_traits::node node; typedef typename detail::add_const_if_c ::type vtype; typedef typename detail::add_const_if_c ::type ntype; typedef typename boost::pointer_to_other ::type npointer; node_to_value(const Container *cont) : Base(cont) {} typedef vtype & result_type; typedef ntype & first_argument_type; const Container *get_container() const { if(store_container_ptr) return static_cast(Base::get_ptr()); else return 0; } const real_value_traits *get_real_value_traits() const { if(store_container_ptr) return &this->get_container()->get_real_value_traits(); else return 0; } result_type operator()(first_argument_type arg) const { return *(this->get_real_value_traits()->to_value_ptr(npointer(&arg))); } }; } //namespace detail } //namespace intrusive } //namespace boost #include #endif //BOOST_INTRUSIVE_DETAIL_UTILITIES_HPP