1.1 --- a/lemon/smart_graph.h Fri Jun 30 12:14:36 2006 +0000
1.2 +++ b/lemon/smart_graph.h Fri Jun 30 12:15:45 2006 +0000
1.3 @@ -21,15 +21,17 @@
1.4
1.5 ///\ingroup graphs
1.6 ///\file
1.7 -///\brief SmartGraph class.
1.8 +///\brief SmartGraph and SmartUGraph classes.
1.9
1.10 #include <vector>
1.11
1.12 #include <lemon/bits/invalid.h>
1.13
1.14 +#include <lemon/bits/base_extender.h>
1.15 #include <lemon/bits/graph_extender.h>
1.16
1.17 #include <lemon/bits/utility.h>
1.18 +#include <lemon/error.h>
1.19
1.20 #include <lemon/bits/graph_extender.h>
1.21
1.22 @@ -354,6 +356,261 @@
1.23 };
1.24
1.25
1.26 + /**************** Undirected List Graph ****************/
1.27 +
1.28 + typedef UGraphExtender<UndirGraphExtender<SmartGraphBase> >
1.29 + ExtendedSmartUGraphBase;
1.30 +
1.31 + /// \ingroup graphs
1.32 + ///
1.33 + /// \brief A smart undirected graph class.
1.34 + ///
1.35 + /// This is a simple and fast undirected graph implementation.
1.36 + /// It is also quite memory efficient, but at the price
1.37 + /// that <b> it does support only limited (only stack-like)
1.38 + /// node and edge deletions</b>.
1.39 + /// Except from this it conforms to
1.40 + /// the \ref concept::UGraph "UGraph" concept.
1.41 + /// \sa concept::UGraph.
1.42 + ///
1.43 + /// \todo Snapshot hasn't been implemented yet.
1.44 + ///
1.45 + class SmartUGraph : public ExtendedSmartUGraphBase {
1.46 + };
1.47 +
1.48 +
1.49 + class SmartBpUGraphBase {
1.50 + public:
1.51 +
1.52 + class NodeSetError : public LogicError {
1.53 + virtual const char* exceptionName() const {
1.54 + return "lemon::SmartBpUGraph::NodeSetError";
1.55 + }
1.56 + };
1.57 +
1.58 + protected:
1.59 +
1.60 + struct NodeT {
1.61 + int first;
1.62 + NodeT() {}
1.63 + NodeT(int _first) : first(_first) {}
1.64 + };
1.65 +
1.66 + struct UEdgeT {
1.67 + int aNode, next_out;
1.68 + int bNode, next_in;
1.69 + };
1.70 +
1.71 + std::vector<NodeT> aNodes;
1.72 + std::vector<NodeT> bNodes;
1.73 +
1.74 + std::vector<UEdgeT> edges;
1.75 +
1.76 + public:
1.77 +
1.78 + class Node {
1.79 + friend class SmartBpUGraphBase;
1.80 + protected:
1.81 + int id;
1.82 +
1.83 + Node(int _id) : id(_id) {}
1.84 + public:
1.85 + Node() {}
1.86 + Node(Invalid) { id = -1; }
1.87 + bool operator==(const Node i) const {return id==i.id;}
1.88 + bool operator!=(const Node i) const {return id!=i.id;}
1.89 + bool operator<(const Node i) const {return id<i.id;}
1.90 + };
1.91 +
1.92 + class UEdge {
1.93 + friend class SmartBpUGraphBase;
1.94 + protected:
1.95 + int id;
1.96 +
1.97 + UEdge(int _id) { id = _id;}
1.98 + public:
1.99 + UEdge() {}
1.100 + UEdge (Invalid) { id = -1; }
1.101 + bool operator==(const UEdge i) const {return id==i.id;}
1.102 + bool operator!=(const UEdge i) const {return id!=i.id;}
1.103 + bool operator<(const UEdge i) const {return id<i.id;}
1.104 + };
1.105 +
1.106 + void firstANode(Node& node) const {
1.107 + node.id = 2 * aNodes.size() - 2;
1.108 + if (node.id < 0) node.id = -1;
1.109 + }
1.110 + void nextANode(Node& node) const {
1.111 + node.id -= 2;
1.112 + if (node.id < 0) node.id = -1;
1.113 + }
1.114 +
1.115 + void firstBNode(Node& node) const {
1.116 + node.id = 2 * bNodes.size() - 1;
1.117 + }
1.118 + void nextBNode(Node& node) const {
1.119 + node.id -= 2;
1.120 + }
1.121 +
1.122 + void first(Node& node) const {
1.123 + if (aNodes.size() > 0) {
1.124 + node.id = 2 * aNodes.size() - 2;
1.125 + } else {
1.126 + node.id = 2 * bNodes.size() - 1;
1.127 + }
1.128 + }
1.129 + void next(Node& node) const {
1.130 + node.id -= 2;
1.131 + if (node.id == -2) {
1.132 + node.id = 2 * bNodes.size() - 1;
1.133 + }
1.134 + }
1.135 +
1.136 + void first(UEdge& edge) const {
1.137 + edge.id = edges.size() - 1;
1.138 + }
1.139 + void next(UEdge& edge) const {
1.140 + --edge.id;
1.141 + }
1.142 +
1.143 + void firstFromANode(UEdge& edge, const Node& node) const {
1.144 + LEMON_ASSERT((node.id & 1) == 0, NodeSetError());
1.145 + edge.id = aNodes[node.id >> 1].first;
1.146 + }
1.147 + void nextFromANode(UEdge& edge) const {
1.148 + edge.id = edges[edge.id].next_out;
1.149 + }
1.150 +
1.151 + void firstFromBNode(UEdge& edge, const Node& node) const {
1.152 + LEMON_ASSERT((node.id & 1) == 1, NodeSetError());
1.153 + edge.id = bNodes[node.id >> 1].first;
1.154 + }
1.155 + void nextFromBNode(UEdge& edge) const {
1.156 + edge.id = edges[edge.id].next_in;
1.157 + }
1.158 +
1.159 + static int id(const Node& node) {
1.160 + return node.id;
1.161 + }
1.162 + static Node nodeFromId(int id) {
1.163 + return Node(id);
1.164 + }
1.165 + int maxNodeId() const {
1.166 + return aNodes.size() > bNodes.size() ?
1.167 + aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1;
1.168 + }
1.169 +
1.170 + static int id(const UEdge& edge) {
1.171 + return edge.id;
1.172 + }
1.173 + static UEdge uEdgeFromId(int id) {
1.174 + return UEdge(id);
1.175 + }
1.176 + int maxUEdgeId() const {
1.177 + return edges.size();
1.178 + }
1.179 +
1.180 + static int aNodeId(const Node& node) {
1.181 + return node.id >> 1;
1.182 + }
1.183 + static Node fromANodeId(int id) {
1.184 + return Node(id << 1);
1.185 + }
1.186 + int maxANodeId() const {
1.187 + return aNodes.size();
1.188 + }
1.189 +
1.190 + static int bNodeId(const Node& node) {
1.191 + return node.id >> 1;
1.192 + }
1.193 + static Node fromBNodeId(int id) {
1.194 + return Node((id << 1) + 1);
1.195 + }
1.196 + int maxBNodeId() const {
1.197 + return bNodes.size();
1.198 + }
1.199 +
1.200 + Node aNode(const UEdge& edge) const {
1.201 + return Node(edges[edge.id].aNode);
1.202 + }
1.203 + Node bNode(const UEdge& edge) const {
1.204 + return Node(edges[edge.id].bNode);
1.205 + }
1.206 +
1.207 + static bool aNode(const Node& node) {
1.208 + return (node.id & 1) == 0;
1.209 + }
1.210 +
1.211 + static bool bNode(const Node& node) {
1.212 + return (node.id & 1) == 1;
1.213 + }
1.214 +
1.215 + Node addANode() {
1.216 + NodeT nodeT;
1.217 + nodeT.first = -1;
1.218 + aNodes.push_back(nodeT);
1.219 + return Node(aNodes.size() * 2 - 2);
1.220 + }
1.221 +
1.222 + Node addBNode() {
1.223 + NodeT nodeT;
1.224 + nodeT.first = -1;
1.225 + bNodes.push_back(nodeT);
1.226 + return Node(bNodes.size() * 2 - 1);
1.227 + }
1.228 +
1.229 + UEdge addEdge(const Node& source, const Node& target) {
1.230 + LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError());
1.231 + UEdgeT edgeT;
1.232 + if ((source.id & 1) == 0) {
1.233 + edgeT.aNode = source.id;
1.234 + edgeT.bNode = target.id;
1.235 + } else {
1.236 + edgeT.aNode = target.id;
1.237 + edgeT.bNode = source.id;
1.238 + }
1.239 + edgeT.next_out = aNodes[edgeT.aNode >> 1].first;
1.240 + aNodes[edgeT.aNode >> 1].first = edges.size();
1.241 + edgeT.next_in = bNodes[edgeT.bNode >> 1].first;
1.242 + bNodes[edgeT.bNode >> 1].first = edges.size();
1.243 + edges.push_back(edgeT);
1.244 + return UEdge(edges.size() - 1);
1.245 + }
1.246 +
1.247 + void clear() {
1.248 + aNodes.clear();
1.249 + bNodes.clear();
1.250 + edges.clear();
1.251 + }
1.252 +
1.253 + typedef True NodeNumTag;
1.254 + int nodeNum() const { return aNodes.size() + bNodes.size(); }
1.255 + int aNodeNum() const { return aNodes.size(); }
1.256 + int bNodeNum() const { return bNodes.size(); }
1.257 +
1.258 + typedef True EdgeNumTag;
1.259 + int uEdgeNum() const { return edges.size(); }
1.260 +
1.261 + };
1.262 +
1.263 +
1.264 + typedef BpUGraphExtender<SmartBpUGraphBase> ExtendedSmartBpUGraphBase;
1.265 +
1.266 + /// \ingroup graphs
1.267 + ///
1.268 + /// \brief A smart bipartite undirected graph class.
1.269 + ///
1.270 + /// This is a simple and fast bipartite undirected graph implementation.
1.271 + /// It is also quite memory efficient, but at the price
1.272 + /// that <b> it does not support node and edge deletions</b>.
1.273 + /// Except from this it conforms to
1.274 + /// the \ref concept::BpUGraph "BpUGraph" concept.
1.275 + /// \sa concept::BpUGraph.
1.276 + ///
1.277 + class SmartBpUGraph : public ExtendedSmartBpUGraphBase {};
1.278 +
1.279 +
1.280 + /// @}
1.281 } //namespace lemon
1.282
1.283