File size: 4,757 Bytes
158b61b |
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 |
#pragma once
#include <stack>
#include <vector>
namespace MosesTraining {
namespace Syntax {
template<typename T>
Tree<T>::~Tree() {
for (typename std::vector<Tree *>::iterator p = children_.begin();
p != children_.end(); ++p) {
delete *p;
}
}
template<typename T>
void Tree<T>::SetParents() {
for (typename std::vector<Tree *>::iterator p = children_.begin();
p != children_.end(); ++p) {
(*p)->parent() = this;
(*p)->SetParents();
}
}
template<typename T>
std::size_t Tree<T>::Depth() const {
std::size_t depth = 0;
Tree *ancestor = parent_;
while (ancestor != 0) {
++depth;
ancestor = ancestor->parent_;
}
return depth;
}
template<typename T>
template<typename V>
class Tree<T>::PreOrderIter {
public:
PreOrderIter();
PreOrderIter(V &);
V &operator*() { return *node_; }
V *operator->() { return node_; }
PreOrderIter &operator++();
PreOrderIter operator++(int);
bool operator==(const PreOrderIter &);
bool operator!=(const PreOrderIter &);
private:
// Pointer to the current node.
V *node_;
// Stack of indices defining the position of node_ within the child vectors
// of its ancestors.
std::stack<std::size_t> index_stack_;
};
template<typename T>
template<typename V>
Tree<T>::PreOrderIter<V>::PreOrderIter()
: node_(0) {
}
template<typename T>
template<typename V>
Tree<T>::PreOrderIter<V>::PreOrderIter(V &t)
: node_(&t) {
}
template<typename T>
template<typename V>
Tree<T>::PreOrderIter<V> &Tree<T>::PreOrderIter<V>::operator++() {
// If the current node has children then visit the left-most child next.
if (!node_->children().empty()) {
index_stack_.push(0);
node_ = node_->children()[0];
return *this;
}
// Otherwise, try node's ancestors until either a node is found with a
// sibling to the right or we reach the root (in which case the traversal
// is complete).
V *ancestor = node_->parent_;
while (ancestor) {
std::size_t index = index_stack_.top();
index_stack_.pop();
if (index+1 < ancestor->children_.size()) {
index_stack_.push(index+1);
node_ = ancestor->children()[index+1];
return *this;
}
ancestor = ancestor->parent_;
}
node_ = 0;
return *this;
}
template<typename T>
template<typename V>
Tree<T>::PreOrderIter<V> Tree<T>::PreOrderIter<V>::operator++(int) {
PreOrderIter tmp(*this);
++*this;
return tmp;
}
template<typename T>
template<typename V>
bool Tree<T>::PreOrderIter<V>::operator==(const PreOrderIter &rhs) {
return node_ == rhs.node_;
}
template<typename T>
template<typename V>
bool Tree<T>::PreOrderIter<V>::operator!=(const PreOrderIter &rhs) {
return node_ != rhs.node_;
}
template<typename T>
template<typename V>
class Tree<T>::LeafIter {
public:
LeafIter();
LeafIter(V &);
V &operator*() { return *node_; }
V *operator->() { return node_; }
LeafIter &operator++();
LeafIter operator++(int);
bool operator==(const LeafIter &);
bool operator!=(const LeafIter &);
private:
// Pointer to the current node.
V *node_;
// Stack of indices defining the position of node_ within the child vectors
// of its ancestors.
std::stack<std::size_t> index_stack_;
};
template<typename T>
template<typename V>
Tree<T>::LeafIter<V>::LeafIter()
: node_(0) {
}
template<typename T>
template<typename V>
Tree<T>::LeafIter<V>::LeafIter(V &t)
: node_(&t) {
// Navigate to the first leaf.
while (!node_->IsLeaf()) {
index_stack_.push(0);
node_ = node_->children()[0];
}
}
template<typename T>
template<typename V>
Tree<T>::LeafIter<V> &Tree<T>::LeafIter<V>::operator++() {
// Try node's ancestors until either a node is found with a sibling to the
// right or we reach the root (in which case the traversal is complete).
V *ancestor = node_->parent_;
while (ancestor) {
std::size_t index = index_stack_.top();
index_stack_.pop();
if (index+1 < ancestor->children_.size()) {
index_stack_.push(index+1);
node_ = ancestor->children()[index+1];
// Navigate to the first leaf.
while (!node_->IsLeaf()) {
index_stack_.push(0);
node_ = node_->children()[0];
}
return *this;
}
ancestor = ancestor->parent_;
}
node_ = 0;
return *this;
}
template<typename T>
template<typename V>
Tree<T>::LeafIter<V> Tree<T>::LeafIter<V>::operator++(int) {
LeafIter tmp(*this);
++*this;
return tmp;
}
template<typename T>
template<typename V>
bool Tree<T>::LeafIter<V>::operator==(const LeafIter &rhs) {
return node_ == rhs.node_;
}
template<typename T>
template<typename V>
bool Tree<T>::LeafIter<V>::operator!=(const LeafIter &rhs) {
return node_ != rhs.node_;
}
} // namespace Syntax
} // namespace MosesTraining
|