aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerländer2014-02-06 18:37:43 +0100
committerAdrian Kummerländer2014-02-06 18:37:43 +0100
commit35f814706c4ff244bc15c4285804fc0656824c41 (patch)
treebd15383618cf9e0e0b319df4e46fb51adec6494b
parentc86f957426a2399217bcca466dec7a83d0981ba3 (diff)
downloadTrie-35f814706c4ff244bc15c4285804fc0656824c41.tar
Trie-35f814706c4ff244bc15c4285804fc0656824c41.tar.gz
Trie-35f814706c4ff244bc15c4285804fc0656824c41.tar.bz2
Trie-35f814706c4ff244bc15c4285804fc0656824c41.tar.xz
Trie-35f814706c4ff244bc15c4285804fc0656824c41.zip
Added support for changing values
* member method set changes the value of a given path to the provided value ** returns false if the specified path doesn't exist
-rw-r--r--src/trie.h13
-rw-r--r--test.cc8
2 files changed, 21 insertions, 0 deletions
diff --git a/src/trie.h b/src/trie.h
index 46fcb4b..d4763ef 100644
--- a/src/trie.h
+++ b/src/trie.h
@@ -51,6 +51,19 @@ class Trie {
}
}
+ inline bool set(key_list path, Value value) {
+ std::pair<bool, Trie*> tmp(this->resolve(path));
+
+ if ( tmp.first ) {
+ tmp.second->value_.first = true;
+ tmp.second->value_.second = value;
+
+ return true;
+ } else {
+ return false;
+ }
+ }
+
private:
std::pair<bool, Value> value_;
std::map<Key, Trie> children_;
diff --git a/test.cc b/test.cc
index ff0b04f..2a99619 100644
--- a/test.cc
+++ b/test.cc
@@ -61,6 +61,14 @@ TEST_F(TrieTest, Value) {
EXPECT_EQ(trie.get({1, 2}).first, false);
EXPECT_EQ(trie.get({1, 2}).second, nullptr);
+
+ trie.set({1, 2}, 42);
+ trie.set({1, 1, 1, 1}, 255);
+
+ EXPECT_EQ(trie.get({1, 2}).first, true);
+ EXPECT_EQ(*trie.get({1, 2}).second, 42);
+ EXPECT_EQ(trie.get({1, 1, 1, 1}).first, true);
+ EXPECT_EQ(*trie.get({1, 1, 1, 1}).second, 255);
}
int main(int argc, char **argv) {