1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/bits/base_extender.h Mon Mar 06 10:28:37 2006 +0000
1.3 @@ -0,0 +1,473 @@
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-2006
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/concept/maps.h>
1.33 +
1.34 +///\ingroup graphbits
1.35 +///\file
1.36 +///\brief Extenders for the graph types
1.37 +namespace lemon {
1.38 +
1.39 + /// \ingroup graphbits
1.40 + ///
1.41 + /// \brief BaseExtender for the UGraphs
1.42 + template <typename Base>
1.43 + class UGraphBaseExtender : public Base {
1.44 +
1.45 + public:
1.46 +
1.47 + typedef Base Parent;
1.48 + typedef typename Parent::Edge UEdge;
1.49 + typedef typename Parent::Node Node;
1.50 +
1.51 + typedef True UndirectedTag;
1.52 +
1.53 + class Edge : public UEdge {
1.54 + friend class UGraphBaseExtender;
1.55 +
1.56 + protected:
1.57 + bool forward;
1.58 +
1.59 + Edge(const UEdge &ue, bool _forward) :
1.60 + UEdge(ue), forward(_forward) {}
1.61 +
1.62 + public:
1.63 + Edge() {}
1.64 +
1.65 + /// Invalid edge constructor
1.66 + Edge(Invalid i) : UEdge(i), forward(true) {}
1.67 +
1.68 + bool operator==(const Edge &that) const {
1.69 + return forward==that.forward && UEdge(*this)==UEdge(that);
1.70 + }
1.71 + bool operator!=(const Edge &that) const {
1.72 + return forward!=that.forward || UEdge(*this)!=UEdge(that);
1.73 + }
1.74 + bool operator<(const Edge &that) const {
1.75 + return forward<that.forward ||
1.76 + (!(that.forward<forward) && UEdge(*this)<UEdge(that));
1.77 + }
1.78 + };
1.79 +
1.80 +
1.81 +
1.82 + using Parent::source;
1.83 +
1.84 + /// Source of the given Edge.
1.85 + Node source(const Edge &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 Edge.
1.92 + Node target(const Edge &e) const {
1.93 + return e.forward ? Parent::target(e) : Parent::source(e);
1.94 + }
1.95 +
1.96 + /// \brief Directed edge from an undirected edge.
1.97 + ///
1.98 + /// Returns a directed edge corresponding to the specified UEdge.
1.99 + /// If the given bool is true the given undirected edge and the
1.100 + /// returned edge have the same source node.
1.101 + static Edge direct(const UEdge &ue, bool d) {
1.102 + return Edge(ue, d);
1.103 + }
1.104 +
1.105 + /// Returns whether the given directed edge is same orientation as the
1.106 + /// corresponding undirected edge.
1.107 + ///
1.108 + /// \todo reference to the corresponding point of the undirected graph
1.109 + /// concept. "What does the direction of an undirected edge mean?"
1.110 + static bool direction(const Edge &e) { return e.forward; }
1.111 +
1.112 +
1.113 + using Parent::first;
1.114 + using Parent::next;
1.115 +
1.116 + void first(Edge &e) const {
1.117 + Parent::first(e);
1.118 + e.forward=true;
1.119 + }
1.120 +
1.121 + void next(Edge &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(Edge &e, const Node &n) const {
1.132 + Parent::firstIn(e,n);
1.133 + if( UEdge(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(Edge &e) const {
1.142 + if( ! e.forward ) {
1.143 + Node n = Parent::target(e);
1.144 + Parent::nextIn(e);
1.145 + if( UEdge(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(Edge &e, const Node &n) const {
1.156 + Parent::firstOut(e,n);
1.157 + if( UEdge(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(Edge &e) const {
1.166 + if( ! e.forward ) {
1.167 + Node n = Parent::source(e);
1.168 + Parent::nextOut(e);
1.169 + if( UEdge(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(UEdge &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(UEdge &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 id) const {
1.200 + return Parent::nodeFromId(id);
1.201 + }
1.202 +
1.203 + Edge edgeFromId(int id) const {
1.204 + return direct(Parent::edgeFromId(id >> 1), bool(id & 1));
1.205 + }
1.206 +
1.207 + UEdge uEdgeFromId(int id) const {
1.208 + return Parent::edgeFromId(id >> 1);
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 UEdge &e) const {
1.216 + return Parent::id(e);
1.217 + }
1.218 +
1.219 + int id(const Edge &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 maxEdgeId() const {
1.228 + return 2 * Parent::maxEdgeId() + 1;
1.229 + }
1.230 +
1.231 + int maxUEdgeId() const {
1.232 + return Parent::maxEdgeId();
1.233 + }
1.234 +
1.235 +
1.236 + int edgeNum() const {
1.237 + return 2 * Parent::edgeNum();
1.238 + }
1.239 +
1.240 + int uEdgeNum() const {
1.241 + return Parent::edgeNum();
1.242 + }
1.243 +
1.244 + Edge findEdge(Node source, Node target, Edge prev) const {
1.245 + if (prev == INVALID) {
1.246 + UEdge edge = Parent::findEdge(source, target);
1.247 + if (edge != INVALID) return direct(edge, true);
1.248 + edge = Parent::findEdge(target, source);
1.249 + if (edge != INVALID) return direct(edge, false);
1.250 + } else if (direction(prev)) {
1.251 + UEdge edge = Parent::findEdge(source, target, prev);
1.252 + if (edge != INVALID) return direct(edge, true);
1.253 + edge = Parent::findEdge(target, source);
1.254 + if (edge != INVALID) return direct(edge, false);
1.255 + } else {
1.256 + UEdge edge = Parent::findEdge(target, source, prev);
1.257 + if (edge != INVALID) return direct(edge, false);
1.258 + }
1.259 + return INVALID;
1.260 + }
1.261 +
1.262 + UEdge findUEdge(Node source, Node target, UEdge prev) const {
1.263 + if (prev == INVALID) {
1.264 + UEdge edge = Parent::findEdge(source, target);
1.265 + if (edge != INVALID) return edge;
1.266 + edge = Parent::findEdge(target, source);
1.267 + if (edge != INVALID) return edge;
1.268 + } else if (Parent::source(prev) == source) {
1.269 + UEdge edge = Parent::findEdge(source, target, prev);
1.270 + if (edge != INVALID) return edge;
1.271 + edge = Parent::findEdge(target, source);
1.272 + if (edge != INVALID) return edge;
1.273 + } else {
1.274 + UEdge edge = Parent::findEdge(target, source, prev);
1.275 + if (edge != INVALID) return edge;
1.276 + }
1.277 + return INVALID;
1.278 + }
1.279 + };
1.280 +
1.281 +
1.282 + /// \ingroup graphbits
1.283 + ///
1.284 + /// \brief BaseExtender for the BpUGraphs
1.285 + template <typename Base>
1.286 + class BpUGraphBaseExtender : public Base {
1.287 + public:
1.288 + typedef Base Parent;
1.289 + typedef BpUGraphBaseExtender Graph;
1.290 +
1.291 + typedef typename Parent::Node Node;
1.292 + typedef typename Parent::Edge UEdge;
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 ANode : public Node {
1.301 + friend class BpUGraphBaseExtender;
1.302 + public:
1.303 + ANode() {}
1.304 + ANode(const Node& node) : Node(node) {
1.305 + LEMON_ASSERT(Parent::aNode(node) || node == INVALID,
1.306 + typename Parent::NodeSetError());
1.307 + }
1.308 + ANode(Invalid) : Node(INVALID) {}
1.309 + };
1.310 +
1.311 + void first(ANode& node) const {
1.312 + Parent::firstANode(static_cast<Node&>(node));
1.313 + }
1.314 + void next(ANode& node) const {
1.315 + Parent::nextANode(static_cast<Node&>(node));
1.316 + }
1.317 +
1.318 + int id(const ANode& node) const {
1.319 + return Parent::aNodeId(node);
1.320 + }
1.321 +
1.322 + class BNode : public Node {
1.323 + friend class BpUGraphBaseExtender;
1.324 + public:
1.325 + BNode() {}
1.326 + BNode(const Node& node) : Node(node) {
1.327 + LEMON_ASSERT(Parent::bNode(node) || node == INVALID,
1.328 + typename Parent::NodeSetError());
1.329 + }
1.330 + BNode(Invalid) : Node(INVALID) {}
1.331 + };
1.332 +
1.333 + void first(BNode& node) const {
1.334 + Parent::firstBNode(static_cast<Node&>(node));
1.335 + }
1.336 + void next(BNode& node) const {
1.337 + Parent::nextBNode(static_cast<Node&>(node));
1.338 + }
1.339 +
1.340 + int id(const BNode& node) const {
1.341 + return Parent::aNodeId(node);
1.342 + }
1.343 +
1.344 + Node source(const UEdge& edge) const {
1.345 + return aNode(edge);
1.346 + }
1.347 + Node target(const UEdge& edge) const {
1.348 + return bNode(edge);
1.349 + }
1.350 +
1.351 + void firstInc(UEdge& edge, bool& direction, const Node& node) const {
1.352 + if (Parent::aNode(node)) {
1.353 + Parent::firstOut(edge, node);
1.354 + direction = true;
1.355 + } else {
1.356 + Parent::firstIn(edge, node);
1.357 + direction = static_cast<UEdge&>(edge) == INVALID;
1.358 + }
1.359 + }
1.360 + void nextInc(UEdge& edge, bool& direction) const {
1.361 + if (direction) {
1.362 + Parent::nextOut(edge);
1.363 + } else {
1.364 + Parent::nextIn(edge);
1.365 + if (edge == INVALID) direction = true;
1.366 + }
1.367 + }
1.368 +
1.369 + int maxUEdgeId() const {
1.370 + return Parent::maxEdgeId();
1.371 + }
1.372 +
1.373 + UEdge uEdgeFromId(int id) const {
1.374 + return Parent::edgeFromId(id);
1.375 + }
1.376 +
1.377 + class Edge : public UEdge {
1.378 + friend class BpUGraphBaseExtender;
1.379 + protected:
1.380 + bool forward;
1.381 +
1.382 + Edge(const UEdge& edge, bool _forward)
1.383 + : UEdge(edge), forward(_forward) {}
1.384 +
1.385 + public:
1.386 + Edge() {}
1.387 + Edge (Invalid) : UEdge(INVALID), forward(true) {}
1.388 + bool operator==(const Edge& i) const {
1.389 + return UEdge::operator==(i) && forward == i.forward;
1.390 + }
1.391 + bool operator!=(const Edge& i) const {
1.392 + return UEdge::operator!=(i) || forward != i.forward;
1.393 + }
1.394 + bool operator<(const Edge& i) const {
1.395 + return UEdge::operator<(i) ||
1.396 + (!(i.forward<forward) && UEdge(*this)<UEdge(i));
1.397 + }
1.398 + };
1.399 +
1.400 + void first(Edge& edge) const {
1.401 + Parent::first(static_cast<UEdge&>(edge));
1.402 + edge.forward = true;
1.403 + }
1.404 +
1.405 + void next(Edge& edge) const {
1.406 + if (!edge.forward) {
1.407 + Parent::next(static_cast<UEdge&>(edge));
1.408 + }
1.409 + edge.forward = !edge.forward;
1.410 + }
1.411 +
1.412 + void firstOut(Edge& edge, const Node& node) const {
1.413 + if (Parent::aNode(node)) {
1.414 + Parent::firstOut(edge, node);
1.415 + edge.forward = true;
1.416 + } else {
1.417 + Parent::firstIn(edge, node);
1.418 + edge.forward = static_cast<UEdge&>(edge) == INVALID;
1.419 + }
1.420 + }
1.421 + void nextOut(Edge& edge) const {
1.422 + if (edge.forward) {
1.423 + Parent::nextOut(edge);
1.424 + } else {
1.425 + Parent::nextIn(edge);
1.426 + edge.forward = static_cast<UEdge&>(edge) == INVALID;
1.427 + }
1.428 + }
1.429 +
1.430 + void firstIn(Edge& edge, const Node& node) const {
1.431 + if (Parent::bNode(node)) {
1.432 + Parent::firstIn(edge, node);
1.433 + edge.forward = true;
1.434 + } else {
1.435 + Parent::firstOut(edge, node);
1.436 + edge.forward = static_cast<UEdge&>(edge) == INVALID;
1.437 + }
1.438 + }
1.439 + void nextIn(Edge& edge) const {
1.440 + if (edge.forward) {
1.441 + Parent::nextIn(edge);
1.442 + } else {
1.443 + Parent::nextOut(edge);
1.444 + edge.forward = static_cast<UEdge&>(edge) == INVALID;
1.445 + }
1.446 + }
1.447 +
1.448 + Node source(const Edge& edge) const {
1.449 + return edge.forward ? Parent::aNode(edge) : Parent::bNode(edge);
1.450 + }
1.451 + Node target(const Edge& edge) const {
1.452 + return edge.forward ? Parent::bNode(edge) : Parent::aNode(edge);
1.453 + }
1.454 +
1.455 + int id(const Edge& edge) const {
1.456 + return (Parent::id(edge) << 1) + (edge.forward ? 0 : 1);
1.457 + }
1.458 + Edge edgeFromId(int id) const {
1.459 + return Edge(Parent::fromId(id >> 1, UEdge()), (id & 1) == 0);
1.460 + }
1.461 + int maxEdgeId() const {
1.462 + return (Parent::maxId(UEdge()) << 1) + 1;
1.463 + }
1.464 +
1.465 + bool direction(const Edge& edge) const {
1.466 + return edge.forward;
1.467 + }
1.468 +
1.469 + Edge direct(const UEdge& edge, bool direction) const {
1.470 + return Edge(edge, direction);
1.471 + }
1.472 + };
1.473 +
1.474 +}
1.475 +
1.476 +#endif