// Boost.Geometry // // varray details // // Copyright (c) 2012-2013 Adam Wulkiewicz, Lodz, Poland. // Copyright (c) 2011-2013 Andrew Hundt. // // Use, modification and distribution is subject to 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) #ifndef BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_DETAIL_HPP #define BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_DETAIL_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include //#include //#include //#include #include #include #include #include #include // TODO - move vectors iterators optimization to the other, optional file instead of checking defines? #if defined(BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_ENABLE_VECTOR_OPTIMIZATION) && !defined(BOOST_NO_EXCEPTIONS) #include #include #endif // BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_ENABLE_VECTOR_OPTIMIZATION && !BOOST_NO_EXCEPTIONS namespace boost { namespace geometry { namespace index { namespace detail { namespace varray_detail { template struct are_elements_contiguous : boost::is_pointer {}; // EXPERIMENTAL - not finished // Conditional setup - mark vector iterators defined in known implementations // as iterators pointing to contiguous ranges #if defined(BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_ENABLE_VECTOR_OPTIMIZATION) && !defined(BOOST_NO_EXCEPTIONS) template struct are_elements_contiguous< boost::container::container_detail::vector_const_iterator > : boost::true_type {}; template struct are_elements_contiguous< boost::container::container_detail::vector_iterator > : boost::true_type {}; #if defined(BOOST_DINKUMWARE_STDLIB) template struct are_elements_contiguous< std::_Vector_const_iterator > : boost::true_type {}; template struct are_elements_contiguous< std::_Vector_iterator > : boost::true_type {}; #elif defined(BOOST_GNU_STDLIB) template struct are_elements_contiguous< __gnu_cxx::__normal_iterator > > : boost::true_type {}; #elif defined(_LIBCPP_VERSION) // TODO - test it first //template //struct are_elements_contiguous< // __wrap_iter

