diff options
author | tobo <tobo@yandex-team.com> | 2022-11-11 18:08:58 +0300 |
---|---|---|
committer | tobo <tobo@yandex-team.com> | 2022-11-11 18:08:58 +0300 |
commit | 9a5c5fc2b1980b22c0e398cb025dc6b19982da53 (patch) | |
tree | b2c3778c401788df654eec328ff8e27ffb3d8cea | |
parent | 43b8f65925b179307952889246ec94261f3702cf (diff) | |
download | ydb-9a5c5fc2b1980b22c0e398cb025dc6b19982da53.tar.gz |
make class more compatible with std::vector
-rw-r--r-- | library/cpp/containers/compact_vector/compact_vector.h | 46 | ||||
-rw-r--r-- | library/cpp/containers/compact_vector/compact_vector_ut.cpp | 31 |
2 files changed, 72 insertions, 5 deletions
diff --git a/library/cpp/containers/compact_vector/compact_vector.h b/library/cpp/containers/compact_vector/compact_vector.h index b3795708a5..b31a8b8464 100644 --- a/library/cpp/containers/compact_vector/compact_vector.h +++ b/library/cpp/containers/compact_vector/compact_vector.h @@ -31,12 +31,21 @@ private: return ((THeader*)Ptr) - 1; } + void destruct_at(size_t pos) { + (*this)[pos].~T(); + } + public: - typedef T* TIterator; - typedef const T* TConstIterator; + using value_type = T; + + using TIterator = T*; + using TConstIterator = const T*; + + using iterator = TIterator ; + using const_iterator = TConstIterator; - typedef TIterator iterator; - typedef TConstIterator const_iterator; + using reverse_iterator = std::reverse_iterator<iterator>; + using const_reverse_iterator = std::reverse_iterator<const_iterator>; TCompactVector() : Ptr(nullptr) @@ -81,7 +90,7 @@ public: ~TCompactVector() { for (size_t i = 0; i < Size(); ++i) { try { - (*this)[i].~T(); + destruct_at(i); } catch (...) { } } @@ -132,6 +141,22 @@ public: return End(); } + reverse_iterator rbegin() { + return std::make_reverse_iterator(end()); + } + + const_reverse_iterator rbegin() const { + return std::make_reverse_iterator(end()); + } + + reverse_iterator rend() { + return std::make_reverse_iterator(begin()); + } + + const_reverse_iterator rend() const { + return std::make_reverse_iterator(begin()); + } + void Swap(TThis& that) noexcept { DoSwap(Ptr, that.Ptr); } @@ -166,6 +191,10 @@ public: } } + void reserve(size_t newCapacity) { + Reserve(newCapacity); + } + size_t Size() const { return Ptr ? Header()->Size : 0; } @@ -248,6 +277,13 @@ public: Clear(); } + void erase(iterator position) { + Y_ENSURE(position >= begin() && position < end()); + std::move(position + 1, end(), position); + destruct_at(Size() - 1); + Header()->Size -= 1; + } + T& operator[](size_t index) { Y_ASSERT(index < Size()); return Ptr[index]; diff --git a/library/cpp/containers/compact_vector/compact_vector_ut.cpp b/library/cpp/containers/compact_vector/compact_vector_ut.cpp index 84cb7b645a..ed2936fb9e 100644 --- a/library/cpp/containers/compact_vector/compact_vector_ut.cpp +++ b/library/cpp/containers/compact_vector/compact_vector_ut.cpp @@ -94,4 +94,35 @@ Y_UNIT_TEST_SUITE(TCompactVectorTest) { UNIT_ASSERT_VALUES_EQUAL(3u, vector2[3]); UNIT_ASSERT_VALUES_EQUAL(5u, vector2[4]); } + + Y_UNIT_TEST(TestReverseIterators) { + TCompactVector<std::string> vector = { + "мама", + "мыла", + "раму" + }; + + TCompactVector<std::string> reverseVector(vector.rbegin(), vector.rend()); + UNIT_ASSERT_VALUES_EQUAL(3u, reverseVector.Size()); + + UNIT_ASSERT_VALUES_EQUAL("раму", reverseVector[0]); + UNIT_ASSERT_VALUES_EQUAL("мыла", reverseVector[1]); + UNIT_ASSERT_VALUES_EQUAL("мама", reverseVector[2]); + } + + Y_UNIT_TEST(TestErase) { + TCompactVector<std::string> vector = { + "мама", + "утром", + "мыла", + "раму" + }; + + vector.erase(vector.begin() + 1); + UNIT_ASSERT_VALUES_EQUAL(3u, vector.Size()); + + UNIT_ASSERT_VALUES_EQUAL("мама", vector[0]); + UNIT_ASSERT_VALUES_EQUAL("мыла", vector[1]); + UNIT_ASSERT_VALUES_EQUAL("раму", vector[2]); + } } |