marci@281: // -*- c++ -*- marci@281: #ifndef HUGO_BFS_ITERATOR_H marci@281: #define HUGO_BFS_ITERATOR_H marci@281: marci@281: #include marci@281: #include marci@281: #include marci@281: #include marci@281: marci@281: namespace hugo { marci@281: marci@281: // template 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 reached; marci@281: // typename Graph::NodeMap pred; marci@281: // typename Graph::NodeMap dist; marci@281: // std::queue 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(); 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(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 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 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& 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(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& _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& 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 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& bfs_queue; marci@281: // ReachedMap& reached; marci@281: // bool _newly_reached; marci@281: // bfs_iterator1(Graph& _G, std::queue& _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(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& 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(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 marci@281: // struct BfsIterator { marci@281: // typedef typename Graph::Node Node; marci@281: // Graph& G; marci@281: // std::queue& 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& _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& 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 marci@281: // struct DfsIterator { marci@281: // typedef typename Graph::Node Node; marci@281: // Graph& G; marci@281: // std::stack& 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& _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& 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 marci@281: // struct BfsIterator1 { marci@281: // typedef typename Graph::Node Node; marci@281: // Graph& G; marci@281: // std::queue& 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& _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 marci@281: // struct DfsIterator1 { marci@281: // typedef typename Graph::Node Node; marci@281: // Graph& G; marci@281: // std::stack& 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& _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 marci@281: // class BfsIterator2 { marci@281: // typedef typename Graph::Node Node; marci@281: // const Graph& G; marci@281: // std::queue 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(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(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(s)); marci@281: // } marci@281: // } marci@281: // BfsIterator2& 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(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(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& getBfsQueue() const { return bfs_queue; } marci@281: // }; marci@281: marci@281: marci@281: // template marci@281: // class BfsIterator3 { marci@281: // typedef typename Graph::Node Node; marci@281: // const Graph& G; marci@281: // std::queue< std::pair > 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(s, G.template first(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(w, G.template first(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(s, G.template first(s))); marci@281: // } marci@281: // } marci@281: // BfsIterator3& 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(w, G.template first(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(w, G.template first(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 >& getBfsQueue() const { return bfs_queue; } marci@281: // }; marci@281: marci@281: marci@281: // template */ > marci@281: // class BfsIterator4 { marci@281: // typedef typename Graph::Node Node; marci@281: // const Graph& G; marci@281: // std::queue 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& 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& getBfsQueue() const { return bfs_queue; } marci@281: // }; marci@281: marci@281: marci@298: template */ > marci@281: class BfsIterator5 { marci@298: protected: marci@298: typedef typename Graph::Node Node; marci@298: typedef typename Graph::OutEdgeIt OutEdgeIt; marci@298: const Graph* graph; marci@281: std::queue 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@298: BfsIterator5(const Graph& _graph, ReachedMap& _reached) : marci@298: graph(&_graph), reached(_reached), marci@281: own_reached_map(false) { } marci@298: BfsIterator5(const Graph& _graph) : marci@298: graph(&_graph), reached(*(new ReachedMap(*graph /*, 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@298: graph->first(actual_edge, s); marci@298: if (graph->valid(actual_edge)) { marci@298: Node w=graph->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@298: BfsIterator5& marci@281: operator++() { marci@298: if (graph->valid(actual_edge)) { marci@298: graph->next(actual_edge); marci@298: if (graph->valid(actual_edge)) { marci@298: Node w=graph->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@298: graph->first(actual_edge, bfs_queue.front()); marci@298: if (graph->valid(actual_edge)) { marci@298: Node w=graph->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@298: bool isANodeExamined() const { return !(graph->valid(actual_edge)); } marci@281: Node aNode() const { return bfs_queue.front(); } marci@298: Node bNode() const { return graph->bNode(actual_edge); } marci@281: const ReachedMap& getReachedMap() const { return reached; } marci@281: const std::queue& getBfsQueue() const { return bfs_queue; } marci@281: }; marci@281: marci@281: // template */ > marci@281: // class DfsIterator4 { marci@281: // typedef typename Graph::Node Node; marci@281: // const Graph& G; marci@281: // std::stack 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(s)); marci@281: // } marci@281: // DfsIterator4& 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(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& getDfsStack() const { return dfs_stack; } marci@281: // }; marci@281: marci@298: template */ > marci@281: class DfsIterator5 { marci@298: protected: marci@298: typedef typename Graph::Node Node; marci@298: typedef typename Graph::OutEdgeIt OutEdgeIt; marci@298: const Graph* graph; marci@281: std::stack 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@298: DfsIterator5(const Graph& _graph, ReachedMap& _reached) : marci@298: graph(&_graph), reached(_reached), marci@281: own_reached_map(false) { } marci@298: DfsIterator5(const Graph& _graph) : marci@298: graph(&_graph), reached(*(new ReachedMap(*graph /*, 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@298: graph->first(e, s); marci@281: dfs_stack.push(e); marci@281: } marci@298: DfsIterator5& marci@281: operator++() { marci@281: actual_edge=dfs_stack.top(); marci@281: //actual_node=G.aNode(actual_edge); marci@298: if (graph->valid(actual_edge)/*.valid()*/) { marci@298: Node w=graph->bNode(actual_edge); marci@281: actual_node=w; marci@281: if (!reached.get(w)) { marci@281: OutEdgeIt e; marci@298: graph->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@298: actual_node=graph->aNode(actual_edge); marci@298: graph->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@298: bool isANodeExamined() const { return !(graph->valid(actual_edge)); } 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& 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