diff options
author | innokentii <innokentii@yandex-team.com> | 2022-09-12 18:24:51 +0300 |
---|---|---|
committer | innokentii <innokentii@yandex-team.com> | 2022-09-12 18:24:51 +0300 |
commit | f68b69214c16dbfdf12930b40f4e415abf2e4093 (patch) | |
tree | 0b692b85cd10bf2288b17ffffc3b25b2758c7ae3 /library/cpp | |
parent | 2c619590073f5a8e9cc36236dd860e9dc0db9466 (diff) | |
download | ydb-f68b69214c16dbfdf12930b40f4e415abf2e4093.tar.gz |
Replace TString with TRope TEvVPut
add TRope to string comparators
make TRope from string constructor explicit
replace TString to TRope in couple places
Diffstat (limited to 'library/cpp')
-rw-r--r-- | library/cpp/actors/util/rope.h | 43 | ||||
-rw-r--r-- | library/cpp/actors/util/rope_ut.cpp | 36 |
2 files changed, 75 insertions, 4 deletions
diff --git a/library/cpp/actors/util/rope.h b/library/cpp/actors/util/rope.h index fd4a5acdba1..9b234755f8c 100644 --- a/library/cpp/actors/util/rope.h +++ b/library/cpp/actors/util/rope.h @@ -676,7 +676,7 @@ public: rope.InvalidateIterators(); } - TRope(TString s) { + explicit TRope(TString s) { if (s) { Size = s.size(); if (s.capacity() < 32) { @@ -971,6 +971,25 @@ public: return xIter.Valid() - yIter.Valid(); } + static int Compare(const TRope& x, const TString& y) { + TConstIterator xIter = x.Begin(); + const char* yData = y.data(); + size_t yOffset = 0; + while (xIter.Valid() && yOffset != y.size()) { + const size_t step = std::min(xIter.ContiguousSize(), y.size() - yOffset); + if (int res = memcmp(xIter.ContiguousData(), yData + yOffset, step)) { + return res; + } + xIter += step; + yOffset += step; + } + return xIter.Valid() - (yOffset != y.size()); + } + + static int Compare(const TString& x, const TRope& y) { + return -Compare(y, x); + } + // Use this method carefully -- it may significantly reduce performance when misused. TString ConvertToString() const { // TODO(innokentii): could be microoptimized for single TString case @@ -1073,10 +1092,16 @@ public: TString res = TString::Uninitialized(GetSize()); Begin().ExtractPlainDataAndAdvance(res.Detach(), res.size()); Erase(Begin(), End()); - Insert(End(), res); + Insert(End(), TRope(res)); } } + static TRope Uninitialized(size_t size) + { + TString res = TString::Uninitialized(size); + return TRope(res); + } + /** * Compacts data and calls GetData() on undelying container * WARN: Will copy if data isn't contiguous @@ -1133,6 +1158,20 @@ public: friend bool operator> (const TRope& x, const TRope& y) { return Compare(x, y) > 0; } friend bool operator>=(const TRope& x, const TRope& y) { return Compare(x, y) >= 0; } + friend bool operator==(const TRope& x, const TString& y) { return Compare(x, y) == 0; } + friend bool operator!=(const TRope& x, const TString& y) { return Compare(x, y) != 0; } + friend bool operator< (const TRope& x, const TString& y) { return Compare(x, y) < 0; } + friend bool operator<=(const TRope& x, const TString& y) { return Compare(x, y) <= 0; } + friend bool operator> (const TRope& x, const TString& y) { return Compare(x, y) > 0; } + friend bool operator>=(const TRope& x, const TString& y) { return Compare(x, y) >= 0; } + + friend bool operator==(const TString& x, const TRope& y) { return Compare(x, y) == 0; } + friend bool operator!=(const TString& x, const TRope& y) { return Compare(x, y) != 0; } + friend bool operator< (const TString& x, const TRope& y) { return Compare(x, y) < 0; } + friend bool operator<=(const TString& x, const TRope& y) { return Compare(x, y) <= 0; } + friend bool operator> (const TString& x, const TRope& y) { return Compare(x, y) > 0; } + friend bool operator>=(const TString& x, const TRope& y) { return Compare(x, y) >= 0; } + private: void Cut(TIterator begin, TIterator end, TRope *target) { // ensure all iterators are belong to us diff --git a/library/cpp/actors/util/rope_ut.cpp b/library/cpp/actors/util/rope_ut.cpp index b9ca3715ce1..55f995b66ab 100644 --- a/library/cpp/actors/util/rope_ut.cpp +++ b/library/cpp/actors/util/rope_ut.cpp @@ -61,6 +61,26 @@ TString RopeToString(const TRope& rope) { TString Text = "No elements are copied or moved, only the internal pointers of the list nodes are re-pointed."; Y_UNIT_TEST_SUITE(TRope) { + Y_UNIT_TEST(StringCompare) { + TRope rope = CreateRope(Text, 10); + UNIT_ASSERT_EQUAL(rope, Text); + UNIT_ASSERT_EQUAL(Text, rope); + rope.Erase(rope.Begin() + 10, rope.Begin() + 11); + UNIT_ASSERT_UNEQUAL(rope, Text); + UNIT_ASSERT_UNEQUAL(Text, rope); + TString str("aa"); + rope = TRope(TString("ab")); + UNIT_ASSERT_LT(str, rope); + UNIT_ASSERT_GT(rope, str); + str = TString("aa"); + rope = TRope(TString("a")); + UNIT_ASSERT_LT(rope, str); + UNIT_ASSERT_GT(str, rope); + str = TString("a"); + rope = TRope(TString("aa")); + UNIT_ASSERT_LT(str, rope); + UNIT_ASSERT_GT(rope, str); + } Y_UNIT_TEST(Leak) { const size_t begin = 10, end = 20; @@ -79,7 +99,7 @@ Y_UNIT_TEST_SUITE(TRope) { TRope pf; TRope rope; TString string = TString(Text.data(), Text.size()); - rope = string; + rope = TRope(string); pf = rope; pf.GetContiguousSpanMut(); UNIT_ASSERT(!string.IsDetached()); @@ -91,7 +111,7 @@ Y_UNIT_TEST_SUITE(TRope) { TRope pf; TRope rope; TString string = TString(Text.data(), Text.size()); - rope = string; + rope = TRope(string); pf = rope; UNIT_ASSERT(pf.IsContiguous()); UNIT_ASSERT_EQUAL(pf.UnsafeGetContiguousSpanMut().data(), string.data()); @@ -241,11 +261,23 @@ Y_UNIT_TEST_SUITE(TRope) { const TRope xRope = CreateRope(x, 7); const TRope yRope = CreateRope(y, 11); UNIT_ASSERT_VALUES_EQUAL(xRope == yRope, x == y); + UNIT_ASSERT_VALUES_EQUAL(xRope == y, x == y); + UNIT_ASSERT_VALUES_EQUAL(x == yRope, x == y); UNIT_ASSERT_VALUES_EQUAL(xRope != yRope, x != y); + UNIT_ASSERT_VALUES_EQUAL(xRope != y, x != y); + UNIT_ASSERT_VALUES_EQUAL(x != yRope, x != y); UNIT_ASSERT_VALUES_EQUAL(xRope < yRope, x < y); + UNIT_ASSERT_VALUES_EQUAL(xRope < y, x < y); + UNIT_ASSERT_VALUES_EQUAL(x < yRope, x < y); UNIT_ASSERT_VALUES_EQUAL(xRope <= yRope, x <= y); + UNIT_ASSERT_VALUES_EQUAL(xRope <= y, x <= y); + UNIT_ASSERT_VALUES_EQUAL(x <= yRope, x <= y); UNIT_ASSERT_VALUES_EQUAL(xRope > yRope, x > y); + UNIT_ASSERT_VALUES_EQUAL(xRope > y, x > y); + UNIT_ASSERT_VALUES_EQUAL(x > yRope, x > y); UNIT_ASSERT_VALUES_EQUAL(xRope >= yRope, x >= y); + UNIT_ASSERT_VALUES_EQUAL(xRope >= y, x >= y); + UNIT_ASSERT_VALUES_EQUAL(x >= yRope, x >= y); }; TVector<TString> pool; |