forked from haguvenir/Turkish_TTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTTSHashTrie.cpp
206 lines (183 loc) · 3.91 KB
/
TTSHashTrie.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "TextStruct.h"
TTSHashTrie::TTSHashTrie()
{
root = Create(LEAF_NODE);
}
TTSHashTrie::~TTSHashTrie()
{
DeleteAll();
}
void TTSHashTrie::Delete(TTSHashTrieNode *node)
{
if (node->type == LEAF_NODE)
{
delete [] node->data;
delete [] node->info;
delete node;
} else
{
Delete(node->left);
Delete(node->right);
delete [] node->data;
delete [] node->info;
delete node;
}
}
void TTSHashTrie::DeleteAll()
{
if (root != NULL)
Delete(root);
}
void TTSHashTrie::Insert(string str, string data)
{
Insert(root,str,data);
}
void TTSHashTrie::Display()
{
cout<<"\nHash-TTSHashTrie\n--------"<<endl;
Display(root);
}
string TTSHashTrie::Exists(string str)
{
return Exists(str,root);
}
/*
* Recursive private methods that the users cannot directly call
*/
string TTSHashTrie::Exists(string str, TTSHashTrieNode *node)
{
if(node->type == LEAF_NODE)
{
// AT leaf
for(int i=0; i<node->elements; i++)
if(str == node->info[i])
return node->data[i];
return "";
}
if (strcmp(&str.substr(0,node->index)[0],&node->compare[0]) < 1)
{ // GO left
if(node->type == INTERNAL_NODE)
return Exists(str,node->left);
}
else
{ // GO right
if(node->type == INTERNAL_NODE)
return Exists(str,node->right);
}
return "";
}
void TTSHashTrie::Display(TTSHashTrieNode *node)
{
if(node == NULL)
{
if(DEBUG_0) cout<<"null"<<endl;
return;
}
if(node->type == INTERNAL_NODE)
{
if(DEBUG_0) cout<<"<<"<<endl;
Display(node->left);
if(DEBUG_0) cout<<"root: "<<node->index<<endl;
if(DEBUG_0) cout<<">>"<<endl;
Display(node->right);
}
else
{
for(int i=0; i<node->elements; i++)
cout<<node->info[i]<<endl;
}
}
void TTSHashTrie::Insert(TTSHashTrieNode *node, string str, string data)
{
// case of root
if(node->type == LEAF_NODE)
{
int i=0;
for(i=node->elements; i>=1; i--)
{
if(Compare(str,node->info[i-1]) != 1)
{
node->info[i] = node->info[i-1];
node->data[i] = node->data[i-1];
}
else break;
}
node->info[i] = str;
node->data[i] = data;
node->elements++;
Check(node);
return;
}
if (strcmp(&str.substr(0,node->index)[0],&node->compare[0]) < 1)
{ // left
if(node->type == INTERNAL_NODE)
Insert(node->left,str,data);
}
else
{ // right
if(node->type == INTERNAL_NODE)
Insert(node->right,str,data);
}
}
void TTSHashTrie::Check(TTSHashTrieNode * node)
{
if(node->elements <= HASH_TRIE_BUFFER) return;
//if(DEBUG_0) cout<<"**** Overflow"<<endl;
int lower = HASH_TRIE_BUFFER/2;
node->index ++;
node->compare = node->info[lower].substr(0,node->index);
node->left = Create(LEAF_NODE);
for(int ii=0; ii<HASH_TRIE_BUFFER+1; ii++)
if( Compare(node->info[ii].substr(0,node->index), node->compare) == 1)
{ // insert to right
if(node->right == NULL)
node->right = Create(LEAF_NODE);
node->right->info[node->right->elements] = node->info[ii];
node->right->data[node->right->elements] = node->data[ii];
node->right->elements++;
}
else
{ // insert to left
node->left->info[node->left->elements] = node->info[ii];
node->left->data[node->left->elements] = node->data[ii];
node->left->elements++;
}
if(node->left->elements > HASH_TRIE_BUFFER)
{
// if(DEBUG_0) cout<<"**** OLMADI bir DAHA"<<endl;
Check(node);
return;
}
node->elements = 0;
node->type = INTERNAL_NODE;
}
TTSHashTrieNode * TTSHashTrie::Create(TRIE_NODE_TYPE type)
{
TTSHashTrieNode * node = new TTSHashTrieNode();
node->data = new string[HASH_TRIE_BUFFER+1];
node->info = new string[HASH_TRIE_BUFFER+1];
node->elements = 0;
node->left = NULL;
node->right = NULL;
node->compare = '\0';
node->index = 0;
node->type = type;
return node;
}
int Compare(string str, string str2)
{
int i=0;
int len1 = (int)str.length();
int len2 = (int)str2.length();
while(str[i] != '\0' && str2[i] != '\0')
{
if(str[i] < str2[i])
return -1;
if(str[i] > str2[i])
return 1;
i++;
}
if(len1 == len2) return 0;
if(len1 < len2) return -1;
else return 1;
}