|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r82090 - in sandbox/static_vector: boost/container boost/container/detail examples
From: adam.wulkiewicz_at_[hidden]
Date: 2012-12-18 21:01:10
Author: awulkiew
Date: 2012-12-18 21:01:09 EST (Tue, 18 Dec 2012)
New Revision: 82090
URL: http://svn.boost.org/trac/boost/changeset/82090
Log:
uninitialized_copy(), uninitialized_fill(), move() moved from static_vector class to detail::static_vector namespace
Text files modified:
sandbox/static_vector/boost/container/detail/static_vector_util.hpp | 139 ++++++++++++++++++++++++++++++++++++++-
sandbox/static_vector/boost/container/static_vector.hpp | 94 ++-------------------------
sandbox/static_vector/examples/times.cpp | 10 --
3 files changed, 143 insertions(+), 100 deletions(-)
Modified: sandbox/static_vector/boost/container/detail/static_vector_util.hpp
==============================================================================
--- sandbox/static_vector/boost/container/detail/static_vector_util.hpp (original)
+++ sandbox/static_vector/boost/container/detail/static_vector_util.hpp 2012-12-18 21:01:09 EST (Tue, 18 Dec 2012)
@@ -30,6 +30,7 @@
//#include <boost/type_traits/has_nothrow_assign.hpp>
//#include <boost/type_traits/has_nothrow_destructor.hpp>
+#include <boost/utility/addressof.hpp>
#include <boost/iterator/iterator_traits.hpp>
namespace boost { namespace container { namespace detail { namespace static_vector {
@@ -52,12 +53,25 @@
>
{};
+template <typename I, typename V>
+struct is_corresponding_value :
+ ::boost::is_same<
+ ::boost::remove_const<
+ typename ::boost::iterator_value<I>::type
+ >,
+ ::boost::remove_const<V>
+ >
+{};
+
+// copy
+
template <typename V>
inline V * copy_dispatch(const V * first, const V * last, V * dst,
boost::mpl::bool_<true> const& /*use_memcpy*/)
{
- ::memcpy(dst, first, sizeof(V) * std::distance(first, last));
- return dst;
+ typename std::iterator_traits<V*>::difference_type d = std::distance(first, last);
+ ::memcpy(dst, first, sizeof(V) * d);
+ return dst + d;
}
template <typename I, typename O>
@@ -70,11 +84,10 @@
template <typename I, typename O>
inline O copy(I first, I last, O dst)
{
- namespace bm = ::boost::mpl;
typedef typename
- bm::and_<
+ ::boost::mpl::and_<
are_corresponding_pointers<I, O>,
- has_trivial_assign<
+ ::boost::has_trivial_assign<
typename ::boost::iterator_value<O>::type
>
>::type use_memcpy;
@@ -82,12 +95,126 @@
return copy_dispatch(first, last, dst, use_memcpy()); // may throw
}
+// uninitialized_copy
+
+template <typename V>
+V * uninitialized_copy_dispatch(const V * first, const V * last, V * dst,
+ boost::mpl::bool_<true> const& /*use_memcpy*/)
+{
+ typename std::iterator_traits<V*>::difference_type d = std::distance(first, last);
+ ::memcpy(dst, first, sizeof(V) * d);
+ return dst + d;
+}
+
+template <typename I, typename F>
+F uninitialized_copy_dispatch(I first, I last, F dst,
+ boost::mpl::bool_<false> const& /*use_memcpy*/)
+{
+ return std::uninitialized_copy(first, last, dst); // may throw
+}
+
+template <typename I, typename F>
+F uninitialized_copy(I first, I last, F dst)
+{
+ typedef typename
+ ::boost::mpl::and_<
+ are_corresponding_pointers<I, F>,
+ ::boost::has_trivial_copy<
+ typename ::boost::iterator_value<F>::type
+ >
+ >::type use_memcpy;
+
+ return uninitialized_copy_dispatch(first, last, dst, use_memcpy()); // may throw
+}
+
+// uninitialized_fill
+
+template <typename V>
+void uninitialized_fill_dispatch(V * ptr, V const& v,
+ boost::mpl::bool_<true> const& /*use_memcpy*/)
+{
+ ::memcpy(ptr, boost::addressof(v), sizeof(V));
+}
+
+template <typename I, typename V>
+void uninitialized_fill_dispatch(I pos, V const& v,
+ boost::mpl::bool_<false> const& /*use_memcpy*/)
+{
+ new (static_cast<void*>(&*pos)) V(v); // may throw
+}
+
+template <typename I, typename V>
+void uninitialized_fill(I dst, V const& v)
+{
+ typedef typename
+ ::boost::mpl::and_<
+ is_corresponding_value<I, V>,
+ ::boost::has_trivial_copy<V>
+ >::type
+ use_memcpy;
+
+ uninitialized_fill_dispatch(dst, v, use_memcpy()); // may throw
+}
+
+// move
+
+// TODO use boost::move(I, I, O) instead of copy/assignment
+
+template <typename V>
+V * move_dispatch(const V * first, const V * last, V * dst,
+ boost::mpl::bool_<true> const& /*use_memmove*/)
+{
+ typename std::iterator_traits<V*>::difference_type d = std::distance(first, last);
+ ::memmove(dst, first, sizeof(V) * d);
+ return dst + d;
+}
+
+template <typename I, typename O>
+O move_dispatch(I first, I last, O dst,
+ boost::mpl::bool_<false> const& /*use_memmove*/)
+{
+ return std::copy(first, last, dst); // may throw
+}
+
+template <typename I, typename O>
+O move(I first, I last, O dst)
+{
+ typedef typename
+ ::boost::mpl::and_<
+ are_corresponding_pointers<I, O>,
+ ::boost::has_trivial_assign<
+ typename ::boost::iterator_value<O>::type
+ >
+ >::type
+ use_memmove;
+
+ return move_dispatch(first, last, dst, use_memmove()); // may throw
+}
+
#else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template <typename I, typename O>
inline O copy(I first, I last, O dst)
{
- return std::copy(first, last, dst);
+ return std::copy(first, last, dst); // may throw
+}
+
+template <typename I, typename F>
+F uninitialized_copy(I first, I last, F dst)
+{
+ return std::uninitialized_copy(first, last, dst); // may throw
+}
+
+template <typename I, typename V>
+void uninitialized_fill(I pos, V const& v)
+{
+ new (static_cast<void*>(&*pos)) V(v); // may throw
+}
+
+template <typename I, typename O>
+O move(I first, I last, O dst)
+{
+ return std::copy(first, last, dst); // may throw
}
#endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
Modified: sandbox/static_vector/boost/container/static_vector.hpp
==============================================================================
--- sandbox/static_vector/boost/container/static_vector.hpp (original)
+++ sandbox/static_vector/boost/container/static_vector.hpp 2012-12-18 21:01:09 EST (Tue, 18 Dec 2012)
@@ -20,7 +20,6 @@
#include <boost/swap.hpp>
#include <boost/integer.hpp>
#include <boost/iterator/reverse_iterator.hpp>
-#include <boost/utility/addressof.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/if.hpp>
@@ -701,64 +700,15 @@
{
namespace sv = detail::static_vector;
sv::copy(first, last, dst);
-
- //detail::static_vector::copy(first, last, dst);
-
- //typedef typename
- // mpl::and_<
- // has_trivial_assign<value_type>,
- // mpl::or_<
- // is_same<Iterator, value_type *>,
- // is_same<Iterator, const value_type *>
- // >
- // >::type
- //use_memcpy;
- //
- //copy_dispatch(first, last, dst, use_memcpy()); // may throw
- }
-
- //inline static void copy_dispatch(const value_type * first, const value_type * last, value_type * dst,
- // boost::mpl::bool_<true> const& /*use_memcpy*/)
- //{
- // ::memcpy(dst, first, sizeof(value_type) * std::distance(first, last));
- //}
-
- //template <typename Iterator>
- //inline static void copy_dispatch(Iterator first, Iterator last, value_type * dst,
- // boost::mpl::bool_<false> const& /*use_memcpy*/)
- //{
- // std::copy(first, last, dst); // may throw
- //}
+ }
// uninitialized_copy
template <typename Iterator>
void uninitialized_copy(Iterator first, Iterator last, iterator dst)
{
- typedef typename
- mpl::and_<
- has_trivial_copy<value_type>,
- mpl::or_<
- is_same<Iterator, value_type *>,
- is_same<Iterator, const value_type *>
- >
- >::type
- use_memcpy;
-
- this->uninitialized_copy_dispatch(first, last, dst, use_memcpy()); // may throw
- }
-
- void uninitialized_copy_dispatch(const value_type * first, const value_type * last, value_type * dst,
- boost::mpl::bool_<true> const& /*use_memcpy*/)
- {
- ::memcpy(dst, first, sizeof(value_type) * std::distance(first, last));
- }
-
- template <typename Iterator>
- void uninitialized_copy_dispatch(Iterator first, Iterator last, value_type * dst,
- boost::mpl::bool_<false> const& /*use_memcpy*/)
- {
- std::uninitialized_copy(first, last, dst); // may throw
+ namespace sv = detail::static_vector;
+ sv::uninitialized_copy(first, last, dst);
}
// uninitialized_fill
@@ -766,46 +716,16 @@
template <typename V>
void uninitialized_fill(iterator dst, V const& v)
{
- typedef typename
- mpl::and_<
- has_trivial_copy<value_type>,
- is_same<Value, value_type>
- >::type
- use_memcpy;
-
- uninitialized_fill_dispatch(dst, v, use_memcpy()); // may throw
- }
-
- void uninitialized_fill_dispatch(value_type * ptr, value_type const& v,
- boost::mpl::bool_<true> const& /*use_memcpy*/)
- {
- ::memcpy(ptr, boost::addressof(v), sizeof(value_type));
- }
-
- template <typename V>
- void uninitialized_fill_dispatch(value_type * ptr, V const& v,
- boost::mpl::bool_<false> const& /*use_memcpy*/)
- {
- new (ptr) value_type(v); // may throw
+ namespace sv = detail::static_vector;
+ sv::uninitialized_fill(dst, v);
}
// move
void move(iterator first, iterator last, iterator dst)
{
- this->move_dispatch(first, last, dst, has_trivial_assign<value_type>()); // may throw
- }
-
- void move_dispatch(value_type * first, value_type * last, value_type * dst,
- boost::true_type const& /*has_trivial_assign*/)
- {
- ::memmove(dst, first, sizeof(value_type) * std::distance(first, last));
- }
-
- void move_dispatch(value_type * first, value_type * last, value_type * dst,
- boost::false_type const& /*has_trivial_assign*/)
- {
- std::copy(first, last, dst); // may throw
+ namespace sv = detail::static_vector;
+ sv::move(first, last, dst);
}
// move_backward
Modified: sandbox/static_vector/examples/times.cpp
==============================================================================
--- sandbox/static_vector/examples/times.cpp (original)
+++ sandbox/static_vector/examples/times.cpp 2012-12-18 21:01:09 EST (Tue, 18 Dec 2012)
@@ -38,10 +38,10 @@
for ( size_t i = 0 ; i < sv.capacity() ; ++i)
sv.push_back(V(i));
- static_vector<V, N> sv2;
size_t dummy = 0;
for ( size_t i = 0 ; i < count ; ++i )
{
+ static_vector<V, N> sv2(sv);
sv2.assign(sv.begin(), sv.end());
dummy += sv2[0];
}
@@ -51,11 +51,7 @@
int main()
{
-#ifdef NDEBUG
- size_t count = 5000000;
-#else
- size_t count = 50000;
-#endif
+ size_t count = 2000000;
std::fstream f("foobar.txt", std::ios::in | std::ios::out);
f >> count;
@@ -82,4 +78,4 @@
}
}
-}
\ No newline at end of file
+}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk