diff --git a/include/boost/geometry/extensions/index/static_vector.hpp b/include/boost/geometry/extensions/index/static_vector.hpp index 5ceadba4f..3a66287aa 100644 --- a/include/boost/geometry/extensions/index/static_vector.hpp +++ b/include/boost/geometry/extensions/index/static_vector.hpp @@ -58,18 +58,17 @@ public: {} // strong - explicit static_vector(size_type s) + explicit static_vector(size_type count) : m_size(0) { - resize(s); // may throw + resize(count); // may throw } // strong - //template - static_vector(size_type s, value_type const& value) + static_vector(size_type count, value_type const& value) : m_size(0) { - resize(s, value); // may throw + resize(count, value); // may throw } // strong @@ -105,47 +104,45 @@ public: } // strong - void resize(size_type s) + void resize(size_type count) { - if ( s < m_size ) + if ( count < m_size ) { - this->destroy(this->begin() + s, this->end()); + this->destroy(this->begin() + count, this->end()); } else { - BOOST_ASSERT_MSG(s <= Capacity, "size can't exceed the capacity"); - //if ( Capacity <= s ) throw std::bad_alloc(); - this->construct(this->ptr(m_size), this->ptr(s)); // may throw + BOOST_ASSERT_MSG(count <= Capacity, "size can't exceed the capacity"); + //if ( Capacity <= count ) throw std::bad_alloc(); + this->construct(this->ptr(m_size), this->ptr(count)); // may throw } - m_size = s; // update end + m_size = count; // update end } // strong - //template - void resize(size_type s, value_type const& value) + void resize(size_type count, value_type const& value) { - if ( s < m_size ) + if ( count < m_size ) { - this->destroy(this->begin() + s, this->end()); + this->destroy(this->begin() + count, this->end()); } else { - BOOST_ASSERT_MSG(s <= Capacity, "size can't exceed the capacity"); - //if ( Capacity <= s ) throw std::bad_alloc(); - std::uninitialized_fill(this->ptr(m_size), this->ptr(s), value); // may throw + BOOST_ASSERT_MSG(count <= Capacity, "size can't exceed the capacity"); + //if ( Capacity <= count ) throw std::bad_alloc(); + std::uninitialized_fill(this->ptr(m_size), this->ptr(count), value); // may throw } - m_size = s; // update end + m_size = count; // update end } // nothrow - void reserve(size_type BOOST_GEOMETRY_INDEX_ASSERT_UNUSED_PARAM(s)) + void reserve(size_type BOOST_GEOMETRY_INDEX_ASSERT_UNUSED_PARAM(count)) { - BOOST_ASSERT_MSG(s <= Capacity, "size can't exceed the capacity"); - //if ( Capacity <= s ) throw std::bad_alloc(); + BOOST_ASSERT_MSG(count <= Capacity, "size can't exceed the capacity"); + //if ( Capacity <= count ) throw std::bad_alloc(); } // strong - //template void push_back(value_type const& value) { BOOST_ASSERT_MSG(m_size < Capacity, "size can't exceed the capacity"); @@ -170,6 +167,24 @@ public: assign_dispatch(first, last, traversal()); // may throw } + // basic + void assign(size_type count, value_type const& value) + { + if ( count < m_size ) + { + std::fill_n(this->begin(), count, value); + this->destroy(this->begin() + count, this->end()); + } + else + { + std::fill_n(this->begin(), m_size, value); + BOOST_ASSERT_MSG(count <= Capacity, "size can't exceed the capacity"); + //if ( Capacity <= count ) throw std::bad_alloc(); + std::uninitialized_fill(this->ptr(m_size), this->ptr(count), value); // may throw + } + m_size = count; // update end + } + // nothrow void clear() { @@ -236,8 +251,8 @@ public: } // nothrow - Value * data() { return this->ptr(0); } - const Value * data() const { return this->ptr(0); } + Value * data() { return this->ptr(); } + const Value * data() const { return this->ptr(); } // nothrow iterator begin() { return this->ptr(); } diff --git a/test/static_vector.cpp b/test/static_vector.cpp index 4e123ddee..6e122d9b6 100644 --- a/test/static_vector.cpp +++ b/test/static_vector.cpp @@ -17,7 +17,7 @@ using namespace boost::geometry::index; class value_ndc { public: - value_ndc(int a) : aa(a) {} + explicit value_ndc(int a) : aa(a) {} ~value_ndc() {} bool operator==(value_ndc const& v) const { return aa == v.aa; } private: @@ -29,7 +29,7 @@ private: class value_nd { public: - value_nd(int a) : aa(a) {} + explicit value_nd(int a) : aa(a) {} ~value_nd() {} bool operator==(value_nd const& v) const { return aa == v.aa; } private: @@ -39,7 +39,7 @@ private: class value_nc { public: - value_nc(int a = 0) : aa(a) {} + explicit value_nc(int a = 0) : aa(a) {} ~value_nc() {} bool operator==(value_nc const& v) const { return aa == v.aa; } private: @@ -51,7 +51,7 @@ private: class counting_value { public: - counting_value(int a = 0) : aa(a) { ++c(); } + explicit counting_value(int a = 0) : aa(a) { ++c(); } counting_value(counting_value const& v) : aa(v.aa) { ++c(); } counting_value & operator=(counting_value const& v) { aa = v.aa; return *this; } ~counting_value() { --c(); } @@ -181,7 +181,7 @@ void test_pop_back_nd() static_vector s; for ( size_t i = 0 ; i < N ; ++i ) - s.push_back(i); + s.push_back(T(i)); for ( size_t i = N ; i > 1 ; --i ) { @@ -204,7 +204,7 @@ void test_compare_ranges(It1 first1, It1 last1, It2 first2, It2 last2) } template -void test_copy_and_assign_nd() +void test_copy_and_assign_nd(T const& val) { static_vector s; std::vector v; @@ -212,9 +212,9 @@ void test_copy_and_assign_nd() for ( size_t i = 0 ; i < N ; ++i ) { - s.push_back(i); - v.push_back(i); - l.push_back(i); + s.push_back(T(i)); + v.push_back(T(i)); + l.push_back(T(i)); } // copy ctor { @@ -268,6 +268,35 @@ void test_copy_and_assign_nd() BOOST_CHECK(l.size() == s1.size()); test_compare_ranges(l.begin(), l.end(), s1.begin(), s1.end()); } + // assign(N, V) + { + static_vector s1(s); + test_compare_ranges(s.begin(), s.end(), s1.begin(), s1.end()); + std::vector a(N, val); + s1.assign(N, val); + test_compare_ranges(a.begin(), a.end(), s1.begin(), s1.end()); + } +} + +template +void test_iterators_nd() +{ + static_vector s; + std::vector v; + + for ( size_t i = 0 ; i < N ; ++i ) + { + s.push_back(T(i)); + v.push_back(T(i)); + } + + test_compare_ranges(s.begin(), s.end(), v.begin(), v.end()); + test_compare_ranges(s.rbegin(), s.rend(), v.rbegin(), v.rend()); + + s.assign(v.rbegin(), v.rend()); + + test_compare_ranges(s.begin(), s.end(), v.rbegin(), v.rend()); + test_compare_ranges(s.rbegin(), s.rend(), v.begin(), v.end()); } int test_main(int, char* []) @@ -285,8 +314,8 @@ int test_main(int, char* []) BOOST_CHECK(counting_value::count() == 0); test_ctor_nd(5, 1); - test_ctor_nd(5, 1); - test_ctor_nd(5, 1); + test_ctor_nd(5, value_nd(1)); + test_ctor_nd(5, counting_value(1)); BOOST_CHECK(counting_value::count() == 0); test_resize_nc(5); @@ -295,8 +324,8 @@ int test_main(int, char* []) BOOST_CHECK(counting_value::count() == 0); test_resize_nd(5, 1); - test_resize_nd(5, 1); - test_resize_nd(5, 1); + test_resize_nd(5, value_nd(1)); + test_resize_nd(5, counting_value(1)); BOOST_CHECK(counting_value::count() == 0); test_push_back_nd(); @@ -309,9 +338,14 @@ int test_main(int, char* []) test_pop_back_nd(); BOOST_CHECK(counting_value::count() == 0); - test_copy_and_assign_nd(); - test_copy_and_assign_nd(); - test_copy_and_assign_nd(); + test_copy_and_assign_nd(1); + test_copy_and_assign_nd(value_nd(1)); + test_copy_and_assign_nd(counting_value(1)); + BOOST_CHECK(counting_value::count() == 0); + + test_iterators_nd(); + test_iterators_nd(); + test_iterators_nd(); BOOST_CHECK(counting_value::count() == 0); return 0;