1.1 --- a/lemon/edge_set.h Fri Oct 19 13:50:13 2007 +0000
1.2 +++ b/lemon/edge_set.h Fri Oct 19 15:21:07 2007 +0000
1.3 @@ -241,8 +241,6 @@
1.4 /// this class. Its interface must conform to the \ref concepts::Graph
1.5 /// "Graph" concept.
1.6 ///
1.7 - /// In the edge extension and removing it conforms to the
1.8 - /// \ref concepts::Graph "Graph" concept.
1.9 template <typename _Graph>
1.10 class ListEdgeSet : public EdgeSetExtender<ListEdgeSetBase<_Graph> > {
1.11
1.12 @@ -320,6 +318,315 @@
1.13
1.14 };
1.15
1.16 + template <typename _Graph>
1.17 + class ListUEdgeSetBase {
1.18 + public:
1.19 +
1.20 + typedef _Graph Graph;
1.21 + typedef typename Graph::Node Node;
1.22 + typedef typename Graph::NodeIt NodeIt;
1.23 +
1.24 + protected:
1.25 +
1.26 + struct NodeT {
1.27 + int first_out;
1.28 + NodeT() : first_out(-1) {}
1.29 + };
1.30 +
1.31 + typedef DefaultMap<Graph, Node, NodeT> NodesImplBase;
1.32 +
1.33 + NodesImplBase* nodes;
1.34 +
1.35 + struct EdgeT {
1.36 + Node target;
1.37 + int prev_out, next_out;
1.38 + EdgeT() : prev_out(-1), next_out(-1) {}
1.39 + };
1.40 +
1.41 + std::vector<EdgeT> edges;
1.42 +
1.43 + int first_edge;
1.44 + int first_free_edge;
1.45 +
1.46 + const Graph* graph;
1.47 +
1.48 + void initalize(const Graph& _graph, NodesImplBase& _nodes) {
1.49 + graph = &_graph;
1.50 + nodes = &_nodes;
1.51 + }
1.52 +
1.53 + public:
1.54 +
1.55 + class UEdge {
1.56 + friend class ListUEdgeSetBase;
1.57 + protected:
1.58 +
1.59 + int id;
1.60 + explicit UEdge(int _id) { id = _id;}
1.61 +
1.62 + public:
1.63 + UEdge() {}
1.64 + UEdge (Invalid) { id = -1; }
1.65 + bool operator==(const UEdge& edge) const {return id == edge.id;}
1.66 + bool operator!=(const UEdge& edge) const {return id != edge.id;}
1.67 + bool operator<(const UEdge& edge) const {return id < edge.id;}
1.68 + };
1.69 +
1.70 + class Edge {
1.71 + friend class ListUEdgeSetBase;
1.72 + protected:
1.73 + Edge(int _id) : id(_id) {}
1.74 + int id;
1.75 + public:
1.76 + operator UEdge() const { return uEdgeFromId(id / 2); }
1.77 +
1.78 + Edge() {}
1.79 + Edge(Invalid) : id(-1) {}
1.80 + bool operator==(const Edge& edge) const { return id == edge.id; }
1.81 + bool operator!=(const Edge& edge) const { return id != edge.id; }
1.82 + bool operator<(const Edge& edge) const { return id < edge.id; }
1.83 + };
1.84 +
1.85 + ListUEdgeSetBase() : first_edge(-1), first_free_edge(-1) {}
1.86 +
1.87 + UEdge addEdge(const Node& u, const Node& v) {
1.88 + int n;
1.89 +
1.90 + if (first_free_edge == -1) {
1.91 + n = edges.size();
1.92 + edges.push_back(EdgeT());
1.93 + edges.push_back(EdgeT());
1.94 + } else {
1.95 + n = first_free_edge;
1.96 + first_free_edge = edges[n].next_out;
1.97 + }
1.98 +
1.99 + edges[n].target = u;
1.100 + edges[n | 1].target = v;
1.101 +
1.102 + edges[n].next_out = (*nodes)[v].first_out;
1.103 + if ((*nodes)[v].first_out != -1) {
1.104 + edges[(*nodes)[v].first_out].prev_out = n;
1.105 + }
1.106 + (*nodes)[v].first_out = n;
1.107 + edges[n].prev_out = -1;
1.108 +
1.109 + if ((*nodes)[u].first_out != -1) {
1.110 + edges[(*nodes)[u].first_out].prev_out = (n | 1);
1.111 + }
1.112 + edges[n | 1].next_out = (*nodes)[u].first_out;
1.113 + (*nodes)[u].first_out = (n | 1);
1.114 + edges[n | 1].prev_out = -1;
1.115 +
1.116 + return UEdge(n / 2);
1.117 + }
1.118 +
1.119 + void erase(const UEdge& edge) {
1.120 + int n = edge.id * 2;
1.121 +
1.122 + if (edges[n].next_out != -1) {
1.123 + edges[edges[n].next_out].prev_out = edges[n].prev_out;
1.124 + }
1.125 +
1.126 + if (edges[n].prev_out != -1) {
1.127 + edges[edges[n].prev_out].next_out = edges[n].next_out;
1.128 + } else {
1.129 + (*nodes)[edges[n | 1].target].first_out = edges[n].next_out;
1.130 + }
1.131 +
1.132 + if (edges[n | 1].next_out != -1) {
1.133 + edges[edges[n | 1].next_out].prev_out = edges[n | 1].prev_out;
1.134 + }
1.135 +
1.136 + if (edges[n | 1].prev_out != -1) {
1.137 + edges[edges[n | 1].prev_out].next_out = edges[n | 1].next_out;
1.138 + } else {
1.139 + (*nodes)[edges[n].target].first_out = edges[n | 1].next_out;
1.140 + }
1.141 +
1.142 + edges[n].next_out = first_free_edge;
1.143 + first_free_edge = n;
1.144 +
1.145 + }
1.146 +
1.147 + void clear() {
1.148 + Node node;
1.149 + for (first(node); node != INVALID; next(node)) {
1.150 + (*nodes)[node].first_out = -1;
1.151 + }
1.152 + edges.clear();
1.153 + first_edge = -1;
1.154 + first_free_edge = -1;
1.155 + }
1.156 +
1.157 + void first(Node& node) const {
1.158 + graph->first(node);
1.159 + }
1.160 +
1.161 + void next(Node& node) const {
1.162 + graph->next(node);
1.163 + }
1.164 +
1.165 + void first(Edge& edge) const {
1.166 + Node node;
1.167 + first(node);
1.168 + while (node != INVALID && (*nodes)[node].first_out == -1) {
1.169 + next(node);
1.170 + }
1.171 + edge.id = (node == INVALID) ? -1 : (*nodes)[node].first_out;
1.172 + }
1.173 +
1.174 + void next(Edge& edge) const {
1.175 + if (edges[edge.id].next_out != -1) {
1.176 + edge.id = edges[edge.id].next_out;
1.177 + } else {
1.178 + Node node = edges[edge.id ^ 1].target;
1.179 + next(node);
1.180 + while(node != INVALID && (*nodes)[node].first_out == -1) {
1.181 + next(node);
1.182 + }
1.183 + edge.id = (node == INVALID) ? -1 : (*nodes)[node].first_out;
1.184 + }
1.185 + }
1.186 +
1.187 + void first(UEdge& uedge) const {
1.188 + Node node;
1.189 + first(node);
1.190 + while (node != INVALID) {
1.191 + uedge.id = (*nodes)[node].first_out;
1.192 + while ((uedge.id & 1) != 1) {
1.193 + uedge.id = edges[uedge.id].next_out;
1.194 + }
1.195 + if (uedge.id != -1) {
1.196 + uedge.id /= 2;
1.197 + return;
1.198 + }
1.199 + next(node);
1.200 + }
1.201 + uedge.id = -1;
1.202 + }
1.203 +
1.204 + void next(UEdge& uedge) const {
1.205 + Node node = edges[uedge.id * 2].target;
1.206 + uedge.id = edges[(uedge.id * 2) | 1].next_out;
1.207 + while ((uedge.id & 1) != 1) {
1.208 + uedge.id = edges[uedge.id].next_out;
1.209 + }
1.210 + if (uedge.id != -1) {
1.211 + uedge.id /= 2;
1.212 + return;
1.213 + }
1.214 + next(node);
1.215 + while (node != INVALID) {
1.216 + uedge.id = (*nodes)[node].first_out;
1.217 + while ((uedge.id & 1) != 1) {
1.218 + uedge.id = edges[uedge.id].next_out;
1.219 + }
1.220 + if (uedge.id != -1) {
1.221 + uedge.id /= 2;
1.222 + return;
1.223 + }
1.224 + next(node);
1.225 + }
1.226 + uedge.id = -1;
1.227 + }
1.228 +
1.229 + void firstOut(Edge& edge, const Node& node) const {
1.230 + edge.id = (*nodes)[node].first_out;
1.231 + }
1.232 +
1.233 + void nextOut(Edge& edge) const {
1.234 + edge.id = edges[edge.id].next_out;
1.235 + }
1.236 +
1.237 + void firstIn(Edge& edge, const Node& node) const {
1.238 + edge.id = (((*nodes)[node].first_out) ^ 1);
1.239 + if (edge.id == -2) edge.id = -1;
1.240 + }
1.241 +
1.242 + void nextIn(Edge& edge) const {
1.243 + edge.id = ((edges[edge.id ^ 1].next_out) ^ 1);
1.244 + if (edge.id == -2) edge.id = -1;
1.245 + }
1.246 +
1.247 + void firstInc(UEdge &edge, bool& dir, const Node& node) const {
1.248 + int de = (*nodes)[node].first_out;
1.249 + if (de != -1 ) {
1.250 + edge.id = de / 2;
1.251 + dir = ((de & 1) == 1);
1.252 + } else {
1.253 + edge.id = -1;
1.254 + dir = true;
1.255 + }
1.256 + }
1.257 + void nextInc(UEdge &edge, bool& dir) const {
1.258 + int de = (edges[(edge.id * 2) | (dir ? 1 : 0)].next_out);
1.259 + if (de != -1 ) {
1.260 + edge.id = de / 2;
1.261 + dir = ((de & 1) == 1);
1.262 + } else {
1.263 + edge.id = -1;
1.264 + dir = true;
1.265 + }
1.266 + }
1.267 +
1.268 + static bool direction(Edge edge) {
1.269 + return (edge.id & 1) == 1;
1.270 + }
1.271 +
1.272 + static Edge direct(UEdge uedge, bool dir) {
1.273 + return Edge(uedge.id * 2 + (dir ? 1 : 0));
1.274 + }
1.275 +
1.276 + int id(const Node& node) const { return graph->id(node); }
1.277 + static int id(Edge e) { return e.id; }
1.278 + static int id(UEdge e) { return e.id; }
1.279 +
1.280 + Node nodeFromId(int id) const { return graph->nodeFromId(id); }
1.281 + static Edge edgeFromId(int id) { return Edge(id);}
1.282 + static UEdge uEdgeFromId(int id) { return UEdge(id);}
1.283 +
1.284 + int maxNodeId() const { return graph->maxNodeId(); };
1.285 + int maxUEdgeId() const { return edges.size() / 2 - 1; }
1.286 + int maxEdgeId() const { return edges.size()-1; }
1.287 +
1.288 + Node source(Edge e) const { return edges[e.id ^ 1].target; }
1.289 + Node target(Edge e) const { return edges[e.id].target; }
1.290 +
1.291 + Node source(UEdge e) const { return edges[2 * e.id].target; }
1.292 + Node target(UEdge e) const { return edges[2 * e.id + 1].target; }
1.293 +
1.294 + typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
1.295 +
1.296 + NodeNotifier& notifier(Node) const {
1.297 + return graph->notifier(Node());
1.298 + }
1.299 +
1.300 + template <typename _Value>
1.301 + class NodeMap : public Graph::template NodeMap<_Value> {
1.302 + public:
1.303 +
1.304 + typedef typename _Graph::template NodeMap<_Value> Parent;
1.305 +
1.306 + explicit NodeMap(const ListUEdgeSetBase<Graph>& edgeset)
1.307 + : Parent(*edgeset.graph) {}
1.308 +
1.309 + NodeMap(const ListUEdgeSetBase<Graph>& edgeset, const _Value& value)
1.310 + : Parent(*edgeset.graph, value) {}
1.311 +
1.312 + NodeMap& operator=(const NodeMap& cmap) {
1.313 + return operator=<NodeMap>(cmap);
1.314 + }
1.315 +
1.316 + template <typename CMap>
1.317 + NodeMap& operator=(const CMap& cmap) {
1.318 + Parent::operator=(cmap);
1.319 + return *this;
1.320 + }
1.321 + };
1.322 +
1.323 + };
1.324 +
1.325 /// \ingroup semi_adaptors
1.326 ///
1.327 /// \brief Graph using a node set of another graph and an
1.328 @@ -336,13 +643,11 @@
1.329 /// In the edge extension and removing it conforms to the
1.330 /// \ref concepts::UGraph "UGraph" concept.
1.331 template <typename _Graph>
1.332 - class ListUEdgeSet
1.333 - : public UEdgeSetExtender<UndirGraphExtender<ListEdgeSetBase<_Graph> > > {
1.334 + class ListUEdgeSet : public UEdgeSetExtender<ListUEdgeSetBase<_Graph> > {
1.335
1.336 public:
1.337
1.338 - typedef UEdgeSetExtender<UndirGraphExtender<
1.339 - ListEdgeSetBase<_Graph> > > Parent;
1.340 + typedef UEdgeSetExtender<ListUEdgeSetBase<_Graph> > Parent;
1.341
1.342 typedef typename Parent::Node Node;
1.343 typedef typename Parent::Edge Edge;
1.344 @@ -661,6 +966,220 @@
1.345
1.346 };
1.347
1.348 +
1.349 + template <typename _Graph>
1.350 + class SmartUEdgeSetBase {
1.351 + public:
1.352 +
1.353 + typedef _Graph Graph;
1.354 + typedef typename Graph::Node Node;
1.355 + typedef typename Graph::NodeIt NodeIt;
1.356 +
1.357 + protected:
1.358 +
1.359 + struct NodeT {
1.360 + int first_out;
1.361 + NodeT() : first_out(-1) {}
1.362 + };
1.363 +
1.364 + typedef DefaultMap<Graph, Node, NodeT> NodesImplBase;
1.365 +
1.366 + NodesImplBase* nodes;
1.367 +
1.368 + struct EdgeT {
1.369 + Node target;
1.370 + int next_out;
1.371 + EdgeT() {}
1.372 + };
1.373 +
1.374 + std::vector<EdgeT> edges;
1.375 +
1.376 + const Graph* graph;
1.377 +
1.378 + void initalize(const Graph& _graph, NodesImplBase& _nodes) {
1.379 + graph = &_graph;
1.380 + nodes = &_nodes;
1.381 + }
1.382 +
1.383 + public:
1.384 +
1.385 + class UEdge {
1.386 + friend class SmartUEdgeSetBase;
1.387 + protected:
1.388 +
1.389 + int id;
1.390 + explicit UEdge(int _id) { id = _id;}
1.391 +
1.392 + public:
1.393 + UEdge() {}
1.394 + UEdge (Invalid) { id = -1; }
1.395 + bool operator==(const UEdge& edge) const {return id == edge.id;}
1.396 + bool operator!=(const UEdge& edge) const {return id != edge.id;}
1.397 + bool operator<(const UEdge& edge) const {return id < edge.id;}
1.398 + };
1.399 +
1.400 + class Edge {
1.401 + friend class SmartUEdgeSetBase;
1.402 + protected:
1.403 + Edge(int _id) : id(_id) {}
1.404 + int id;
1.405 + public:
1.406 + operator UEdge() const { return uEdgeFromId(id / 2); }
1.407 +
1.408 + Edge() {}
1.409 + Edge(Invalid) : id(-1) {}
1.410 + bool operator==(const Edge& edge) const { return id == edge.id; }
1.411 + bool operator!=(const Edge& edge) const { return id != edge.id; }
1.412 + bool operator<(const Edge& edge) const { return id < edge.id; }
1.413 + };
1.414 +
1.415 + SmartUEdgeSetBase() {}
1.416 +
1.417 + UEdge addEdge(const Node& u, const Node& v) {
1.418 + int n = edges.size();
1.419 + edges.push_back(EdgeT());
1.420 + edges.push_back(EdgeT());
1.421 +
1.422 + edges[n].target = u;
1.423 + edges[n | 1].target = v;
1.424 +
1.425 + edges[n].next_out = (*nodes)[v].first_out;
1.426 + (*nodes)[v].first_out = n;
1.427 +
1.428 + edges[n | 1].next_out = (*nodes)[u].first_out;
1.429 + (*nodes)[u].first_out = (n | 1);
1.430 +
1.431 + return UEdge(n / 2);
1.432 + }
1.433 +
1.434 + void clear() {
1.435 + Node node;
1.436 + for (first(node); node != INVALID; next(node)) {
1.437 + (*nodes)[node].first_out = -1;
1.438 + }
1.439 + edges.clear();
1.440 + }
1.441 +
1.442 + void first(Node& node) const {
1.443 + graph->first(node);
1.444 + }
1.445 +
1.446 + void next(Node& node) const {
1.447 + graph->next(node);
1.448 + }
1.449 +
1.450 + void first(Edge& edge) const {
1.451 + edge.id = edges.size() - 1;
1.452 + }
1.453 +
1.454 + void next(Edge& edge) const {
1.455 + --edge.id;
1.456 + }
1.457 +
1.458 + void first(UEdge& edge) const {
1.459 + edge.id = edges.size() / 2 - 1;
1.460 + }
1.461 +
1.462 + void next(UEdge& edge) const {
1.463 + --edge.id;
1.464 + }
1.465 +
1.466 + void firstOut(Edge& edge, const Node& node) const {
1.467 + edge.id = (*nodes)[node].first_out;
1.468 + }
1.469 +
1.470 + void nextOut(Edge& edge) const {
1.471 + edge.id = edges[edge.id].next_out;
1.472 + }
1.473 +
1.474 + void firstIn(Edge& edge, const Node& node) const {
1.475 + edge.id = (((*nodes)[node].first_out) ^ 1);
1.476 + if (edge.id == -2) edge.id = -1;
1.477 + }
1.478 +
1.479 + void nextIn(Edge& edge) const {
1.480 + edge.id = ((edges[edge.id ^ 1].next_out) ^ 1);
1.481 + if (edge.id == -2) edge.id = -1;
1.482 + }
1.483 +
1.484 + void firstInc(UEdge &edge, bool& dir, const Node& node) const {
1.485 + int de = (*nodes)[node].first_out;
1.486 + if (de != -1 ) {
1.487 + edge.id = de / 2;
1.488 + dir = ((de & 1) == 1);
1.489 + } else {
1.490 + edge.id = -1;
1.491 + dir = true;
1.492 + }
1.493 + }
1.494 + void nextInc(UEdge &edge, bool& dir) const {
1.495 + int de = (edges[(edge.id * 2) | (dir ? 1 : 0)].next_out);
1.496 + if (de != -1 ) {
1.497 + edge.id = de / 2;
1.498 + dir = ((de & 1) == 1);
1.499 + } else {
1.500 + edge.id = -1;
1.501 + dir = true;
1.502 + }
1.503 + }
1.504 +
1.505 + static bool direction(Edge edge) {
1.506 + return (edge.id & 1) == 1;
1.507 + }
1.508 +
1.509 + static Edge direct(UEdge uedge, bool dir) {
1.510 + return Edge(uedge.id * 2 + (dir ? 1 : 0));
1.511 + }
1.512 +
1.513 + int id(Node node) const { return graph->id(node); }
1.514 + static int id(Edge edge) { return edge.id; }
1.515 + static int id(UEdge edge) { return edge.id; }
1.516 +
1.517 + Node nodeFromId(int id) const { return graph->nodeFromId(id); }
1.518 + static Edge edgeFromId(int id) { return Edge(id); }
1.519 + static UEdge uEdgeFromId(int id) { return UEdge(id);}
1.520 +
1.521 + int maxNodeId() const { return graph->maxNodeId(); };
1.522 + int maxEdgeId() const { return edges.size() - 1; }
1.523 + int maxUEdgeId() const { return edges.size() / 2 - 1; }
1.524 +
1.525 + Node source(Edge e) const { return edges[e.id ^ 1].target; }
1.526 + Node target(Edge e) const { return edges[e.id].target; }
1.527 +
1.528 + Node source(UEdge e) const { return edges[2 * e.id].target; }
1.529 + Node target(UEdge e) const { return edges[2 * e.id + 1].target; }
1.530 +
1.531 + typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier;
1.532 +
1.533 + NodeNotifier& notifier(Node) const {
1.534 + return graph->notifier(Node());
1.535 + }
1.536 +
1.537 + template <typename _Value>
1.538 + class NodeMap : public Graph::template NodeMap<_Value> {
1.539 + public:
1.540 +
1.541 + typedef typename _Graph::template NodeMap<_Value> Parent;
1.542 +
1.543 + explicit NodeMap(const SmartUEdgeSetBase<Graph>& edgeset)
1.544 + : Parent(*edgeset.graph) { }
1.545 +
1.546 + NodeMap(const SmartUEdgeSetBase<Graph>& edgeset, const _Value& value)
1.547 + : Parent(*edgeset.graph, value) { }
1.548 +
1.549 + NodeMap& operator=(const NodeMap& cmap) {
1.550 + return operator=<NodeMap>(cmap);
1.551 + }
1.552 +
1.553 + template <typename CMap>
1.554 + NodeMap& operator=(const CMap& cmap) {
1.555 + Parent::operator=(cmap);
1.556 + return *this;
1.557 + }
1.558 + };
1.559 +
1.560 + };
1.561 +
1.562 /// \ingroup semi_adaptors
1.563 ///
1.564 /// \brief Graph using a node set of another graph and an
1.565 @@ -677,13 +1196,11 @@
1.566 /// In the edge extension and removing it conforms to the
1.567 /// \ref concepts::UGraph "UGraph" concept.
1.568 template <typename _Graph>
1.569 - class SmartUEdgeSet
1.570 - : public UEdgeSetExtender<UndirGraphExtender<SmartEdgeSetBase<_Graph> > > {
1.571 + class SmartUEdgeSet : public UEdgeSetExtender<SmartUEdgeSetBase<_Graph> > {
1.572
1.573 public:
1.574
1.575 - typedef UEdgeSetExtender<UndirGraphExtender<
1.576 - SmartEdgeSetBase<_Graph> > > Parent;
1.577 + typedef UEdgeSetExtender<SmartUEdgeSetBase<_Graph> > Parent;
1.578
1.579 typedef typename Parent::Node Node;
1.580 typedef typename Parent::Edge Edge;