lemon/bits/base_extender.h
changeset 708 994c7df296c9
parent 707 e9c203fb003d
parent 706 703ebf476a1d
child 709 76689f2fc02d
child 711 5100072d83ca
     1.1 --- a/lemon/bits/base_extender.h	Fri Nov 13 12:33:33 2009 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,494 +0,0 @@
     1.4 -/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 - *
     1.6 - * This file is a part of LEMON, a generic C++ optimization library.
     1.7 - *
     1.8 - * Copyright (C) 2003-2008
     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/core.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 -    // First node of the edge
    1.81 -    Node u(const Edge &e) const {
    1.82 -      return Parent::source(e);
    1.83 -    }
    1.84 -
    1.85 -    // Source of the given arc
    1.86 -    Node source(const Arc &e) const {
    1.87 -      return e.forward ? Parent::source(e) : Parent::target(e);
    1.88 -    }
    1.89 -
    1.90 -    // Second node of the edge
    1.91 -    Node v(const Edge &e) const {
    1.92 -      return Parent::target(e);
    1.93 -    }
    1.94 -
    1.95 -    // Target of the given arc
    1.96 -    Node target(const Arc &e) const {
    1.97 -      return e.forward ? Parent::target(e) : Parent::source(e);
    1.98 -    }
    1.99 -
   1.100 -    // \brief Directed arc from an edge.
   1.101 -    //
   1.102 -    // Returns a directed arc corresponding to the specified edge.
   1.103 -    // If the given bool is true, the first node of the given edge and
   1.104 -    // the source node of the returned arc are the same.
   1.105 -    static Arc direct(const Edge &e, bool d) {
   1.106 -      return Arc(e, d);
   1.107 -    }
   1.108 -
   1.109 -    // Returns whether the given directed arc has the same orientation
   1.110 -    // as the corresponding edge.
   1.111 -    static bool direction(const Arc &a) { return a.forward; }
   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 -    int arcNum() const {
   1.236 -      return 2 * Parent::arcNum();
   1.237 -    }
   1.238 -
   1.239 -    int edgeNum() const {
   1.240 -      return Parent::arcNum();
   1.241 -    }
   1.242 -
   1.243 -    Arc findArc(Node s, Node t, Arc p = INVALID) const {
   1.244 -      if (p == INVALID) {
   1.245 -        Edge arc = Parent::findArc(s, t);
   1.246 -        if (arc != INVALID) return direct(arc, true);
   1.247 -        arc = Parent::findArc(t, s);
   1.248 -        if (arc != INVALID) return direct(arc, false);
   1.249 -      } else if (direction(p)) {
   1.250 -        Edge arc = Parent::findArc(s, t, p);
   1.251 -        if (arc != INVALID) return direct(arc, true);
   1.252 -        arc = Parent::findArc(t, s);
   1.253 -        if (arc != INVALID) return direct(arc, false);
   1.254 -      } else {
   1.255 -        Edge arc = Parent::findArc(t, s, p);
   1.256 -        if (arc != INVALID) return direct(arc, false);
   1.257 -      }
   1.258 -      return INVALID;
   1.259 -    }
   1.260 -
   1.261 -    Edge findEdge(Node s, Node t, Edge p = INVALID) const {
   1.262 -      if (s != t) {
   1.263 -        if (p == INVALID) {
   1.264 -          Edge arc = Parent::findArc(s, t);
   1.265 -          if (arc != INVALID) return arc;
   1.266 -          arc = Parent::findArc(t, s);
   1.267 -          if (arc != INVALID) return arc;
   1.268 -        } else if (Parent::s(p) == s) {
   1.269 -          Edge arc = Parent::findArc(s, t, p);
   1.270 -          if (arc != INVALID) return arc;
   1.271 -          arc = Parent::findArc(t, s);
   1.272 -          if (arc != INVALID) return arc;
   1.273 -        } else {
   1.274 -          Edge arc = Parent::findArc(t, s, p);
   1.275 -          if (arc != INVALID) return arc;
   1.276 -        }
   1.277 -      } else {
   1.278 -        return Parent::findArc(s, t, p);
   1.279 -      }
   1.280 -      return INVALID;
   1.281 -    }
   1.282 -  };
   1.283 -
   1.284 -  template <typename Base>
   1.285 -  class BidirBpGraphExtender : public Base {
   1.286 -  public:
   1.287 -    typedef Base Parent;
   1.288 -    typedef BidirBpGraphExtender Digraph;
   1.289 -
   1.290 -    typedef typename Parent::Node Node;
   1.291 -    typedef typename Parent::Edge Edge;
   1.292 -
   1.293 -
   1.294 -    using Parent::first;
   1.295 -    using Parent::next;
   1.296 -
   1.297 -    using Parent::id;
   1.298 -
   1.299 -    class Red : public Node {
   1.300 -      friend class BidirBpGraphExtender;
   1.301 -    public:
   1.302 -      Red() {}
   1.303 -      Red(const Node& node) : Node(node) {
   1.304 -        LEMON_DEBUG(Parent::red(node) || node == INVALID,
   1.305 -                    typename Parent::NodeSetError());
   1.306 -      }
   1.307 -      Red& operator=(const Node& node) {
   1.308 -        LEMON_DEBUG(Parent::red(node) || node == INVALID,
   1.309 -                    typename Parent::NodeSetError());
   1.310 -        Node::operator=(node);
   1.311 -        return *this;
   1.312 -      }
   1.313 -      Red(Invalid) : Node(INVALID) {}
   1.314 -      Red& operator=(Invalid) {
   1.315 -        Node::operator=(INVALID);
   1.316 -        return *this;
   1.317 -      }
   1.318 -    };
   1.319 -
   1.320 -    void first(Red& node) const {
   1.321 -      Parent::firstRed(static_cast<Node&>(node));
   1.322 -    }
   1.323 -    void next(Red& node) const {
   1.324 -      Parent::nextRed(static_cast<Node&>(node));
   1.325 -    }
   1.326 -
   1.327 -    int id(const Red& node) const {
   1.328 -      return Parent::redId(node);
   1.329 -    }
   1.330 -
   1.331 -    class Blue : public Node {
   1.332 -      friend class BidirBpGraphExtender;
   1.333 -    public:
   1.334 -      Blue() {}
   1.335 -      Blue(const Node& node) : Node(node) {
   1.336 -        LEMON_DEBUG(Parent::blue(node) || node == INVALID,
   1.337 -                    typename Parent::NodeSetError());
   1.338 -      }
   1.339 -      Blue& operator=(const Node& node) {
   1.340 -        LEMON_DEBUG(Parent::blue(node) || node == INVALID,
   1.341 -                    typename Parent::NodeSetError());
   1.342 -        Node::operator=(node);
   1.343 -        return *this;
   1.344 -      }
   1.345 -      Blue(Invalid) : Node(INVALID) {}
   1.346 -      Blue& operator=(Invalid) {
   1.347 -        Node::operator=(INVALID);
   1.348 -        return *this;
   1.349 -      }
   1.350 -    };
   1.351 -
   1.352 -    void first(Blue& node) const {
   1.353 -      Parent::firstBlue(static_cast<Node&>(node));
   1.354 -    }
   1.355 -    void next(Blue& node) const {
   1.356 -      Parent::nextBlue(static_cast<Node&>(node));
   1.357 -    }
   1.358 -
   1.359 -    int id(const Blue& node) const {
   1.360 -      return Parent::redId(node);
   1.361 -    }
   1.362 -
   1.363 -    Node source(const Edge& arc) const {
   1.364 -      return red(arc);
   1.365 -    }
   1.366 -    Node target(const Edge& arc) const {
   1.367 -      return blue(arc);
   1.368 -    }
   1.369 -
   1.370 -    void firstInc(Edge& arc, bool& dir, const Node& node) const {
   1.371 -      if (Parent::red(node)) {
   1.372 -        Parent::firstFromRed(arc, node);
   1.373 -        dir = true;
   1.374 -      } else {
   1.375 -        Parent::firstFromBlue(arc, node);
   1.376 -        dir = static_cast<Edge&>(arc) == INVALID;
   1.377 -      }
   1.378 -    }
   1.379 -    void nextInc(Edge& arc, bool& dir) const {
   1.380 -      if (dir) {
   1.381 -        Parent::nextFromRed(arc);
   1.382 -      } else {
   1.383 -        Parent::nextFromBlue(arc);
   1.384 -        if (arc == INVALID) dir = true;
   1.385 -      }
   1.386 -    }
   1.387 -
   1.388 -    class Arc : public Edge {
   1.389 -      friend class BidirBpGraphExtender;
   1.390 -    protected:
   1.391 -      bool forward;
   1.392 -
   1.393 -      Arc(const Edge& arc, bool _forward)
   1.394 -        : Edge(arc), forward(_forward) {}
   1.395 -
   1.396 -    public:
   1.397 -      Arc() {}
   1.398 -      Arc (Invalid) : Edge(INVALID), forward(true) {}
   1.399 -      bool operator==(const Arc& i) const {
   1.400 -        return Edge::operator==(i) && forward == i.forward;
   1.401 -      }
   1.402 -      bool operator!=(const Arc& i) const {
   1.403 -        return Edge::operator!=(i) || forward != i.forward;
   1.404 -      }
   1.405 -      bool operator<(const Arc& i) const {
   1.406 -        return Edge::operator<(i) ||
   1.407 -          (!(i.forward<forward) && Edge(*this)<Edge(i));
   1.408 -      }
   1.409 -    };
   1.410 -
   1.411 -    void first(Arc& arc) const {
   1.412 -      Parent::first(static_cast<Edge&>(arc));
   1.413 -      arc.forward = true;
   1.414 -    }
   1.415 -
   1.416 -    void next(Arc& arc) const {
   1.417 -      if (!arc.forward) {
   1.418 -        Parent::next(static_cast<Edge&>(arc));
   1.419 -      }
   1.420 -      arc.forward = !arc.forward;
   1.421 -    }
   1.422 -
   1.423 -    void firstOut(Arc& arc, const Node& node) const {
   1.424 -      if (Parent::red(node)) {
   1.425 -        Parent::firstFromRed(arc, node);
   1.426 -        arc.forward = true;
   1.427 -      } else {
   1.428 -        Parent::firstFromBlue(arc, node);
   1.429 -        arc.forward = static_cast<Edge&>(arc) == INVALID;
   1.430 -      }
   1.431 -    }
   1.432 -    void nextOut(Arc& arc) const {
   1.433 -      if (arc.forward) {
   1.434 -        Parent::nextFromRed(arc);
   1.435 -      } else {
   1.436 -        Parent::nextFromBlue(arc);
   1.437 -        arc.forward = static_cast<Edge&>(arc) == INVALID;
   1.438 -      }
   1.439 -    }
   1.440 -
   1.441 -    void firstIn(Arc& arc, const Node& node) const {
   1.442 -      if (Parent::blue(node)) {
   1.443 -        Parent::firstFromBlue(arc, node);
   1.444 -        arc.forward = true;
   1.445 -      } else {
   1.446 -        Parent::firstFromRed(arc, node);
   1.447 -        arc.forward = static_cast<Edge&>(arc) == INVALID;
   1.448 -      }
   1.449 -    }
   1.450 -    void nextIn(Arc& arc) const {
   1.451 -      if (arc.forward) {
   1.452 -        Parent::nextFromBlue(arc);
   1.453 -      } else {
   1.454 -        Parent::nextFromRed(arc);
   1.455 -        arc.forward = static_cast<Edge&>(arc) == INVALID;
   1.456 -      }
   1.457 -    }
   1.458 -
   1.459 -    Node source(const Arc& arc) const {
   1.460 -      return arc.forward ? Parent::red(arc) : Parent::blue(arc);
   1.461 -    }
   1.462 -    Node target(const Arc& arc) const {
   1.463 -      return arc.forward ? Parent::blue(arc) : Parent::red(arc);
   1.464 -    }
   1.465 -
   1.466 -    int id(const Arc& arc) const {
   1.467 -      return (Parent::id(static_cast<const Edge&>(arc)) << 1) +
   1.468 -        (arc.forward ? 0 : 1);
   1.469 -    }
   1.470 -    Arc arcFromId(int ix) const {
   1.471 -      return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0);
   1.472 -    }
   1.473 -    int maxArcId() const {
   1.474 -      return (Parent::maxEdgeId() << 1) + 1;
   1.475 -    }
   1.476 -
   1.477 -    bool direction(const Arc& arc) const {
   1.478 -      return arc.forward;
   1.479 -    }
   1.480 -
   1.481 -    Arc direct(const Edge& arc, bool dir) const {
   1.482 -      return Arc(arc, dir);
   1.483 -    }
   1.484 -
   1.485 -    int arcNum() const {
   1.486 -      return 2 * Parent::edgeNum();
   1.487 -    }
   1.488 -
   1.489 -    int edgeNum() const {
   1.490 -      return Parent::edgeNum();
   1.491 -    }
   1.492 -
   1.493 -
   1.494 -  };
   1.495 -}
   1.496 -
   1.497 -#endif