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