|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r81930 - sandbox-branches/geometry/index/boost/geometry/extensions/index
From: adam.wulkiewicz_at_[hidden]
Date: 2012-12-13 21:02:53
Author: awulkiew
Date: 2012-12-13 21:02:52 EST (Thu, 13 Dec 2012)
New Revision: 81930
URL: http://svn.boost.org/trac/boost/changeset/81930
Log:
added optimized versions of static_vector operations, however preliminary tests on linux shows no speed difference
Text files modified:
sandbox-branches/geometry/index/boost/geometry/extensions/index/static_vector.hpp | 115 +++++++++++++++++++++++++--------------
1 files changed, 72 insertions(+), 43 deletions(-)
Modified: sandbox-branches/geometry/index/boost/geometry/extensions/index/static_vector.hpp
==============================================================================
--- sandbox-branches/geometry/index/boost/geometry/extensions/index/static_vector.hpp (original)
+++ sandbox-branches/geometry/index/boost/geometry/extensions/index/static_vector.hpp 2012-12-13 21:02:52 EST (Thu, 13 Dec 2012)
@@ -54,7 +54,7 @@
//BOOST_ASSERT_MSG(other.m_size <= Capacity, "capacity too small");
this->uninitialized_copy(other.ptr(0), other.ptr(other.m_size), this->ptr(0),
- boost::has_trivial_copy_constructor<value_type>());
+ boost::has_trivial_copy<value_type>());
m_size = other.m_size;
}
@@ -63,16 +63,24 @@
{
//BOOST_ASSERT_MSG(other.m_size <= Capacity, "capacity too small");
- size_t min_size = m_size < other.m_size ? m_size : other.m_size;
-
- for ( size_t i = 0 ; i < min_size ; ++i )
- (*this)[i] = other[i]; // may throw
+ if ( m_size <= other.m_size )
+ {
+ this->copy(other.ptr(0), other.ptr(m_size), this->ptr(0),
+ boost::has_trivial_assign<value_type>()); // may throw
- for ( ; m_size < other.m_size ; ++m_size )
- this->construct(m_size, other[m_size]); // may throw
+ this->uninitialized_copy(other.ptr(m_size), other.ptr(other.m_size), this->ptr(m_size),
+ boost::has_trivial_copy<value_type>()); // may throw
+ m_size = other.m_size;
+ }
+ else
+ {
+ this->copy(other.ptr(0), other.ptr(other.m_size), this->ptr(0),
+ boost::has_trivial_assign<value_type>()); // may throw
- for ( size_t i = other.m_size ; i < m_size ; ++i )
- this->destroy(i);
+ this->destroy(this->ptr(other.m_size), this->ptr(m_size),
+ boost::has_trivial_destructor<value_type>());
+ m_size = other.m_size;
+ }
return *this;
}
@@ -80,31 +88,24 @@
// nothrow
~static_vector()
{
- this->destroy(0, m_size);
+ this->destroy(this->ptr(0), this->ptr(m_size),
+ boost::has_trivial_destructor<value_type>());
}
- // nothrow if s <= size(), strong otherwise
+ // strong
void resize(size_type s)
{
if ( s < m_size )
{
- this->destroy(s, m_size);
+ this->destroy(this->ptr(s), this->ptr(m_size),
+ boost::has_trivial_destructor<value_type>());
m_size = s;
}
else
{
- size_type i = m_size;
- try
- {
- for ( ; i < s ; ++i )
- this->construct(i); // may throw
- m_size = s;
- }
- catch(...)
- {
- this->destroy(m_size, i);
- throw; // rethrow
- }
+ this->construct(this->ptr(m_size), this->ptr(s),
+ boost::has_trivial_constructor<value_type>()); // may throw
+ m_size = s;
}
}
@@ -117,8 +118,11 @@
// strong
void push_back(Value const& value)
{
+ if ( Capacity <= m_size )
+ std::cout << m_size << '\n';
BOOST_ASSERT_MSG(m_size < Capacity, "max capacity reached");
- this->construct(m_size, value); // may throw
+ this->uninitialized_copy(this->ptr(m_size), value,
+ boost::has_trivial_copy<value_type>()); // may throw
++m_size;
}
@@ -127,13 +131,14 @@
{
BOOST_ASSERT_MSG(0 < m_size, "the container is empty");
--m_size;
- this->destroy(m_size);
+ this->destroy(this->ptr(m_size), boost::has_trivial_destructor<value_type>());
}
// nothrow
void clear()
{
- this->destroy(0, m_size);
+ this->destroy(this->ptr(0), this->ptr(m_size),
+ boost::has_trivial_destructor<value_type>());
m_size = 0;
}
@@ -196,6 +201,16 @@
bool empty() const { return 0 == m_size; }
private:
+ void copy(const value_type * first, const value_type * last, value_type * dst, boost::true_type const&)
+ {
+ ::memcpy(dst, first, sizeof(value_type) * std::distance(first, last));
+ }
+
+ void copy(const value_type * first, const value_type * last, value_type * dst, boost::false_type const&)
+ {
+ std::copy(first, last, dst); // may throw
+ }
+
void uninitialized_copy(const value_type * first, const value_type * last, value_type * dst, boost::true_type const&)
{
::memcpy(dst, first, sizeof(value_type) * std::distance(first, last));
@@ -203,10 +218,20 @@
void uninitialized_copy(const value_type * first, const value_type * last, value_type * dst, boost::false_type const&)
{
- std::uninitialized_copy(first, last, dst); // may throw
+ std::uninitialized_copy(first, last, dst); // may throw
}
- void destroy(const value_type * first, const value_type * last, boost::true_type const&)
+ void uninitialized_copy(value_type * ptr, value_type const& v, boost::true_type const&)
+ {
+ ::memcpy(ptr, &v, sizeof(value_type));
+ }
+
+ void uninitialized_copy(value_type * ptr, value_type const& v, boost::false_type const&)
+ {
+ new (ptr) value_type(v); // may throw
+ }
+
+ void destroy(const value_type *, const value_type *, boost::true_type const&)
{}
void destroy(const value_type * first, const value_type * last, boost::false_type const&)
@@ -215,26 +240,30 @@
first->~value_type();
}
- // old version - remove
- void destroy(size_type first_i, size_type last_i)
- {
- for ( size_type i = first_i ; i < last_i ; ++i )
- this->destroy(i);
- }
+ void destroy(const value_type *, boost::true_type const&)
+ {}
- void construct(size_type i)
+ void destroy(const value_type * ptr, boost::false_type const&)
{
- new (this->ptr(i)) value_type(); // may throw
+ ptr->~value_type();
}
- void construct(size_type i, Value const& value)
- {
- new (this->ptr(i)) value_type(value); // may throw
- }
+ void construct(value_type *, value_type *, boost::true_type const&)
+ {}
- void destroy(size_type i)
+ void construct(value_type * first, value_type * last, boost::false_type const&)
{
- this->ptr(i)->~value_type();
+ value_type * it = first;
+ try
+ {
+ for ( ; it != last ; ++it )
+ new (it) value_type(); // may throw
+ }
+ catch(...)
+ {
+ this->destroy(first, it, boost::has_trivial_destructor<value_type>());
+ throw;
+ }
}
Value * ptr(size_type i)
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