[606] | 1 | // -*- c++ -*- |
---|
[591] | 2 | |
---|
| 3 | #ifndef HUGO_FULL_GRAPH_H |
---|
| 4 | #define HUGO_FULL_GRAPH_H |
---|
| 5 | |
---|
| 6 | ///\ingroup graphs |
---|
| 7 | ///\file |
---|
| 8 | ///\brief FullGraph and SymFullGraph classes. |
---|
| 9 | |
---|
| 10 | #include <vector> |
---|
[782] | 11 | #include <climits> |
---|
[591] | 12 | |
---|
| 13 | #include <hugo/invalid.h> |
---|
| 14 | |
---|
[782] | 15 | #include <hugo/map_registry.h> |
---|
[798] | 16 | #include <hugo/default_map_factory.h> |
---|
[782] | 17 | |
---|
[591] | 18 | namespace hugo { |
---|
| 19 | |
---|
| 20 | /// \addtogroup graphs |
---|
| 21 | /// @{ |
---|
| 22 | |
---|
| 23 | ///A full graph class. |
---|
| 24 | |
---|
| 25 | ///This is a simple and fast directed full graph implementation. |
---|
[606] | 26 | ///It is completely static, so you can neither add nor delete either |
---|
[591] | 27 | ///edges or nodes. |
---|
| 28 | ///Otherwise it conforms to the graph interface documented under |
---|
| 29 | ///the description of \ref GraphSkeleton. |
---|
| 30 | ///\sa \ref GraphSkeleton. |
---|
[752] | 31 | ///\todo What about loops? |
---|
| 32 | ///\todo Don't we need SymEdgeMap? |
---|
[591] | 33 | /// |
---|
| 34 | ///\author Alpar Juttner |
---|
| 35 | class FullGraph { |
---|
| 36 | int NodeNum; |
---|
| 37 | int EdgeNum; |
---|
| 38 | public: |
---|
[782] | 39 | |
---|
| 40 | typedef FullGraph Graph; |
---|
[591] | 41 | |
---|
| 42 | class Node; |
---|
| 43 | class Edge; |
---|
[782] | 44 | |
---|
[591] | 45 | class NodeIt; |
---|
| 46 | class EdgeIt; |
---|
| 47 | class OutEdgeIt; |
---|
| 48 | class InEdgeIt; |
---|
| 49 | |
---|
[782] | 50 | CREATE_MAP_REGISTRIES; |
---|
[798] | 51 | CREATE_MAPS(DefaultMapFactory); |
---|
[591] | 52 | |
---|
| 53 | public: |
---|
| 54 | |
---|
| 55 | ///Creates a full graph with \c n nodes. |
---|
| 56 | FullGraph(int n) : NodeNum(n), EdgeNum(NodeNum*NodeNum) { } |
---|
| 57 | /// |
---|
| 58 | FullGraph(const FullGraph &_g) |
---|
| 59 | : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { } |
---|
| 60 | |
---|
[813] | 61 | ///Number of nodes. |
---|
| 62 | int nodeNum() const { return NodeNum; } |
---|
| 63 | ///Number of edges. |
---|
| 64 | int edgeNum() const { return EdgeNum; } |
---|
[591] | 65 | |
---|
[813] | 66 | /// Maximum node ID. |
---|
| 67 | |
---|
| 68 | /// Maximum node ID. |
---|
| 69 | ///\sa id(Node) |
---|
| 70 | int maxNodeId() const { return NodeNum-1; } |
---|
| 71 | /// Maximum edge ID. |
---|
| 72 | |
---|
| 73 | /// Maximum edge ID. |
---|
| 74 | ///\sa id(Edge) |
---|
| 75 | int maxEdgeId() const { return EdgeNum-1; } |
---|
[591] | 76 | |
---|
| 77 | Node tail(Edge e) const { return e.n%NodeNum; } |
---|
| 78 | Node head(Edge e) const { return e.n/NodeNum; } |
---|
| 79 | |
---|
| 80 | NodeIt& first(NodeIt& v) const { |
---|
| 81 | v=NodeIt(*this); return v; } |
---|
| 82 | EdgeIt& first(EdgeIt& e) const { |
---|
| 83 | e=EdgeIt(*this); return e; } |
---|
| 84 | OutEdgeIt& first(OutEdgeIt& e, const Node v) const { |
---|
| 85 | e=OutEdgeIt(*this,v); return e; } |
---|
| 86 | InEdgeIt& first(InEdgeIt& e, const Node v) const { |
---|
| 87 | e=InEdgeIt(*this,v); return e; } |
---|
| 88 | |
---|
[813] | 89 | /// Node ID. |
---|
| 90 | |
---|
| 91 | /// The ID of a valid Node is a nonnegative integer not greater than |
---|
| 92 | /// \ref maxNodeId(). The range of the ID's is not surely continuous |
---|
| 93 | /// and the greatest node ID can be actually less then \ref maxNodeId(). |
---|
| 94 | /// |
---|
| 95 | /// The ID of the \ref INVALID node is -1. |
---|
| 96 | ///\return The ID of the node \c v. |
---|
[713] | 97 | static int id(Node v) { return v.n; } |
---|
[813] | 98 | /// Edge ID. |
---|
| 99 | |
---|
| 100 | /// The ID of a valid Edge is a nonnegative integer not greater than |
---|
| 101 | /// \ref maxEdgeId(). The range of the ID's is not surely continuous |
---|
| 102 | /// and the greatest edge ID can be actually less then \ref maxEdgeId(). |
---|
| 103 | /// |
---|
| 104 | /// The ID of the \ref INVALID edge is -1. |
---|
| 105 | ///\return The ID of the edge \c e. |
---|
[713] | 106 | static int id(Edge e) { return e.n; } |
---|
[591] | 107 | |
---|
[774] | 108 | /// Finds an edge between two nodes. |
---|
| 109 | |
---|
| 110 | /// Finds an edge from node \c u to node \c v. |
---|
| 111 | /// |
---|
| 112 | /// If \c prev is \ref INVALID (this is the default value), then |
---|
| 113 | /// It finds the first edge from \c u to \c v. Otherwise it looks for |
---|
| 114 | /// the next edge from \c u to \c v after \c prev. |
---|
| 115 | /// \return The found edge or INVALID if there is no such an edge. |
---|
| 116 | Edge findEdge(Node u,Node v, Edge prev = INVALID) |
---|
| 117 | { |
---|
[782] | 118 | return prev.n == -1 ? Edge(*this, u.n, v.n) : INVALID; |
---|
[774] | 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
[591] | 122 | class Node { |
---|
| 123 | friend class FullGraph; |
---|
| 124 | template <typename T> friend class NodeMap; |
---|
| 125 | |
---|
| 126 | friend class Edge; |
---|
| 127 | friend class OutEdgeIt; |
---|
| 128 | friend class InEdgeIt; |
---|
| 129 | friend class SymEdge; |
---|
| 130 | |
---|
| 131 | protected: |
---|
| 132 | int n; |
---|
[722] | 133 | friend int FullGraph::id(Node v); |
---|
[591] | 134 | Node(int nn) {n=nn;} |
---|
| 135 | public: |
---|
| 136 | Node() {} |
---|
| 137 | Node (Invalid) { n=-1; } |
---|
| 138 | bool operator==(const Node i) const {return n==i.n;} |
---|
| 139 | bool operator!=(const Node i) const {return n!=i.n;} |
---|
| 140 | bool operator<(const Node i) const {return n<i.n;} |
---|
| 141 | }; |
---|
| 142 | |
---|
| 143 | class NodeIt : public Node { |
---|
[774] | 144 | const FullGraph *G; |
---|
[591] | 145 | friend class FullGraph; |
---|
| 146 | public: |
---|
| 147 | NodeIt() : Node() { } |
---|
[774] | 148 | NodeIt(const FullGraph& _G,Node n) : Node(n), G(&_G) { } |
---|
[591] | 149 | NodeIt(Invalid i) : Node(i) { } |
---|
[774] | 150 | NodeIt(const FullGraph& _G) : Node(_G.NodeNum?0:-1), G(&_G) { } |
---|
[591] | 151 | ///\todo Undocumented conversion Node -\> NodeIt. |
---|
[774] | 152 | NodeIt& operator++() { n=(n+2)%(G->NodeNum+1)-1;return *this; } |
---|
[591] | 153 | }; |
---|
| 154 | |
---|
| 155 | class Edge { |
---|
| 156 | friend class FullGraph; |
---|
| 157 | template <typename T> friend class EdgeMap; |
---|
| 158 | |
---|
| 159 | friend class Node; |
---|
| 160 | friend class NodeIt; |
---|
| 161 | protected: |
---|
| 162 | int n; //NodeNum*head+tail; |
---|
[722] | 163 | friend int FullGraph::id(Edge e); |
---|
[591] | 164 | |
---|
[774] | 165 | Edge(int nn) : n(nn) {} |
---|
| 166 | Edge(const FullGraph &G, int tail, int head) : n(G.NodeNum*head+tail) {} |
---|
[591] | 167 | public: |
---|
| 168 | Edge() { } |
---|
| 169 | Edge (Invalid) { n=-1; } |
---|
| 170 | bool operator==(const Edge i) const {return n==i.n;} |
---|
| 171 | bool operator!=(const Edge i) const {return n!=i.n;} |
---|
| 172 | bool operator<(const Edge i) const {return n<i.n;} |
---|
| 173 | ///\bug This is a workaround until somebody tells me how to |
---|
| 174 | ///make class \c SymFullGraph::SymEdgeMap friend of Edge |
---|
| 175 | int &idref() {return n;} |
---|
| 176 | const int &idref() const {return n;} |
---|
| 177 | }; |
---|
| 178 | |
---|
| 179 | class EdgeIt : public Edge { |
---|
| 180 | friend class FullGraph; |
---|
| 181 | public: |
---|
[774] | 182 | EdgeIt(const FullGraph& _G) : Edge(_G.EdgeNum-1) { } |
---|
| 183 | EdgeIt(const FullGraph&, Edge e) : Edge(e) { } |
---|
[591] | 184 | EdgeIt (Invalid i) : Edge(i) { } |
---|
| 185 | EdgeIt() : Edge() { } |
---|
[774] | 186 | EdgeIt& operator++() { --n; return *this; } |
---|
| 187 | |
---|
[591] | 188 | ///\bug This is a workaround until somebody tells me how to |
---|
| 189 | ///make class \c SymFullGraph::SymEdgeMap friend of Edge |
---|
| 190 | int &idref() {return n;} |
---|
| 191 | }; |
---|
| 192 | |
---|
| 193 | class OutEdgeIt : public Edge { |
---|
[774] | 194 | const FullGraph *G; |
---|
[591] | 195 | friend class FullGraph; |
---|
| 196 | public: |
---|
| 197 | OutEdgeIt() : Edge() { } |
---|
[774] | 198 | OutEdgeIt(const FullGraph& _G, Edge e) : Edge(e), G(&_G) { } |
---|
[591] | 199 | OutEdgeIt (Invalid i) : Edge(i) { } |
---|
| 200 | |
---|
[774] | 201 | OutEdgeIt(const FullGraph& _G,const Node v) : Edge(v.n), G(&_G) {} |
---|
| 202 | |
---|
| 203 | OutEdgeIt& operator++() |
---|
| 204 | { n+=G->NodeNum; if(n>=G->EdgeNum) n=-1; return *this; } |
---|
| 205 | |
---|
[591] | 206 | }; |
---|
| 207 | |
---|
| 208 | class InEdgeIt : public Edge { |
---|
[774] | 209 | const FullGraph *G; |
---|
[591] | 210 | friend class FullGraph; |
---|
| 211 | public: |
---|
| 212 | InEdgeIt() : Edge() { } |
---|
[774] | 213 | InEdgeIt(const FullGraph& _G, Edge e) : Edge(e), G(&_G) { } |
---|
[591] | 214 | InEdgeIt (Invalid i) : Edge(i) { } |
---|
[774] | 215 | InEdgeIt(const FullGraph& _G,Node v) : Edge(v.n*_G.NodeNum), G(&_G) {} |
---|
| 216 | InEdgeIt& operator++() |
---|
| 217 | { if(!((++n)%G->NodeNum)) n=-1; return *this; } |
---|
[591] | 218 | }; |
---|
| 219 | |
---|
| 220 | }; |
---|
| 221 | |
---|
| 222 | /// @} |
---|
| 223 | |
---|
| 224 | } //namespace hugo |
---|
| 225 | |
---|
| 226 | |
---|
| 227 | |
---|
| 228 | |
---|
| 229 | #endif //HUGO_FULL_GRAPH_H |
---|