From 63ea42e7e91b8f4fad83b0674d7604d2a05f626a Mon Sep 17 00:00:00 2001 From: cxl Date: Tue, 20 Aug 2013 18:44:11 +0000 Subject: [PATCH] Core: ValueMap::RemoveKey #470 git-svn-id: svn://ultimatepp.org/upp/trunk@6252 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/ValueUtil.cpp | 17 +++++++++++++++++ uppsrc/Core/ValueUtil.h | 7 +++++++ uppsrc/Core/src.tpp/ValueMap$en-us.tpp | 12 ++++++++++++ 3 files changed, 36 insertions(+) diff --git a/uppsrc/Core/ValueUtil.cpp b/uppsrc/Core/ValueUtil.cpp index aaeb4d925..264944294 100644 --- a/uppsrc/Core/ValueUtil.cpp +++ b/uppsrc/Core/ValueUtil.cpp @@ -500,6 +500,23 @@ void ValueMap::SetKey(int i, const Value& k) { Clone().key.Set(i, k); } +int ValueMap::RemoveKey(const Value& key) +{ + Data& d = Clone(); + Vector rk; + int q = d.key.Find(key); + while(q >= 0) { + rk.Add(q); + q = d.key.FindNext(q); + } + int count = rk.GetCount(); + if(count) { + d.key.Remove(rk); + d.value.data->data.Remove(rk); + } + return count; +} + void ValueMap::Remove(int i) { Data& d = Clone(); diff --git a/uppsrc/Core/ValueUtil.h b/uppsrc/Core/ValueUtil.h index db2b21c5d..eab05cb99 100644 --- a/uppsrc/Core/ValueUtil.h +++ b/uppsrc/Core/ValueUtil.h @@ -209,6 +209,7 @@ class ValueArray : ValueType > { friend Value::Void *ValueArrayDataCreate(); friend class Value; + friend class ValueMap; public: ValueArray() { Init0(); } @@ -323,6 +324,12 @@ public: void SetKey(int i, const char* key) { SetKey(i, Value(key)); } void SetKey(int i, int key) { SetKey(i, Value(key)); } void SetKey(int i, Id key) { SetKey(i, Value(key.ToString())); } + + int RemoveKey(const Value& key); + int RemoveKey(const String& key) { return RemoveKey(Value(key)); } + int RemoveKey(const char* key) { return RemoveKey(Value(key)); } + int RemoveKey(int key) { return RemoveKey(Value(key)); } + int RemoveKey(Id key) { return RemoveKey(Value(key.ToString())); } void Remove(int i); const Index& GetKeys() const { return data->key; } diff --git a/uppsrc/Core/src.tpp/ValueMap$en-us.tpp b/uppsrc/Core/src.tpp/ValueMap$en-us.tpp index 9af65bcea..79acb3991 100644 --- a/uppsrc/Core/src.tpp/ValueMap$en-us.tpp +++ b/uppsrc/Core/src.tpp/ValueMap$en-us.tpp @@ -168,6 +168,18 @@ t]_[*@3 i], [@(0.0.255) const]_[@(0.0.255) char`*]_[*@3 key])&] required to make Id and SqlId work with ValueMap.&] [s3; &] [s4;%- &] +[s5;:ValueMap`:`:RemoveKey`(const Value`&`):%- [@(0.0.255) int]_[* RemoveKey]([@(0.0.255) c +onst]_[_^Value^ Value][@(0.0.255) `&]_[*@3 key])&] +[s5;:ValueMap`:`:RemoveKey`(const String`&`):%- [@(0.0.255) int]_[* RemoveKey]([@(0.0.255) c +onst]_[_^String^ String][@(0.0.255) `&]_[*@3 key])&] +[s5;:ValueMap`:`:RemoveKey`(const char`*`):%- [@(0.0.255) int]_[* RemoveKey]([@(0.0.255) co +nst]_[@(0.0.255) char`*]_[*@3 key])&] +[s5;:ValueMap`:`:RemoveKey`(int`):%- [@(0.0.255) int]_[* RemoveKey]([@(0.0.255) int]_[*@3 key +])&] +[s5;:ValueMap`:`:RemoveKey`(Id`):%- [@(0.0.255) int]_[* RemoveKey]([_^Id^ Id]_[*@3 key])&] +[s2; Removes all key`-value pairs with given key. Slow O(n) operation.&] +[s3; &] +[s4;%- &] [s5;:ValueMap`:`:Remove`(int`):%- [@(0.0.255) void]_[* Remove]([@(0.0.255) int]_[*@3 i])&] [s2; Removes key`-value pair at index [%-*@3 i].&] [s3; &]