aboutsummaryrefslogtreecommitdiff
path: root/src/trie.h
blob: b31900f411d2f8ad12ab84d7222295232948188c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#ifndef TRIE_SRC_TRIE_H_
#define TRIE_SRC_TRIE_H_

#include <vector>
#include <map>

#include "utility.h"

template <
	typename Key,
	typename Value = std::nullptr_t
>
class Trie {
	public:
		typedef std::vector<Key> key_list;

		Trie():
			value_(),
			children_() {
			this->value_.first = false;
		}

		inline void add(key_list path) {
			this->add(path, path.begin());
		}

		inline void add(key_list path, Value value) {
			Trie* tmp = this->add(path, path.begin());

			tmp->value_.first  = true;
			tmp->value_.second = value;
		}

		inline void remove(key_list path) {
			this->remove(path, path.begin());
		}

		inline bool check(key_list path) {
			return this->resolve(path);
		}

		inline detail::optional_ptr<Value> get() {
			if ( this->value_.first ) {
				return detail::optional_ptr<Value>(&this->value_.second);
			} else {
				return detail::optional_ptr<Value>();
			}
		}

		inline detail::optional_ptr<Value> get(key_list path) {
			if ( auto tmp = this->resolve(path) ) {
				return tmp.get()->get();
			} else {
				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()->set(value);

				return true;
			} else {
				return false;
			}
		}

	private:
		std::pair<bool, Value> value_;
		std::map<Key, Trie> children_;

		inline Trie* add(
			key_list& path,
			typename key_list::const_iterator currStep
		) {
			if ( currStep != path.end() ) {
				Trie& trie(
					this->children_[*currStep]
				);

				return trie.add(path, ++currStep);
			} else {
				return this;
			}
		}

		inline void remove(
			key_list& path,
			typename key_list::const_iterator currStep
		) {
			if ( currStep != path.end() ) {
				typename std::map<Key, Trie>::iterator matchingTrie(
					this->children_.find(*currStep)
				);

				if ( matchingTrie != this->children_.end() ) {
					typename key_list::const_iterator nextStep(
						++currStep
					);

					if ( (*matchingTrie).second.children_.empty() ||
					     nextStep == path.end() ) {
						this->children_.erase(matchingTrie);
					} else {
						(*matchingTrie).second.remove(path, nextStep);
					}
				}
			}
		}

		inline detail::optional_ptr<Trie> resolve(key_list path) {
			return this->resolve(path, path.begin());
		}

		inline detail::optional_ptr<Trie> resolve(
			key_list& path,
			typename key_list::const_iterator currStep
		) {
			typename std::map<Key, Trie>::iterator matchingTrie(
				this->children_.find(*currStep)
			);

			if ( matchingTrie != this->children_.end() ) {
				typename key_list::const_iterator nextStep(
					++currStep
				);

				if ( nextStep == path.end() ) {
					return detail::optional_ptr<Trie>(
						&(*matchingTrie).second
					);
				} else {
					return (*matchingTrie).second.resolve(path, nextStep);
				}
			} else {
				return detail::optional_ptr<Trie>();
			}
		}

};

#endif  // TRIE_SRC_TRIE_H_