//> : boost::true_type //{}; #else // OTHER_STDLIB // TODO - add other iterators implementations #endif // STDLIB #endif // BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_ENABLE_VECTOR_OPTIMIZATION && !BOOST_NO_EXCEPTIONS // True if iterator values are the same and both iterators points to the ranges of contiguous elements template struct are_corresponding : ::boost::mpl::and_< ::boost::is_same< ::boost::remove_const< typename ::boost::iterator_value::type >, ::boost::remove_const< typename ::boost::iterator_value::type > >, are_elements_contiguous, are_elements_contiguous > {}; template struct is_corresponding_value : ::boost::is_same< ::boost::remove_const< typename ::boost::iterator_value::type >, ::boost::remove_const > {}; // destroy(I, I) template void destroy_dispatch(I /*first*/, I /*last*/, boost::true_type const& /*has_trivial_destructor*/) {} template void destroy_dispatch(I first, I last, boost::false_type const& /*has_trivial_destructor*/) { typedef typename boost::iterator_value::type value_type; for ( ; first != last ; ++first ) first->~value_type(); } template void destroy(I first, I last) { typedef typename boost::iterator_value::type value_type; destroy_dispatch(first, last, has_trivial_destructor()); } // destroy(I) template void destroy_dispatch(I /*pos*/, boost::true_type const& /*has_trivial_destructor*/) {} template void destroy_dispatch(I pos, boost::false_type const& /*has_trivial_destructor*/) { typedef typename boost::iterator_value::type value_type; pos->~value_type(); } template void destroy(I pos) { typedef typename boost::iterator_value::type value_type; destroy_dispatch(pos, has_trivial_destructor()); } // copy(I, I, O) template inline O copy_dispatch(I first, I last, O dst, boost::mpl::bool_ const& /*use_memmove*/) { typedef typename boost::iterator_value::type value_type; typename boost::iterator_difference::type d = std::distance(first, last); ::memmove(boost::addressof(*dst), boost::addressof(*first), sizeof(value_type) * d); return dst + d; } template inline O copy_dispatch(I first, I last, O dst, boost::mpl::bool_ const& /*use_memmove*/) { return std::copy(first, last, dst); // may throw } template inline O copy(I first, I last, O dst) { typedef typename ::boost::mpl::and_< are_corresponding, ::boost::has_trivial_assign< typename ::boost::iterator_value::type > >::type use_memmove; return copy_dispatch(first, last, dst, use_memmove()); // may throw } // uninitialized_copy(I, I, O) template inline O uninitialized_copy_dispatch(I first, I last, O dst, boost::mpl::bool_ const& /*use_memcpy*/) { typedef typename boost::iterator_value::type value_type; typename boost::iterator_difference::type d = std::distance(first, last); ::memcpy(boost::addressof(*dst), boost::addressof(*first), sizeof(value_type) * d); return dst + d; } template inline F uninitialized_copy_dispatch(I first, I last, F dst, boost::mpl::bool_ const& /*use_memcpy*/) { return std::uninitialized_copy(first, last, dst); // may throw } template inline F uninitialized_copy(I first, I last, F dst) { typedef typename ::boost::mpl::and_< are_corresponding, ::boost::has_trivial_copy< typename ::boost::iterator_value::type > >::type use_memcpy; return uninitialized_copy_dispatch(first, last, dst, use_memcpy()); // may throw } // uninitialized_move(I, I, O) template inline O uninitialized_move_dispatch(I first, I last, O dst, boost::mpl::bool_ const& /*use_memcpy*/) { typedef typename boost::iterator_value::type value_type; typename boost::iterator_difference::type d = std::distance(first, last); ::memcpy(boost::addressof(*dst), boost::addressof(*first), sizeof(value_type) * d); return dst + d; } template inline O uninitialized_move_dispatch(I first, I last, O dst, boost::mpl::bool_ const& /*use_memcpy*/) { //return boost::uninitialized_move(first, last, dst); // may throw O o = dst; BOOST_TRY { typedef typename std::iterator_traits::value_type value_type; for (; first != last; ++first, ++o ) new (boost::addressof(*o)) value_type(boost::move(*first)); } BOOST_CATCH(...) { destroy(dst, o); BOOST_RETHROW; } BOOST_CATCH_END return dst; } template inline O uninitialized_move(I first, I last, O dst) { typedef typename ::boost::mpl::and_< are_corresponding, ::boost::has_trivial_copy< typename ::boost::iterator_value::type > >::type use_memcpy; return uninitialized_move_dispatch(first, last, dst, use_memcpy()); // may throw } // TODO - move uses memmove - implement 2nd version using memcpy? // move(I, I, O) template inline O move_dispatch(I first, I last, O dst, boost::mpl::bool_ const& /*use_memmove*/) { typedef typename boost::iterator_value::type value_type; typename boost::iterator_difference::type d = std::distance(first, last); ::memmove(boost::addressof(*dst), boost::addressof(*first), sizeof(value_type) * d); return dst + d; } template inline O move_dispatch(I first, I last, O dst, boost::mpl::bool_ const& /*use_memmove*/) { return boost::move(first, last, dst); // may throw } template inline O move(I first, I last, O dst) { typedef typename ::boost::mpl::and_< are_corresponding, ::boost::has_trivial_assign< typename ::boost::iterator_value::type > >::type use_memmove; return move_dispatch(first, last, dst, use_memmove()); // may throw } // move_backward(BDI, BDI, BDO) template inline BDO move_backward_dispatch(BDI first, BDI last, BDO dst, boost::mpl::bool_ const& /*use_memmove*/) { typedef typename boost::iterator_value::type value_type; typename boost::iterator_difference::type d = std::distance(first, last); BDO foo(dst - d); ::memmove(boost::addressof(*foo), boost::addressof(*first), sizeof(value_type) * d); return foo; } template inline BDO move_backward_dispatch(BDI first, BDI last, BDO dst, boost::mpl::bool_ const& /*use_memmove*/) { return boost::move_backward(first, last, dst); // may throw } template inline BDO move_backward(BDI first, BDI last, BDO dst) { typedef typename ::boost::mpl::and_< are_corresponding, ::boost::has_trivial_assign< typename ::boost::iterator_value::type > >::type use_memmove; return move_backward_dispatch(first, last, dst, use_memmove()); // may throw } template struct has_nothrow_move : public ::boost::mpl::or_< boost::mpl::bool_< ::boost::has_nothrow_move< typename ::boost::remove_const::type >::value >, boost::mpl::bool_< ::boost::has_nothrow_move::value > > {}; // uninitialized_move_if_noexcept(I, I, O) template inline O uninitialized_move_if_noexcept_dispatch(I first, I last, O dst, boost::mpl::bool_ const& /*use_move*/) { return varray_detail::uninitialized_move(first, last, dst); } template inline O uninitialized_move_if_noexcept_dispatch(I first, I last, O dst, boost::mpl::bool_ const& /*use_move*/) { return varray_detail::uninitialized_copy(first, last, dst); } template inline O uninitialized_move_if_noexcept(I first, I last, O dst) { typedef typename has_nothrow_move< typename ::boost::iterator_value::type >::type use_move; return uninitialized_move_if_noexcept_dispatch(first, last, dst, use_move()); // may throw } // move_if_noexcept(I, I, O) template inline O move_if_noexcept_dispatch(I first, I last, O dst, boost::mpl::bool_ const& /*use_move*/) { return move(first, last, dst); } template inline O move_if_noexcept_dispatch(I first, I last, O dst, boost::mpl::bool_ const& /*use_move*/) { return copy(first, last, dst); } template inline O move_if_noexcept(I first, I last, O dst) { typedef typename has_nothrow_move< typename ::boost::iterator_value::type >::type use_move; return move_if_noexcept_dispatch(first, last, dst, use_move()); // may throw } // uninitialized_fill(I, I) template inline void uninitialized_fill_dispatch(I /*first*/, I /*last*/, boost::true_type const& /*has_trivial_constructor*/, boost::true_type const& /*disable_trivial_init*/) {} template inline void uninitialized_fill_dispatch(I first, I last, boost::true_type const& /*has_trivial_constructor*/, boost::false_type const& /*disable_trivial_init*/) { typedef typename boost::iterator_value::type value_type; for ( ; first != last ; ++first ) new (boost::addressof(*first)) value_type(); } template inline void uninitialized_fill_dispatch(I first, I last, boost::false_type const& /*has_trivial_constructor*/, DisableTrivialInit const& /*not_used*/) { typedef typename boost::iterator_value::type value_type; I it = first; BOOST_TRY { for ( ; it != last ; ++it ) new (boost::addressof(*it)) value_type(); // may throw } BOOST_CATCH(...) { destroy(first, it); BOOST_RETHROW; } BOOST_CATCH_END } template inline void uninitialized_fill(I first, I last, DisableTrivialInit const& disable_trivial_init) { typedef typename boost::iterator_value::type value_type; uninitialized_fill_dispatch(first, last, boost::has_trivial_constructor(), disable_trivial_init); // may throw } // construct(I) template inline void construct_dispatch(boost::mpl::bool_ const& /*dont_init*/, I /*pos*/) {} template inline void construct_dispatch(boost::mpl::bool_ const& /*dont_init*/, I pos) { typedef typename ::boost::iterator_value::type value_type; new (static_cast(::boost::addressof(*pos))) value_type(); // may throw } template inline void construct(DisableTrivialInit const&, I pos) { typedef typename ::boost::iterator_value::type value_type; typedef typename ::boost::mpl::and_< boost::has_trivial_constructor, DisableTrivialInit >::type dont_init; construct_dispatch(dont_init(), pos); // may throw } // construct(I, V) template inline void construct_copy_dispatch(I pos, V const& v, boost::mpl::bool_ const& /*use_memcpy*/) { ::memcpy(boost::addressof(*pos), boost::addressof(v), sizeof(V)); } template inline void construct_copy_dispatch(I pos, P const& p, boost::mpl::bool_ const& /*use_memcpy*/) { typedef typename boost::iterator_value::type V; new (static_cast(boost::addressof(*pos))) V(p); // may throw } template inline void construct(DisableTrivialInit const&, I pos, P const& p) { typedef typename ::boost::mpl::and_< is_corresponding_value, ::boost::has_trivial_copy

