aboutsummaryrefslogtreecommitdiff
path: root/src/trie.h
blob: d34912f73bba1a2add0e470afdbc8e672c2410cb (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
#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_(false, Value{}),
			children_() { }

		void add(key_list path) {
			this->add(path.cbegin(), path.cend());
		}

		void add(key_list path, Value value) {
			Trie* const target = this->add(path.cbegin(), path.cend());

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

		void remove(key_list path) {
			this->remove(path.cbegin(), path.cend());
		}

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

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

		detail::optional_ptr<Value> get(key_list path) {
			if ( const auto source = this->resolve(path) ) {
				return source.get()->get();
			} else {
				return detail::optional_ptr<Value>();
			}
		}

		void set(const Value& value) {
			this->value_.first  = true;
			this->value_.second = value;
		}

		bool set(key_list path, const Value& value) {
			if ( auto target = this->resolve(path) ) {
				target.get()->set(value);

				return true;
			} else {
				return false;
			}
		}

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

		Trie* add(
			typename key_list::const_iterator current,
			typename key_list::const_iterator end
		) {
			if ( current != end ) {
				return this->children_[*current].add(++current, end);
			} else {
				return this;
			}
		}

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

				if ( matching != this->children_.cend() ) {
					typename key_list::const_iterator next(
						++current
					);

					if ( next == end ) {
						this->children_.erase(matching);
					} else {
						(*matching).second.remove(next, end);
					}
				}
			}
		}

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

		detail::optional_ptr<Trie> resolve(
			typename key_list::const_iterator current,
			typename key_list::const_iterator end
		) {
			typename std::map<Key, Trie>::iterator matching(
				this->children_.find(*current)
			);

			if ( matching != this->children_.cend() ) {
				typename key_list::const_iterator next(
					++current
				);

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

};

#endif  // TRIE_SRC_TRIE_H_