diff options
| author | denisostashov <[email protected]> | 2025-11-12 05:44:32 +0300 |
|---|---|---|
| committer | denisostashov <[email protected]> | 2025-11-12 06:02:49 +0300 |
| commit | 75c9feef6c4e47ecfaad0dd54be4fe610f541fb4 (patch) | |
| tree | b4ec05ea010a73b31d782976473bef6bbb114edf /library/cpp | |
| parent | fa3573678f6f3a6bc1425f13804af7cd114b93cf (diff) | |
[library/cpp/scheme] Add const GetNoAdd methods, move return value in Delete method
[library/cpp/scheme] Add const GetNoAdd methods, move return value in Delete method
commit_hash:94e4b516948789e0092e2d88f2e3d1079da2b99a
Diffstat (limited to 'library/cpp')
| -rw-r--r-- | library/cpp/scheme/scheme.h | 2 | ||||
| -rw-r--r-- | library/cpp/scheme/scimpl.h | 24 | ||||
| -rw-r--r-- | library/cpp/scheme/tests/ut/scheme_ut.cpp | 42 |
3 files changed, 66 insertions, 2 deletions
diff --git a/library/cpp/scheme/scheme.h b/library/cpp/scheme/scheme.h index 78336d16c8a..4f56a0053f3 100644 --- a/library/cpp/scheme/scheme.h +++ b/library/cpp/scheme/scheme.h @@ -209,6 +209,7 @@ namespace NSc { inline const TValue& Get(size_t idx) const; // returns child or default inline TValue* GetNoAdd(size_t idx); // returns link to existing child or nullptr + inline const TValue* GetNoAdd(size_t idx) const; // returns const link to existing child or nullptr inline TValue& Push(); // returns new child @@ -276,6 +277,7 @@ namespace NSc { inline const TValue& Get(TStringBuf idx) const; inline TValue* GetNoAdd(TStringBuf idx); // returns link to existing child or nullptr + inline const TValue* GetNoAdd(TStringBuf idx) const; // returns const link to existing child or nullptr TValue& Add(TStringBuf idx) { return GetOrAdd(idx); diff --git a/library/cpp/scheme/scimpl.h b/library/cpp/scheme/scimpl.h index 4f68f16290f..41004965ad8 100644 --- a/library/cpp/scheme/scimpl.h +++ b/library/cpp/scheme/scimpl.h @@ -232,6 +232,10 @@ namespace NSc { return IsArray() && Array.size() > key ? &Array[key] : nullptr; } + const TValue* GetNoAdd(size_t key) const { + return IsArray() && Array.size() > key ? &Array[key] : nullptr; + } + TValue& GetOrAdd(size_t key) { SetArray(); for (size_t i = Array.size(); i <= key; ++i) { @@ -268,7 +272,7 @@ namespace NSc { return TValue::DefaultValue(); } - TValue v = Array[key]; + TValue v = std::move(Array[key]); Array.erase(Array.begin() + key); return v; } @@ -290,6 +294,14 @@ namespace NSc { return Dict.FindPtr(key); } + const TValue* GetNoAdd(TStringBuf key) const { + if (!IsDict()) { + return nullptr; + } + + return Dict.FindPtr(key); + } + TValue& Add(TStringBuf key) { SetDict(); TDict::iterator it = Dict.insert(std::make_pair(Pool->AppendBuf(key), TValue(Pool))).first; @@ -321,7 +333,7 @@ namespace NSc { return TValue::DefaultValue(); } - TValue v = it->second; + TValue v = std::move(it->second); Dict.erase(key); return v; } @@ -571,6 +583,10 @@ namespace NSc { return CoreMutable().GetNoAdd(idx); } + const TValue* TValue::GetNoAdd(size_t idx) const { + return Core().GetNoAdd(idx); + } + const TValue& TValue::Get(TStringBuf idx) const { return Core().Get(idx); } @@ -579,6 +595,10 @@ namespace NSc { return CoreMutable().GetNoAdd(key); } + const TValue* TValue::GetNoAdd(TStringBuf key) const { + return Core().GetNoAdd(key); + } + TValue& TValue::Back() { return CoreMutable().Back(); } diff --git a/library/cpp/scheme/tests/ut/scheme_ut.cpp b/library/cpp/scheme/tests/ut/scheme_ut.cpp index 182f65fac71..97cd4524354 100644 --- a/library/cpp/scheme/tests/ut/scheme_ut.cpp +++ b/library/cpp/scheme/tests/ut/scheme_ut.cpp @@ -877,6 +877,48 @@ Y_UNIT_TEST_SUITE(TSchemeTest) { UNIT_ASSERT_VALUES_EQUAL(v, expectedResult); } + Y_UNIT_TEST(TestConstGetNoAdd) { + NSc::TValue v = NSc::NUt::AssertFromJson("{a:[null,-1,2,3.4],b:3,c:{d:5,x:y}}"); + UNIT_ASSERT(v.GetNoAdd("a") != nullptr); + UNIT_ASSERT(v.GetNoAdd("b") != nullptr); + UNIT_ASSERT(v.GetNoAdd("c") != nullptr); + UNIT_ASSERT(v.GetNoAdd("d") == nullptr); + UNIT_ASSERT(v.GetNoAdd("value") == nullptr); + { + const NSc::TValue* childPtr = v.GetNoAdd("c"); + UNIT_ASSERT(childPtr != nullptr); + UNIT_ASSERT(childPtr->GetNoAdd("d") != nullptr); + UNIT_ASSERT(childPtr->GetNoAdd("x") != nullptr); + UNIT_ASSERT(childPtr->GetNoAdd("e") == nullptr); + NSc::TValue child = *childPtr; + child["e"]["f"] = 42; + UNIT_ASSERT(child.GetNoAdd("e") != nullptr); + UNIT_ASSERT(childPtr->GetNoAdd("e") == nullptr); + UNIT_ASSERT_VALUES_EQUAL(child.Delete("x"), NSc::NUt::AssertFromJson("y")); + UNIT_ASSERT_VALUES_EQUAL(v, NSc::NUt::AssertFromJson("{a:[null,-1,2,3.4],b:3,c:{d:5,x:y}}")); + UNIT_ASSERT_VALUES_EQUAL(child, NSc::NUt::AssertFromJson("{d:5,e:{f:42}}")); + } + { + const NSc::TValue* childPtr = v.GetNoAdd("a"); + UNIT_ASSERT(childPtr != nullptr); + UNIT_ASSERT(childPtr->GetNoAdd(0) != nullptr); + UNIT_ASSERT(childPtr->GetNoAdd(1) != nullptr); + UNIT_ASSERT(childPtr->GetNoAdd(2) != nullptr); + UNIT_ASSERT(childPtr->GetNoAdd(3) != nullptr); + UNIT_ASSERT(childPtr->GetNoAdd(4) == nullptr); + UNIT_ASSERT(childPtr->GetNoAdd(5) == nullptr); + NSc::TValue child = *childPtr; + child.Push("y"); + child.Push("z"); + child[2] = 5; + UNIT_ASSERT_VALUES_EQUAL(child.Delete(1), NSc::NUt::AssertFromJson("-1")); + UNIT_ASSERT(child.GetNoAdd(4) != nullptr); + UNIT_ASSERT(childPtr->GetNoAdd(4) == nullptr); + UNIT_ASSERT_VALUES_EQUAL(v, NSc::NUt::AssertFromJson("{a:[null,-1,2,3.4],b:3,c:{d:5,x:y}}")); + UNIT_ASSERT_VALUES_EQUAL(child, NSc::NUt::AssertFromJson("[null,5,3.4,y,z]")); + } + } + Y_UNIT_TEST(TestNewNodeOnCurrentPool) { NSc::TValue parent; parent["foo"] = "bar"; |
