aboutsummaryrefslogtreecommitdiff
path: root/src
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 /src
parent35f814706c4ff244bc15c4285804fc0656824c41 (diff)
downloadTrie-13f079c5150d86bb87b71094e545e720e245658d.tar
Trie-13f079c5150d86bb87b71094e545e720e245658d.tar.gz
Trie-13f079c5150d86bb87b71094e545e720e245658d.tar.bz2
Trie-13f079c5150d86bb87b71094e545e720e245658d.tar.lz
Trie-13f079c5150d86bb87b71094e545e720e245658d.tar.xz
Trie-13f079c5150d86bb87b71094e545e720e245658d.tar.zst
Trie-13f079c5150d86bb87b71094e545e720e245658d.zip
Introduced simple result wrapper template
* offers bool() operator for easy assignment checks inside conditional statements
Diffstat (limited to 'src')
-rw-r--r--src/trie.h34
-rw-r--r--src/utility.h29
2 files changed, 45 insertions, 18 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_