-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringDict.cpp
136 lines (93 loc) · 2.61 KB
/
StringDict.cpp
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
//
// Created by Matthieu Rudelle on 26/05/16.
//
#include "StringDict.h"
#include <string>
#include <iostream>
using namespace std;
StringDict::StringDict() {
tempId = 0;
size = 0;
// root element has no key
root = new trie_node();
root->key = -1;
}
int StringDict::add(string text) {
int strPos = 0;
trie_node *current = root;
while(strPos < text.length()) {
char ordinal = text.at((unsigned long) strPos);
if (!current->children[ordinal]) {
current->children[ordinal] = new trie_node();
// non leaf nodes are not assigned to a key
current->children[ordinal]->key = -1;
}
current = current->children[ordinal];
strPos++;
}
if (current->key < 0) {
current->key = generateKey();
}
return current->key;
}
// returns the key or -1 if there is no key for this string
int StringDict::getKey(string text) {
int strPos = 0;
trie_node* current = root;
while(strPos < text.length()) {
char ordinal = text.at((unsigned long) strPos);
if (!current->children[ordinal]) {
return -1;
}
current = current->children[ordinal];
strPos++;
}
return current->key;
}
void StringDict::getBatchKeys(int size, string* texts, int* keysOut) {
for (int i = 0; i < size; ++i) {
keysOut[i] = getKey(texts[i]);
}
}
int StringDict::generateKey() {
size++;
return tempId++;
}
void StringDict::print() {
print("", root);
}
void StringDict::print(string prefix, trie_node* node) {
if (node->key >= 0) {
printf("%s[%d]\n", prefix.c_str(), node->key);
}
for (child_map::iterator it=node->children.begin(); it!=node->children.end(); ++it) {
string newPrefix = prefix;
newPrefix += it->first;
print(newPrefix, it->second);
}
}
int StringDict::nodeCount() {
return nodeCount(root);
}
int StringDict::nodeCount(trie_node* node) {
int count = 0;
for (child_map::iterator it=node->children.begin(); it!=node->children.end(); ++it) {
count += nodeCount(it->second);
}
count+=node->children.size();
return count;
}
void StringDict::sort(int* translation) {
sort(translation, root, 0);
}
int StringDict::sort(int* translation, trie_node* node, int idStart) {
if (node->key >= 0) {
translation[node->key] = idStart;
node->key = idStart;
idStart++;
}
for (child_map::iterator it=node->children.begin(); it!=node->children.end(); ++it) {
idStart = sort(translation, it->second, idStart);
}
return idStart;
}