llvm.org GIT mirror llvm / 08b934e
Add iterators for child traversal. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45246 91177308-0d34-0410-b5e6-96231b3b80d8 Anton Korobeynikov 12 years ago
1 changed file(s) with 30 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
3333 public:
3434 class Node {
3535 friend class Trie;
36 friend class GraphTraits >;
37
36
37 public:
38 typedef std::vector NodeVectorType;
39 typedef typename NodeVectorType::iterator iterator;
40 typedef typename NodeVectorType::const_iterator const_iterator;
41
42 private:
3843 typedef enum {
3944 Same = -3,
4045 StringIsPrefix = -2,
4247 DontMatch = 0,
4348 HaveCommonPart
4449 } QueryResult;
45 typedef std::vector NodeVector;
46 typedef typename std::vector::iterator NodeVectorIter;
4750
4851 struct NodeCmp {
4952 bool operator() (Node* N1, Node* N2) {
5659
5760 std::string Label;
5861 Payload Data;
59 NodeVector Children;
62 NodeVectorType Children;
6063
6164 // Do not implement
6265 Node(const Node&);
6669 if (Children.empty())
6770 Children.push_back(N);
6871 else {
69 NodeVectorIter I = std::lower_bound(Children.begin(), Children.end(),
70 N, NodeCmp());
72 iterator I = std::lower_bound(Children.begin(), Children.end(),
73 N, NodeCmp());
7174 // FIXME: no dups are allowed
7275 Children.insert(I, N);
7376 }
7578
7679 inline void setEdge(Node* N) {
7780 char Id = N->Label[0];
78 NodeVectorIter I = std::lower_bound(Children.begin(), Children.end(),
79 Id, NodeCmp());
81 iterator I = std::lower_bound(Children.begin(), Children.end(),
82 Id, NodeCmp());
8083 assert(I != Children.end() && "Node does not exists!");
8184 *I = N;
8285 }
118121 << "Label: " << Label << "\n"
119122 << "Children:\n";
120123
121 for (NodeVectorIter I = Children.begin(), E = Children.end(); I != E; ++I)
124 for (iterator I = Children.begin(), E = Children.end(); I != E; ++I)
122125 std::cerr << (*I)->Label << "\n";
123126 }
124127 #endif
125128
126129 inline Node* getEdge(char Id) {
127130 Node* fNode = NULL;
128 NodeVectorIter I = std::lower_bound(Children.begin(), Children.end(),
131 iterator I = std::lower_bound(Children.begin(), Children.end(),
129132 Id, NodeCmp());
130133 if (I != Children.end() && (*I)->Label[0] == Id)
131134 fNode = *I;
132135
133136 return fNode;
134137 }
138
139 inline iterator begin() { return Children.begin(); }
140 inline const_iterator begin() const { return Children.begin(); }
141 inline iterator end () { return Children.end(); }
142 inline const_iterator end () const { return Children.end(); }
143
144 inline size_t size () const { return Children.size(); }
145 inline bool empty() const { return Children.empty(); }
146 inline const Node* &front() const { return Children.front(); }
147 inline Node* &front() { return Children.front(); }
148 inline const Node* &back() const { return Children.back(); }
149 inline Node* &back() { return Children.back(); }
150
135151 };
136152
137153 private:
259275 template
260276 struct GraphTraits > {
261277 typedef typename Trie::Node NodeType;
262 typedef typename std::vector::iterator ChildIteratorType;
278 typedef typename Trie::Node::iterator ChildIteratorType;
263279
264280 static inline NodeType *getEntryNode(const Trie& T) {
265281 return T.getRoot();
266282 }
267283
268284 static inline ChildIteratorType child_begin(NodeType *N) {
269 return N->Children.begin();
285 return N->begin();
270286 }
271287 static inline ChildIteratorType child_end(NodeType *N) {
272 return N->Children.end();
288 return N->end();
273289 }
274290
275291 typedef typename std::vector::const_iterator nodes_iterator;