5 // Copyright (c) 2012-2015 Adam Wulkiewicz, Lodz, Poland.
6 // Copyright (c) 2011-2013 Andrew Hundt.
8 // Use, modification and distribution is subject to the Boost Software License,
9 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt)
12 #ifndef BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_DETAIL_HPP
13 #define BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_DETAIL_HPP
20 #include <boost/mpl/if.hpp>
21 #include <boost/mpl/and.hpp>
22 #include <boost/mpl/or.hpp>
23 #include <boost/mpl/int.hpp>
25 #include <boost/type_traits/is_same.hpp>
26 #include <boost/type_traits/remove_const.hpp>
27 #include <boost/type_traits/remove_reference.hpp>
28 #include <boost/type_traits/has_trivial_assign.hpp>
29 #include <boost/type_traits/has_trivial_copy.hpp>
30 #include <boost/type_traits/has_trivial_constructor.hpp>
31 #include <boost/type_traits/has_trivial_destructor.hpp>
32 #include <boost/type_traits/has_trivial_move_constructor.hpp>
33 #include <boost/type_traits/has_trivial_move_assign.hpp>
34 //#include <boost/type_traits/has_nothrow_constructor.hpp>
35 //#include <boost/type_traits/has_nothrow_copy.hpp>
36 //#include <boost/type_traits/has_nothrow_assign.hpp>
37 //#include <boost/type_traits/has_nothrow_destructor.hpp>
39 #include <boost/detail/no_exceptions_support.hpp>
40 #include <boost/config.hpp>
41 #include <boost/move/move.hpp>
42 #include <boost/core/addressof.hpp>
43 #include <boost/iterator/iterator_traits.hpp>
45 #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
46 #include <boost/move/detail/fwd_macros.hpp>
49 // TODO - move vectors iterators optimization to the other, optional file instead of checking defines?
51 #if defined(BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_ENABLE_VECTOR_OPTIMIZATION) && !defined(BOOST_NO_EXCEPTIONS)
53 #include <boost/container/vector.hpp>
54 #endif // BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_ENABLE_VECTOR_OPTIMIZATION && !BOOST_NO_EXCEPTIONS
56 namespace boost { namespace geometry { namespace index { namespace detail { namespace varray_detail {
59 struct are_elements_contiguous : boost::is_pointer<I>
62 // EXPERIMENTAL - not finished
63 // Conditional setup - mark vector iterators defined in known implementations
64 // as iterators pointing to contiguous ranges
66 #if defined(BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_ENABLE_VECTOR_OPTIMIZATION) && !defined(BOOST_NO_EXCEPTIONS)
68 template <typename Pointer>
69 struct are_elements_contiguous<
70 boost::container::container_detail::vector_const_iterator<Pointer>
74 template <typename Pointer>
75 struct are_elements_contiguous<
76 boost::container::container_detail::vector_iterator<Pointer>
80 #if defined(BOOST_DINKUMWARE_STDLIB)
83 struct are_elements_contiguous<
84 std::_Vector_const_iterator<T>
89 struct are_elements_contiguous<
90 std::_Vector_iterator<T>
94 #elif defined(BOOST_GNU_STDLIB)
96 template <typename P, typename T, typename A>
97 struct are_elements_contiguous<
98 __gnu_cxx::__normal_iterator<P, std::vector<T, A> >
102 #elif defined(_LIBCPP_VERSION)
104 // TODO - test it first
105 //template <typename P>
106 //struct are_elements_contiguous<
108 //> : boost::true_type
111 #else // OTHER_STDLIB
113 // TODO - add other iterators implementations
117 #endif // BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_ENABLE_VECTOR_OPTIMIZATION && !BOOST_NO_EXCEPTIONS
119 // True if iterator values are the same and both iterators points to the ranges of contiguous elements
121 template <typename I, typename O>
122 struct are_corresponding :
125 ::boost::remove_const<
126 typename ::boost::iterator_value<I>::type
128 ::boost::remove_const<
129 typename ::boost::iterator_value<O>::type
132 are_elements_contiguous<I>,
133 are_elements_contiguous<O>
137 template <typename I, typename V>
138 struct is_corresponding_value :
140 ::boost::remove_const<
141 typename ::boost::iterator_value<I>::type
143 ::boost::remove_const<V>
149 template <typename I>
150 void destroy_dispatch(I /*first*/, I /*last*/,
151 boost::true_type const& /*has_trivial_destructor*/)
154 template <typename I>
155 void destroy_dispatch(I first, I last,
156 boost::false_type const& /*has_trivial_destructor*/)
158 typedef typename boost::iterator_value<I>::type value_type;
159 for ( ; first != last ; ++first )
160 first->~value_type();
163 template <typename I>
164 void destroy(I first, I last)
166 typedef typename boost::iterator_value<I>::type value_type;
167 destroy_dispatch(first, last, has_trivial_destructor<value_type>());
172 template <typename I>
173 void destroy_dispatch(I /*pos*/,
174 boost::true_type const& /*has_trivial_destructor*/)
177 template <typename I>
178 void destroy_dispatch(I pos,
179 boost::false_type const& /*has_trivial_destructor*/)
181 typedef typename boost::iterator_value<I>::type value_type;
185 template <typename I>
188 typedef typename boost::iterator_value<I>::type value_type;
189 destroy_dispatch(pos, has_trivial_destructor<value_type>());
194 template <typename I, typename O>
195 inline O copy_dispatch(I first, I last, O dst,
196 boost::mpl::bool_<true> const& /*use_memmove*/)
198 typedef typename boost::iterator_value<I>::type value_type;
199 typename boost::iterator_difference<I>::type d = std::distance(first, last);
201 ::memmove(boost::addressof(*dst), boost::addressof(*first), sizeof(value_type) * d);
205 template <typename I, typename O>
206 inline O copy_dispatch(I first, I last, O dst,
207 boost::mpl::bool_<false> const& /*use_memmove*/)
209 return std::copy(first, last, dst); // may throw
212 template <typename I, typename O>
213 inline O copy(I first, I last, O dst)
217 are_corresponding<I, O>,
218 ::boost::has_trivial_assign<
219 typename ::boost::iterator_value<O>::type
224 return copy_dispatch(first, last, dst, use_memmove()); // may throw
227 // uninitialized_copy(I, I, O)
229 template <typename I, typename O>
231 O uninitialized_copy_dispatch(I first, I last, O dst,
232 boost::mpl::bool_<true> const& /*use_memcpy*/)
234 typedef typename boost::iterator_value<I>::type value_type;
235 typename boost::iterator_difference<I>::type d = std::distance(first, last);
237 ::memcpy(boost::addressof(*dst), boost::addressof(*first), sizeof(value_type) * d);
241 template <typename I, typename F>
243 F uninitialized_copy_dispatch(I first, I last, F dst,
244 boost::mpl::bool_<false> const& /*use_memcpy*/)
246 return std::uninitialized_copy(first, last, dst); // may throw
249 template <typename I, typename F>
251 F uninitialized_copy(I first, I last, F dst)
255 are_corresponding<I, F>,
256 ::boost::has_trivial_copy<
257 typename ::boost::iterator_value<F>::type
262 return uninitialized_copy_dispatch(first, last, dst, use_memcpy()); // may throw
265 // uninitialized_move(I, I, O)
267 template <typename I, typename O>
269 O uninitialized_move_dispatch(I first, I last, O dst,
270 boost::mpl::bool_<true> const& /*use_memcpy*/)
272 typedef typename boost::iterator_value<I>::type value_type;
273 typename boost::iterator_difference<I>::type d = std::distance(first, last);
275 ::memcpy(boost::addressof(*dst), boost::addressof(*first), sizeof(value_type) * d);
279 template <typename I, typename O>
281 O uninitialized_move_dispatch(I first, I last, O dst,
282 boost::mpl::bool_<false> const& /*use_memcpy*/)
284 //return boost::uninitialized_move(first, last, dst); // may throw
290 typedef typename std::iterator_traits<O>::value_type value_type;
291 for (; first != last; ++first, ++o )
292 new (boost::addressof(*o)) value_type(boost::move(*first));
304 template <typename I, typename O>
306 O uninitialized_move(I first, I last, O dst)
310 are_corresponding<I, O>,
311 ::boost::has_trivial_copy<
312 typename ::boost::iterator_value<O>::type
317 return uninitialized_move_dispatch(first, last, dst, use_memcpy()); // may throw
320 // TODO - move uses memmove - implement 2nd version using memcpy?
324 template <typename I, typename O>
326 O move_dispatch(I first, I last, O dst,
327 boost::mpl::bool_<true> const& /*use_memmove*/)
329 typedef typename boost::iterator_value<I>::type value_type;
330 typename boost::iterator_difference<I>::type d = std::distance(first, last);
332 ::memmove(boost::addressof(*dst), boost::addressof(*first), sizeof(value_type) * d);
336 template <typename I, typename O>
338 O move_dispatch(I first, I last, O dst,
339 boost::mpl::bool_<false> const& /*use_memmove*/)
341 return boost::move(first, last, dst); // may throw
344 template <typename I, typename O>
346 O move(I first, I last, O dst)
350 are_corresponding<I, O>,
351 ::boost::has_trivial_assign<
352 typename ::boost::iterator_value<O>::type
357 return move_dispatch(first, last, dst, use_memmove()); // may throw
360 // move_backward(BDI, BDI, BDO)
362 template <typename BDI, typename BDO>
364 BDO move_backward_dispatch(BDI first, BDI last, BDO dst,
365 boost::mpl::bool_<true> const& /*use_memmove*/)
367 typedef typename boost::iterator_value<BDI>::type value_type;
368 typename boost::iterator_difference<BDI>::type d = std::distance(first, last);
371 ::memmove(boost::addressof(*foo), boost::addressof(*first), sizeof(value_type) * d);
375 template <typename BDI, typename BDO>
377 BDO move_backward_dispatch(BDI first, BDI last, BDO dst,
378 boost::mpl::bool_<false> const& /*use_memmove*/)
380 return boost::move_backward(first, last, dst); // may throw
383 template <typename BDI, typename BDO>
385 BDO move_backward(BDI first, BDI last, BDO dst)
389 are_corresponding<BDI, BDO>,
390 ::boost::has_trivial_assign<
391 typename ::boost::iterator_value<BDO>::type
396 return move_backward_dispatch(first, last, dst, use_memmove()); // may throw
399 template <typename T>
400 struct has_nothrow_move : public
403 ::boost::has_nothrow_move<
404 typename ::boost::remove_const<T>::type
408 ::boost::has_nothrow_move<T>::value
413 // uninitialized_move_if_noexcept(I, I, O)
415 template <typename I, typename O>
417 O uninitialized_move_if_noexcept_dispatch(I first, I last, O dst, boost::mpl::bool_<true> const& /*use_move*/)
418 { return varray_detail::uninitialized_move(first, last, dst); }
420 template <typename I, typename O>
422 O uninitialized_move_if_noexcept_dispatch(I first, I last, O dst, boost::mpl::bool_<false> const& /*use_move*/)
423 { return varray_detail::uninitialized_copy(first, last, dst); }
425 template <typename I, typename O>
427 O uninitialized_move_if_noexcept(I first, I last, O dst)
429 typedef typename has_nothrow_move<
430 typename ::boost::iterator_value<O>::type
433 return uninitialized_move_if_noexcept_dispatch(first, last, dst, use_move()); // may throw
436 // move_if_noexcept(I, I, O)
438 template <typename I, typename O>
440 O move_if_noexcept_dispatch(I first, I last, O dst, boost::mpl::bool_<true> const& /*use_move*/)
441 { return move(first, last, dst); }
443 template <typename I, typename O>
445 O move_if_noexcept_dispatch(I first, I last, O dst, boost::mpl::bool_<false> const& /*use_move*/)
446 { return copy(first, last, dst); }
448 template <typename I, typename O>
450 O move_if_noexcept(I first, I last, O dst)
452 typedef typename has_nothrow_move<
453 typename ::boost::iterator_value<O>::type
456 return move_if_noexcept_dispatch(first, last, dst, use_move()); // may throw
459 // uninitialized_fill(I, I)
461 template <typename I>
463 void uninitialized_fill_dispatch(I /*first*/, I /*last*/,
464 boost::true_type const& /*has_trivial_constructor*/,
465 boost::true_type const& /*disable_trivial_init*/)
468 template <typename I>
470 void uninitialized_fill_dispatch(I first, I last,
471 boost::true_type const& /*has_trivial_constructor*/,
472 boost::false_type const& /*disable_trivial_init*/)
474 typedef typename boost::iterator_value<I>::type value_type;
475 for ( ; first != last ; ++first )
476 new (boost::addressof(*first)) value_type();
479 template <typename I, typename DisableTrivialInit>
481 void uninitialized_fill_dispatch(I first, I last,
482 boost::false_type const& /*has_trivial_constructor*/,
483 DisableTrivialInit const& /*not_used*/)
485 typedef typename boost::iterator_value<I>::type value_type;
490 for ( ; it != last ; ++it )
491 new (boost::addressof(*it)) value_type(); // may throw
501 template <typename I, typename DisableTrivialInit>
503 void uninitialized_fill(I first, I last, DisableTrivialInit const& disable_trivial_init)
505 typedef typename boost::iterator_value<I>::type value_type;
506 uninitialized_fill_dispatch(first, last, boost::has_trivial_constructor<value_type>(), disable_trivial_init); // may throw
511 template <typename I>
513 void construct_dispatch(boost::mpl::bool_<true> const& /*dont_init*/, I /*pos*/)
516 template <typename I>
518 void construct_dispatch(boost::mpl::bool_<false> const& /*dont_init*/, I pos)
520 typedef typename ::boost::iterator_value<I>::type value_type;
521 new (static_cast<void*>(::boost::addressof(*pos))) value_type(); // may throw
524 template <typename DisableTrivialInit, typename I>
526 void construct(DisableTrivialInit const&, I pos)
528 typedef typename ::boost::iterator_value<I>::type value_type;
529 typedef typename ::boost::mpl::and_<
530 boost::has_trivial_constructor<value_type>,
534 construct_dispatch(dont_init(), pos); // may throw
539 template <typename I, typename V>
541 void construct_copy_dispatch(I pos, V const& v,
542 boost::mpl::bool_<true> const& /*use_memcpy*/)
544 ::memcpy(boost::addressof(*pos), boost::addressof(v), sizeof(V));
547 template <typename I, typename P>
549 void construct_copy_dispatch(I pos, P const& p,
550 boost::mpl::bool_<false> const& /*use_memcpy*/)
552 typedef typename boost::iterator_value<I>::type V;
553 new (static_cast<void*>(boost::addressof(*pos))) V(p); // may throw
556 template <typename DisableTrivialInit, typename I, typename P>
558 void construct(DisableTrivialInit const&,
563 is_corresponding_value<I, P>,
564 ::boost::has_trivial_copy<P>
568 construct_copy_dispatch(pos, p, use_memcpy()); // may throw
571 // Needed by push_back(V &&)
573 template <typename I, typename V>
575 void construct_move_dispatch(I pos, V const& v,
576 boost::mpl::bool_<true> const& /*use_memcpy*/)
578 ::memcpy(boost::addressof(*pos), boost::addressof(v), sizeof(V));
581 template <typename I, typename P>
583 void construct_move_dispatch(I pos, BOOST_RV_REF(P) p,
584 boost::mpl::bool_<false> const& /*use_memcpy*/)
586 typedef typename boost::iterator_value<I>::type V;
587 new (static_cast<void*>(boost::addressof(*pos))) V(::boost::move(p)); // may throw
590 template <typename DisableTrivialInit, typename I, typename P>
592 void construct(DisableTrivialInit const&, I pos, BOOST_RV_REF(P) p)
596 is_corresponding_value<I, P>,
597 ::boost::has_trivial_move_constructor<P>
601 construct_move_dispatch(pos, ::boost::move(p), use_memcpy()); // may throw
604 // Needed by emplace_back() and emplace()
606 #if !defined(BOOST_CONTAINER_VARRAY_DISABLE_EMPLACE)
607 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
609 template <typename DisableTrivialInit, typename I, class ...Args>
611 void construct(DisableTrivialInit const&,
613 BOOST_FWD_REF(Args) ...args)
615 typedef typename boost::iterator_value<I>::type V;
616 new (static_cast<void*>(boost::addressof(*pos))) V(::boost::forward<Args>(args)...); // may throw
619 #else // !BOOST_NO_CXX11_VARIADIC_TEMPLATES
621 // BOOST_NO_CXX11_RVALUE_REFERENCES -> P0 const& p0
622 // !BOOST_NO_CXX11_RVALUE_REFERENCES -> P0 && p0
623 // which means that version with one parameter may take V const& v
625 #define BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_DETAIL_CONSTRUCT(N) \
626 template <typename DisableTrivialInit, typename I, typename P BOOST_MOVE_I##N BOOST_MOVE_CLASS##N > \
628 void construct(DisableTrivialInit const&, \
631 BOOST_MOVE_I##N BOOST_MOVE_UREF##N) \
633 typedef typename boost::iterator_value<I>::type V; \
635 (static_cast<void*>(boost::addressof(*pos))) \
636 V(boost::forward<P>(p) BOOST_MOVE_I##N BOOST_MOVE_FWD##N); /*may throw*/ \
639 BOOST_MOVE_ITERATE_1TO9(BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_DETAIL_CONSTRUCT)
640 #undef BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_DETAIL_CONSTRUCT
642 #endif // !BOOST_NO_CXX11_VARIADIC_TEMPLATES
643 #endif // !BOOST_CONTAINER_VARRAY_DISABLE_EMPLACE
647 template <typename I, typename V>
649 void assign_copy_dispatch(I pos, V const& v,
650 boost::mpl::bool_<true> const& /*use_memcpy*/)
652 // TODO - use memmove here?
653 ::memcpy(boost::addressof(*pos), boost::addressof(v), sizeof(V));
656 template <typename I, typename V>
658 void assign_copy_dispatch(I pos, V const& v,
659 boost::mpl::bool_<false> const& /*use_memcpy*/)
661 *pos = v; // may throw
664 template <typename I, typename V>
666 void assign(I pos, V const& v)
670 is_corresponding_value<I, V>,
671 ::boost::has_trivial_assign<V>
675 assign_copy_dispatch(pos, v, use_memcpy()); // may throw
678 template <typename I, typename V>
680 void assign_move_dispatch(I pos, V const& v,
681 boost::mpl::bool_<true> const& /*use_memcpy*/)
683 // TODO - use memmove here?
684 ::memcpy(boost::addressof(*pos), boost::addressof(v), sizeof(V));
687 template <typename I, typename V>
689 void assign_move_dispatch(I pos, BOOST_RV_REF(V) v,
690 boost::mpl::bool_<false> const& /*use_memcpy*/)
692 *pos = boost::move(v); // may throw
695 template <typename I, typename V>
697 void assign(I pos, BOOST_RV_REF(V) v)
701 is_corresponding_value<I, V>,
702 ::boost::has_trivial_move_assign<V>
706 assign_move_dispatch(pos, ::boost::move(v), use_memcpy());
709 // uninitialized_copy_s
711 template <typename I, typename F>
712 inline std::size_t uninitialized_copy_s(I first, I last, F dest, std::size_t max_count)
714 std::size_t count = 0;
719 for ( ; first != last ; ++it, ++first, ++count )
721 if ( max_count <= count )
722 return (std::numeric_limits<std::size_t>::max)();
724 // dummy 0 as DisableTrivialInit
725 construct(0, it, *first); // may throw
741 class scoped_destructor
744 scoped_destructor(T * ptr) : m_ptr(ptr) {}
752 void release() { m_ptr = 0; }
758 }}}}} // namespace boost::geometry::index::detail::varray_detail
760 #endif // BOOST_GEOMETRY_INDEX_DETAIL_VARRAY_DETAIL_HPP