From 5669e8dbe9ae5ee515ebc8a55e48f3659dd341eb Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 18 Nov 2017 16:28:22 +0100 Subject: [PATCH 1/5] Add wxVector::operator==() and !=() Make this class more consistent with std::vector<>. --- include/wx/vector.h | 19 +++++++++++++++++++ interface/wx/vector.h | 14 ++++++++++++++ tests/vectors/vectors.cpp | 17 +++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/include/wx/vector.h b/include/wx/vector.h index 686dc0a587..ed4bbd3a40 100644 --- a/include/wx/vector.h +++ b/include/wx/vector.h @@ -368,6 +368,25 @@ public: return *this; } + bool operator==(const wxVector& vb) const + { + if ( vb.m_size != m_size ) + return false; + + for ( size_type i = 0; i < m_size; i++ ) + { + if ( vb.m_values[i] != m_values[i] ) + return false; + } + + return true; + } + + bool operator!=(const wxVector& vb) const + { + return !(*this == vb); + } + void push_back(const value_type& v) { reserve(size() + 1); diff --git a/interface/wx/vector.h b/interface/wx/vector.h index f8b32e26d2..982f5e26d9 100644 --- a/interface/wx/vector.h +++ b/interface/wx/vector.h @@ -199,6 +199,20 @@ public: */ wxVector& operator=(const wxVector& vb); + /** + Equality operator. + + @since 3.1.1 + */ + wxVector& operator==(const wxVector& vb) const; + + /** + Inequality operator. + + @since 3.1.1 + */ + wxVector& operator!=(const wxVector& vb) const; + /** Returns item at position @a idx. */ diff --git a/tests/vectors/vectors.cpp b/tests/vectors/vectors.cpp index 001f79eabc..070babe160 100644 --- a/tests/vectors/vectors.cpp +++ b/tests/vectors/vectors.cpp @@ -314,3 +314,20 @@ void VectorsTestCase::Sort() CPPUNIT_ASSERT( v[idx-1] <= v[idx] ); } } + +TEST_CASE("wxVector::operator==", "[vector][compare]") +{ + wxVector v1, v2; + CHECK( v1 == v2 ); + CHECK( !(v1 != v2) ); + + v1.push_back("foo"); + CHECK( v1 != v2 ); + + v2.push_back("foo"); + CHECK( v1 == v2 ); + + v1.push_back("bar"); + v2.push_back("baz"); + CHECK( v1 != v2 ); +} From 25a7c706319f29687f3977f0931a2bd384ed97f9 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 18 Nov 2017 16:32:31 +0100 Subject: [PATCH 2/5] Implement wxVector::reverse_iterator::operator-() Make wxVector reverse iterators (and const versions of them) more compatible with the std::vector ones. --- include/wx/vector.h | 4 ++++ tests/vectors/vectors.cpp | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/wx/vector.h b/include/wx/vector.h index ed4bbd3a40..446af9a12c 100644 --- a/include/wx/vector.h +++ b/include/wx/vector.h @@ -170,6 +170,8 @@ public: { return reverse_iterator(m_ptr + n); } reverse_iterator& operator-=(difference_type n) { m_ptr += n; return *this; } + difference_type operator-(const reverse_iterator& it) const + { return it.m_ptr - m_ptr; } reference operator[](difference_type n) const { return *(*this + n); } @@ -215,6 +217,8 @@ public: { return const_reverse_iterator(m_ptr + n); } const_reverse_iterator& operator-=(difference_type n) { m_ptr += n; return *this; } + difference_type operator-(const const_reverse_iterator& it) const + { return it.m_ptr - m_ptr; } const_reference operator[](difference_type n) const { return *(*this + n); } diff --git a/tests/vectors/vectors.cpp b/tests/vectors/vectors.cpp index 070babe160..62e12e290e 100644 --- a/tests/vectors/vectors.cpp +++ b/tests/vectors/vectors.cpp @@ -331,3 +331,23 @@ TEST_CASE("wxVector::operator==", "[vector][compare]") v2.push_back("baz"); CHECK( v1 != v2 ); } + +TEST_CASE("wxVector::reverse_iterator", "[vector][reverse_iterator]") +{ + wxVector v; + for ( int i = 0; i < 10; ++i ) + v.push_back(i + 1); + + const wxVector::reverse_iterator rb = v.rbegin(); + const wxVector::reverse_iterator re = v.rend(); + CHECK( re - rb == 10 ); + + wxVector::reverse_iterator ri = rb; + ++ri; + CHECK( ri - rb == 1 ); + CHECK( re - ri == 9 ); + + ri = rb + 2; + CHECK( ri - rb == 2 ); + CHECK( re - ri == 8 ); +} From 8246c922afee0324d01fd626f625aa6e4fed0f60 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 18 Nov 2017 16:36:41 +0100 Subject: [PATCH 3/5] Add wxVector::insert() overload taking count of items to insert Generalize the existing insert() to be more compatible with std::vector. --- include/wx/vector.h | 18 ++++++++++++------ interface/wx/vector.h | 9 +++++++++ tests/vectors/vectors.cpp | 10 ++++++++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/include/wx/vector.h b/include/wx/vector.h index 446af9a12c..594a43cc58 100644 --- a/include/wx/vector.h +++ b/include/wx/vector.h @@ -442,14 +442,14 @@ public: const_reverse_iterator rbegin() const { return const_reverse_iterator(end() - 1); } const_reverse_iterator rend() const { return const_reverse_iterator(begin() - 1); } - iterator insert(iterator it, const value_type& v = value_type()) + iterator insert(iterator it, size_type count, const value_type& v) { // NB: this must be done before reserve(), because reserve() // invalidates iterators! const size_t idx = it - begin(); const size_t after = end() - it; - reserve(size() + 1); + reserve(size() + count); // the place where the new element is going to be inserted value_type * const place = m_values + idx; @@ -457,27 +457,33 @@ public: // unless we're inserting at the end, move following elements out of // the way: if ( after > 0 ) - Ops::MemmoveForward(place + 1, place, after); + Ops::MemmoveForward(place + count, place, after); // if the ctor called below throws an exception, we need to move all // the elements back to their original positions in m_values wxScopeGuard moveBack = wxMakeGuard( - Ops::MemmoveBackward, place, place + 1, after); + Ops::MemmoveBackward, place, place + count, after); if ( !after ) moveBack.Dismiss(); // use placement new to initialize new object in preallocated place in // m_values and store 'v' in it: - ::new(place) value_type(v); + for ( size_type i = 0; i < count; i++ ) + ::new(place + i) value_type(v); // now that we did successfully add the new element, increment the size // and disable moving the items back moveBack.Dismiss(); - m_size++; + m_size += count; return begin() + idx; } + iterator insert(iterator it, const value_type& v = value_type()) + { + return insert(it, 1, v); + } + iterator erase(iterator it) { return erase(it, it + 1); diff --git a/interface/wx/vector.h b/interface/wx/vector.h index 982f5e26d9..0f55c1b460 100644 --- a/interface/wx/vector.h +++ b/interface/wx/vector.h @@ -194,6 +194,15 @@ public: */ iterator insert(iterator it, const value_type& v = value_type()); + /** + Insert the given number of copies of @a v at the given position. + + @return Iterator for the first inserted item. + + @since 3.1.1 + */ + iterator insert(iterator it, size_type count, const value_type& v); + /** Assignment operator. */ diff --git a/tests/vectors/vectors.cpp b/tests/vectors/vectors.cpp index 62e12e290e..92517636ef 100644 --- a/tests/vectors/vectors.cpp +++ b/tests/vectors/vectors.cpp @@ -162,6 +162,16 @@ void VectorsTestCase::Insert() CPPUNIT_ASSERT( v[1] == 'a' ); CPPUNIT_ASSERT( v[2] == 'X' ); CPPUNIT_ASSERT( v[3] == 'b' ); + + v.insert(v.begin() + 3, 3, 'Z'); + REQUIRE( v.size() == 7 ); + CHECK( v[0] == '0' ); + CHECK( v[1] == 'a' ); + CHECK( v[2] == 'X' ); + CHECK( v[3] == 'Z' ); + CHECK( v[4] == 'Z' ); + CHECK( v[5] == 'Z' ); + CHECK( v[6] == 'b' ); } void VectorsTestCase::Erase() From 9e714fdb5ec791983d15d1ec48f12e9f0c74d717 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 18 Nov 2017 16:55:08 +0100 Subject: [PATCH 4/5] Fix wxVector::assign() for integer parameters Using assign() with int (in fact, any integral type) should select the (size_type count, const T& value) overload, but didn't, which was incompatible with std::vector<>. Fix this by adding the same tag-dispatching technique as used by the real std::vector<> implementations themselves, except that we dispatch on integer types because we can't be totally certain that std::iterator_traits<> are specialized for whatever iterator-like object could be used with wxVector. --- include/wx/vector.h | 74 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 13 deletions(-) diff --git a/include/wx/vector.h b/include/wx/vector.h index 594a43cc58..aa74c6586e 100644 --- a/include/wx/vector.h +++ b/include/wx/vector.h @@ -113,6 +113,34 @@ struct wxVectorMemOpsGeneric } }; +// We need to distinguish integers from iterators in assign() overloads and the +// simplest way to do it would be by using std::iterator_traits<>, however this +// might break existing code using custom iterator classes but not specializing +// iterator_traits<> for them, so we approach the problem from the other end +// and use our own traits that we specialize for all integer types. + +struct IsIntType {}; +struct IsNotIntType {}; + +template struct IsInt : IsNotIntType {}; + +#define WX_DECLARE_TYPE_IS_INT(type) \ + template <> struct IsInt : IsIntType {} + +WX_DECLARE_TYPE_IS_INT(unsigned char); +WX_DECLARE_TYPE_IS_INT(signed char); +WX_DECLARE_TYPE_IS_INT(unsigned short int); +WX_DECLARE_TYPE_IS_INT(signed short int); +WX_DECLARE_TYPE_IS_INT(unsigned int); +WX_DECLARE_TYPE_IS_INT(signed int); +WX_DECLARE_TYPE_IS_INT(unsigned long int); +WX_DECLARE_TYPE_IS_INT(signed long int); +#ifdef wxLongLong_t +WX_DECLARE_TYPE_IS_INT(wxLongLong_t); +WX_DECLARE_TYPE_IS_INT(wxULongLong_t); +#endif + +#undef WX_DECLARE_TYPE_IS_INT } // namespace wxPrivate @@ -269,23 +297,13 @@ public: void assign(size_type p_size, const value_type& v) { - clear(); - reserve(p_size); - for ( size_t n = 0; n < p_size; n++ ) - push_back(v); + AssignFromValue(p_size, v); } - template + template void assign(InputIterator first, InputIterator last) { - clear(); - - // Notice that it would be nice to call reserve() here but we can't do - // it for arbitrary input iterators, we should have a dispatch on - // iterator type and call it if possible. - - for ( InputIterator it = first; it != last; ++it ) - push_back(*it); + AssignDispatch(first, last, typename wxPrivate::IsInt()); } void swap(wxVector& v) @@ -545,6 +563,36 @@ private: push_back(v); } + void AssignFromValue(size_type p_size, const value_type& v) + { + clear(); + reserve(p_size); + for ( size_t n = 0; n < p_size; n++ ) + push_back(v); + } + + template + void AssignDispatch(InputIterator first, InputIterator last, + wxPrivate::IsIntType) + { + AssignFromValue(static_cast(first), + static_cast(last)); + } + + template + void AssignDispatch(InputIterator first, InputIterator last, + wxPrivate::IsNotIntType) + { + clear(); + + // Notice that it would be nice to call reserve() here but we can't do + // it for arbitrary input iterators, we should have a dispatch on + // iterator type and call it if possible. + + for ( InputIterator it = first; it != last; ++it ) + push_back(*it); + } + size_type m_size, m_capacity; value_type *m_values; From 53443b537075cb3ad0ce153596c79f442320f268 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 19 Nov 2017 22:09:37 +0100 Subject: [PATCH 5/5] Add wxVector::shrink_to_fit() for C++11 compatibility Also use this for wxArray::Shrink() implementation as it's more efficient than the old swap-based implementation which requires an extra memory allocation instead of really shrinking the existing one. --- include/wx/vector.h | 6 ++++++ interface/wx/vector.h | 12 ++++++++++++ tests/vectors/vectors.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/include/wx/vector.h b/include/wx/vector.h index aa74c6586e..df99c6b0c0 100644 --- a/include/wx/vector.h +++ b/include/wx/vector.h @@ -375,6 +375,12 @@ public: return m_capacity; } + void shrink_to_fit() + { + m_values = Ops::Realloc(m_values, m_size, m_size); + m_capacity = m_size; + } + bool empty() const { return size() == 0; diff --git a/interface/wx/vector.h b/interface/wx/vector.h index 0f55c1b460..60d953d94b 100644 --- a/interface/wx/vector.h +++ b/interface/wx/vector.h @@ -262,6 +262,18 @@ public: void resize(size_type n, const value_type& v); //@} + /** + Free unused memory allocated by the vector. + + Reduces the memory used by the vector to the bare minimum required to + hold its current number of elements, possibly 0. + + After calling this method, capacity() returns the same as size(). + + @since 3.1.1 + */ + void shrink_to_fit(); + /** Returns the size of the vector. */ diff --git a/tests/vectors/vectors.cpp b/tests/vectors/vectors.cpp index 92517636ef..8495cdca58 100644 --- a/tests/vectors/vectors.cpp +++ b/tests/vectors/vectors.cpp @@ -361,3 +361,27 @@ TEST_CASE("wxVector::reverse_iterator", "[vector][reverse_iterator]") CHECK( ri - rb == 2 ); CHECK( re - ri == 8 ); } + +TEST_CASE("wxVector::capacity", "[vector][capacity][shrink_to_fit]") +{ + wxVector v; + CHECK( v.capacity() == 0 ); + + v.push_back(0); + // When using the standard library vector, we don't know what growth + // strategy it uses, so we can't rely on this check passing, but with our + // own one we can, allowing us to check that shrink_to_fit() really shrinks + // the capacity below. +#if !wxUSE_STD_CONTAINERS + CHECK( v.capacity() > 1 ); +#endif + + v.shrink_to_fit(); + CHECK( v.capacity() == 1 ); + + v.erase(v.begin()); + CHECK( v.capacity() == 1 ); + + v.shrink_to_fit(); + CHECK( v.capacity() == 0 ); +}