aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerländer2014-02-06 20:26:45 +0100
committerAdrian Kummerländer2014-02-06 20:26:45 +0100
commit13f079c5150d86bb87b71094e545e720e245658d (patch)
treed0c0edc44f3bcd30b50cd947868011a0108bd7e5
parent35f814706c4ff244bc15c4285804fc0656824c41 (diff)
downloadTrie-13f079c5150d86bb87b71094e545e720e245658d.tar
Trie-13f079c5150d86bb87b71094e545e720e245658d.tar.gz
Trie-13f079c5150d86bb87b71094e545e720e245658d.tar.bz2
Trie-13f079c5150d86bb87b71094e545e720e245658d.tar.xz
Trie-13f079c5150d86bb87b71094e545e720e245658d.zip
Introduced simple result wrapper template
* offers bool() operator for easy assignment checks inside conditional statements
-rw-r--r--src/trie.h34
-rw-r--r--src/utility.h29
-rw-r--r--test.cc30
3 files changed, 60 insertions, 33 deletions
diff --git a/src/trie.h b/src/trie.h
index d4763ef..629e724 100644
--- a/src/trie.h
+++ b/src/trie.h
@@ -4,6 +4,8 @@
#include <vector>
#include <map>
+#include "utility.h"
+
template <
typename Key,
typename Value = std::nullptr_t
@@ -34,29 +36,25 @@ class Trie {
}
inline bool check(key_list path) {
- return this->resolve(path).first;
+ return this->resolve(path);
}
- inline std::pair<bool, Value*> get(key_list path) {
- std::pair<bool, Trie*> tmp(this->resolve(path));
-
- if ( tmp.first ) {
- if ( tmp.second->value_.first ) {
- return std::make_pair(true, &tmp.second->value_.second);
+ inline detail::Result<Value*> get(key_list path) {
+ if ( auto tmp = this->resolve(path) ) {
+ if ( tmp.get()->value_.first ) {
+ return detail::Result<Value*>(&tmp.get()->value_.second);
} else {
- return std::make_pair(false, nullptr);
+ return detail::Result<Value*>();
}
} else {
- return std::make_pair(false, nullptr);
+ return detail::Result<Value*>();
}
}
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;
+ if ( auto tmp = this->resolve(path) ) {
+ tmp.get()->value_.first = true;
+ tmp.get()->value_.second = value;
return true;
} else {
@@ -107,11 +105,11 @@ class Trie {
}
}
- inline std::pair<bool, Trie*> resolve(key_list path) {
+ inline detail::Result<Trie*> resolve(key_list path) {
return this->resolve(path, path.begin());
}
- inline std::pair<bool, Trie*> resolve(
+ inline detail::Result<Trie*> resolve(
key_list& path,
typename key_list::const_iterator currStep
) {
@@ -125,12 +123,12 @@ class Trie {
);
if ( nextStep == path.end() ) {
- return std::make_pair(true, &(*matchingTrie).second);
+ return detail::Result<Trie*>(&(*matchingTrie).second);
} else {
return (*matchingTrie).second.resolve(path, nextStep);
}
} else {
- return std::make_pair(false, nullptr);
+ return detail::Result<Trie*>();
}
}
diff --git a/src/utility.h b/src/utility.h
new file mode 100644
index 0000000..6342420
--- /dev/null
+++ b/src/utility.h
@@ -0,0 +1,29 @@
+#ifndef TRIE_SRC_UTILITY_H_
+#define TRIE_SRC_UTILITY_H_
+
+namespace detail {
+
+template <
+ typename Value
+>
+struct Result {
+ Result():
+ value_(false, Value()) { }
+
+ Result(Value value):
+ value_(true, value) { }
+
+ inline operator bool() const {
+ return this->value_.first;
+ }
+
+ inline Value get() const {
+ return this->value_.second;
+ }
+
+ const std::pair<bool, Value> value_;
+};
+
+}
+
+#endif // TRIE_SRC_UTILITY_H_
diff --git a/test.cc b/test.cc
index 2a99619..46ed5cd 100644
--- a/test.cc
+++ b/test.cc
@@ -50,25 +50,25 @@ TEST_F(TrieTest, Value) {
trie.add({2, 3, 4, 5}, 44);
trie.add({2, 3, 1, 2}, 45);
- EXPECT_EQ(trie.get({1, 1, 1, 1}).first, true);
- EXPECT_EQ(*trie.get({1, 1, 1, 1}).second, 42);
- EXPECT_EQ(trie.get({1, 2, 1, 2}).first, true);
- EXPECT_EQ(*trie.get({1, 2, 1, 2}).second, 43);
- EXPECT_EQ(trie.get({2, 3, 4, 5}).first, true);
- EXPECT_EQ(*trie.get({2, 3, 4, 5}).second, 44);
- EXPECT_EQ(trie.get({2, 3, 4, 5}).first, true);
- EXPECT_EQ(*trie.get({2, 3, 1, 2}).second, 45);
-
- EXPECT_EQ(trie.get({1, 2}).first, false);
- EXPECT_EQ(trie.get({1, 2}).second, nullptr);
+ EXPECT_EQ(trie.get({1, 1, 1, 1}), true);
+ EXPECT_EQ(*trie.get({1, 1, 1, 1}).get(), 42);
+ EXPECT_EQ(trie.get({1, 2, 1, 2}), true);
+ EXPECT_EQ(*trie.get({1, 2, 1, 2}).get(), 43);
+ EXPECT_EQ(trie.get({2, 3, 4, 5}), true);
+ EXPECT_EQ(*trie.get({2, 3, 4, 5}).get(), 44);
+ EXPECT_EQ(trie.get({2, 3, 4, 5}), true);
+ EXPECT_EQ(*trie.get({2, 3, 1, 2}).get(), 45);
+
+ EXPECT_EQ(trie.get({1, 2}), false);
+ EXPECT_EQ(trie.get({1, 2}).get(), 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);
+ EXPECT_EQ(trie.get({1, 2}), true);
+ EXPECT_EQ(*trie.get({1, 2}).get(), 42);
+ EXPECT_EQ(trie.get({1, 1, 1, 1}), true);
+ EXPECT_EQ(*trie.get({1, 1, 1, 1}).get(), 255);
}
int main(int argc, char **argv) {