aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerländer2014-02-06 20:53:38 +0100
committerAdrian Kummerländer2014-02-06 20:53:38 +0100
commit74935056c496814a3c9d1380437812536b159203 (patch)
tree9417e7604740cc53fc26c3b74604d901ffdb94b0
parent13f079c5150d86bb87b71094e545e720e245658d (diff)
downloadTrie-74935056c496814a3c9d1380437812536b159203.tar
Trie-74935056c496814a3c9d1380437812536b159203.tar.gz
Trie-74935056c496814a3c9d1380437812536b159203.tar.bz2
Trie-74935056c496814a3c9d1380437812536b159203.tar.xz
Trie-74935056c496814a3c9d1380437812536b159203.zip
Converted detail::Result into general optional_ptr type
* Added set member method overload for easier value modification * Idea: expand detail::optional_ptr into general optional type
-rw-r--r--src/trie.h28
-rw-r--r--src/utility.h32
2 files changed, 37 insertions, 23 deletions
diff --git a/src/trie.h b/src/trie.h
index 629e724..6262ab3 100644
--- a/src/trie.h
+++ b/src/trie.h
@@ -39,22 +39,28 @@ class Trie {
return this->resolve(path);
}
- inline detail::Result<Value*> get(key_list path) {
+ inline detail::optional_ptr<Value> get(key_list path) {
if ( auto tmp = this->resolve(path) ) {
if ( tmp.get()->value_.first ) {
- return detail::Result<Value*>(&tmp.get()->value_.second);
+ return detail::optional_ptr<Value>(
+ &tmp.get()->value_.second
+ );
} else {
- return detail::Result<Value*>();
+ return detail::optional_ptr<Value>();
}
} else {
- return detail::Result<Value*>();
+ return detail::optional_ptr<Value>();
}
}
+ inline void set(Value value) {
+ this->value_.first = true;
+ this->value_.second = value;
+ }
+
inline bool set(key_list path, Value value) {
if ( auto tmp = this->resolve(path) ) {
- tmp.get()->value_.first = true;
- tmp.get()->value_.second = value;
+ tmp.get()->set(value);
return true;
} else {
@@ -105,11 +111,11 @@ class Trie {
}
}
- inline detail::Result<Trie*> resolve(key_list path) {
+ inline detail::optional_ptr<Trie> resolve(key_list path) {
return this->resolve(path, path.begin());
}
- inline detail::Result<Trie*> resolve(
+ inline detail::optional_ptr<Trie> resolve(
key_list& path,
typename key_list::const_iterator currStep
) {
@@ -123,12 +129,14 @@ class Trie {
);
if ( nextStep == path.end() ) {
- return detail::Result<Trie*>(&(*matchingTrie).second);
+ return detail::optional_ptr<Trie>(
+ &(*matchingTrie).second
+ );
} else {
return (*matchingTrie).second.resolve(path, nextStep);
}
} else {
- return detail::Result<Trie*>();
+ return detail::optional_ptr<Trie>();
}
}
diff --git a/src/utility.h b/src/utility.h
index 6342420..db33608 100644
--- a/src/utility.h
+++ b/src/utility.h
@@ -4,24 +4,30 @@
namespace detail {
template <
- typename Value
+ typename Type
>
-struct Result {
- Result():
- value_(false, Value()) { }
+class optional_ptr {
+ public:
+ typedef typename std::add_pointer<Type>::type pointer;
+ typedef Type element_type;
- Result(Value value):
- value_(true, value) { }
+ optional_ptr():
+ value_(false, nullptr) { }
- inline operator bool() const {
- return this->value_.first;
- }
+ optional_ptr(pointer ptr):
+ value_(true, ptr) { }
- inline Value get() const {
- return this->value_.second;
- }
+ inline operator bool() const {
+ return this->value_.first;
+ }
+
+ inline pointer get() const {
+ return this->value_.second;
+ }
+
+ private:
+ const std::pair<bool, pointer> value_;
- const std::pair<bool, Value> value_;
};
}