marci@281: // -*- c++ -*-
marci@281: #ifndef HUGO_BFS_ITERATOR_H
marci@281: #define HUGO_BFS_ITERATOR_H
marci@281: 
marci@281: #include <queue>
marci@281: #include <stack>
marci@281: #include <utility>
marci@281: #include <graph_wrapper.h>
marci@281: 
marci@281: namespace hugo {
marci@281: 
marci@281: //   template <typename Graph>
marci@281: //   struct bfs {
marci@281: //     typedef typename Graph::Node Node;
marci@281: //     typedef typename Graph::Edge Edge;
marci@281: //     typedef typename Graph::NodeIt NodeIt;
marci@281: //     typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@281: //     Graph& G;
marci@281: //     Node s;
marci@281: //     typename Graph::NodeMap<bool> reached;
marci@281: //     typename Graph::NodeMap<Edge> pred;
marci@281: //     typename Graph::NodeMap<int> dist;
marci@281: //     std::queue<Node> bfs_queue;
marci@281: //     bfs(Graph& _G, Node _s) : G(_G), s(_s), reached(_G), pred(_G), dist(_G) { 
marci@281: //       bfs_queue.push(s); 
marci@281: //       for(NodeIt i=G.template first<NodeIt>(); i.valid(); ++i) 
marci@281: // 	reached.set(i, false);
marci@281: //       reached.set(s, true);
marci@281: //       dist.set(s, 0); 
marci@281: //     }
marci@281:     
marci@281: //     void run() {
marci@281: //       while (!bfs_queue.empty()) {
marci@281: // 	Node v=bfs_queue.front();
marci@281: // 	OutEdgeIt e=G.template first<OutEdgeIt>(v);
marci@281: // 	bfs_queue.pop();
marci@281: // 	for( ; e.valid(); ++e) {
marci@281: // 	  Node w=G.bNode(e);
marci@281: // 	  std::cout << "scan node " << G.id(w) << " from node " << G.id(v) << std::endl;
marci@281: // 	  if (!reached.get(w)) {
marci@281: // 	    std::cout << G.id(w) << " is newly reached :-)" << std::endl;
marci@281: // 	    bfs_queue.push(w);
marci@281: // 	    dist.set(w, dist.get(v)+1);
marci@281: // 	    pred.set(w, e);
marci@281: // 	    reached.set(w, true);
marci@281: // 	  } else {
marci@281: // 	    std::cout << G.id(w) << " is already reached" << std::endl;
marci@281: // 	  }
marci@281: // 	}
marci@281: //       }
marci@281: //     }
marci@281: //   };
marci@281: 
marci@281: //   template <typename Graph> 
marci@281: //   struct bfs_visitor {
marci@281: //     typedef typename Graph::Node Node;
marci@281: //     typedef typename Graph::Edge Edge;
marci@281: //     typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@281: //     Graph& G;
marci@281: //     bfs_visitor(Graph& _G) : G(_G) { }
marci@281: //     void at_previously_reached(OutEdgeIt& e) { 
marci@281: //       //Node v=G.aNode(e);
marci@281: //       Node w=G.bNode(e);
marci@281: //       std::cout << G.id(w) << " is already reached" << std::endl;
marci@281: //    }
marci@281: //     void at_newly_reached(OutEdgeIt& e) { 
marci@281: //       //Node v=G.aNode(e);
marci@281: //       Node w=G.bNode(e);
marci@281: //       std::cout << G.id(w) << " is newly reached :-)" << std::endl;
marci@281: //     }
marci@281: //   };
marci@281: 
marci@281: //   template <typename Graph, typename ReachedMap, typename visitor_type>
marci@281: //   struct bfs_iterator {
marci@281: //     typedef typename Graph::Node Node;
marci@281: //     typedef typename Graph::Edge Edge;
marci@281: //     typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@281: //     Graph& G;
marci@281: //     std::queue<OutEdgeIt>& bfs_queue;
marci@281: //     ReachedMap& reached;
marci@281: //     visitor_type& visitor;
marci@281: //     void process() {
marci@281: //       while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@281: //       if (bfs_queue.empty()) return;
marci@281: //       OutEdgeIt e=bfs_queue.front();
marci@281: //       //Node v=G.aNode(e);
marci@281: //       Node w=G.bNode(e);
marci@281: //       if (!reached.get(w)) {
marci@281: // 	visitor.at_newly_reached(e);
marci@281: // 	bfs_queue.push(G.template first<OutEdgeIt>(w));
marci@281: // 	reached.set(w, true);
marci@281: //       } else {
marci@281: // 	visitor.at_previously_reached(e);
marci@281: //       }
marci@281: //     }
marci@281: //     bfs_iterator(Graph& _G, std::queue<OutEdgeIt>& _bfs_queue, ReachedMap& _reached, visitor_type& _visitor) : G(_G), bfs_queue(_bfs_queue), reached(_reached), visitor(_visitor) { 
marci@281: //       //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@281: //       valid();
marci@281: //     }
marci@281: //     bfs_iterator<Graph, ReachedMap, visitor_type>& operator++() { 
marci@281: //       //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@281: //       //if (bfs_queue.empty()) return *this;
marci@281: //       if (!valid()) return *this;
marci@281: //       ++(bfs_queue.front());
marci@281: //       //while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@281: //       valid();
marci@281: //       return *this;
marci@281: //     }
marci@281: //     //void next() { 
marci@281: //     //  while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@281: //     //  if (bfs_queue.empty()) return;
marci@281: //     //  ++(bfs_queue.front());
marci@281: //     //  while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@281: //     //}
marci@281: //     bool valid() { 
marci@281: //       while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@281: //       if (bfs_queue.empty()) return false; else return true;
marci@281: //     }
marci@281: //     //bool finished() { 
marci@281: //     //  while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@281: //     //  if (bfs_queue.empty()) return true; else return false;
marci@281: //     //}
marci@281: //     operator Edge () { return bfs_queue.front(); }
marci@281: 
marci@281: //   };
marci@281: 
marci@281: //   template <typename Graph, typename ReachedMap>
marci@281: //   struct bfs_iterator1 {
marci@281: //     typedef typename Graph::Node Node;
marci@281: //     typedef typename Graph::Edge Edge;
marci@281: //     typedef typename Graph::OutEdgeIt OutEdgeIt;
marci@281: //     Graph& G;
marci@281: //     std::queue<OutEdgeIt>& bfs_queue;
marci@281: //     ReachedMap& reached;
marci@281: //     bool _newly_reached;
marci@281: //     bfs_iterator1(Graph& _G, std::queue<OutEdgeIt>& _bfs_queue, ReachedMap& _reached) : G(_G), bfs_queue(_bfs_queue), reached(_reached) { 
marci@281: //       valid();
marci@281: //       if (!bfs_queue.empty() && bfs_queue.front().valid()) { 
marci@281: // 	OutEdgeIt e=bfs_queue.front();
marci@281: // 	Node w=G.bNode(e);
marci@281: // 	if (!reached.get(w)) {
marci@281: // 	  bfs_queue.push(G.template first<OutEdgeIt>(w));
marci@281: // 	  reached.set(w, true);
marci@281: // 	  _newly_reached=true;
marci@281: // 	} else {
marci@281: // 	  _newly_reached=false;
marci@281: // 	}
marci@281: //       }
marci@281: //     }
marci@281: //     bfs_iterator1<Graph, ReachedMap>& operator++() { 
marci@281: //       if (!valid()) return *this;
marci@281: //       ++(bfs_queue.front());
marci@281: //       valid();
marci@281: //       if (!bfs_queue.empty() && bfs_queue.front().valid()) { 
marci@281: // 	OutEdgeIt e=bfs_queue.front();
marci@281: // 	Node w=G.bNode(e);
marci@281: // 	if (!reached.get(w)) {
marci@281: // 	  bfs_queue.push(G.template first<OutEdgeIt>(w));
marci@281: // 	  reached.set(w, true);
marci@281: // 	  _newly_reached=true;
marci@281: // 	} else {
marci@281: // 	  _newly_reached=false;
marci@281: // 	}
marci@281: //       }
marci@281: //       return *this;
marci@281: //     }
marci@281: //     bool valid() { 
marci@281: //       while ( !bfs_queue.empty() && !bfs_queue.front().valid() ) { bfs_queue.pop(); } 
marci@281: //       if (bfs_queue.empty()) return false; else return true;
marci@281: //     }
marci@281: //     operator OutEdgeIt() { return bfs_queue.front(); }
marci@281: //     //ize
marci@281: //     bool newly_reached() { return _newly_reached; }
marci@281: 
marci@281: //   };
marci@281: 
marci@281: //   template <typename Graph, typename OutEdgeIt, typename ReachedMap>
marci@281: //   struct BfsIterator {
marci@281: //     typedef typename Graph::Node Node;
marci@281: //     Graph& G;
marci@281: //     std::queue<OutEdgeIt>& bfs_queue;
marci@281: //     ReachedMap& reached;
marci@281: //     bool b_node_newly_reached;
marci@281: //     OutEdgeIt actual_edge;
marci@281: //     BfsIterator(Graph& _G, 
marci@281: // 		std::queue<OutEdgeIt>& _bfs_queue, 
marci@281: // 		ReachedMap& _reached) : 
marci@281: //       G(_G), bfs_queue(_bfs_queue), reached(_reached) { 
marci@281: //       actual_edge=bfs_queue.front();
marci@281: //       if (actual_edge.valid()) { 
marci@281: // 	Node w=G.bNode(actual_edge);
marci@281: // 	if (!reached.get(w)) {
marci@281: // 	  bfs_queue.push(G.firstOutEdge(w));
marci@281: // 	  reached.set(w, true);
marci@281: // 	  b_node_newly_reached=true;
marci@281: // 	} else {
marci@281: // 	  b_node_newly_reached=false;
marci@281: // 	}
marci@281: //       }
marci@281: //     }
marci@281: //     BfsIterator<Graph, OutEdgeIt, ReachedMap>& 
marci@281: //     operator++() { 
marci@281: //       if (bfs_queue.front().valid()) { 
marci@281: // 	++(bfs_queue.front());
marci@281: // 	actual_edge=bfs_queue.front();
marci@281: // 	if (actual_edge.valid()) {
marci@281: // 	  Node w=G.bNode(actual_edge);
marci@281: // 	  if (!reached.get(w)) {
marci@281: // 	    bfs_queue.push(G.firstOutEdge(w));
marci@281: // 	    reached.set(w, true);
marci@281: // 	    b_node_newly_reached=true;
marci@281: // 	  } else {
marci@281: // 	    b_node_newly_reached=false;
marci@281: // 	  }
marci@281: // 	}
marci@281: //       } else {
marci@281: // 	bfs_queue.pop(); 
marci@281: // 	actual_edge=bfs_queue.front();
marci@281: // 	if (actual_edge.valid()) {
marci@281: // 	  Node w=G.bNode(actual_edge);
marci@281: // 	  if (!reached.get(w)) {
marci@281: // 	    bfs_queue.push(G.firstOutEdge(w));
marci@281: // 	    reached.set(w, true);
marci@281: // 	    b_node_newly_reached=true;
marci@281: // 	  } else {
marci@281: // 	    b_node_newly_reached=false;
marci@281: // 	  }
marci@281: // 	}
marci@281: //       }
marci@281: //       return *this;
marci@281: //     }
marci@281: //     bool finished() { return bfs_queue.empty(); }
marci@281: //     operator OutEdgeIt () { return actual_edge; }
marci@281: //     bool bNodeIsNewlyReached() { return b_node_newly_reached; }
marci@281: //     bool aNodeIsExamined() { return !(actual_edge.valid()); }
marci@281: //   };
marci@281: 
marci@281: 
marci@281: //   template <typename Graph, typename OutEdgeIt, typename ReachedMap>
marci@281: //   struct DfsIterator {
marci@281: //     typedef typename Graph::Node Node;
marci@281: //     Graph& G;
marci@281: //     std::stack<OutEdgeIt>& bfs_queue;
marci@281: //     ReachedMap& reached;
marci@281: //     bool b_node_newly_reached;
marci@281: //     OutEdgeIt actual_edge;
marci@281: //     DfsIterator(Graph& _G, 
marci@281: // 		std::stack<OutEdgeIt>& _bfs_queue, 
marci@281: // 		ReachedMap& _reached) : 
marci@281: //       G(_G), bfs_queue(_bfs_queue), reached(_reached) { 
marci@281: //       actual_edge=bfs_queue.top();
marci@281: //       if (actual_edge.valid()) { 
marci@281: // 	Node w=G.bNode(actual_edge);
marci@281: // 	if (!reached.get(w)) {
marci@281: // 	  bfs_queue.push(G.firstOutEdge(w));
marci@281: // 	  reached.set(w, true);
marci@281: // 	  b_node_newly_reached=true;
marci@281: // 	} else {
marci@281: // 	  ++(bfs_queue.top());
marci@281: // 	  b_node_newly_reached=false;
marci@281: // 	}
marci@281: //       } else {
marci@281: // 	bfs_queue.pop();
marci@281: //       }
marci@281: //     }
marci@281: //     DfsIterator<Graph, OutEdgeIt, ReachedMap>& 
marci@281: //     operator++() { 
marci@281: //       actual_edge=bfs_queue.top();
marci@281: //       if (actual_edge.valid()) { 
marci@281: // 	Node w=G.bNode(actual_edge);
marci@281: // 	if (!reached.get(w)) {
marci@281: // 	  bfs_queue.push(G.firstOutEdge(w));
marci@281: // 	  reached.set(w, true);
marci@281: // 	  b_node_newly_reached=true;
marci@281: // 	} else {
marci@281: // 	  ++(bfs_queue.top());
marci@281: // 	  b_node_newly_reached=false;
marci@281: // 	}
marci@281: //       } else {
marci@281: // 	bfs_queue.pop();
marci@281: //       }
marci@281: //       return *this;
marci@281: //     }
marci@281: //     bool finished() { return bfs_queue.empty(); }
marci@281: //     operator OutEdgeIt () { return actual_edge; }
marci@281: //     bool bNodeIsNewlyReached() { return b_node_newly_reached; }
marci@281: //     bool aNodeIsExamined() { return !(actual_edge.valid()); }
marci@281: //   };
marci@281: 
marci@281: //   template <typename Graph, typename OutEdgeIt, typename ReachedMap>
marci@281: //   struct BfsIterator1 {
marci@281: //     typedef typename Graph::Node Node;
marci@281: //     Graph& G;
marci@281: //     std::queue<OutEdgeIt>& bfs_queue;
marci@281: //     ReachedMap& reached;
marci@281: //     bool b_node_newly_reached;
marci@281: //     OutEdgeIt actual_edge;
marci@281: //     BfsIterator1(Graph& _G, 
marci@281: // 		std::queue<OutEdgeIt>& _bfs_queue, 
marci@281: // 		ReachedMap& _reached) : 
marci@281: //       G(_G), bfs_queue(_bfs_queue), reached(_reached) { 
marci@281: //       actual_edge=bfs_queue.front();
marci@281: //       if (actual_edge.valid()) { 
marci@281: //       	Node w=G.bNode(actual_edge);
marci@281: // 	if (!reached.get(w)) {
marci@281: // 	  bfs_queue.push(OutEdgeIt(G, w));
marci@281: // 	  reached.set(w, true);
marci@281: // 	  b_node_newly_reached=true;
marci@281: // 	} else {
marci@281: // 	  b_node_newly_reached=false;
marci@281: // 	}
marci@281: //       }
marci@281: //     }
marci@281: //     void next() { 
marci@281: //       if (bfs_queue.front().valid()) { 
marci@281: // 	++(bfs_queue.front());
marci@281: // 	actual_edge=bfs_queue.front();
marci@281: // 	if (actual_edge.valid()) {
marci@281: // 	  Node w=G.bNode(actual_edge);
marci@281: // 	  if (!reached.get(w)) {
marci@281: // 	    bfs_queue.push(OutEdgeIt(G, w));
marci@281: // 	    reached.set(w, true);
marci@281: // 	    b_node_newly_reached=true;
marci@281: // 	  } else {
marci@281: // 	    b_node_newly_reached=false;
marci@281: // 	  }
marci@281: // 	}
marci@281: //       } else {
marci@281: // 	bfs_queue.pop(); 
marci@281: // 	actual_edge=bfs_queue.front();
marci@281: // 	if (actual_edge.valid()) {
marci@281: // 	  Node w=G.bNode(actual_edge);
marci@281: // 	  if (!reached.get(w)) {
marci@281: // 	    bfs_queue.push(OutEdgeIt(G, w));
marci@281: // 	    reached.set(w, true);
marci@281: // 	    b_node_newly_reached=true;
marci@281: // 	  } else {
marci@281: // 	    b_node_newly_reached=false;
marci@281: // 	  }
marci@281: // 	}
marci@281: //       }
marci@281: //       //return *this;
marci@281: //     }
marci@281: //     bool finished() { return bfs_queue.empty(); }
marci@281: //     operator OutEdgeIt () { return actual_edge; }
marci@281: //     bool bNodeIsNewlyReached() { return b_node_newly_reached; }
marci@281: //     bool aNodeIsExamined() { return !(actual_edge.valid()); }
marci@281: //   };
marci@281: 
marci@281: 
marci@281: //   template <typename Graph, typename OutEdgeIt, typename ReachedMap>
marci@281: //   struct DfsIterator1 {
marci@281: //     typedef typename Graph::Node Node;
marci@281: //     Graph& G;
marci@281: //     std::stack<OutEdgeIt>& bfs_queue;
marci@281: //     ReachedMap& reached;
marci@281: //     bool b_node_newly_reached;
marci@281: //     OutEdgeIt actual_edge;
marci@281: //     DfsIterator1(Graph& _G, 
marci@281: // 		std::stack<OutEdgeIt>& _bfs_queue, 
marci@281: // 		ReachedMap& _reached) : 
marci@281: //       G(_G), bfs_queue(_bfs_queue), reached(_reached) { 
marci@281: //       //actual_edge=bfs_queue.top();
marci@281: //       //if (actual_edge.valid()) { 
marci@281: //       //	Node w=G.bNode(actual_edge);
marci@281: //       //if (!reached.get(w)) {
marci@281: //       //  bfs_queue.push(OutEdgeIt(G, w));
marci@281: //       //  reached.set(w, true);
marci@281: //       //  b_node_newly_reached=true;
marci@281: //       //} else {
marci@281: //       //  ++(bfs_queue.top());
marci@281: //       //  b_node_newly_reached=false;
marci@281: //       //}
marci@281: //       //} else {
marci@281: //       //	bfs_queue.pop();
marci@281: //       //}
marci@281: //     }
marci@281: //     void next() { 
marci@281: //       actual_edge=bfs_queue.top();
marci@281: //       if (actual_edge.valid()) { 
marci@281: // 	Node w=G.bNode(actual_edge);
marci@281: // 	if (!reached.get(w)) {
marci@281: // 	  bfs_queue.push(OutEdgeIt(G, w));
marci@281: // 	  reached.set(w, true);
marci@281: // 	  b_node_newly_reached=true;
marci@281: // 	} else {
marci@281: // 	  ++(bfs_queue.top());
marci@281: // 	  b_node_newly_reached=false;
marci@281: // 	}
marci@281: //       } else {
marci@281: // 	bfs_queue.pop();
marci@281: //       }
marci@281: //       //return *this;
marci@281: //     }
marci@281: //     bool finished() { return bfs_queue.empty(); }
marci@281: //     operator OutEdgeIt () { return actual_edge; }
marci@281: //     bool bNodeIsNewlyReached() { return b_node_newly_reached; }
marci@281: //     bool aNodeIsLeaved() { return !(actual_edge.valid()); }
marci@281: //   };
marci@281: 
marci@281: //   template <typename Graph, typename OutEdgeIt, typename ReachedMap>
marci@281: //   class BfsIterator2 {
marci@281: //     typedef typename Graph::Node Node;
marci@281: //     const Graph& G;
marci@281: //     std::queue<OutEdgeIt> bfs_queue;
marci@281: //     ReachedMap reached;
marci@281: //     bool b_node_newly_reached;
marci@281: //     OutEdgeIt actual_edge;
marci@281: //   public:
marci@281: //     BfsIterator2(const Graph& _G) : G(_G), reached(G, false) { }
marci@281: //     void pushAndSetReached(Node s) { 
marci@281: //       reached.set(s, true);
marci@281: //       if (bfs_queue.empty()) {
marci@281: // 	bfs_queue.push(G.template first<OutEdgeIt>(s));
marci@281: // 	actual_edge=bfs_queue.front();
marci@281: // 	if (actual_edge.valid()) { 
marci@281: // 	  Node w=G.bNode(actual_edge);
marci@281: // 	  if (!reached.get(w)) {
marci@281: // 	    bfs_queue.push(G.template first<OutEdgeIt>(w));
marci@281: // 	    reached.set(w, true);
marci@281: // 	    b_node_newly_reached=true;
marci@281: // 	  } else {
marci@281: // 	    b_node_newly_reached=false;
marci@281: // 	  }
marci@281: // 	} //else {
marci@281: // 	//}
marci@281: //       } else {
marci@281: // 	bfs_queue.push(G.template first<OutEdgeIt>(s));
marci@281: //       }
marci@281: //     }
marci@281: //     BfsIterator2<Graph, OutEdgeIt, ReachedMap>& 
marci@281: //     operator++() { 
marci@281: //       if (bfs_queue.front().valid()) { 
marci@281: // 	++(bfs_queue.front());
marci@281: // 	actual_edge=bfs_queue.front();
marci@281: // 	if (actual_edge.valid()) {
marci@281: // 	  Node w=G.bNode(actual_edge);
marci@281: // 	  if (!reached.get(w)) {
marci@281: // 	    bfs_queue.push(G.template first<OutEdgeIt>(w));
marci@281: // 	    reached.set(w, true);
marci@281: // 	    b_node_newly_reached=true;
marci@281: // 	  } else {
marci@281: // 	    b_node_newly_reached=false;
marci@281: // 	  }
marci@281: // 	}
marci@281: //       } else {
marci@281: // 	bfs_queue.pop(); 
marci@281: // 	if (!bfs_queue.empty()) {
marci@281: // 	  actual_edge=bfs_queue.front();
marci@281: // 	  if (actual_edge.valid()) {
marci@281: // 	    Node w=G.bNode(actual_edge);
marci@281: // 	    if (!reached.get(w)) {
marci@281: // 	      bfs_queue.push(G.template first<OutEdgeIt>(w));
marci@281: // 	      reached.set(w, true);
marci@281: // 	      b_node_newly_reached=true;
marci@281: // 	    } else {
marci@281: // 	      b_node_newly_reached=false;
marci@281: // 	    }
marci@281: // 	  }
marci@281: // 	}
marci@281: //       }
marci@281: //       return *this;
marci@281: //     }
marci@281: //     bool finished() const { return bfs_queue.empty(); }
marci@281: //     operator OutEdgeIt () const { return actual_edge; }
marci@281: //     bool isBNodeNewlyReached() const { return b_node_newly_reached; }
marci@281: //     bool isANodeExamined() const { return !(actual_edge.valid()); }
marci@281: //     const ReachedMap& getReachedMap() const { return reached; }
marci@281: //     const std::queue<OutEdgeIt>& getBfsQueue() const { return bfs_queue; }
marci@281: //  };
marci@281: 
marci@281: 
marci@281: //   template <typename Graph, typename OutEdgeIt, typename ReachedMap>
marci@281: //   class BfsIterator3 {
marci@281: //     typedef typename Graph::Node Node;
marci@281: //     const Graph& G;
marci@281: //     std::queue< std::pair<Node, OutEdgeIt> > bfs_queue;
marci@281: //     ReachedMap reached;
marci@281: //     bool b_node_newly_reached;
marci@281: //     OutEdgeIt actual_edge;
marci@281: //   public:
marci@281: //     BfsIterator3(const Graph& _G) : G(_G), reached(G, false) { }
marci@281: //     void pushAndSetReached(Node s) { 
marci@281: //       reached.set(s, true);
marci@281: //       if (bfs_queue.empty()) {
marci@281: // 	bfs_queue.push(std::pair<Node, OutEdgeIt>(s, G.template first<OutEdgeIt>(s)));
marci@281: // 	actual_edge=bfs_queue.front().second;
marci@281: // 	if (actual_edge.valid()) { 
marci@281: // 	  Node w=G.bNode(actual_edge);
marci@281: // 	  if (!reached.get(w)) {
marci@281: // 	    bfs_queue.push(std::pair<Node, OutEdgeIt>(w, G.template first<OutEdgeIt>(w)));
marci@281: // 	    reached.set(w, true);
marci@281: // 	    b_node_newly_reached=true;
marci@281: // 	  } else {
marci@281: // 	    b_node_newly_reached=false;
marci@281: // 	  }
marci@281: // 	} //else {
marci@281: // 	//}
marci@281: //       } else {
marci@281: // 	bfs_queue.push(std::pair<Node, OutEdgeIt>(s, G.template first<OutEdgeIt>(s)));
marci@281: //       }
marci@281: //     }
marci@281: //     BfsIterator3<Graph, OutEdgeIt, ReachedMap>& 
marci@281: //     operator++() { 
marci@281: //       if (bfs_queue.front().second.valid()) { 
marci@281: // 	++(bfs_queue.front().second);
marci@281: // 	actual_edge=bfs_queue.front().second;
marci@281: // 	if (actual_edge.valid()) {
marci@281: // 	  Node w=G.bNode(actual_edge);
marci@281: // 	  if (!reached.get(w)) {
marci@281: // 	    bfs_queue.push(std::pair<Node, OutEdgeIt>(w, G.template first<OutEdgeIt>(w)));
marci@281: // 	    reached.set(w, true);
marci@281: // 	    b_node_newly_reached=true;
marci@281: // 	  } else {
marci@281: // 	    b_node_newly_reached=false;
marci@281: // 	  }
marci@281: // 	}
marci@281: //       } else {
marci@281: // 	bfs_queue.pop(); 
marci@281: // 	if (!bfs_queue.empty()) {
marci@281: // 	  actual_edge=bfs_queue.front().second;
marci@281: // 	  if (actual_edge.valid()) {
marci@281: // 	    Node w=G.bNode(actual_edge);
marci@281: // 	    if (!reached.get(w)) {
marci@281: // 	      bfs_queue.push(std::pair<Node, OutEdgeIt>(w, G.template first<OutEdgeIt>(w)));
marci@281: // 	      reached.set(w, true);
marci@281: // 	      b_node_newly_reached=true;
marci@281: // 	    } else {
marci@281: // 	      b_node_newly_reached=false;
marci@281: // 	    }
marci@281: // 	  }
marci@281: // 	}
marci@281: //       }
marci@281: //       return *this;
marci@281: //     }
marci@281: //     bool finished() const { return bfs_queue.empty(); }
marci@281: //     operator OutEdgeIt () const { return actual_edge; }
marci@281: //     bool isBNodeNewlyReached() const { return b_node_newly_reached; }
marci@281: //     bool isANodeExamined() const { return !(actual_edge.valid()); }
marci@281: //     Node aNode() const { return bfs_queue.front().first; }
marci@281: //     Node bNode() const { return G.bNode(actual_edge); }
marci@281: //     const ReachedMap& getReachedMap() const { return reached; }
marci@281: //     //const std::queue< std::pair<Node, OutEdgeIt> >& getBfsQueue() const { return bfs_queue; }
marci@281: //  };
marci@281: 
marci@281: 
marci@281: //   template <typename Graph, typename OutEdgeIt, 
marci@281: // 	    typename ReachedMap/*=typename Graph::NodeMap<bool>*/ >
marci@281: //   class BfsIterator4 {
marci@281: //     typedef typename Graph::Node Node;
marci@281: //     const Graph& G;
marci@281: //     std::queue<Node> bfs_queue;
marci@281: //     ReachedMap& reached;
marci@281: //     bool b_node_newly_reached;
marci@281: //     OutEdgeIt actual_edge;
marci@281: //     bool own_reached_map;
marci@281: //   public:
marci@281: //     BfsIterator4(const Graph& _G, ReachedMap& _reached) : 
marci@281: //       G(_G), reached(_reached), 
marci@281: //       own_reached_map(false) { }
marci@281: //     BfsIterator4(const Graph& _G) : 
marci@281: //       G(_G), reached(*(new ReachedMap(G /*, false*/))), 
marci@281: //       own_reached_map(true) { }
marci@281: //     ~BfsIterator4() { if (own_reached_map) delete &reached; }
marci@281: //     void pushAndSetReached(Node s) { 
marci@281: //       //std::cout << "mimi" << &reached << std::endl;
marci@281: //       reached.set(s, true);
marci@281: //       //std::cout << "mumus" << std::endl;
marci@281: //       if (bfs_queue.empty()) {
marci@281: // 	//std::cout << "bibi1" << std::endl;
marci@281: // 	bfs_queue.push(s);
marci@281: // 	//std::cout << "zizi" << std::endl;
marci@281: // 	G./*getF*/first(actual_edge, s);
marci@281: // 	//std::cout << "kiki" << std::endl;
marci@281: // 	if (G.valid(actual_edge)/*.valid()*/) { 
marci@281: // 	  Node w=G.bNode(actual_edge);
marci@281: // 	  if (!reached.get(w)) {
marci@281: // 	    bfs_queue.push(w);
marci@281: // 	    reached.set(w, true);
marci@281: // 	    b_node_newly_reached=true;
marci@281: // 	  } else {
marci@281: // 	    b_node_newly_reached=false;
marci@281: // 	  }
marci@281: // 	} 
marci@281: //       } else {
marci@281: // 	//std::cout << "bibi2" << std::endl;
marci@281: // 	bfs_queue.push(s);
marci@281: //       }
marci@281: //     }
marci@281: //     BfsIterator4<Graph, OutEdgeIt, ReachedMap>& 
marci@281: //     operator++() { 
marci@281: //       if (G.valid(actual_edge)/*.valid()*/) { 
marci@281: // 	/*++*/G.next(actual_edge);
marci@281: // 	if (G.valid(actual_edge)/*.valid()*/) {
marci@281: // 	  Node w=G.bNode(actual_edge);
marci@281: // 	  if (!reached.get(w)) {
marci@281: // 	    bfs_queue.push(w);
marci@281: // 	    reached.set(w, true);
marci@281: // 	    b_node_newly_reached=true;
marci@281: // 	  } else {
marci@281: // 	    b_node_newly_reached=false;
marci@281: // 	  }
marci@281: // 	}
marci@281: //       } else {
marci@281: // 	bfs_queue.pop(); 
marci@281: // 	if (!bfs_queue.empty()) {
marci@281: // 	  G./*getF*/first(actual_edge, bfs_queue.front());
marci@281: // 	  if (G.valid(actual_edge)/*.valid()*/) {
marci@281: // 	    Node w=G.bNode(actual_edge);
marci@281: // 	    if (!reached.get(w)) {
marci@281: // 	      bfs_queue.push(w);
marci@281: // 	      reached.set(w, true);
marci@281: // 	      b_node_newly_reached=true;
marci@281: // 	    } else {
marci@281: // 	      b_node_newly_reached=false;
marci@281: // 	    }
marci@281: // 	  }
marci@281: // 	}
marci@281: //       }
marci@281: //       return *this;
marci@281: //     }
marci@281: //     bool finished() const { return bfs_queue.empty(); }
marci@281: //     operator OutEdgeIt () const { return actual_edge; }
marci@281: //     bool isBNodeNewlyReached() const { return b_node_newly_reached; }
marci@281: //     bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
marci@281: //     Node aNode() const { return bfs_queue.front(); }
marci@281: //     Node bNode() const { return G.bNode(actual_edge); }
marci@281: //     const ReachedMap& getReachedMap() const { return reached; }
marci@281: //     const std::queue<Node>& getBfsQueue() const { return bfs_queue; }
marci@281: //  };  
marci@281: 
marci@281: 
marci@281:   template <typename GraphWrapper, /*typename OutEdgeIt,*/ 
marci@281: 	    typename ReachedMap/*=typename GraphWrapper::NodeMap<bool>*/ >
marci@281:   class BfsIterator5 {
marci@281:     typedef typename GraphWrapper::Node Node;
marci@281:     typedef typename GraphWrapper::OutEdgeIt OutEdgeIt;
marci@281:     GraphWrapper G;
marci@281:     std::queue<Node> bfs_queue;
marci@281:     ReachedMap& reached;
marci@281:     bool b_node_newly_reached;
marci@281:     OutEdgeIt actual_edge;
marci@281:     bool own_reached_map;
marci@281:   public:
marci@281:     BfsIterator5(const GraphWrapper& _G, ReachedMap& _reached) : 
marci@281:       G(_G), reached(_reached), 
marci@281:       own_reached_map(false) { }
marci@281:     BfsIterator5(const GraphWrapper& _G) : 
marci@281:       G(_G), reached(*(new ReachedMap(G /*, false*/))), 
marci@281:       own_reached_map(true) { }
marci@281: //     BfsIterator5(const typename GraphWrapper::BaseGraph& _G, 
marci@281: // 		 ReachedMap& _reached) : 
marci@281: //       G(_G), reached(_reached), 
marci@281: //       own_reached_map(false) { }
marci@281: //     BfsIterator5(const typename GraphWrapper::BaseGraph& _G) : 
marci@281: //       G(_G), reached(*(new ReachedMap(G /*, false*/))), 
marci@281: //       own_reached_map(true) { }
marci@281:     ~BfsIterator5() { if (own_reached_map) delete &reached; }
marci@281:     void pushAndSetReached(Node s) { 
marci@281:       reached.set(s, true);
marci@281:       if (bfs_queue.empty()) {
marci@281: 	bfs_queue.push(s);
marci@281: 	G./*getF*/first(actual_edge, s);
marci@281: 	if (G.valid(actual_edge)/*.valid()*/) { 
marci@281: 	  Node w=G.bNode(actual_edge);
marci@281: 	  if (!reached.get(w)) {
marci@281: 	    bfs_queue.push(w);
marci@281: 	    reached.set(w, true);
marci@281: 	    b_node_newly_reached=true;
marci@281: 	  } else {
marci@281: 	    b_node_newly_reached=false;
marci@281: 	  }
marci@281: 	} 
marci@281:       } else {
marci@281: 	bfs_queue.push(s);
marci@281:       }
marci@281:     }
marci@281:     BfsIterator5<GraphWrapper, /*OutEdgeIt,*/ ReachedMap>& 
marci@281:     operator++() { 
marci@281:       if (G.valid(actual_edge)/*.valid()*/) { 
marci@281: 	/*++*/G.next(actual_edge);
marci@281: 	if (G.valid(actual_edge)/*.valid()*/) {
marci@281: 	  Node w=G.bNode(actual_edge);
marci@281: 	  if (!reached.get(w)) {
marci@281: 	    bfs_queue.push(w);
marci@281: 	    reached.set(w, true);
marci@281: 	    b_node_newly_reached=true;
marci@281: 	  } else {
marci@281: 	    b_node_newly_reached=false;
marci@281: 	  }
marci@281: 	}
marci@281:       } else {
marci@281: 	bfs_queue.pop(); 
marci@281: 	if (!bfs_queue.empty()) {
marci@281: 	  G./*getF*/first(actual_edge, bfs_queue.front());
marci@281: 	  if (G.valid(actual_edge)/*.valid()*/) {
marci@281: 	    Node w=G.bNode(actual_edge);
marci@281: 	    if (!reached.get(w)) {
marci@281: 	      bfs_queue.push(w);
marci@281: 	      reached.set(w, true);
marci@281: 	      b_node_newly_reached=true;
marci@281: 	    } else {
marci@281: 	      b_node_newly_reached=false;
marci@281: 	    }
marci@281: 	  }
marci@281: 	}
marci@281:       }
marci@281:       return *this;
marci@281:     }
marci@281:     bool finished() const { return bfs_queue.empty(); }
marci@281:     operator OutEdgeIt () const { return actual_edge; }
marci@281:     bool isBNodeNewlyReached() const { return b_node_newly_reached; }
marci@281:     bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
marci@281:     Node aNode() const { return bfs_queue.front(); }
marci@281:     Node bNode() const { return G.bNode(actual_edge); }
marci@281:     const ReachedMap& getReachedMap() const { return reached; }
marci@281:     const std::queue<Node>& getBfsQueue() const { return bfs_queue; }
marci@281:   };  
marci@281: 
marci@281: //   template <typename Graph, typename OutEdgeIt, 
marci@281: // 	    typename ReachedMap/*=typename Graph::NodeMap<bool>*/ >
marci@281: //   class DfsIterator4 {
marci@281: //     typedef typename Graph::Node Node;
marci@281: //     const Graph& G;
marci@281: //     std::stack<OutEdgeIt> dfs_stack;
marci@281: //     bool b_node_newly_reached;
marci@281: //     OutEdgeIt actual_edge;
marci@281: //     Node actual_node;
marci@281: //     ReachedMap& reached;
marci@281: //     bool own_reached_map;
marci@281: //   public:
marci@281: //     DfsIterator4(const Graph& _G, ReachedMap& _reached) : 
marci@281: //       G(_G), reached(_reached), 
marci@281: //       own_reached_map(false) { }
marci@281: //     DfsIterator4(const Graph& _G) : 
marci@281: //       G(_G), reached(*(new ReachedMap(G /*, false*/))), 
marci@281: //       own_reached_map(true) { }
marci@281: //     ~DfsIterator4() { if (own_reached_map) delete &reached; }
marci@281: //     void pushAndSetReached(Node s) { 
marci@281: //       actual_node=s;
marci@281: //       reached.set(s, true);
marci@281: //       dfs_stack.push(G.template first<OutEdgeIt>(s)); 
marci@281: //     }
marci@281: //     DfsIterator4<Graph, OutEdgeIt, ReachedMap>& 
marci@281: //     operator++() { 
marci@281: //       actual_edge=dfs_stack.top();
marci@281: //       //actual_node=G.aNode(actual_edge);
marci@281: //       if (G.valid(actual_edge)/*.valid()*/) { 
marci@281: // 	Node w=G.bNode(actual_edge);
marci@281: // 	actual_node=w;
marci@281: // 	if (!reached.get(w)) {
marci@281: // 	  dfs_stack.push(G.template first<OutEdgeIt>(w));
marci@281: // 	  reached.set(w, true);
marci@281: // 	  b_node_newly_reached=true;
marci@281: // 	} else {
marci@281: // 	  actual_node=G.aNode(actual_edge);
marci@281: // 	  /*++*/G.next(dfs_stack.top());
marci@281: // 	  b_node_newly_reached=false;
marci@281: // 	}
marci@281: //       } else {
marci@281: // 	//actual_node=G.aNode(dfs_stack.top());
marci@281: // 	dfs_stack.pop();
marci@281: //       }
marci@281: //       return *this;
marci@281: //     }
marci@281: //     bool finished() const { return dfs_stack.empty(); }
marci@281: //     operator OutEdgeIt () const { return actual_edge; }
marci@281: //     bool isBNodeNewlyReached() const { return b_node_newly_reached; }
marci@281: //     bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
marci@281: //     Node aNode() const { return actual_node; /*FIXME*/}
marci@281: //     Node bNode() const { return G.bNode(actual_edge); }
marci@281: //     const ReachedMap& getReachedMap() const { return reached; }
marci@281: //     const std::stack<OutEdgeIt>& getDfsStack() const { return dfs_stack; }
marci@281: //   };
marci@281: 
marci@281:   template <typename GraphWrapper, /*typename OutEdgeIt,*/ 
marci@281: 	    typename ReachedMap/*=typename GraphWrapper::NodeMap<bool>*/ >
marci@281:   class DfsIterator5 {
marci@281:     typedef typename GraphWrapper::Node Node;
marci@281:     typedef typename GraphWrapper::OutEdgeIt OutEdgeIt;
marci@281:     GraphWrapper G;
marci@281:     std::stack<OutEdgeIt> dfs_stack;
marci@281:     bool b_node_newly_reached;
marci@281:     OutEdgeIt actual_edge;
marci@281:     Node actual_node;
marci@281:     ReachedMap& reached;
marci@281:     bool own_reached_map;
marci@281:   public:
marci@281:     DfsIterator5(const GraphWrapper& _G, ReachedMap& _reached) : 
marci@281:       G(_G), reached(_reached), 
marci@281:       own_reached_map(false) { }
marci@281:     DfsIterator5(const GraphWrapper& _G) : 
marci@281:       G(_G), reached(*(new ReachedMap(G /*, false*/))), 
marci@281:       own_reached_map(true) { }
marci@281:     ~DfsIterator5() { if (own_reached_map) delete &reached; }
marci@281:     void pushAndSetReached(Node s) { 
marci@281:       actual_node=s;
marci@281:       reached.set(s, true);
marci@281:       OutEdgeIt e;
marci@281:       G.first(e, s);
marci@281:       dfs_stack.push(e); 
marci@281:     }
marci@281:     DfsIterator5<GraphWrapper, /*OutEdgeIt,*/ ReachedMap>& 
marci@281:     operator++() { 
marci@281:       actual_edge=dfs_stack.top();
marci@281:       //actual_node=G.aNode(actual_edge);
marci@281:       if (G.valid(actual_edge)/*.valid()*/) { 
marci@281: 	Node w=G.bNode(actual_edge);
marci@281: 	actual_node=w;
marci@281: 	if (!reached.get(w)) {
marci@281: 	  OutEdgeIt e;
marci@281: 	  G.first(e, w);
marci@281: 	  dfs_stack.push(e);
marci@281: 	  reached.set(w, true);
marci@281: 	  b_node_newly_reached=true;
marci@281: 	} else {
marci@281: 	  actual_node=G.aNode(actual_edge);
marci@281: 	  /*++*/G.next(dfs_stack.top());
marci@281: 	  b_node_newly_reached=false;
marci@281: 	}
marci@281:       } else {
marci@281: 	//actual_node=G.aNode(dfs_stack.top());
marci@281: 	dfs_stack.pop();
marci@281:       }
marci@281:       return *this;
marci@281:     }
marci@281:     bool finished() const { return dfs_stack.empty(); }
marci@281:     operator OutEdgeIt () const { return actual_edge; }
marci@281:     bool isBNodeNewlyReached() const { return b_node_newly_reached; }
marci@281:     bool isANodeExamined() const { return !(G.valid(actual_edge)/*.valid()*/); }
marci@281:     Node aNode() const { return actual_node; /*FIXME*/}
marci@281:     Node bNode() const { return G.bNode(actual_edge); }
marci@281:     const ReachedMap& getReachedMap() const { return reached; }
marci@281:     const std::stack<OutEdgeIt>& getDfsStack() const { return dfs_stack; }
marci@281:   };
marci@281: 
marci@281: 
marci@281: 
marci@281: } // namespace hugo
marci@281: 
marci@281: #endif //HUGO_BFS_ITERATOR_H