aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerländer2014-02-04 17:32:51 +0100
committerAdrian Kummerländer2014-02-04 17:32:51 +0100
commit7125d86a61030d5bec037d287840e0565d62a594 (patch)
treeab3a69873906cd6005b65442374f03974f75a143
parentc835984a6f0d8826139def2d284e03310f8d050a (diff)
downloadTrie-7125d86a61030d5bec037d287840e0565d62a594.tar
Trie-7125d86a61030d5bec037d287840e0565d62a594.tar.gz
Trie-7125d86a61030d5bec037d287840e0565d62a594.tar.bz2
Trie-7125d86a61030d5bec037d287840e0565d62a594.tar.lz
Trie-7125d86a61030d5bec037d287840e0565d62a594.tar.xz
Trie-7125d86a61030d5bec037d287840e0565d62a594.tar.zst
Trie-7125d86a61030d5bec037d287840e0565d62a594.zip
Switched resolve method to std::pair based return type
* enables more expressive passing of the information if the resolve call was successfull
-rw-r--r--trie.cc28
1 files changed, 15 insertions, 13 deletions
diff --git a/trie.cc b/trie.cc
index 3338a8c..55dee38 100644
--- a/trie.cc
+++ b/trie.cc
@@ -26,26 +26,28 @@ class Trie {
}
}
- inline const Trie* resolve(std::forward_list<Key> path) const {
+ inline std::pair<bool, const Trie*> resolve(
+ std::forward_list<Key> path
+ ) const {
return this->resolve(path, path.begin());
}
- inline const Trie* resolve(
+ inline std::pair<bool, const Trie*> resolve(
std::forward_list<Key>& path,
- typename std::forward_list<Key>::const_iterator curr
+ typename std::forward_list<Key>::const_iterator currStep
) const {
- auto trie = this->children_.find(*curr);
+ auto matchingTrie = this->children_.find(*curr);
- if ( trie != this->children_.end() ) {
- auto next = ++curr;
+ if ( matchingTrie != this->children_.end() ) {
+ auto nextStep = ++currStep;
- if ( next == path.end() ) {
- return &(*trie).second;
+ if ( nextStep == path.end() ) {
+ return std::make_pair(true, &(*trie).second);
} else {
- return (*trie).second.resolve(path, next);
+ return (*trie).second.resolve(path, nextStep);
}
} else {
- return nullptr;
+ return std::make_pair(false, nullptr);
}
}
@@ -62,7 +64,7 @@ int main() {
test.add({2, 1});
test.add({2, 1, 1});
- assert(test.resolve({1, 2}) != nullptr);
- assert(test.resolve({1, 2, 4}) != nullptr);
- assert(test.resolve({3}) == nullptr);
+ assert(test.resolve({1, 2}).second != nullptr);
+ assert(test.resolve({1, 2, 4}).second != nullptr);
+ assert(test.resolve({3}).second == nullptr);
}