-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.cpp
703 lines (605 loc) · 24.9 KB
/
trie.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
#include "trie.h"
//-----------------------------------------------------------------------------------------------------------------------
Trie::Trie(int nRank) : chainRank(nRank),rootNode(Node(UnicodeString(""),shared_ptr<NodeElement>(new NodeElement(0))))
{
lastNode = &rootNode;
rootNode.second->parent = nullptr;
nodesCount = 1;
}
//-----------------------------------------------------------------------------------------------------------------------
Trie::Trie(const Trie & t) : rootNode(Node(UnicodeString(""),shared_ptr<NodeElement>(new NodeElement(0)))) //root node is always "clear" node - it is a starting point only;
{
chainRank = t.chainRank;
lastNode = &rootNode;
rootNode.second->parent = nullptr;
nodesCount = 1;
//Copying the tree:
for (auto keyValuePair : t.rootNode.second->childNodes)
CopyNodesRecursive(keyValuePair,rootNode);
}
//-----------------------------------------------------------------------------------------------------------------------
Trie & Trie::operator=(const Trie & t)
{
if (&t!=this)
{
//Clear current tree
for (auto & keyValuePair : rootNode.second->childNodes)
DeleteNode(keyValuePair);
if(rootNode.second->childNodes.size()!=0)
rootNode.second->childNodes.clear();
lastNode = &rootNode;
nodesCount = 1;
//Copy tree
chainRank = t.chainRank;
//Copy tree
for (auto & keyValuePair : t.rootNode.second->childNodes)
CopyNodesRecursive(keyValuePair,rootNode);
}
return *this;
}
//-----------------------------------------------------------------------------------------------------------------------
ostream & operator<<(ostream & os, const Trie & t)
{
int totalNodes=0;
os<<"rank = "<<t.chainRank<<", nodes = "<<totalNodes<<endl;
os<<"root"<<endl;
//Print nodes without recursion
for (const auto & keyValuePair : t.rootNode.second->childNodes)
os<<"\t"<<keyValuePair.first<<":"<< keyValuePair.second->count<<endl;
// for (const Trie::Node *nd = t.Begin(&t.rootNode);nd;nd=t.Next())
// os<<"\t"<<nd->first<<":"<< nd->second->count<<endl;
return os;
}
//-----------------------------------------------------------------------------------------------------------------------
ostream & operator<<(ostream & os, const Trie::Node & n)
{
os<<"Node:"<<n.first<<" ,count:"<<n.second->count<<" ,child:"<<n.second->childNodes.size()<<endl;
return os;
}
//-----------------------------------------------------------------------------------------------------------------------
Trie::~Trie()
{
for (auto & keyValuePair : rootNode.second->childNodes)
DeleteNode(keyValuePair);
if(rootNode.second->childNodes.size()!=0)
rootNode.second->childNodes.clear();
}
//-----------------------------------------------------------------------------------------------------------------------
void Trie::DeleteNode(Node & nd)
{
if (nd.second->childNodes.size() == 0)
return;
else
{
for (auto & keyValuePair : nd.second->childNodes)
DeleteNode(keyValuePair);
}
try{nd.second->childNodes.clear();}
catch(exception &e)
{throw runtime_error(string(__FUNCTION__)+": Fatal error - can't delete node from tree. Exception content:"+e.what());}
return;
}
//-----------------------------------------------------------------------------------------------------------------------
void Trie::CopyNodesRecursive(const Node & sourceNode,Node & parent)
{
try{
auto res = parent.second->childNodes.insert(Node(sourceNode.first,shared_ptr<NodeElement>(new NodeElement(sourceNode.second->count))));
if (!res.second)
throw runtime_error("Insert method failed...");
res.first->second->parent = &parent;
for (auto & sourceNodeChild : sourceNode.second->childNodes)
CopyNodesRecursive(sourceNodeChild,*res.first);
lastNode = &*res.first;
return;
}
catch (exception & e)
{throw runtime_error(string(__FUNCTION__)+": Fatal error: can't insert node into tree. Reason:"+e.what());}
}
//-----------------------------------------------------------------------------------------------------------------------
bool Trie::InsertNGram(const deque<UnicodeString> & nGram)
{
Node * currNode = &rootNode;
Container::iterator tmpNode;
if(nGram.size()==0){cout<<"nGram is empty..."<<endl;return false;}
try
{
for (auto word : nGram)
{
// create a new node if path doesn't exists
tmpNode = currNode->second->childNodes.find(word);
if (tmpNode == currNode->second->childNodes.end()) //Node is not exist
{
auto res = currNode->second->childNodes.insert(Node(word,shared_ptr<NodeElement>(new NodeElement)));
if (!res.second) throw runtime_error("Insert method failed...");
nodesCount++;
tmpNode = res.first;
tmpNode->second->parent = currNode;
lastNode = &*tmpNode;
}
else // The node is already in the trie, increase the node count
{tmpNode->second->count +=1;}
// go to the next node // Don't like this as it can be not safe...
currNode = &*tmpNode;
}
return true;
}
catch(const exception &e)
{
cout<<__FUNCTION__<<": Fatal error - can't insert node into tree. Reason:"<<e.what()<<endl;
return false;
}
}
//-----------------------------------------------------------------------------------------------------------------------
Trie::Node * Trie::SearchNode(const deque<UnicodeString> & nodePath)
{
//For return value it should be "boost::optional<Trie::Node&>()" - it will be part of C++17 standard with std::optional.
Node* currNode = &rootNode;
Container::iterator tmpNode;
try
{
if(nodePath.size()==0)
throw logic_error(string(__FUNCTION__)+": nodePath is empty...");
for(auto word : nodePath)
{
// go to next node
// cout<<word<<endl;
tmpNode = currNode->second->childNodes.find(word);
if (tmpNode != currNode->second->childNodes.end())
{
// cout<<"k:"<<tmpNode->first<<endl;
currNode = &*tmpNode;
// cout<<"cn_k:"<<currNode->first<<endl;
// cout<<"chld:"<<currNode->second->childNodes.size()<<endl;
// for (auto elem: currNode->second->childNodes)
// cout<<"\t"<<elem.first<<":"<<elem.second->count<<endl;
}
else//reach end of path in Trie
return nullptr;
}
}
catch(logic_error &e)
{
cout<<e.what()<<endl;
return nullptr;
}
return currNode;
}
//-----------------------------------------------------------------------------------------------------------------------
UnicodeString Trie::GetMostFrequentChildNode(const deque<UnicodeString> & nodePath)
{
Node * currNode = SearchNode(nodePath);
if (!currNode){return "Node is not found";}
Container childNodes = currNode->second->childNodes;
UnicodeString currKey;
unsigned short currUiCount=0;
if (childNodes.size()==0){return "Node child not found";}
//if node has only one child node then just return the first child node name
else if (childNodes.size()==1){return childNodes.begin()->first;}
else // node has more than one child, simply return the one that has max uiCount (most frequent)
{
//This is actually a trade-off between using unordered_map vs other ordered containers (getting the most frequent element be comparing uiCount)...
//unordered_map shows better performance for the find operation...that is crucial as we have a lot of operation for finding keys...
//Can be also a possible option to copy contents of child nodes to any ordered container swapping key and value... then get either top or bottom element
for (const auto & keyValuePair : childNodes)
if (currUiCount < keyValuePair.second->count)// In case we have one or more nodes with the same uiCount we
// just pick the first which returned by iterator as we assume
// the same probability for every node
{
currUiCount = keyValuePair.second->count;
currKey = keyValuePair.first;
}
}
return currKey;
}
//-----------------------------------------------------------------------------------------------------------------------
UnicodeString Trie::GetRandomChildNode(const deque<UnicodeString> & nodePath)
{
Node* currNode = SearchNode(nodePath);
if (!currNode){return UnicodeString("Node is not found");}
Container & childNodes = currNode->second->childNodes; //Just to make things simpler to read
UnicodeString key;
//
vector<int> vFreq;
vector<UnicodeString> vKey;
if (childNodes.size()==0){return "Node child not found";}
//if node has only one child node then just return the first child node name
else if (childNodes.size()==1){return childNodes.begin()->first;}
else // node has more than one child, simply return the one that has max uiCount (most frequent)
{
for (auto & keyValuePair : childNodes)
{
vFreq.push_back(keyValuePair.second->count);
vKey.push_back(keyValuePair.first);
}
std::random_device rd;
std::mt19937 gen(rd());
std::discrete_distribution<> disc_dist(vFreq.begin(),vFreq.end());
key = vKey.at(disc_dist(gen));
}
return key;
}
//-----------------------------------------------------------------------------------------------------------------------
deque<UnicodeString> Trie::String2Deque(const UnicodeString & str)
{
deque<UnicodeString> cont;
Locale bilocale("ru_RU");
UErrorCode status = U_ZERO_ERROR;
UnicodeString word;
int32_t start =0, end =0;
unique_ptr<BreakIterator> wordIterator (BreakIterator::createWordInstance(bilocale, status));
if(U_FAILURE(status))
throw runtime_error(string(__FUNCTION__)+": Can't create word iterator...");
wordIterator->setText(str);
for(end = wordIterator->next();
end != BreakIterator::DONE;
start = end, end = wordIterator->next())
{
word = str.tempSubString(start,end-start);
if (word==" "){continue;}
cont.push_back(word);
}
return cont;
}
//-----------------------------------------------------------------------------------------------------------------------
bool Trie::CompleteText(const UnicodeString & startText,int iLength,enum Mode mode)
{
UnicodeString word;
deque<UnicodeString> vText;
try
{
vText = String2Deque(startText);
if(vText.size()==0)throw runtime_error("Start text is empty..."); //smth goes wrong
cout<<startText << " ";
while(iLength--)
{
if (mode == Mode::MostFreq)
word = GetMostFrequentChildNode(vText);
else if (mode == Mode::ProbDistrib)
word = GetRandomChildNode(vText);
if(word=="Node is not found") throw runtime_error("Processing stopped, n-gram is not found...");
if(word=="Node child not found")throw runtime_error("Processing stopped, no child nodes for n-gram found...");
cout<< word << " ";
vText.push_back(word);
vText.pop_front();
}
cout<<endl;
vText.clear();
return true;
}
catch (exception & e)
{
vText.clear();
cout<<endl<<__FUNCTION__<<": "<<e.what()<<endl;
return false;
}
}
//-----------------------------------------------------------------------------------------------------------------------
bool Trie::BuildTrieFromFile(const string & filename)
{
int const windowWidth = chainRank +2;
struct stat st;
int32_t length = 0,start=0,end=0, ruleStatus=0;
int wordsCount = 0;
UBreakIterator * bi = nullptr; // Conflicts with unique_ptr
Locale bilocale("ru_RU");
UErrorCode status = U_ZERO_ERROR;
int totalWords=0;
ifstream fin;
deque<UnicodeString> nGram; // Fast insertion and removal from both sides O(1)
UnicodeString usContent,word;
unique_ptr<char[]> rawContent;
if (windowWidth<=1)
{cout<< "windowWidth is less than 2. Supported values are: from 2 to words count in the corpus/string/file..."<< endl;return false;}
try{
fin.exceptions(std::ios_base::badbit|std::ios_base::failbit);
fin.open(filename.c_str(),std::ios_base::in|std::ios_base::binary);
if(!fin.is_open()){throw runtime_error("Error: can't open file: "+filename);}
//Calculating the file size:
stat(filename.c_str(), &st);
length = st.st_size;
//Read entire file
rawContent.reset(new char[length+1]);
fin.read(rawContent.get(),length);
rawContent[length]='\0';
if (!fin || length!=fin.gcount())throw runtime_error("Error reading "+filename);
fin.close();
//Build UTF8 content
usContent = UnicodeString::fromUTF8(rawContent.get());
//Freeing memory
rawContent.reset();
//Create break iterator
bi = ubrk_open(UBRK_WORD, "ru_RU", usContent.getBuffer(), usContent.length(), &status);
if(U_FAILURE(status))
throw std::runtime_error("Can't create break iterator object...");
for(end = ubrk_next(bi);end != UBRK_DONE;start = end, end = ubrk_next(bi))
{
ruleStatus= ubrk_getRuleStatus(bi);
if (ruleStatus == UBRK_WORD_NONE || ruleStatus == UBRK_WORD_NUMBER)// Count only words (except numbers, spaces & punctuation)
continue;
//Extract iterated word
word = usContent.tempSubString(start,end-start).toLower();
if(wordsCount == windowWidth)
{
//Forward nGram to trie
if (!InsertNGram(nGram))throw std::runtime_error("Can't insert n-gram into tree...");
//delete first element
nGram.pop_front();
// add current element
nGram.push_back(word);
}
else{
nGram.push_back(word);
wordsCount+=1;
}
totalWords++;
}
//Forward last nGram to trie
if (!InsertNGram(nGram))throw std::runtime_error("Can't insert n-gram into tree...");
nGram.clear();
ubrk_close(bi);
cout<<__FUNCTION__<<": Totalwords:"<<totalWords<<endl;
return true;
}
catch(const exception &e)
{
cout<<__FUNCTION__<<": "<<e.what()<<endl;
if(nGram.size()!=0)nGram.clear();
if(bi)ubrk_close(bi);
if(fin.is_open())fin.close();
return false;
}
}
//-----------------------------------------------------------------------------------------------------------------------
bool Trie::BuildTrieFromFolder(const string & folder)
{
DIR *pDir = NULL;
struct dirent *entry;
struct stat statbuf;
char currentDir[256];
try
{
pDir = opendir(folder.c_str());
if(!pDir){throw runtime_error("Can't open directory with corpus files...");}
if(!getcwd(currentDir,256)){strcpy(currentDir,folder.c_str());}
if(chdir(folder.c_str())==-1){throw runtime_error("Can't set current directory to "+ folder);}
while((entry = readdir(pDir)) != NULL)
{
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)){continue;}
cout<<"=> Loading file: "<<folder<<"/"<<entry->d_name<<" into trie tree..."<<endl;
if(!BuildTrieFromFile(string(entry->d_name))){throw runtime_error("Can't build trie from file "+string(entry->d_name));}
}
if(chdir(currentDir)==-1){throw runtime_error("Can't set current directory to "+ string(currentDir));}
if(pDir){closedir(pDir);}
return true;
}
catch(const exception &e)
{
cout<<__FUNCTION__<<": "<<e.what()<<endl;
if(pDir){closedir(pDir);}
return false;
}
}
//-----------------------------------------------------------------------------------------------------------------------
void Trie::WriteNodeToFile(ofstream & fout,const Node & node)
{
size_t childNodesCount = node.second->childNodes.size();
unsigned short uiCount = node.second->count;
int32_t keyLength = node.first.length();
fout.write((char*)&uiCount,sizeof(uiCount)); //Node count
fout.write((char*)&childNodesCount,sizeof(childNodesCount));//child nodes count
//std::cout<<"writing node: "<<key<<std::endl;
fout.write((char*)&keyLength,sizeof(keyLength)); //key length
fout.write((char*)node.first.getBuffer(),sizeof(UChar)*keyLength); //key
//std::cout<<"writing node: "<<key2<<std::endl;
for (auto keyValuePair : node.second->childNodes)
WriteNodeToFile(fout,keyValuePair);
return;
}
//-----------------------------------------------------------------------------------------------------------------------
bool Trie::SerializeTree(const string & filename)
{
ofstream fout;
size_t childNodesCount = rootNode.second->childNodes.size();
cout<<"=> Start dumping Markov chain into file: "<<filename<<endl;
try
{
fout.exceptions(std::ios_base::badbit|std::ios_base::failbit); //throw exceptions in case of any write error
fout.open(filename.c_str(),std::ios_base::out|std::ios_base::trunc|std::ios_base::binary);
if (!fout.is_open()) {throw std::runtime_error("Can't create file for serializing trie tree...");}
//write general info
fout.write((char*)&chainRank,sizeof(chainRank)); //Rank
fout.write((char*)&childNodesCount,sizeof(childNodesCount)); //Root node child count
for (auto keyValuePair : rootNode.second->childNodes)
WriteNodeToFile(fout,keyValuePair);
fout.close();
return true;
}
catch(const exception &e)
{
cout<<__FUNCTION__<<": "<<e.what()<<endl;
if(fout.is_open()){fout.close();}
return false;
}
}
//-----------------------------------------------------------------------------------------------------------------------
void Trie::ReadNodeFromFile(ifstream & fin, Node & node)
{
size_t childNodesCount = 0;
unsigned short uiCount = 0;
int32_t keyLength = 0;
Container::iterator itNode;
fin.read((char*)&uiCount,sizeof(unsigned short));
fin.read((char*)&childNodesCount,sizeof(size_t));
fin.read((char*)&keyLength,sizeof(int32_t));
unique_ptr<UChar[]> buffer(new UChar[keyLength]);
fin.read((char *)buffer.get(),sizeof(UChar)*keyLength);
UnicodeString key(buffer.get(),keyLength);
//std::cout<<"reading node: "<<key<<std::endl;
auto res = node.second->childNodes.insert(Node(key,shared_ptr<NodeElement>(new NodeElement(uiCount))));
if (!res.second) throw runtime_error("Can't insert node into tree...");
while(childNodesCount--)
ReadNodeFromFile(fin,*res.first);
return;
}
//-----------------------------------------------------------------------------------------------------------------------
bool Trie::DeserializeTree(const string & filename)
{
ifstream fin;
size_t childNodesCount;
cout<<"=> Start loading Markov chain from file: "<<filename<<endl;
try
{
fin.exceptions(std::ios_base::badbit|std::ios_base::failbit|std::ios_base::eofbit); //throw exceptions in case of any read error
//(we need to read all the data and the data should
// fit exactly into data structure, so no eof should encountered
// while reading.
fin.open(filename.c_str(),std::ios_base::in|std::ios_base::binary);
if (!fin.is_open()) {throw std::runtime_error("Can't open file "+filename);}
//Clear current tree
for (auto & keyValuePair : rootNode.second->childNodes)
DeleteNode(keyValuePair);
if(rootNode.second->childNodes.size()!=0)
rootNode.second->childNodes.clear();
//
rootNode.second->count = 0;
//read general info
fin.read((char*)&chainRank,sizeof(chainRank)); //Rank
fin.read((char*)&childNodesCount,sizeof(childNodesCount)); //Root node child count
while(childNodesCount--)
ReadNodeFromFile(fin,rootNode);
fin.close();
return true;
}
catch(const std::exception &e)
{
cout<<__FUNCTION__<<": "<<e.what()<<endl;
if(fin.is_open())fin.close();
return false;
}
}
//-----------------------------------------------------------------------------------------------------------------------
Trie::iterator Trie::begin()
{
return iterator(&rootNode,lastNode);
}
Trie::iterator Trie::end()
{
return iterator(lastNode +1,lastNode);
}
//-----------------------------------------------------------------------------------------------------------------------
//#
//# ITERATOR
//#
//-----------------------------------------------------------------------------------------------------------------------
template <typename ValueType>
bool Trie::TrieIterator<ValueType>::operator!=(const TrieIterator & it) const
{
if (p != &*it)
return true;
else
return false;
}
//-----------------------------------------------------------------------------------------------------------------------
template <typename ValueType>
bool Trie::TrieIterator<ValueType>::operator==(const TrieIterator & it) const
{
if (p == &*it)
return true;
else
return false;
}
//-----------------------------------------------------------------------------------------------------------------------
template <typename ValueType>
ValueType & Trie::TrieIterator<ValueType>::operator*() const
{
return *p;
}
//-----------------------------------------------------------------------------------------------------------------------
template <typename ValueType>
Trie::TrieIterator<ValueType> & Trie::TrieIterator<ValueType>::operator++()
{
if (dir == direction::down) // drill down
{
futureParent = nullptr;
chNodeIteratorPos = curParent->second->childNodes.begin();
auto size = curParent->second->childNodes.size();
if (size != 0)
{
p = &*chNodeIteratorPos;
childProcessed = size-1; // one node is processed already
dir = direction::right;
//Set the future parent
if (p->second->childNodes.size()!=0)
futureParent = p;
}
else //up and right, if nowhere to go, then we reach the end
{
if (isRoot(curParent))
{
p = pLn+1;
return *this;
}
auto pNodeIteratorPos = curParent->second->parent->second->childNodes.find(curParent->first);
++pNodeIteratorPos;
if (pNodeIteratorPos == curParent->second->parent->second->childNodes.end())
{
dir = direction::right;
curParent = curParent->second->parent;
return operator++();
}
curParent = &*pNodeIteratorPos;
return operator++();
}
}
else //direction::right
{
if (childProcessed == 0)
{
dir = direction::down; // try to go down
//Set the next parent
if (futureParent)
curParent = futureParent; //drill down to one of the child node of the curParent
//Can't drill down, no candidates among nodes, just processed...
else //up and right, if nowhere to go, then we reach the end
{
if (isRoot(curParent))
{
p = pLn+1;
return *this;
}
auto pNodeIteratorPos = curParent->second->parent->second->childNodes.find(curParent->first);
++pNodeIteratorPos;
if (pNodeIteratorPos == curParent->second->parent->second->childNodes.end())
{
dir = direction::right;
curParent = curParent->second->parent;
return operator++();
}
curParent = &*pNodeIteratorPos;
return operator++();
}
return operator++();
}
p =&*(++chNodeIteratorPos);
if ((!futureParent) & (p->second->childNodes.size()!=0))
futureParent = p;
childProcessed--;
}
return *this;
}
//-----------------------------------------------------------------------------------------------------------------------
template <typename ValueType>
bool Trie::TrieIterator<ValueType>::isRoot(const ValueType* p)
{
if (p->second->parent)
return false;
else
return true;
}
//-----------------------------------------------------------------------------------------------------------------------
//Force compiler to generate instances:
template bool Trie::TrieIterator<Trie::Node>::operator!=(const TrieIterator & it) const;
template bool Trie::TrieIterator<Trie::Node>::operator==(const TrieIterator & it) const;
template Trie::Node & Trie::TrieIterator<Trie::Node>::operator*() const;
template Trie::TrieIterator<Trie::Node>& Trie::TrieIterator<Trie::Node>::operator++();