Otawa  0.10
PreorderIterator.h
Go to the documentation of this file.
1 /*
2  * $Id$
3  * PreorderIterator class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2008, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef OTAWA_GRAPH_PREORDERITERATOR_H_
23 #define OTAWA_GRAPH_PREORDERITERATOR_H_
24 
25 #include <otawa/graph/GenGraph.h>
26 
27 namespace otawa { namespace graph {
28 
29 // PreorderIterator class
30 template <class G>
31 class PreorderIterator: public elm::PreIterator<PreorderIterator<G>, typename G::Vertex> {
32 public:
33 
34  PreorderIterator(const G& graph, typename G::Vertex entry)
35  : _graph(graph),
36  visited(_graph.count()),
37  queued(_graph.count())
38  {
39  queue.put(entry);
40  queued.set(entry->index());
41  }
42 
43  inline bool ended(void) const { return queue.isEmpty(); }
44 
45  inline typename G::Vertex item(void) const { return queue.head(); }
46 
47  void next(void) {
48  typename G::Vertex node = queue.get();
49  visited.set(_graph.indexOf(node));
50  for(typename G::OutIterator succ(_graph, node); succ; succ++)
51  if(!queued.bit(_graph.indexOf(succ->target()))) {
52  ASSERT(!visited.bit(_graph.indexOf(succ->target())));
53  bool check = true;
54  for(typename G::InIterator pred(succ->target()); pred; pred++) {
55  check = visited.bit(_graph.indexOf(pred->source()));
56  if(!check)
57  break;
58  }
59  if(check) {
60  queue.put(succ->target());
61  queued.set(_graph.indexOf(succ->target()));
62  }
63  }
64  queued.clear(node->index());
65  }
66 
67 private:
68  const G& _graph;
71 };
72 
73 } } // otawa::namespace
74 
75 #endif /* OTAWA_GRAPH_PREORDERITERATOR_H_ */
bool ended(void) const
Definition: PreorderIterator.h:43
elm::genstruct::VectorQueue< typename G::Vertex > queue
Definition: PreorderIterator.h:70
bool isEmpty(void) const
bool bit(int index) const
void next(void)
Definition: PreorderIterator.h:47
PreorderIterator(const G &graph, typename G::Vertex entry)
Definition: PreorderIterator.h:34
An iterator allowing to traverse the graph using preorder, that is, a node is only traversed when its...
Definition: PreorderIterator.h:31
dtd::Element entry(dtd::make("entry", _ENTRY).attr(id))
const G & _graph
Definition: PreorderIterator.h:68
elm::BitVector queued
Definition: PreorderIterator.h:69
void put(const T &value)
void set(int index) const
void clear(int index) const
G::Vertex item(void) const
Definition: PreorderIterator.h:45
elm::BitVector visited
Definition: PreorderIterator.h:69