aboutsummaryrefslogtreecommitdiff
path: root/trie.h
blob: 4e01e837f5561ac7ba1f718dfade9a0e9ceaf3ff (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#ifndef TRIE_H
#define TRIE_H

#include <iostream>

#include <cstring>
#include <cstdlib>

template <class T>
struct trie_node {
	char c;
	T* value = NULL;
	trie_node<T>* next  = nullptr;
	trie_node<T>* child = nullptr;

	trie_node (char c) : c(c) { };
	trie_node (char c, trie_node<T>* next, trie_node<T>* child);
};

template <class T>
std::ostream& operator<<(std::ostream&, trie_node<T>* node);

template <class T>
struct trie {
	trie_node<T>* root;

	trie () : root (new trie_node<T> ('\0')) { }

	int push (const char* key, T* item);

	T* operator[] ( const char* key );

	bool empty () { return this->root->child == NULL; }
};

template <class T>
std::ostream& operator<<(std::ostream&, trie<T>* trie);

template <class T>
trie_node<T>::trie_node (char c, trie_node<T>* next, trie_node<T>* child) {
	this->c = c;
	this->next = next;
	this->child = child;
}

template <class T>
int trie<T>::push (const char* key, T* item) {
	trie_node<T> *cur, *last;

	last = this->root;
	cur = last->child;

	// TODO subkey is never collected
	char* subkey = static_cast<char*>(malloc(strlen(key)));
	strcpy (subkey, key);

	while (true) {
		if (cur == NULL) {
			/* Build direct LL for remaining subkey */
			for (char* c = subkey; c[0] != '\0'; c++) {
				// NEW(TRIE_NODE(TYPE), t, *c);
				auto t = new trie_node<T>(*c);
				last->child = t;
				last = t;
			}
			// TODO fix resolve
			// last->value = RESOLVE(TYPE)(last->value, val);
			last->value = item;
			return 0;
		} else if (cur->c == subkey[0]) {
			/* This node belongs to the key,
			 * Decend further */
			last = cur;
			cur = cur->child;
			subkey++;
		} else if (subkey[0] == '\0') {
			/* Key finished */
			// TODO fix resolve
			// last->value = RESOLVE(TYPE)(last->value, val);
			last->value = item;
			return 0;
		} else if (cur->next != NULL) {
			/* This node was not part of the set, but it's sibling might */
			cur = cur->next;
			/* `last` not set since we aren't moving down */ 
		} else {
			/* No node on self level was part of the set, create a new__
			 * sibling and follow down that parse */
			// NEW(TRIE_NODE(TYPE), t, *subkey);
			auto t = new trie_node<T>(*subkey);
			cur->next = t;
			last = cur;
			cur = t;
		}
	}
}

template <class T>
T* trie<T>::operator[] (const char* key) {
	trie_node<T>* n = this->root->child;

	// TODO subkey is never collected
	char* subkey = static_cast<char*>(malloc(strlen(key)));
	strcpy (subkey, key);

	while (n != NULL) {
		if (subkey[1] == '\0') {
			return n->value;
		} else if (subkey[0] == n->c) {
			n = n->child;
			subkey++;
		} else {
			n = n->next;
		}
	}

	// ERR("Position not found");
	// return 0;
	return nullptr;
}

template <class T>
std::ostream& operator<<(std::ostream& o, trie_node<T>* node) {
#if 0
	va_list ap;
	va_start(ap, buf);
	int argc = va_arg(ap, int);
	int depth = argc >= 1
		? va_arg(ap, int)
		: 0;
	va_end(ap);

	int seek = 0;
#endif
	// TODO figure out depth
	int depth = 1;

	trie_node<T>* n = node;


	if (n == NULL) { o << std::endl; }
	while (n != NULL) {
		o << '|';
		for (int i = 0; i < depth; i++) o << ' ';
		o << (n->c == '\0' ? '0' : n->c);
		if (n->value != NULL) {
			o << n->value << std::endl;
		}

		if (n->child != NULL) {
			o << std::endl;
			o << n->child; // depth + 1
		}
		n = n->next;
	}
	return o;
}

template <class T>
std::ostream& operator<<(std::ostream& o, trie<T>* trie) {
	o << "Trie: " << trie << " {\n";
	o << trie->root->child;
	o << "}";
	return o;
}

#endif /* TRIE_H */