diff options
author | tobo <tobo@yandex-team.com> | 2022-09-30 16:06:24 +0300 |
---|---|---|
committer | tobo <tobo@yandex-team.com> | 2022-09-30 16:06:24 +0300 |
commit | c391c3a2b201e8f113aea0fb26d947520eba0626 (patch) | |
tree | 6181c84c38316b35dd59bcd8b6bfa19b3c25e01c | |
parent | 0eb8f19e3c049c6509b831b0bf2935f1b9250290 (diff) | |
download | ydb-c391c3a2b201e8f113aea0fb26d947520eba0626.tar.gz |
emplace_back() with arguments support + return reference for consistency with TVector
-rw-r--r-- | library/cpp/containers/paged_vector/paged_vector.h | 7 | ||||
-rw-r--r-- | library/cpp/containers/paged_vector/ut/paged_vector_ut.cpp | 35 |
2 files changed, 39 insertions, 3 deletions
diff --git a/library/cpp/containers/paged_vector/paged_vector.h b/library/cpp/containers/paged_vector/paged_vector.h index 62a8574476..0a6dd8f83c 100644 --- a/library/cpp/containers/paged_vector/paged_vector.h +++ b/library/cpp/containers/paged_vector/paged_vector.h @@ -7,7 +7,7 @@ #include <iterator> namespace NPagedVector { - template <class T, ui32 PageSize = 1u << 20, class A = std::allocator<T>> + template <class T, ui32 PageSize = 1u << 20u, class A = std::allocator<T>> class TPagedVector; namespace NPrivate { @@ -263,9 +263,10 @@ namespace NPagedVector { return !empty(); } - void emplace_back() { + template<typename... Args> + reference emplace_back(Args&&... args) { PrepareAppend(); - CurrentPage().emplace_back(); + return CurrentPage().emplace_back(std::forward<Args>(args)...); } void push_back(const_reference t) { diff --git a/library/cpp/containers/paged_vector/ut/paged_vector_ut.cpp b/library/cpp/containers/paged_vector/ut/paged_vector_ut.cpp index e867808ee4..d059ce34ec 100644 --- a/library/cpp/containers/paged_vector/ut/paged_vector_ut.cpp +++ b/library/cpp/containers/paged_vector/ut/paged_vector_ut.cpp @@ -16,6 +16,8 @@ class TPagedVectorTest: public TTestBase { UNIT_TEST(TestAt) UNIT_TEST(TestAutoRef) UNIT_TEST(TestIterators) + UNIT_TEST(TestEmplaceBack1) + UNIT_TEST(TestEmplaceBack2) //UNIT_TEST(TestEbo) UNIT_TEST_SUITE_END(); @@ -341,6 +343,39 @@ private: UNIT_ASSERT(crvint.rbegin() != crvint.rend()); } + void TestEmplaceBack1() { + NPagedVector::TPagedVector<int, 3> vint; + + for (int i = 0; i < 55; ++i) { + UNIT_ASSERT_EQUAL(vint.emplace_back(i), i); + } + + UNIT_ASSERT_EQUAL(vint.size(), 55); + + for (int i = 0; i < 55; ++i) { + UNIT_ASSERT_EQUAL(vint[i], i); + } + } + + void TestEmplaceBack2() { + using TPair = std::pair<int, TString>; + NPagedVector::TPagedVector<TPair, 5> arr; + + for (int i = 0; i < 55; ++i) { + auto s = ToString(i); + auto& element = arr.emplace_back(i, s); + UNIT_ASSERT_EQUAL(element, std::make_pair(i, s)); + UNIT_ASSERT_UNEQUAL(element, std::make_pair(i + 1, s)); + } + + UNIT_ASSERT_EQUAL(arr.size(), 55); + + for (int i = 0; i < 55; ++i) { + UNIT_ASSERT_EQUAL(arr[i].first, i); + UNIT_ASSERT_EQUAL(arr[i].second, ToString(i)); + } + } + /* This test check a potential issue with empty base class * optimization. Some compilers (VC6) do not implement it * correctly resulting ina wrong behavior. */ |