1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/bits/base_extender.h Sun Jan 20 20:43:48 2008 +0100
1.3 @@ -0,0 +1,495 @@
1.4 +/* -*- C++ -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library
1.7 + *
1.8 + * Copyright (C) 2003-2007
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +#ifndef LEMON_BITS_BASE_EXTENDER_H
1.23 +#define LEMON_BITS_BASE_EXTENDER_H
1.24 +
1.25 +#include <lemon/bits/invalid.h>
1.26 +#include <lemon/error.h>
1.27 +
1.28 +#include <lemon/bits/map_extender.h>
1.29 +#include <lemon/bits/default_map.h>
1.30 +
1.31 +#include <lemon/concept_check.h>
1.32 +#include <lemon/concepts/maps.h>
1.33 +
1.34 +///\ingroup digraphbits
1.35 +///\file
1.36 +///\brief Extenders for the digraph types
1.37 +namespace lemon {
1.38 +
1.39 + /// \ingroup digraphbits
1.40 + ///
1.41 + /// \brief BaseDigraph to BaseGraph extender
1.42 + template <typename Base>
1.43 + class UndirDigraphExtender : public Base {
1.44 +
1.45 + public:
1.46 +
1.47 + typedef Base Parent;
1.48 + typedef typename Parent::Arc Edge;
1.49 + typedef typename Parent::Node Node;
1.50 +
1.51 + typedef True UndirectedTag;
1.52 +
1.53 + class Arc : public Edge {
1.54 + friend class UndirDigraphExtender;
1.55 +
1.56 + protected:
1.57 + bool forward;
1.58 +
1.59 + Arc(const Edge &ue, bool _forward) :
1.60 + Edge(ue), forward(_forward) {}
1.61 +
1.62 + public:
1.63 + Arc() {}
1.64 +
1.65 + /// Invalid arc constructor
1.66 + Arc(Invalid i) : Edge(i), forward(true) {}
1.67 +
1.68 + bool operator==(const Arc &that) const {
1.69 + return forward==that.forward && Edge(*this)==Edge(that);
1.70 + }
1.71 + bool operator!=(const Arc &that) const {
1.72 + return forward!=that.forward || Edge(*this)!=Edge(that);
1.73 + }
1.74 + bool operator<(const Arc &that) const {
1.75 + return forward<that.forward ||
1.76 + (!(that.forward<forward) && Edge(*this)<Edge(that));
1.77 + }
1.78 + };
1.79 +
1.80 +
1.81 +
1.82 + using Parent::source;
1.83 +
1.84 + /// Source of the given Arc.
1.85 + Node source(const Arc &e) const {
1.86 + return e.forward ? Parent::source(e) : Parent::target(e);
1.87 + }
1.88 +
1.89 + using Parent::target;
1.90 +
1.91 + /// Target of the given Arc.
1.92 + Node target(const Arc &e) const {
1.93 + return e.forward ? Parent::target(e) : Parent::source(e);
1.94 + }
1.95 +
1.96 + /// \brief Directed arc from an edge.
1.97 + ///
1.98 + /// Returns a directed arc corresponding to the specified Edge.
1.99 + /// If the given bool is true the given edge and the
1.100 + /// returned arc have the same source node.
1.101 + static Arc direct(const Edge &ue, bool d) {
1.102 + return Arc(ue, d);
1.103 + }
1.104 +
1.105 + /// Returns whether the given directed arc is same orientation as the
1.106 + /// corresponding edge.
1.107 + ///
1.108 + /// \todo reference to the corresponding point of the undirected digraph
1.109 + /// concept. "What does the direction of an edge mean?"
1.110 + static bool direction(const Arc &e) { return e.forward; }
1.111 +
1.112 +
1.113 + using Parent::first;
1.114 + using Parent::next;
1.115 +
1.116 + void first(Arc &e) const {
1.117 + Parent::first(e);
1.118 + e.forward=true;
1.119 + }
1.120 +
1.121 + void next(Arc &e) const {
1.122 + if( e.forward ) {
1.123 + e.forward = false;
1.124 + }
1.125 + else {
1.126 + Parent::next(e);
1.127 + e.forward = true;
1.128 + }
1.129 + }
1.130 +
1.131 + void firstOut(Arc &e, const Node &n) const {
1.132 + Parent::firstIn(e,n);
1.133 + if( Edge(e) != INVALID ) {
1.134 + e.forward = false;
1.135 + }
1.136 + else {
1.137 + Parent::firstOut(e,n);
1.138 + e.forward = true;
1.139 + }
1.140 + }
1.141 + void nextOut(Arc &e) const {
1.142 + if( ! e.forward ) {
1.143 + Node n = Parent::target(e);
1.144 + Parent::nextIn(e);
1.145 + if( Edge(e) == INVALID ) {
1.146 + Parent::firstOut(e, n);
1.147 + e.forward = true;
1.148 + }
1.149 + }
1.150 + else {
1.151 + Parent::nextOut(e);
1.152 + }
1.153 + }
1.154 +
1.155 + void firstIn(Arc &e, const Node &n) const {
1.156 + Parent::firstOut(e,n);
1.157 + if( Edge(e) != INVALID ) {
1.158 + e.forward = false;
1.159 + }
1.160 + else {
1.161 + Parent::firstIn(e,n);
1.162 + e.forward = true;
1.163 + }
1.164 + }
1.165 + void nextIn(Arc &e) const {
1.166 + if( ! e.forward ) {
1.167 + Node n = Parent::source(e);
1.168 + Parent::nextOut(e);
1.169 + if( Edge(e) == INVALID ) {
1.170 + Parent::firstIn(e, n);
1.171 + e.forward = true;
1.172 + }
1.173 + }
1.174 + else {
1.175 + Parent::nextIn(e);
1.176 + }
1.177 + }
1.178 +
1.179 + void firstInc(Edge &e, bool &d, const Node &n) const {
1.180 + d = true;
1.181 + Parent::firstOut(e, n);
1.182 + if (e != INVALID) return;
1.183 + d = false;
1.184 + Parent::firstIn(e, n);
1.185 + }
1.186 +
1.187 + void nextInc(Edge &e, bool &d) const {
1.188 + if (d) {
1.189 + Node s = Parent::source(e);
1.190 + Parent::nextOut(e);
1.191 + if (e != INVALID) return;
1.192 + d = false;
1.193 + Parent::firstIn(e, s);
1.194 + } else {
1.195 + Parent::nextIn(e);
1.196 + }
1.197 + }
1.198 +
1.199 + Node nodeFromId(int ix) const {
1.200 + return Parent::nodeFromId(ix);
1.201 + }
1.202 +
1.203 + Arc arcFromId(int ix) const {
1.204 + return direct(Parent::arcFromId(ix >> 1), bool(ix & 1));
1.205 + }
1.206 +
1.207 + Edge edgeFromId(int ix) const {
1.208 + return Parent::arcFromId(ix);
1.209 + }
1.210 +
1.211 + int id(const Node &n) const {
1.212 + return Parent::id(n);
1.213 + }
1.214 +
1.215 + int id(const Edge &e) const {
1.216 + return Parent::id(e);
1.217 + }
1.218 +
1.219 + int id(const Arc &e) const {
1.220 + return 2 * Parent::id(e) + int(e.forward);
1.221 + }
1.222 +
1.223 + int maxNodeId() const {
1.224 + return Parent::maxNodeId();
1.225 + }
1.226 +
1.227 + int maxArcId() const {
1.228 + return 2 * Parent::maxArcId() + 1;
1.229 + }
1.230 +
1.231 + int maxEdgeId() const {
1.232 + return Parent::maxArcId();
1.233 + }
1.234 +
1.235 +
1.236 + int arcNum() const {
1.237 + return 2 * Parent::arcNum();
1.238 + }
1.239 +
1.240 + int edgeNum() const {
1.241 + return Parent::arcNum();
1.242 + }
1.243 +
1.244 + Arc findArc(Node s, Node t, Arc p = INVALID) const {
1.245 + if (p == INVALID) {
1.246 + Edge arc = Parent::findArc(s, t);
1.247 + if (arc != INVALID) return direct(arc, true);
1.248 + arc = Parent::findArc(t, s);
1.249 + if (arc != INVALID) return direct(arc, false);
1.250 + } else if (direction(p)) {
1.251 + Edge arc = Parent::findArc(s, t, p);
1.252 + if (arc != INVALID) return direct(arc, true);
1.253 + arc = Parent::findArc(t, s);
1.254 + if (arc != INVALID) return direct(arc, false);
1.255 + } else {
1.256 + Edge arc = Parent::findArc(t, s, p);
1.257 + if (arc != INVALID) return direct(arc, false);
1.258 + }
1.259 + return INVALID;
1.260 + }
1.261 +
1.262 + Edge findEdge(Node s, Node t, Edge p = INVALID) const {
1.263 + if (s != t) {
1.264 + if (p == INVALID) {
1.265 + Edge arc = Parent::findArc(s, t);
1.266 + if (arc != INVALID) return arc;
1.267 + arc = Parent::findArc(t, s);
1.268 + if (arc != INVALID) return arc;
1.269 + } else if (Parent::s(p) == s) {
1.270 + Edge arc = Parent::findArc(s, t, p);
1.271 + if (arc != INVALID) return arc;
1.272 + arc = Parent::findArc(t, s);
1.273 + if (arc != INVALID) return arc;
1.274 + } else {
1.275 + Edge arc = Parent::findArc(t, s, p);
1.276 + if (arc != INVALID) return arc;
1.277 + }
1.278 + } else {
1.279 + return Parent::findArc(s, t, p);
1.280 + }
1.281 + return INVALID;
1.282 + }
1.283 + };
1.284 +
1.285 + template <typename Base>
1.286 + class BidirBpGraphExtender : public Base {
1.287 + public:
1.288 + typedef Base Parent;
1.289 + typedef BidirBpGraphExtender Digraph;
1.290 +
1.291 + typedef typename Parent::Node Node;
1.292 + typedef typename Parent::Edge Edge;
1.293 +
1.294 +
1.295 + using Parent::first;
1.296 + using Parent::next;
1.297 +
1.298 + using Parent::id;
1.299 +
1.300 + class Red : public Node {
1.301 + friend class BidirBpGraphExtender;
1.302 + public:
1.303 + Red() {}
1.304 + Red(const Node& node) : Node(node) {
1.305 + LEMON_ASSERT(Parent::red(node) || node == INVALID,
1.306 + typename Parent::NodeSetError());
1.307 + }
1.308 + Red& operator=(const Node& node) {
1.309 + LEMON_ASSERT(Parent::red(node) || node == INVALID,
1.310 + typename Parent::NodeSetError());
1.311 + Node::operator=(node);
1.312 + return *this;
1.313 + }
1.314 + Red(Invalid) : Node(INVALID) {}
1.315 + Red& operator=(Invalid) {
1.316 + Node::operator=(INVALID);
1.317 + return *this;
1.318 + }
1.319 + };
1.320 +
1.321 + void first(Red& node) const {
1.322 + Parent::firstRed(static_cast<Node&>(node));
1.323 + }
1.324 + void next(Red& node) const {
1.325 + Parent::nextRed(static_cast<Node&>(node));
1.326 + }
1.327 +
1.328 + int id(const Red& node) const {
1.329 + return Parent::redId(node);
1.330 + }
1.331 +
1.332 + class Blue : public Node {
1.333 + friend class BidirBpGraphExtender;
1.334 + public:
1.335 + Blue() {}
1.336 + Blue(const Node& node) : Node(node) {
1.337 + LEMON_ASSERT(Parent::blue(node) || node == INVALID,
1.338 + typename Parent::NodeSetError());
1.339 + }
1.340 + Blue& operator=(const Node& node) {
1.341 + LEMON_ASSERT(Parent::blue(node) || node == INVALID,
1.342 + typename Parent::NodeSetError());
1.343 + Node::operator=(node);
1.344 + return *this;
1.345 + }
1.346 + Blue(Invalid) : Node(INVALID) {}
1.347 + Blue& operator=(Invalid) {
1.348 + Node::operator=(INVALID);
1.349 + return *this;
1.350 + }
1.351 + };
1.352 +
1.353 + void first(Blue& node) const {
1.354 + Parent::firstBlue(static_cast<Node&>(node));
1.355 + }
1.356 + void next(Blue& node) const {
1.357 + Parent::nextBlue(static_cast<Node&>(node));
1.358 + }
1.359 +
1.360 + int id(const Blue& node) const {
1.361 + return Parent::redId(node);
1.362 + }
1.363 +
1.364 + Node source(const Edge& arc) const {
1.365 + return red(arc);
1.366 + }
1.367 + Node target(const Edge& arc) const {
1.368 + return blue(arc);
1.369 + }
1.370 +
1.371 + void firstInc(Edge& arc, bool& dir, const Node& node) const {
1.372 + if (Parent::red(node)) {
1.373 + Parent::firstFromRed(arc, node);
1.374 + dir = true;
1.375 + } else {
1.376 + Parent::firstFromBlue(arc, node);
1.377 + dir = static_cast<Edge&>(arc) == INVALID;
1.378 + }
1.379 + }
1.380 + void nextInc(Edge& arc, bool& dir) const {
1.381 + if (dir) {
1.382 + Parent::nextFromRed(arc);
1.383 + } else {
1.384 + Parent::nextFromBlue(arc);
1.385 + if (arc == INVALID) dir = true;
1.386 + }
1.387 + }
1.388 +
1.389 + class Arc : public Edge {
1.390 + friend class BidirBpGraphExtender;
1.391 + protected:
1.392 + bool forward;
1.393 +
1.394 + Arc(const Edge& arc, bool _forward)
1.395 + : Edge(arc), forward(_forward) {}
1.396 +
1.397 + public:
1.398 + Arc() {}
1.399 + Arc (Invalid) : Edge(INVALID), forward(true) {}
1.400 + bool operator==(const Arc& i) const {
1.401 + return Edge::operator==(i) && forward == i.forward;
1.402 + }
1.403 + bool operator!=(const Arc& i) const {
1.404 + return Edge::operator!=(i) || forward != i.forward;
1.405 + }
1.406 + bool operator<(const Arc& i) const {
1.407 + return Edge::operator<(i) ||
1.408 + (!(i.forward<forward) && Edge(*this)<Edge(i));
1.409 + }
1.410 + };
1.411 +
1.412 + void first(Arc& arc) const {
1.413 + Parent::first(static_cast<Edge&>(arc));
1.414 + arc.forward = true;
1.415 + }
1.416 +
1.417 + void next(Arc& arc) const {
1.418 + if (!arc.forward) {
1.419 + Parent::next(static_cast<Edge&>(arc));
1.420 + }
1.421 + arc.forward = !arc.forward;
1.422 + }
1.423 +
1.424 + void firstOut(Arc& arc, const Node& node) const {
1.425 + if (Parent::red(node)) {
1.426 + Parent::firstFromRed(arc, node);
1.427 + arc.forward = true;
1.428 + } else {
1.429 + Parent::firstFromBlue(arc, node);
1.430 + arc.forward = static_cast<Edge&>(arc) == INVALID;
1.431 + }
1.432 + }
1.433 + void nextOut(Arc& arc) const {
1.434 + if (arc.forward) {
1.435 + Parent::nextFromRed(arc);
1.436 + } else {
1.437 + Parent::nextFromBlue(arc);
1.438 + arc.forward = static_cast<Edge&>(arc) == INVALID;
1.439 + }
1.440 + }
1.441 +
1.442 + void firstIn(Arc& arc, const Node& node) const {
1.443 + if (Parent::blue(node)) {
1.444 + Parent::firstFromBlue(arc, node);
1.445 + arc.forward = true;
1.446 + } else {
1.447 + Parent::firstFromRed(arc, node);
1.448 + arc.forward = static_cast<Edge&>(arc) == INVALID;
1.449 + }
1.450 + }
1.451 + void nextIn(Arc& arc) const {
1.452 + if (arc.forward) {
1.453 + Parent::nextFromBlue(arc);
1.454 + } else {
1.455 + Parent::nextFromRed(arc);
1.456 + arc.forward = static_cast<Edge&>(arc) == INVALID;
1.457 + }
1.458 + }
1.459 +
1.460 + Node source(const Arc& arc) const {
1.461 + return arc.forward ? Parent::red(arc) : Parent::blue(arc);
1.462 + }
1.463 + Node target(const Arc& arc) const {
1.464 + return arc.forward ? Parent::blue(arc) : Parent::red(arc);
1.465 + }
1.466 +
1.467 + int id(const Arc& arc) const {
1.468 + return (Parent::id(static_cast<const Edge&>(arc)) << 1) +
1.469 + (arc.forward ? 0 : 1);
1.470 + }
1.471 + Arc arcFromId(int ix) const {
1.472 + return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0);
1.473 + }
1.474 + int maxArcId() const {
1.475 + return (Parent::maxEdgeId() << 1) + 1;
1.476 + }
1.477 +
1.478 + bool direction(const Arc& arc) const {
1.479 + return arc.forward;
1.480 + }
1.481 +
1.482 + Arc direct(const Edge& arc, bool dir) const {
1.483 + return Arc(arc, dir);
1.484 + }
1.485 +
1.486 + int arcNum() const {
1.487 + return 2 * Parent::edgeNum();
1.488 + }
1.489 +
1.490 + int edgeNum() const {
1.491 + return Parent::edgeNum();
1.492 + }
1.493 +
1.494 +
1.495 + };
1.496 +}
1.497 +
1.498 +#endif