-
Notifications
You must be signed in to change notification settings - Fork 0
/
TSTtest.cxx
237 lines (201 loc) · 5.1 KB
/
TSTtest.cxx
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <thread>
#include <mutex>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
std::mutex g_lockTST;
struct Word
{
char * data;
int count;
Word ( char * data_ ) :
data( ::strdup(data_) )
{};
Word () :
data((char *)"")
{};
~Word() {
if (data) delete data;
};
Word( Word & source){ data = new char[ strlen(source.data)];
strcpy(data, source.data); };
};
class Node {
public:
int count;
char c;
Node * left, *mid, *right;
char getC(){return c;};
};
class TST{
Node *root_;
public:
Node * put(Node * x, string key, int d);
void put(string key);
Node * getRoot(){ return root_;};
bool contains(string key);
int get(string key) ;
Node * get(Node *x, string key, int d) ;
int printAll();
void collect(Node * x, string prefix, queue<string>& q);
~TST();
void del(Node * n);
};
TST::~TST(){
if(root_ != NULL){
del(root_);
}
};
void TST::del(Node * n){
if(n == NULL) return;
del(n->left);
del(n->mid);
del(n->right);
delete n;
};
Node * TST::put(Node * x, string key, int d)
{
char c = key.at(d);
if (x == NULL) { x = new Node(); x->c = c; x->count = 0;}
if (c < x->c) x->left = put(x->left, key, d);
else if (c > x->c) x->right = put(x->right, key, d);
else if (d < key.length() - 1) x->mid = put(x->mid, key, d+1);
else x->count++;
return x;
};
void TST::put(string key){
root_ = put(root_, key, 0);
};
bool TST::contains(string key)
{ return (get(key) != 0); };
int TST::get(string key) {
Node * x = get(root_, key, 0);
if (x == NULL) return 0;
return x->count;
};
Node * TST::get(Node * x, string key, int d) {
if (!x) return x;
char c = key.at(d);
if (c < x->c) return get(x->left, key, d);
else if (c > x->c) return get(x->right, key, d);
else if (d < key.length() - 1) return get(x->mid, key, d+1);
else return x;
};
int TST::printAll(){
queue<string> collectAll ;
collect(root_, "", collectAll);
if(collectAll.empty()) return 0;
int size = collectAll.size();
while(collectAll.size() > 0){
//std::printf("\n %s " , collectAll->front());
std::cout << "\n " << collectAll.front();
collectAll.pop();
}
printf("\n");
return size;
};
void TST::collect(Node * x, string prefix, queue<string>& q)
{
if (x == NULL) return ;
char c = x->c;;
if(x->count > 0) q.push(prefix+c);
collect(x->left, prefix, q);
collect(x->mid, prefix + c , q );
collect(x->right, prefix, q);
}
static TST s_wordsTST;
static Word s_word;
static int s_totalFound;
// Worker thread: consume words passed from the main thread and insert them
// in the 'word list' (s_wordsTST), while removing duplicates. Terminate when
// the word 'end' is encountered.
static void workerThread ()
{
bool endEncountered = false;
bool found = false;
while (!endEncountered)
{
if (s_word.data[0]) // Do we have a new word?
{
Word * w = new Word(s_word); // Copy the word
s_word.data[0] = 0; // Inform the producer that we consumed the word
endEncountered = std::strcmp( w->data, "end" ) == 0;
if (!endEncountered){
g_lockTST.lock();
s_wordsTST.put( string(w->data) );
g_lockTST.unlock();
}
delete w;
}
}
};
// Read input words from STDIN and pass them to the worker thread for
// inclusion in the word list.
// Terminate when the word 'end' has been entered.
//
static void readInputWords ()
{
bool endEncountered = false;
std::thread * worker = new std::thread( workerThread );
char * linebuf = new char[32];
s_word.data = new char[32];
while (!endEncountered)
{
if (!std::gets( linebuf )) // EOF?
return;
endEncountered = std::strcmp( linebuf, "end" ) == 0;
// Pass the word to the worker thread
std::strcpy( s_word.data, linebuf );
while (s_word.data[0]); // Wait for the worker thread to consume it
}
worker->join(); // Wait for the worker to terminate
}
// Repeatedly ask the user for a word and check whether it was present in the word list
// Terminate on EOF
//
static void lookupWords ()
{
bool found;
char * linebuf = new char[32];
for(;;)
{
std::printf( "\nEnter a word for lookup:" );
if (!std::gets( linebuf )) // EOF?
return;
// Search for the word
int count = s_wordsTST.get(string(linebuf));
if(count > 0)
found = true;
if (found)
{
std::printf( "SUCCESS: '%s' was present %d times in the initial word list\n",
linebuf, count );
++s_totalFound;
}
else
std::printf( "'%s' was NOT found in the initial word list\n", linebuf );
}
delete linebuf;
}
int main ()
{
try
{
std::printf( "\n=== Provide list of words (List terminates when the word 'end' has been entered) :\n" );
readInputWords();
// Print the word list
std::printf( "\n=== Word list:\n" );
s_totalFound = s_wordsTST.printAll();
lookupWords();
printf( "\n=== Total words found: %d\n", s_totalFound );
}
catch (std::exception & e)
{
std::printf( "error %s\n", e.what() );
}
return 0;
}