>::type use_memcpy; construct_copy_dispatch(pos, p, use_memcpy()); // may throw } // Needed by push_back(V &&) template inline void construct_move_dispatch(I pos, V const& v, boost::mpl::bool_ const& /*use_memcpy*/) { ::memcpy(boost::addressof(*pos), boost::addressof(v), sizeof(V)); } template inline void construct_move_dispatch(I pos, BOOST_RV_REF(P) p, boost::mpl::bool_ const& /*use_memcpy*/) { typedef typename boost::iterator_value::type V; new (static_cast(boost::addressof(*pos))) V(::boost::move(p)); // may throw } template inline void construct(DisableTrivialInit const&, I pos, BOOST_RV_REF(P) p) { typedef typename ::boost::mpl::and_< is_corresponding_value, ::boost::has_trivial_move_constructor

>::type use_memcpy; construct_move_dispatch(pos, ::boost::move(p), use_memcpy()); // may throw } // Needed by emplace_back() and emplace() #if !defined(BOOST_CONTAINER_VARRAY_DISABLE_EMPLACE) #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template inline void construct(DisableTrivialInit const&, I pos, BOOST_FWD_REF(Args) ...args) { typedef typename boost::iterator_value::type V; new (static_cast(boost::addressof(*pos))) V(::boost::forward(args)...); // may throw } #else // !BOOST_NO_CXX11_VARIADIC_TEMPLATES // BOOST_NO_CXX11_RVALUE_REFERENCES -> P0 const& p0 // !BOOST_NO_CXX11_RVALUE_REFERENCES -> P0 && p0 // which means that version with one parameter may take V const& v #define BOOST_PP_LOCAL_MACRO(n) \ template \ inline \ void construct(DisableTrivialInit const&, \ I pos, \ BOOST_CONTAINER_PP_PARAM(P, p) \ BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \ { \ typedef typename boost::iterator_value::type V; \ new \ (static_cast(boost::addressof(*pos))) \ V(p, BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); /*may throw*/ \ } \ // #define BOOST_PP_LOCAL_LIMITS (1, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS) #include BOOST_PP_LOCAL_ITERATE() #endif // !BOOST_NO_CXX11_VARIADIC_TEMPLATES #endif // !BOOST_CONTAINER_VARRAY_DISABLE_EMPLACE // assign(I, V) template inline void assign_copy_dispatch(I pos, V const& v, boost::mpl::bool_ const& /*use_memcpy*/) { // TODO - use memmove here? ::memcpy(boost::addressof(*pos), boost::addressof(v), sizeof(V)); } template inline void assign_copy_dispatch(I pos, V const& v, boost::mpl::bool_ const& /*use_memcpy*/) { *pos = v; // may throw } template inline void assign(I pos, V const& v) { typedef typename ::boost::mpl::and_< is_corresponding_value, ::boost::has_trivial_assign >::type use_memcpy; assign_copy_dispatch(pos, v, use_memcpy()); // may throw } template inline void assign_move_dispatch(I pos, V const& v, boost::mpl::bool_ const& /*use_memcpy*/) { // TODO - use memmove here? ::memcpy(boost::addressof(*pos), boost::addressof(v), sizeof(V)); } template inline void assign_move_dispatch(I pos, BOOST_RV_REF(V) v, boost::mpl::bool_ const& /*use_memcpy*/) { *pos = boost::move(v); // may throw } template inline void assign(I pos, BOOST_RV_REF(V) v) { typedef typename ::boost::mpl::and_< is_corresponding_value, ::boost::has_trivial_move_assign >::type use_memcpy; assign_move_dispatch(pos, ::boost::move(v), use_memcpy()); } // uninitialized_copy_s template inline std::size_t uninitialized_copy_s(I first, I last, F dest, std::size_t max_count) { std::size_t count = 0; F it = dest; BOOST_TRY { for ( ; first != last ; ++it, ++first, ++count ) { if ( max_count <= count ) return (std::numeric_limits::max)(); // dummy 0 as DisableTrivialInit construct(0, it, *first); // may throw } } BOOST_CATCH(...) { destroy(dest, it); BOOST_RETHROW; } BOOST_CATCH_END return count; } // scoped_destructor template class scoped_destructor { public: scoped_destructor(T * ptr) : m_ptr(ptr) {} ~scoped_destructor() { if(m_ptr) destroy(m_ptr); } void release() { m_ptr = 0; } private: T * m_ptr; }; }}}}} // namespace boost::geometry::index::detail::varray_detail #endif // BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_DETAIL_HPP