-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path_trie_iterator.h
142 lines (125 loc) · 3.31 KB
/
_trie_iterator.h
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
#pragma once
#include <string>
#include <iterator>
#include <iostream>
#include "_trie_node.h"
#include "_trie_util.h"
/*
Iterator class definition.
*/
template <typename T>
class trie_iterator {
private:
tnode<T>* cur_node;
public:
using iterator_category = std::bidirectional_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = value_type*;
using reference = value_type&;
// Constructors
explicit trie_iterator(tnode<T>*);
trie_iterator();
protected:
// Member functions
tnode<T>* getNode();
public:
// Operators
trie_iterator<T>& operator++ ();
trie_iterator<T> operator++ (int);
trie_iterator<T>& operator-- ();
trie_iterator<T> operator-- (int);
bool operator== (const trie_iterator<T>&) const;
bool operator!= (const trie_iterator<T>&) const;
reference operator* () const;
pointer operator-> () const;
};
/*
Function definitions of trie_iterator class member functions.
*/
template <typename T>
trie_iterator<T>::trie_iterator(tnode<T>* root) {
this->cur_node = root;
}
template <typename T>
trie_iterator<T>::trie_iterator() {
this->cur_node = nullptr;
}
template <typename T>
trie_iterator<T>& trie_iterator<T>::operator++ () {
if (this->cur_node == nullptr || this->cur_node->getParentIndex() == 1516) {
return *this;
}
if (this->cur_node->getParentIndex() == -1516) {
this->cur_node = this->cur_node->getParent();
return *this;
}
tnode<T>* tmp = recur(this->cur_node);
if (tmp == nullptr) {
T flag;
tmp = new tnode<T>(flag, this->cur_node, 1516);
}
this->cur_node = tmp;
return *this;
}
template <typename T>
trie_iterator<T> trie_iterator<T>::operator++ (int) {
trie_iterator<T> t = *this;
++(*this);
return t;
}
template <typename T>
trie_iterator<T>& trie_iterator<T>::operator-- () {
if (this->cur_node == nullptr || this->cur_node->getParentIndex() == -1516) {
return *this;
}
if (this->cur_node->getParentIndex() == 1516) {
this->cur_node = this->cur_node->getParent();
return *this;
}
tnode<T>* tmp = rrecur(
this->cur_node->getParent(),
this->cur_node->getParentIndex() - 1
);
if (tmp == nullptr) {
T flag;
tmp = new tnode<T>(flag, this->cur_node, -1516);
}
this->cur_node = tmp;
return *this;
}
template <typename T>
trie_iterator<T> trie_iterator<T>::operator-- (int) {
trie_iterator<T> t = *this;
--(*this);
return t;
}
template <typename T>
bool trie_iterator<T>::operator== (const trie_iterator<T>& t) const {
if (this->cur_node == nullptr && t.cur_node == nullptr) {
return true;
} else if (this->cur_node == nullptr || t.cur_node == nullptr) {
return false;
}
if (this->cur_node->getParentIndex() == t.cur_node->getParentIndex()
&& this->cur_node->getParent() == t.cur_node->getParent()) {
return true;
}
return false;
}
template <typename T>
bool trie_iterator<T>::operator!= (const trie_iterator<T>& t) const {
return !(*this == t);
}
template <typename T>
typename trie_iterator<T>::reference trie_iterator<T>::operator* () const {
return this->cur_node->get();
}
template <typename T>
typename trie_iterator<T>::pointer trie_iterator<T>::operator-> () const {
return &this->cur_node->get();
}
template <typename T>
tnode<T>* trie_iterator<T>::getNode() {
return this->cur_node;
}