deba@491: /* -*- mode: C++; indent-tabs-mode: nil; -*- deba@491: * deba@491: * This file is a part of LEMON, a generic C++ optimization library. deba@491: * deba@491: * Copyright (C) 2003-2008 deba@491: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@491: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@491: * deba@491: * Permission to use, modify and distribute this software is granted deba@491: * provided that this copyright notice appears in all copies. For deba@491: * precise terms see the accompanying LICENSE file. deba@491: * deba@491: * This software is provided "AS IS" with no warranty of any kind, deba@491: * express or implied, and with no claim as to its suitability for any deba@491: * purpose. deba@491: * deba@491: */ deba@491: deba@491: #ifndef LEMON_EDGE_SET_H deba@491: #define LEMON_EDGE_SET_H deba@491: deba@491: #include deba@491: #include deba@491: deba@491: /// \ingroup semi_adaptors deba@491: /// \file deba@491: /// \brief ArcSet and EdgeSet classes. deba@491: /// deba@491: /// Graphs which use another graph's node-set as own. deba@491: namespace lemon { deba@491: deba@559: template deba@491: class ListArcSetBase { deba@491: public: deba@491: deba@559: typedef GR Graph; deba@559: typedef typename GR::Node Node; deba@559: typedef typename GR::NodeIt NodeIt; deba@491: deba@491: protected: deba@491: deba@491: struct NodeT { deba@491: int first_out, first_in; deba@491: NodeT() : first_out(-1), first_in(-1) {} deba@491: }; deba@491: deba@559: typedef typename ItemSetTraits:: deba@491: template Map::Type NodesImplBase; deba@491: deba@559: NodesImplBase* _nodes; deba@491: deba@491: struct ArcT { deba@491: Node source, target; deba@491: int next_out, next_in; deba@491: int prev_out, prev_in; deba@491: ArcT() : prev_out(-1), prev_in(-1) {} deba@491: }; deba@491: deba@491: std::vector arcs; deba@491: deba@491: int first_arc; deba@491: int first_free_arc; deba@491: deba@559: const GR* _graph; deba@491: deba@559: void initalize(const GR& graph, NodesImplBase& nodes) { deba@559: _graph = &graph; deba@559: _nodes = &nodes; deba@491: } deba@491: deba@491: public: deba@491: deba@491: class Arc { deba@559: friend class ListArcSetBase; deba@491: protected: deba@491: Arc(int _id) : id(_id) {} deba@491: int id; deba@491: public: deba@491: Arc() {} deba@491: Arc(Invalid) : id(-1) {} deba@491: bool operator==(const Arc& arc) const { return id == arc.id; } deba@491: bool operator!=(const Arc& arc) const { return id != arc.id; } deba@491: bool operator<(const Arc& arc) const { return id < arc.id; } deba@491: }; deba@491: deba@491: ListArcSetBase() : first_arc(-1), first_free_arc(-1) {} deba@491: deba@491: Arc addArc(const Node& u, const Node& v) { deba@491: int n; deba@491: if (first_free_arc == -1) { deba@491: n = arcs.size(); deba@491: arcs.push_back(ArcT()); deba@491: } else { deba@491: n = first_free_arc; deba@491: first_free_arc = arcs[first_free_arc].next_in; deba@491: } deba@559: arcs[n].next_in = (*_nodes)[v].first_in; deba@559: if ((*_nodes)[v].first_in != -1) { deba@559: arcs[(*_nodes)[v].first_in].prev_in = n; deba@491: } deba@559: (*_nodes)[v].first_in = n; deba@559: arcs[n].next_out = (*_nodes)[u].first_out; deba@559: if ((*_nodes)[u].first_out != -1) { deba@559: arcs[(*_nodes)[u].first_out].prev_out = n; deba@491: } deba@559: (*_nodes)[u].first_out = n; deba@491: arcs[n].source = u; deba@491: arcs[n].target = v; deba@491: return Arc(n); deba@491: } deba@491: deba@491: void erase(const Arc& arc) { deba@491: int n = arc.id; deba@491: if (arcs[n].prev_in != -1) { deba@491: arcs[arcs[n].prev_in].next_in = arcs[n].next_in; deba@491: } else { deba@559: (*_nodes)[arcs[n].target].first_in = arcs[n].next_in; deba@491: } deba@491: if (arcs[n].next_in != -1) { deba@491: arcs[arcs[n].next_in].prev_in = arcs[n].prev_in; deba@491: } deba@491: deba@491: if (arcs[n].prev_out != -1) { deba@491: arcs[arcs[n].prev_out].next_out = arcs[n].next_out; deba@491: } else { deba@559: (*_nodes)[arcs[n].source].first_out = arcs[n].next_out; deba@491: } deba@491: if (arcs[n].next_out != -1) { deba@491: arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; deba@491: } deba@491: deba@491: } deba@491: deba@491: void clear() { deba@491: Node node; deba@491: for (first(node); node != INVALID; next(node)) { deba@559: (*_nodes)[node].first_in = -1; deba@559: (*_nodes)[node].first_out = -1; deba@491: } deba@491: arcs.clear(); deba@491: first_arc = -1; deba@491: first_free_arc = -1; deba@491: } deba@491: deba@491: void first(Node& node) const { deba@559: _graph->first(node); deba@491: } deba@491: deba@491: void next(Node& node) const { deba@559: _graph->next(node); deba@491: } deba@491: deba@491: void first(Arc& arc) const { deba@491: Node node; deba@491: first(node); deba@559: while (node != INVALID && (*_nodes)[node].first_in == -1) { deba@491: next(node); deba@491: } deba@559: arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_in; deba@491: } deba@491: deba@491: void next(Arc& arc) const { deba@491: if (arcs[arc.id].next_in != -1) { deba@491: arc.id = arcs[arc.id].next_in; deba@491: } else { deba@491: Node node = arcs[arc.id].target; deba@491: next(node); deba@559: while (node != INVALID && (*_nodes)[node].first_in == -1) { deba@491: next(node); deba@491: } deba@559: arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_in; deba@491: } deba@491: } deba@491: deba@491: void firstOut(Arc& arc, const Node& node) const { deba@559: arc.id = (*_nodes)[node].first_out; deba@491: } deba@491: deba@491: void nextOut(Arc& arc) const { deba@491: arc.id = arcs[arc.id].next_out; deba@491: } deba@491: deba@491: void firstIn(Arc& arc, const Node& node) const { deba@559: arc.id = (*_nodes)[node].first_in; deba@491: } deba@491: deba@491: void nextIn(Arc& arc) const { deba@491: arc.id = arcs[arc.id].next_in; deba@491: } deba@491: deba@559: int id(const Node& node) const { return _graph->id(node); } deba@491: int id(const Arc& arc) const { return arc.id; } deba@491: deba@559: Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); } deba@491: Arc arcFromId(int ix) const { return Arc(ix); } deba@491: deba@559: int maxNodeId() const { return _graph->maxNodeId(); }; deba@491: int maxArcId() const { return arcs.size() - 1; } deba@491: deba@491: Node source(const Arc& arc) const { return arcs[arc.id].source;} deba@491: Node target(const Arc& arc) const { return arcs[arc.id].target;} deba@491: deba@559: typedef typename ItemSetTraits::ItemNotifier NodeNotifier; deba@491: deba@491: NodeNotifier& notifier(Node) const { deba@559: return _graph->notifier(Node()); deba@491: } deba@491: deba@559: template deba@559: class NodeMap : public GR::template NodeMap { deba@491: public: deba@491: deba@559: typedef typename GR::template NodeMap Parent; deba@491: deba@559: explicit NodeMap(const ListArcSetBase& arcset) deba@559: : Parent(*arcset._graph) {} deba@491: deba@559: NodeMap(const ListArcSetBase& arcset, const V& value) deba@559: : Parent(*arcset._graph, value) {} deba@491: deba@491: NodeMap& operator=(const NodeMap& cmap) { deba@491: return operator=(cmap); deba@491: } deba@491: deba@491: template deba@491: NodeMap& operator=(const CMap& cmap) { deba@491: Parent::operator=(cmap); deba@491: return *this; deba@491: } deba@491: }; deba@491: deba@491: }; deba@491: deba@491: /// \ingroup semi_adaptors deba@491: /// deba@491: /// \brief Digraph using a node set of another digraph or graph and deba@491: /// an own arc set. deba@491: /// deba@491: /// This structure can be used to establish another directed graph deba@491: /// over a node set of an existing one. This class uses the same deba@491: /// Node type as the underlying graph, and each valid node of the deba@491: /// original graph is valid in this arc set, therefore the node deba@491: /// objects of the original graph can be used directly with this deba@491: /// class. The node handling functions (id handling, observing, and deba@491: /// iterators) works equivalently as in the original graph. deba@491: /// deba@491: /// This implementation is based on doubly-linked lists, from each deba@491: /// node the outgoing and the incoming arcs make up lists, therefore deba@491: /// one arc can be erased in constant time. It also makes possible, deba@491: /// that node can be removed from the underlying graph, in this case deba@491: /// all arcs incident to the given node is erased from the arc set. deba@491: /// deba@559: /// \param GR The type of the graph which shares its node set with deba@491: /// this class. Its interface must conform to the deba@491: /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph" deba@491: /// concept. deba@491: /// deba@491: /// This class is fully conform to the \ref concepts::Digraph deba@491: /// "Digraph" concept. deba@559: template deba@559: class ListArcSet : public ArcSetExtender > { deba@491: deba@491: public: deba@491: deba@559: typedef ArcSetExtender > Parent; deba@491: deba@491: typedef typename Parent::Node Node; deba@491: typedef typename Parent::Arc Arc; deba@491: deba@559: typedef GR Graph; deba@491: deba@491: deba@491: typedef typename Parent::NodesImplBase NodesImplBase; deba@491: deba@491: void eraseNode(const Node& node) { deba@491: Arc arc; deba@491: Parent::firstOut(arc, node); deba@491: while (arc != INVALID ) { deba@491: erase(arc); deba@491: Parent::firstOut(arc, node); deba@491: } deba@491: deba@491: Parent::firstIn(arc, node); deba@491: while (arc != INVALID ) { deba@491: erase(arc); deba@491: Parent::firstIn(arc, node); deba@491: } deba@491: } deba@491: deba@491: void clearNodes() { deba@491: Parent::clear(); deba@491: } deba@491: deba@491: class NodesImpl : public NodesImplBase { deba@491: public: deba@491: typedef NodesImplBase Parent; deba@491: deba@559: NodesImpl(const GR& graph, ListArcSet& arcset) deba@491: : Parent(graph), _arcset(arcset) {} deba@491: deba@491: virtual ~NodesImpl() {} deba@491: deba@491: protected: deba@491: deba@491: virtual void erase(const Node& node) { deba@491: _arcset.eraseNode(node); deba@491: Parent::erase(node); deba@491: } deba@491: virtual void erase(const std::vector& nodes) { deba@491: for (int i = 0; i < int(nodes.size()); ++i) { deba@491: _arcset.eraseNode(nodes[i]); deba@491: } deba@491: Parent::erase(nodes); deba@491: } deba@491: virtual void clear() { deba@491: _arcset.clearNodes(); deba@491: Parent::clear(); deba@491: } deba@491: deba@491: private: deba@491: ListArcSet& _arcset; deba@491: }; deba@491: deba@559: NodesImpl _nodes; deba@491: deba@491: public: deba@491: deba@491: /// \brief Constructor of the ArcSet. deba@491: /// deba@491: /// Constructor of the ArcSet. deba@559: ListArcSet(const GR& graph) : _nodes(graph, *this) { deba@559: Parent::initalize(graph, _nodes); deba@491: } deba@491: deba@491: /// \brief Add a new arc to the digraph. deba@491: /// deba@491: /// Add a new arc to the digraph with source node \c s deba@491: /// and target node \c t. deba@491: /// \return the new arc. deba@491: Arc addArc(const Node& s, const Node& t) { deba@491: return Parent::addArc(s, t); deba@491: } deba@491: deba@491: /// \brief Erase an arc from the digraph. deba@491: /// deba@491: /// Erase an arc \c a from the digraph. deba@491: void erase(const Arc& a) { deba@491: return Parent::erase(a); deba@491: } deba@491: deba@491: }; deba@491: deba@559: template deba@491: class ListEdgeSetBase { deba@491: public: deba@491: deba@559: typedef GR Graph; deba@559: typedef typename GR::Node Node; deba@559: typedef typename GR::NodeIt NodeIt; deba@491: deba@491: protected: deba@491: deba@491: struct NodeT { deba@491: int first_out; deba@491: NodeT() : first_out(-1) {} deba@491: }; deba@491: deba@559: typedef typename ItemSetTraits:: deba@491: template Map::Type NodesImplBase; deba@491: deba@559: NodesImplBase* _nodes; deba@491: deba@491: struct ArcT { deba@491: Node target; deba@491: int prev_out, next_out; deba@491: ArcT() : prev_out(-1), next_out(-1) {} deba@491: }; deba@491: deba@491: std::vector arcs; deba@491: deba@491: int first_arc; deba@491: int first_free_arc; deba@491: deba@559: const GR* _graph; deba@491: deba@559: void initalize(const GR& graph, NodesImplBase& nodes) { deba@559: _graph = &graph; deba@559: _nodes = &nodes; deba@491: } deba@491: deba@491: public: deba@491: deba@491: class Edge { deba@491: friend class ListEdgeSetBase; deba@491: protected: deba@491: deba@491: int id; deba@491: explicit Edge(int _id) { id = _id;} deba@491: deba@491: public: deba@491: Edge() {} deba@491: Edge (Invalid) { id = -1; } deba@491: bool operator==(const Edge& arc) const {return id == arc.id;} deba@491: bool operator!=(const Edge& arc) const {return id != arc.id;} deba@491: bool operator<(const Edge& arc) const {return id < arc.id;} deba@491: }; deba@491: deba@491: class Arc { deba@491: friend class ListEdgeSetBase; deba@491: protected: deba@491: Arc(int _id) : id(_id) {} deba@491: int id; deba@491: public: deba@491: operator Edge() const { return edgeFromId(id / 2); } deba@491: deba@491: Arc() {} deba@491: Arc(Invalid) : id(-1) {} deba@491: bool operator==(const Arc& arc) const { return id == arc.id; } deba@491: bool operator!=(const Arc& arc) const { return id != arc.id; } deba@491: bool operator<(const Arc& arc) const { return id < arc.id; } deba@491: }; deba@491: deba@491: ListEdgeSetBase() : first_arc(-1), first_free_arc(-1) {} deba@491: deba@491: Edge addEdge(const Node& u, const Node& v) { deba@491: int n; deba@491: deba@491: if (first_free_arc == -1) { deba@491: n = arcs.size(); deba@491: arcs.push_back(ArcT()); deba@491: arcs.push_back(ArcT()); deba@491: } else { deba@491: n = first_free_arc; deba@491: first_free_arc = arcs[n].next_out; deba@491: } deba@491: deba@491: arcs[n].target = u; deba@491: arcs[n | 1].target = v; deba@491: deba@559: arcs[n].next_out = (*_nodes)[v].first_out; deba@559: if ((*_nodes)[v].first_out != -1) { deba@559: arcs[(*_nodes)[v].first_out].prev_out = n; deba@491: } deba@559: (*_nodes)[v].first_out = n; deba@491: arcs[n].prev_out = -1; deba@491: deba@559: if ((*_nodes)[u].first_out != -1) { deba@559: arcs[(*_nodes)[u].first_out].prev_out = (n | 1); deba@491: } deba@559: arcs[n | 1].next_out = (*_nodes)[u].first_out; deba@559: (*_nodes)[u].first_out = (n | 1); deba@491: arcs[n | 1].prev_out = -1; deba@491: deba@491: return Edge(n / 2); deba@491: } deba@491: deba@491: void erase(const Edge& arc) { deba@491: int n = arc.id * 2; deba@491: deba@491: if (arcs[n].next_out != -1) { deba@491: arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; deba@491: } deba@491: deba@491: if (arcs[n].prev_out != -1) { deba@491: arcs[arcs[n].prev_out].next_out = arcs[n].next_out; deba@491: } else { deba@559: (*_nodes)[arcs[n | 1].target].first_out = arcs[n].next_out; deba@491: } deba@491: deba@491: if (arcs[n | 1].next_out != -1) { deba@491: arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out; deba@491: } deba@491: deba@491: if (arcs[n | 1].prev_out != -1) { deba@491: arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out; deba@491: } else { deba@559: (*_nodes)[arcs[n].target].first_out = arcs[n | 1].next_out; deba@491: } deba@491: deba@491: arcs[n].next_out = first_free_arc; deba@491: first_free_arc = n; deba@491: deba@491: } deba@491: deba@491: void clear() { deba@491: Node node; deba@491: for (first(node); node != INVALID; next(node)) { deba@559: (*_nodes)[node].first_out = -1; deba@491: } deba@491: arcs.clear(); deba@491: first_arc = -1; deba@491: first_free_arc = -1; deba@491: } deba@491: deba@491: void first(Node& node) const { deba@559: _graph->first(node); deba@491: } deba@491: deba@491: void next(Node& node) const { deba@559: _graph->next(node); deba@491: } deba@491: deba@491: void first(Arc& arc) const { deba@491: Node node; deba@491: first(node); deba@559: while (node != INVALID && (*_nodes)[node].first_out == -1) { deba@491: next(node); deba@491: } deba@559: arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_out; deba@491: } deba@491: deba@491: void next(Arc& arc) const { deba@491: if (arcs[arc.id].next_out != -1) { deba@491: arc.id = arcs[arc.id].next_out; deba@491: } else { deba@491: Node node = arcs[arc.id ^ 1].target; deba@491: next(node); deba@559: while(node != INVALID && (*_nodes)[node].first_out == -1) { deba@491: next(node); deba@491: } deba@559: arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_out; deba@491: } deba@491: } deba@491: deba@491: void first(Edge& edge) const { deba@491: Node node; deba@491: first(node); deba@491: while (node != INVALID) { deba@559: edge.id = (*_nodes)[node].first_out; deba@491: while ((edge.id & 1) != 1) { deba@491: edge.id = arcs[edge.id].next_out; deba@491: } deba@491: if (edge.id != -1) { deba@491: edge.id /= 2; deba@491: return; deba@491: } deba@491: next(node); deba@491: } deba@491: edge.id = -1; deba@491: } deba@491: deba@491: void next(Edge& edge) const { deba@491: Node node = arcs[edge.id * 2].target; deba@491: edge.id = arcs[(edge.id * 2) | 1].next_out; deba@491: while ((edge.id & 1) != 1) { deba@491: edge.id = arcs[edge.id].next_out; deba@491: } deba@491: if (edge.id != -1) { deba@491: edge.id /= 2; deba@491: return; deba@491: } deba@491: next(node); deba@491: while (node != INVALID) { deba@559: edge.id = (*_nodes)[node].first_out; deba@491: while ((edge.id & 1) != 1) { deba@491: edge.id = arcs[edge.id].next_out; deba@491: } deba@491: if (edge.id != -1) { deba@491: edge.id /= 2; deba@491: return; deba@491: } deba@491: next(node); deba@491: } deba@491: edge.id = -1; deba@491: } deba@491: deba@491: void firstOut(Arc& arc, const Node& node) const { deba@559: arc.id = (*_nodes)[node].first_out; deba@491: } deba@491: deba@491: void nextOut(Arc& arc) const { deba@491: arc.id = arcs[arc.id].next_out; deba@491: } deba@491: deba@491: void firstIn(Arc& arc, const Node& node) const { deba@559: arc.id = (((*_nodes)[node].first_out) ^ 1); deba@491: if (arc.id == -2) arc.id = -1; deba@491: } deba@491: deba@491: void nextIn(Arc& arc) const { deba@491: arc.id = ((arcs[arc.id ^ 1].next_out) ^ 1); deba@491: if (arc.id == -2) arc.id = -1; deba@491: } deba@491: deba@491: void firstInc(Edge &arc, bool& dir, const Node& node) const { deba@559: int de = (*_nodes)[node].first_out; deba@491: if (de != -1 ) { deba@491: arc.id = de / 2; deba@491: dir = ((de & 1) == 1); deba@491: } else { deba@491: arc.id = -1; deba@491: dir = true; deba@491: } deba@491: } deba@491: void nextInc(Edge &arc, bool& dir) const { deba@491: int de = (arcs[(arc.id * 2) | (dir ? 1 : 0)].next_out); deba@491: if (de != -1 ) { deba@491: arc.id = de / 2; deba@491: dir = ((de & 1) == 1); deba@491: } else { deba@491: arc.id = -1; deba@491: dir = true; deba@491: } deba@491: } deba@491: deba@491: static bool direction(Arc arc) { deba@491: return (arc.id & 1) == 1; deba@491: } deba@491: deba@491: static Arc direct(Edge edge, bool dir) { deba@491: return Arc(edge.id * 2 + (dir ? 1 : 0)); deba@491: } deba@491: deba@559: int id(const Node& node) const { return _graph->id(node); } deba@491: static int id(Arc e) { return e.id; } deba@491: static int id(Edge e) { return e.id; } deba@491: deba@559: Node nodeFromId(int id) const { return _graph->nodeFromId(id); } deba@491: static Arc arcFromId(int id) { return Arc(id);} deba@491: static Edge edgeFromId(int id) { return Edge(id);} deba@491: deba@559: int maxNodeId() const { return _graph->maxNodeId(); }; deba@491: int maxEdgeId() const { return arcs.size() / 2 - 1; } deba@491: int maxArcId() const { return arcs.size()-1; } deba@491: deba@491: Node source(Arc e) const { return arcs[e.id ^ 1].target; } deba@491: Node target(Arc e) const { return arcs[e.id].target; } deba@491: deba@491: Node u(Edge e) const { return arcs[2 * e.id].target; } deba@491: Node v(Edge e) const { return arcs[2 * e.id + 1].target; } deba@491: deba@559: typedef typename ItemSetTraits::ItemNotifier NodeNotifier; deba@491: deba@491: NodeNotifier& notifier(Node) const { deba@559: return _graph->notifier(Node()); deba@491: } deba@491: deba@559: template deba@559: class NodeMap : public GR::template NodeMap { deba@491: public: deba@491: deba@559: typedef typename GR::template NodeMap Parent; deba@491: deba@559: explicit NodeMap(const ListEdgeSetBase& arcset) deba@559: : Parent(*arcset._graph) {} deba@491: deba@559: NodeMap(const ListEdgeSetBase& arcset, const V& value) deba@559: : Parent(*arcset._graph, value) {} deba@491: deba@491: NodeMap& operator=(const NodeMap& cmap) { deba@491: return operator=(cmap); deba@491: } deba@491: deba@491: template deba@491: NodeMap& operator=(const CMap& cmap) { deba@491: Parent::operator=(cmap); deba@491: return *this; deba@491: } deba@491: }; deba@491: deba@491: }; deba@491: deba@491: /// \ingroup semi_adaptors deba@491: /// deba@491: /// \brief Graph using a node set of another digraph or graph and an deba@491: /// own edge set. deba@491: /// deba@491: /// This structure can be used to establish another graph over a deba@491: /// node set of an existing one. This class uses the same Node type deba@491: /// as the underlying graph, and each valid node of the original deba@491: /// graph is valid in this arc set, therefore the node objects of deba@491: /// the original graph can be used directly with this class. The deba@491: /// node handling functions (id handling, observing, and iterators) deba@491: /// works equivalently as in the original graph. deba@491: /// deba@491: /// This implementation is based on doubly-linked lists, from each deba@491: /// node the incident edges make up lists, therefore one edge can be deba@491: /// erased in constant time. It also makes possible, that node can deba@491: /// be removed from the underlying graph, in this case all edges deba@491: /// incident to the given node is erased from the arc set. deba@491: /// deba@559: /// \param GR The type of the graph which shares its node set deba@491: /// with this class. Its interface must conform to the deba@491: /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph" deba@491: /// concept. deba@491: /// deba@491: /// This class is fully conform to the \ref concepts::Graph "Graph" deba@491: /// concept. deba@559: template deba@559: class ListEdgeSet : public EdgeSetExtender > { deba@491: deba@491: public: deba@491: deba@559: typedef EdgeSetExtender > Parent; deba@491: deba@491: typedef typename Parent::Node Node; deba@491: typedef typename Parent::Arc Arc; deba@491: typedef typename Parent::Edge Edge; deba@491: deba@559: typedef GR Graph; deba@491: deba@491: deba@491: typedef typename Parent::NodesImplBase NodesImplBase; deba@491: deba@491: void eraseNode(const Node& node) { deba@491: Arc arc; deba@491: Parent::firstOut(arc, node); deba@491: while (arc != INVALID ) { deba@491: erase(arc); deba@491: Parent::firstOut(arc, node); deba@491: } deba@491: deba@491: } deba@491: deba@491: void clearNodes() { deba@491: Parent::clear(); deba@491: } deba@491: deba@491: class NodesImpl : public NodesImplBase { deba@491: public: deba@491: typedef NodesImplBase Parent; deba@491: deba@559: NodesImpl(const GR& graph, ListEdgeSet& arcset) deba@491: : Parent(graph), _arcset(arcset) {} deba@491: deba@491: virtual ~NodesImpl() {} deba@491: deba@491: protected: deba@491: deba@491: virtual void erase(const Node& node) { deba@491: _arcset.eraseNode(node); deba@491: Parent::erase(node); deba@491: } deba@491: virtual void erase(const std::vector& nodes) { deba@491: for (int i = 0; i < int(nodes.size()); ++i) { deba@491: _arcset.eraseNode(nodes[i]); deba@491: } deba@491: Parent::erase(nodes); deba@491: } deba@491: virtual void clear() { deba@491: _arcset.clearNodes(); deba@491: Parent::clear(); deba@491: } deba@491: deba@491: private: deba@491: ListEdgeSet& _arcset; deba@491: }; deba@491: deba@559: NodesImpl _nodes; deba@491: deba@491: public: deba@491: deba@491: /// \brief Constructor of the EdgeSet. deba@491: /// deba@491: /// Constructor of the EdgeSet. deba@559: ListEdgeSet(const GR& graph) : _nodes(graph, *this) { deba@559: Parent::initalize(graph, _nodes); deba@491: } deba@491: deba@491: /// \brief Add a new edge to the graph. deba@491: /// deba@491: /// Add a new edge to the graph with node \c u deba@491: /// and node \c v endpoints. deba@491: /// \return the new edge. deba@491: Edge addEdge(const Node& u, const Node& v) { deba@491: return Parent::addEdge(u, v); deba@491: } deba@491: deba@491: /// \brief Erase an edge from the graph. deba@491: /// deba@491: /// Erase the edge \c e from the graph. deba@491: void erase(const Edge& e) { deba@491: return Parent::erase(e); deba@491: } deba@491: deba@491: }; deba@491: deba@559: template deba@491: class SmartArcSetBase { deba@491: public: deba@491: deba@559: typedef GR Graph; deba@491: typedef typename Graph::Node Node; deba@491: typedef typename Graph::NodeIt NodeIt; deba@491: deba@491: protected: deba@491: deba@491: struct NodeT { deba@491: int first_out, first_in; deba@491: NodeT() : first_out(-1), first_in(-1) {} deba@491: }; deba@491: deba@559: typedef typename ItemSetTraits:: deba@491: template Map::Type NodesImplBase; deba@491: deba@559: NodesImplBase* _nodes; deba@491: deba@491: struct ArcT { deba@491: Node source, target; deba@491: int next_out, next_in; deba@491: ArcT() {} deba@491: }; deba@491: deba@491: std::vector arcs; deba@491: deba@559: const GR* _graph; deba@491: deba@559: void initalize(const GR& graph, NodesImplBase& nodes) { deba@559: _graph = &graph; deba@559: _nodes = &nodes; deba@491: } deba@491: deba@491: public: deba@491: deba@491: class Arc { deba@559: friend class SmartArcSetBase; deba@491: protected: deba@491: Arc(int _id) : id(_id) {} deba@491: int id; deba@491: public: deba@491: Arc() {} deba@491: Arc(Invalid) : id(-1) {} deba@491: bool operator==(const Arc& arc) const { return id == arc.id; } deba@491: bool operator!=(const Arc& arc) const { return id != arc.id; } deba@491: bool operator<(const Arc& arc) const { return id < arc.id; } deba@491: }; deba@491: deba@491: SmartArcSetBase() {} deba@491: deba@491: Arc addArc(const Node& u, const Node& v) { deba@491: int n = arcs.size(); deba@491: arcs.push_back(ArcT()); deba@559: arcs[n].next_in = (*_nodes)[v].first_in; deba@559: (*_nodes)[v].first_in = n; deba@559: arcs[n].next_out = (*_nodes)[u].first_out; deba@559: (*_nodes)[u].first_out = n; deba@491: arcs[n].source = u; deba@491: arcs[n].target = v; deba@491: return Arc(n); deba@491: } deba@491: deba@491: void clear() { deba@491: Node node; deba@491: for (first(node); node != INVALID; next(node)) { deba@559: (*_nodes)[node].first_in = -1; deba@559: (*_nodes)[node].first_out = -1; deba@491: } deba@491: arcs.clear(); deba@491: } deba@491: deba@491: void first(Node& node) const { deba@559: _graph->first(node); deba@491: } deba@491: deba@491: void next(Node& node) const { deba@559: _graph->next(node); deba@491: } deba@491: deba@491: void first(Arc& arc) const { deba@491: arc.id = arcs.size() - 1; deba@491: } deba@491: deba@491: void next(Arc& arc) const { deba@491: --arc.id; deba@491: } deba@491: deba@491: void firstOut(Arc& arc, const Node& node) const { deba@559: arc.id = (*_nodes)[node].first_out; deba@491: } deba@491: deba@491: void nextOut(Arc& arc) const { deba@491: arc.id = arcs[arc.id].next_out; deba@491: } deba@491: deba@491: void firstIn(Arc& arc, const Node& node) const { deba@559: arc.id = (*_nodes)[node].first_in; deba@491: } deba@491: deba@491: void nextIn(Arc& arc) const { deba@491: arc.id = arcs[arc.id].next_in; deba@491: } deba@491: deba@559: int id(const Node& node) const { return _graph->id(node); } deba@491: int id(const Arc& arc) const { return arc.id; } deba@491: deba@559: Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); } deba@491: Arc arcFromId(int ix) const { return Arc(ix); } deba@491: deba@559: int maxNodeId() const { return _graph->maxNodeId(); }; deba@491: int maxArcId() const { return arcs.size() - 1; } deba@491: deba@491: Node source(const Arc& arc) const { return arcs[arc.id].source;} deba@491: Node target(const Arc& arc) const { return arcs[arc.id].target;} deba@491: deba@559: typedef typename ItemSetTraits::ItemNotifier NodeNotifier; deba@491: deba@491: NodeNotifier& notifier(Node) const { deba@559: return _graph->notifier(Node()); deba@491: } deba@491: deba@559: template deba@559: class NodeMap : public GR::template NodeMap { deba@491: public: deba@491: deba@559: typedef typename GR::template NodeMap Parent; deba@491: deba@559: explicit NodeMap(const SmartArcSetBase& arcset) deba@559: : Parent(*arcset._graph) { } deba@491: deba@559: NodeMap(const SmartArcSetBase& arcset, const V& value) deba@559: : Parent(*arcset._graph, value) { } deba@491: deba@491: NodeMap& operator=(const NodeMap& cmap) { deba@491: return operator=(cmap); deba@491: } deba@491: deba@491: template deba@491: NodeMap& operator=(const CMap& cmap) { deba@491: Parent::operator=(cmap); deba@491: return *this; deba@491: } deba@491: }; deba@491: deba@491: }; deba@491: deba@491: deba@491: /// \ingroup semi_adaptors deba@491: /// deba@491: /// \brief Digraph using a node set of another digraph or graph and deba@491: /// an own arc set. deba@491: /// deba@491: /// This structure can be used to establish another directed graph deba@491: /// over a node set of an existing one. This class uses the same deba@491: /// Node type as the underlying graph, and each valid node of the deba@491: /// original graph is valid in this arc set, therefore the node deba@491: /// objects of the original graph can be used directly with this deba@491: /// class. The node handling functions (id handling, observing, and deba@491: /// iterators) works equivalently as in the original graph. deba@491: /// deba@559: /// \param GR The type of the graph which shares its node set with deba@491: /// this class. Its interface must conform to the deba@491: /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph" deba@491: /// concept. deba@491: /// deba@491: /// This implementation is slightly faster than the \c ListArcSet, deba@491: /// because it uses continuous storage for arcs and it uses just deba@491: /// single-linked lists for enumerate outgoing and incoming deba@491: /// arcs. Therefore the arcs cannot be erased from the arc sets. deba@491: /// deba@491: /// \warning If a node is erased from the underlying graph and this deba@491: /// node is the source or target of one arc in the arc set, then deba@491: /// the arc set is invalidated, and it cannot be used anymore. The deba@491: /// validity can be checked with the \c valid() member function. deba@491: /// deba@491: /// This class is fully conform to the \ref concepts::Digraph deba@491: /// "Digraph" concept. deba@559: template deba@559: class SmartArcSet : public ArcSetExtender > { deba@491: deba@491: public: deba@491: deba@559: typedef ArcSetExtender > Parent; deba@491: deba@491: typedef typename Parent::Node Node; deba@491: typedef typename Parent::Arc Arc; deba@491: deba@559: typedef GR Graph; deba@491: deba@491: protected: deba@491: deba@491: typedef typename Parent::NodesImplBase NodesImplBase; deba@491: deba@491: void eraseNode(const Node& node) { deba@491: if (typename Parent::InArcIt(*this, node) == INVALID && deba@491: typename Parent::OutArcIt(*this, node) == INVALID) { deba@491: return; deba@491: } deba@491: throw typename NodesImplBase::Notifier::ImmediateDetach(); deba@491: } deba@491: deba@491: void clearNodes() { deba@491: Parent::clear(); deba@491: } deba@491: deba@491: class NodesImpl : public NodesImplBase { deba@491: public: deba@491: typedef NodesImplBase Parent; deba@491: deba@559: NodesImpl(const GR& graph, SmartArcSet& arcset) deba@491: : Parent(graph), _arcset(arcset) {} deba@491: deba@491: virtual ~NodesImpl() {} deba@491: deba@491: bool attached() const { deba@491: return Parent::attached(); deba@491: } deba@491: deba@491: protected: deba@491: deba@491: virtual void erase(const Node& node) { deba@491: try { deba@491: _arcset.eraseNode(node); deba@491: Parent::erase(node); deba@491: } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) { deba@491: Parent::clear(); deba@491: throw; deba@491: } deba@491: } deba@491: virtual void erase(const std::vector& nodes) { deba@491: try { deba@491: for (int i = 0; i < int(nodes.size()); ++i) { deba@491: _arcset.eraseNode(nodes[i]); deba@491: } deba@491: Parent::erase(nodes); deba@491: } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) { deba@491: Parent::clear(); deba@491: throw; deba@491: } deba@491: } deba@491: virtual void clear() { deba@491: _arcset.clearNodes(); deba@491: Parent::clear(); deba@491: } deba@491: deba@491: private: deba@491: SmartArcSet& _arcset; deba@491: }; deba@491: deba@559: NodesImpl _nodes; deba@491: deba@491: public: deba@491: deba@491: /// \brief Constructor of the ArcSet. deba@491: /// deba@491: /// Constructor of the ArcSet. deba@559: SmartArcSet(const GR& graph) : _nodes(graph, *this) { deba@559: Parent::initalize(graph, _nodes); deba@491: } deba@491: deba@491: /// \brief Add a new arc to the digraph. deba@491: /// deba@491: /// Add a new arc to the digraph with source node \c s deba@491: /// and target node \c t. deba@491: /// \return the new arc. deba@491: Arc addArc(const Node& s, const Node& t) { deba@491: return Parent::addArc(s, t); deba@491: } deba@491: deba@491: /// \brief Validity check deba@491: /// deba@491: /// This functions gives back false if the ArcSet is deba@491: /// invalidated. It occurs when a node in the underlying graph is deba@491: /// erased and it is not isolated in the ArcSet. deba@491: bool valid() const { deba@559: return _nodes.attached(); deba@491: } deba@491: deba@491: }; deba@491: deba@491: deba@559: template deba@491: class SmartEdgeSetBase { deba@491: public: deba@491: deba@559: typedef GR Graph; deba@559: typedef typename GR::Node Node; deba@559: typedef typename GR::NodeIt NodeIt; deba@491: deba@491: protected: deba@491: deba@491: struct NodeT { deba@491: int first_out; deba@491: NodeT() : first_out(-1) {} deba@491: }; deba@491: deba@559: typedef typename ItemSetTraits:: deba@491: template Map::Type NodesImplBase; deba@491: deba@559: NodesImplBase* _nodes; deba@491: deba@491: struct ArcT { deba@491: Node target; deba@491: int next_out; deba@491: ArcT() {} deba@491: }; deba@491: deba@491: std::vector arcs; deba@491: deba@559: const GR* _graph; deba@491: deba@559: void initalize(const GR& graph, NodesImplBase& nodes) { deba@559: _graph = &graph; deba@559: _nodes = &nodes; deba@491: } deba@491: deba@491: public: deba@491: deba@491: class Edge { deba@491: friend class SmartEdgeSetBase; deba@491: protected: deba@491: deba@491: int id; deba@491: explicit Edge(int _id) { id = _id;} deba@491: deba@491: public: deba@491: Edge() {} deba@491: Edge (Invalid) { id = -1; } deba@491: bool operator==(const Edge& arc) const {return id == arc.id;} deba@491: bool operator!=(const Edge& arc) const {return id != arc.id;} deba@491: bool operator<(const Edge& arc) const {return id < arc.id;} deba@491: }; deba@491: deba@491: class Arc { deba@491: friend class SmartEdgeSetBase; deba@491: protected: deba@491: Arc(int _id) : id(_id) {} deba@491: int id; deba@491: public: deba@491: operator Edge() const { return edgeFromId(id / 2); } deba@491: deba@491: Arc() {} deba@491: Arc(Invalid) : id(-1) {} deba@491: bool operator==(const Arc& arc) const { return id == arc.id; } deba@491: bool operator!=(const Arc& arc) const { return id != arc.id; } deba@491: bool operator<(const Arc& arc) const { return id < arc.id; } deba@491: }; deba@491: deba@491: SmartEdgeSetBase() {} deba@491: deba@491: Edge addEdge(const Node& u, const Node& v) { deba@491: int n = arcs.size(); deba@491: arcs.push_back(ArcT()); deba@491: arcs.push_back(ArcT()); deba@491: deba@491: arcs[n].target = u; deba@491: arcs[n | 1].target = v; deba@491: deba@559: arcs[n].next_out = (*_nodes)[v].first_out; deba@559: (*_nodes)[v].first_out = n; deba@491: deba@559: arcs[n | 1].next_out = (*_nodes)[u].first_out; deba@559: (*_nodes)[u].first_out = (n | 1); deba@491: deba@491: return Edge(n / 2); deba@491: } deba@491: deba@491: void clear() { deba@491: Node node; deba@491: for (first(node); node != INVALID; next(node)) { deba@559: (*_nodes)[node].first_out = -1; deba@491: } deba@491: arcs.clear(); deba@491: } deba@491: deba@491: void first(Node& node) const { deba@559: _graph->first(node); deba@491: } deba@491: deba@491: void next(Node& node) const { deba@559: _graph->next(node); deba@491: } deba@491: deba@491: void first(Arc& arc) const { deba@491: arc.id = arcs.size() - 1; deba@491: } deba@491: deba@491: void next(Arc& arc) const { deba@491: --arc.id; deba@491: } deba@491: deba@491: void first(Edge& arc) const { deba@491: arc.id = arcs.size() / 2 - 1; deba@491: } deba@491: deba@491: void next(Edge& arc) const { deba@491: --arc.id; deba@491: } deba@491: deba@491: void firstOut(Arc& arc, const Node& node) const { deba@559: arc.id = (*_nodes)[node].first_out; deba@491: } deba@491: deba@491: void nextOut(Arc& arc) const { deba@491: arc.id = arcs[arc.id].next_out; deba@491: } deba@491: deba@491: void firstIn(Arc& arc, const Node& node) const { deba@559: arc.id = (((*_nodes)[node].first_out) ^ 1); deba@491: if (arc.id == -2) arc.id = -1; deba@491: } deba@491: deba@491: void nextIn(Arc& arc) const { deba@491: arc.id = ((arcs[arc.id ^ 1].next_out) ^ 1); deba@491: if (arc.id == -2) arc.id = -1; deba@491: } deba@491: deba@491: void firstInc(Edge &arc, bool& dir, const Node& node) const { deba@559: int de = (*_nodes)[node].first_out; deba@491: if (de != -1 ) { deba@491: arc.id = de / 2; deba@491: dir = ((de & 1) == 1); deba@491: } else { deba@491: arc.id = -1; deba@491: dir = true; deba@491: } deba@491: } deba@491: void nextInc(Edge &arc, bool& dir) const { deba@491: int de = (arcs[(arc.id * 2) | (dir ? 1 : 0)].next_out); deba@491: if (de != -1 ) { deba@491: arc.id = de / 2; deba@491: dir = ((de & 1) == 1); deba@491: } else { deba@491: arc.id = -1; deba@491: dir = true; deba@491: } deba@491: } deba@491: deba@491: static bool direction(Arc arc) { deba@491: return (arc.id & 1) == 1; deba@491: } deba@491: deba@491: static Arc direct(Edge edge, bool dir) { deba@491: return Arc(edge.id * 2 + (dir ? 1 : 0)); deba@491: } deba@491: deba@559: int id(Node node) const { return _graph->id(node); } deba@491: static int id(Arc arc) { return arc.id; } deba@491: static int id(Edge arc) { return arc.id; } deba@491: deba@559: Node nodeFromId(int id) const { return _graph->nodeFromId(id); } deba@491: static Arc arcFromId(int id) { return Arc(id); } deba@491: static Edge edgeFromId(int id) { return Edge(id);} deba@491: deba@559: int maxNodeId() const { return _graph->maxNodeId(); }; deba@491: int maxArcId() const { return arcs.size() - 1; } deba@491: int maxEdgeId() const { return arcs.size() / 2 - 1; } deba@491: deba@491: Node source(Arc e) const { return arcs[e.id ^ 1].target; } deba@491: Node target(Arc e) const { return arcs[e.id].target; } deba@491: deba@491: Node u(Edge e) const { return arcs[2 * e.id].target; } deba@491: Node v(Edge e) const { return arcs[2 * e.id + 1].target; } deba@491: deba@559: typedef typename ItemSetTraits::ItemNotifier NodeNotifier; deba@491: deba@491: NodeNotifier& notifier(Node) const { deba@559: return _graph->notifier(Node()); deba@491: } deba@491: deba@559: template deba@559: class NodeMap : public GR::template NodeMap { deba@491: public: deba@491: deba@559: typedef typename GR::template NodeMap Parent; deba@491: deba@559: explicit NodeMap(const SmartEdgeSetBase& arcset) deba@559: : Parent(*arcset._graph) { } deba@491: deba@559: NodeMap(const SmartEdgeSetBase& arcset, const V& value) deba@559: : Parent(*arcset._graph, value) { } deba@491: deba@491: NodeMap& operator=(const NodeMap& cmap) { deba@491: return operator=(cmap); deba@491: } deba@491: deba@491: template deba@491: NodeMap& operator=(const CMap& cmap) { deba@491: Parent::operator=(cmap); deba@491: return *this; deba@491: } deba@491: }; deba@491: deba@491: }; deba@491: deba@491: /// \ingroup semi_adaptors deba@491: /// deba@491: /// \brief Graph using a node set of another digraph or graph and an deba@491: /// own edge set. deba@491: /// deba@491: /// This structure can be used to establish another graph over a deba@491: /// node set of an existing one. This class uses the same Node type deba@491: /// as the underlying graph, and each valid node of the original deba@491: /// graph is valid in this arc set, therefore the node objects of deba@491: /// the original graph can be used directly with this class. The deba@491: /// node handling functions (id handling, observing, and iterators) deba@491: /// works equivalently as in the original graph. deba@491: /// deba@559: /// \param GR The type of the graph which shares its node set deba@491: /// with this class. Its interface must conform to the deba@491: /// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph" deba@491: /// concept. deba@491: /// deba@491: /// This implementation is slightly faster than the \c ListEdgeSet, deba@491: /// because it uses continuous storage for edges and it uses just deba@491: /// single-linked lists for enumerate incident edges. Therefore the deba@491: /// edges cannot be erased from the edge sets. deba@491: /// deba@491: /// \warning If a node is erased from the underlying graph and this deba@491: /// node is incident to one edge in the edge set, then the edge set deba@491: /// is invalidated, and it cannot be used anymore. The validity can deba@491: /// be checked with the \c valid() member function. deba@491: /// deba@491: /// This class is fully conform to the \ref concepts::Graph deba@491: /// "Graph" concept. deba@559: template deba@559: class SmartEdgeSet : public EdgeSetExtender > { deba@491: deba@491: public: deba@491: deba@559: typedef EdgeSetExtender > Parent; deba@491: deba@491: typedef typename Parent::Node Node; deba@491: typedef typename Parent::Arc Arc; deba@491: typedef typename Parent::Edge Edge; deba@491: deba@559: typedef GR Graph; deba@491: deba@491: protected: deba@491: deba@491: typedef typename Parent::NodesImplBase NodesImplBase; deba@491: deba@491: void eraseNode(const Node& node) { deba@491: if (typename Parent::IncEdgeIt(*this, node) == INVALID) { deba@491: return; deba@491: } deba@491: throw typename NodesImplBase::Notifier::ImmediateDetach(); deba@491: } deba@491: deba@491: void clearNodes() { deba@491: Parent::clear(); deba@491: } deba@491: deba@491: class NodesImpl : public NodesImplBase { deba@491: public: deba@491: typedef NodesImplBase Parent; deba@491: deba@559: NodesImpl(const GR& graph, SmartEdgeSet& arcset) deba@491: : Parent(graph), _arcset(arcset) {} deba@491: deba@491: virtual ~NodesImpl() {} deba@491: deba@491: bool attached() const { deba@491: return Parent::attached(); deba@491: } deba@491: deba@491: protected: deba@491: deba@491: virtual void erase(const Node& node) { deba@491: try { deba@491: _arcset.eraseNode(node); deba@491: Parent::erase(node); deba@491: } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) { deba@491: Parent::clear(); deba@491: throw; deba@491: } deba@491: } deba@491: virtual void erase(const std::vector& nodes) { deba@491: try { deba@491: for (int i = 0; i < int(nodes.size()); ++i) { deba@491: _arcset.eraseNode(nodes[i]); deba@491: } deba@491: Parent::erase(nodes); deba@491: } catch (const typename NodesImplBase::Notifier::ImmediateDetach&) { deba@491: Parent::clear(); deba@491: throw; deba@491: } deba@491: } deba@491: virtual void clear() { deba@491: _arcset.clearNodes(); deba@491: Parent::clear(); deba@491: } deba@491: deba@491: private: deba@491: SmartEdgeSet& _arcset; deba@491: }; deba@491: deba@559: NodesImpl _nodes; deba@491: deba@491: public: deba@491: deba@491: /// \brief Constructor of the EdgeSet. deba@491: /// deba@491: /// Constructor of the EdgeSet. deba@559: SmartEdgeSet(const GR& graph) : _nodes(graph, *this) { deba@559: Parent::initalize(graph, _nodes); deba@491: } deba@491: deba@491: /// \brief Add a new edge to the graph. deba@491: /// deba@491: /// Add a new edge to the graph with node \c u deba@491: /// and node \c v endpoints. deba@491: /// \return the new edge. deba@491: Edge addEdge(const Node& u, const Node& v) { deba@491: return Parent::addEdge(u, v); deba@491: } deba@491: deba@491: /// \brief Validity check deba@491: /// deba@491: /// This functions gives back false if the EdgeSet is deba@491: /// invalidated. It occurs when a node in the underlying graph is deba@491: /// erased and it is not isolated in the EdgeSet. deba@491: bool valid() const { deba@559: return _nodes.attached(); deba@491: } deba@491: deba@491: }; deba@491: deba@491: } deba@491: deba@491: #endif