lemon/bits/graph_adaptor_extender.h
changeset 414 05357da973ce
child 416 76287c8caa26
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/bits/graph_adaptor_extender.h	Sun Nov 30 18:57:18 2008 +0100
     1.3 @@ -0,0 +1,444 @@
     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-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_GRAPH_ADAPTOR_EXTENDER_H
    1.23 +#define LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H
    1.24 +
    1.25 +#include <lemon/core.h>
    1.26 +#include <lemon/error.h>
    1.27 +
    1.28 +#include <lemon/bits/default_map.h>
    1.29 +
    1.30 +
    1.31 +///\ingroup digraphbits
    1.32 +///\file
    1.33 +///\brief Extenders for the digraph adaptor types
    1.34 +namespace lemon {
    1.35 +
    1.36 +  /// \ingroup digraphbits
    1.37 +  ///
    1.38 +  /// \brief Extender for the DigraphAdaptors
    1.39 +  template <typename _Digraph>
    1.40 +  class DigraphAdaptorExtender : public _Digraph {
    1.41 +  public:
    1.42 +
    1.43 +    typedef _Digraph Parent;
    1.44 +    typedef _Digraph Digraph;
    1.45 +    typedef DigraphAdaptorExtender Adaptor;
    1.46 +
    1.47 +    // Base extensions
    1.48 +
    1.49 +    typedef typename Parent::Node Node;
    1.50 +    typedef typename Parent::Arc Arc;
    1.51 +
    1.52 +    int maxId(Node) const {
    1.53 +      return Parent::maxNodeId();
    1.54 +    }
    1.55 +
    1.56 +    int maxId(Arc) const {
    1.57 +      return Parent::maxArcId();
    1.58 +    }
    1.59 +
    1.60 +    Node fromId(int id, Node) const {
    1.61 +      return Parent::nodeFromId(id);
    1.62 +    }
    1.63 +
    1.64 +    Arc fromId(int id, Arc) const {
    1.65 +      return Parent::arcFromId(id);
    1.66 +    }
    1.67 +
    1.68 +    Node oppositeNode(const Node &n, const Arc &e) const {
    1.69 +      if (n == Parent::source(e))
    1.70 +	return Parent::target(e);
    1.71 +      else if(n==Parent::target(e))
    1.72 +	return Parent::source(e);
    1.73 +      else
    1.74 +	return INVALID;
    1.75 +    }
    1.76 +
    1.77 +    class NodeIt : public Node { 
    1.78 +      const Adaptor* _adaptor;
    1.79 +    public:
    1.80 +
    1.81 +      NodeIt() {}
    1.82 +
    1.83 +      NodeIt(Invalid i) : Node(i) { }
    1.84 +
    1.85 +      explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
    1.86 +	_adaptor->first(static_cast<Node&>(*this));
    1.87 +      }
    1.88 +
    1.89 +      NodeIt(const Adaptor& adaptor, const Node& node) 
    1.90 +	: Node(node), _adaptor(&adaptor) {}
    1.91 +
    1.92 +      NodeIt& operator++() { 
    1.93 +	_adaptor->next(*this);
    1.94 +	return *this; 
    1.95 +      }
    1.96 +
    1.97 +    };
    1.98 +
    1.99 +
   1.100 +    class ArcIt : public Arc { 
   1.101 +      const Adaptor* _adaptor;
   1.102 +    public:
   1.103 +
   1.104 +      ArcIt() { }
   1.105 +
   1.106 +      ArcIt(Invalid i) : Arc(i) { }
   1.107 +
   1.108 +      explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
   1.109 +	_adaptor->first(static_cast<Arc&>(*this));
   1.110 +      }
   1.111 +
   1.112 +      ArcIt(const Adaptor& adaptor, const Arc& e) : 
   1.113 +	Arc(e), _adaptor(&adaptor) { }
   1.114 +
   1.115 +      ArcIt& operator++() { 
   1.116 +	_adaptor->next(*this);
   1.117 +	return *this; 
   1.118 +      }
   1.119 +
   1.120 +    };
   1.121 +
   1.122 +
   1.123 +    class OutArcIt : public Arc { 
   1.124 +      const Adaptor* _adaptor;
   1.125 +    public:
   1.126 +
   1.127 +      OutArcIt() { }
   1.128 +
   1.129 +      OutArcIt(Invalid i) : Arc(i) { }
   1.130 +
   1.131 +      OutArcIt(const Adaptor& adaptor, const Node& node) 
   1.132 +	: _adaptor(&adaptor) {
   1.133 +	_adaptor->firstOut(*this, node);
   1.134 +      }
   1.135 +
   1.136 +      OutArcIt(const Adaptor& adaptor, const Arc& arc) 
   1.137 +	: Arc(arc), _adaptor(&adaptor) {}
   1.138 +
   1.139 +      OutArcIt& operator++() { 
   1.140 +	_adaptor->nextOut(*this);
   1.141 +	return *this; 
   1.142 +      }
   1.143 +
   1.144 +    };
   1.145 +
   1.146 +
   1.147 +    class InArcIt : public Arc { 
   1.148 +      const Adaptor* _adaptor;
   1.149 +    public:
   1.150 +
   1.151 +      InArcIt() { }
   1.152 +
   1.153 +      InArcIt(Invalid i) : Arc(i) { }
   1.154 +
   1.155 +      InArcIt(const Adaptor& adaptor, const Node& node) 
   1.156 +	: _adaptor(&adaptor) {
   1.157 +	_adaptor->firstIn(*this, node);
   1.158 +      }
   1.159 +
   1.160 +      InArcIt(const Adaptor& adaptor, const Arc& arc) : 
   1.161 +	Arc(arc), _adaptor(&adaptor) {}
   1.162 +
   1.163 +      InArcIt& operator++() { 
   1.164 +	_adaptor->nextIn(*this);
   1.165 +	return *this; 
   1.166 +      }
   1.167 +
   1.168 +    };
   1.169 +
   1.170 +    /// \brief Base node of the iterator
   1.171 +    ///
   1.172 +    /// Returns the base node (ie. the source in this case) of the iterator
   1.173 +    Node baseNode(const OutArcIt &e) const {
   1.174 +      return Parent::source(e);
   1.175 +    }
   1.176 +    /// \brief Running node of the iterator
   1.177 +    ///
   1.178 +    /// Returns the running node (ie. the target in this case) of the
   1.179 +    /// iterator
   1.180 +    Node runningNode(const OutArcIt &e) const {
   1.181 +      return Parent::target(e);
   1.182 +    }
   1.183 +
   1.184 +    /// \brief Base node of the iterator
   1.185 +    ///
   1.186 +    /// Returns the base node (ie. the target in this case) of the iterator
   1.187 +    Node baseNode(const InArcIt &e) const {
   1.188 +      return Parent::target(e);
   1.189 +    }
   1.190 +    /// \brief Running node of the iterator
   1.191 +    ///
   1.192 +    /// Returns the running node (ie. the source in this case) of the
   1.193 +    /// iterator
   1.194 +    Node runningNode(const InArcIt &e) const {
   1.195 +      return Parent::source(e);
   1.196 +    }
   1.197 +
   1.198 +  };
   1.199 +
   1.200 +
   1.201 +  /// \ingroup digraphbits
   1.202 +  ///
   1.203 +  /// \brief Extender for the GraphAdaptors
   1.204 +  template <typename _Graph> 
   1.205 +  class GraphAdaptorExtender : public _Graph {
   1.206 +  public:
   1.207 +    
   1.208 +    typedef _Graph Parent;
   1.209 +    typedef _Graph Graph;
   1.210 +    typedef GraphAdaptorExtender Adaptor;
   1.211 +
   1.212 +    typedef typename Parent::Node Node;
   1.213 +    typedef typename Parent::Arc Arc;
   1.214 +    typedef typename Parent::Edge Edge;
   1.215 +
   1.216 +    // Graph extension    
   1.217 +
   1.218 +    int maxId(Node) const {
   1.219 +      return Parent::maxNodeId();
   1.220 +    }
   1.221 +
   1.222 +    int maxId(Arc) const {
   1.223 +      return Parent::maxArcId();
   1.224 +    }
   1.225 +
   1.226 +    int maxId(Edge) const {
   1.227 +      return Parent::maxEdgeId();
   1.228 +    }
   1.229 +
   1.230 +    Node fromId(int id, Node) const {
   1.231 +      return Parent::nodeFromId(id);
   1.232 +    }
   1.233 +
   1.234 +    Arc fromId(int id, Arc) const {
   1.235 +      return Parent::arcFromId(id);
   1.236 +    }
   1.237 +
   1.238 +    Edge fromId(int id, Edge) const {
   1.239 +      return Parent::edgeFromId(id);
   1.240 +    }
   1.241 +
   1.242 +    Node oppositeNode(const Node &n, const Edge &e) const {
   1.243 +      if( n == Parent::u(e))
   1.244 +	return Parent::v(e);
   1.245 +      else if( n == Parent::v(e))
   1.246 +	return Parent::u(e);
   1.247 +      else
   1.248 +	return INVALID;
   1.249 +    }
   1.250 +
   1.251 +    Arc oppositeArc(const Arc &a) const {
   1.252 +      return Parent::direct(a, !Parent::direction(a));
   1.253 +    }
   1.254 +
   1.255 +    using Parent::direct;
   1.256 +    Arc direct(const Edge &e, const Node &s) const {
   1.257 +      return Parent::direct(e, Parent::u(e) == s);
   1.258 +    }
   1.259 +
   1.260 +
   1.261 +    class NodeIt : public Node { 
   1.262 +      const Adaptor* _adaptor;
   1.263 +    public:
   1.264 +
   1.265 +      NodeIt() {}
   1.266 +
   1.267 +      NodeIt(Invalid i) : Node(i) { }
   1.268 +
   1.269 +      explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
   1.270 +	_adaptor->first(static_cast<Node&>(*this));
   1.271 +      }
   1.272 +
   1.273 +      NodeIt(const Adaptor& adaptor, const Node& node) 
   1.274 +	: Node(node), _adaptor(&adaptor) {}
   1.275 +
   1.276 +      NodeIt& operator++() { 
   1.277 +	_adaptor->next(*this);
   1.278 +	return *this; 
   1.279 +      }
   1.280 +
   1.281 +    };
   1.282 +
   1.283 +
   1.284 +    class ArcIt : public Arc { 
   1.285 +      const Adaptor* _adaptor;
   1.286 +    public:
   1.287 +
   1.288 +      ArcIt() { }
   1.289 +
   1.290 +      ArcIt(Invalid i) : Arc(i) { }
   1.291 +
   1.292 +      explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
   1.293 +	_adaptor->first(static_cast<Arc&>(*this));
   1.294 +      }
   1.295 +
   1.296 +      ArcIt(const Adaptor& adaptor, const Arc& e) : 
   1.297 +	Arc(e), _adaptor(&adaptor) { }
   1.298 +
   1.299 +      ArcIt& operator++() { 
   1.300 +	_adaptor->next(*this);
   1.301 +	return *this; 
   1.302 +      }
   1.303 +
   1.304 +    };
   1.305 +
   1.306 +
   1.307 +    class OutArcIt : public Arc { 
   1.308 +      const Adaptor* _adaptor;
   1.309 +    public:
   1.310 +
   1.311 +      OutArcIt() { }
   1.312 +
   1.313 +      OutArcIt(Invalid i) : Arc(i) { }
   1.314 +
   1.315 +      OutArcIt(const Adaptor& adaptor, const Node& node) 
   1.316 +	: _adaptor(&adaptor) {
   1.317 +	_adaptor->firstOut(*this, node);
   1.318 +      }
   1.319 +
   1.320 +      OutArcIt(const Adaptor& adaptor, const Arc& arc) 
   1.321 +	: Arc(arc), _adaptor(&adaptor) {}
   1.322 +
   1.323 +      OutArcIt& operator++() { 
   1.324 +	_adaptor->nextOut(*this);
   1.325 +	return *this; 
   1.326 +      }
   1.327 +
   1.328 +    };
   1.329 +
   1.330 +
   1.331 +    class InArcIt : public Arc { 
   1.332 +      const Adaptor* _adaptor;
   1.333 +    public:
   1.334 +
   1.335 +      InArcIt() { }
   1.336 +
   1.337 +      InArcIt(Invalid i) : Arc(i) { }
   1.338 +
   1.339 +      InArcIt(const Adaptor& adaptor, const Node& node) 
   1.340 +	: _adaptor(&adaptor) {
   1.341 +	_adaptor->firstIn(*this, node);
   1.342 +      }
   1.343 +
   1.344 +      InArcIt(const Adaptor& adaptor, const Arc& arc) : 
   1.345 +	Arc(arc), _adaptor(&adaptor) {}
   1.346 +
   1.347 +      InArcIt& operator++() { 
   1.348 +	_adaptor->nextIn(*this);
   1.349 +	return *this; 
   1.350 +      }
   1.351 +
   1.352 +    };
   1.353 +
   1.354 +    class EdgeIt : public Parent::Edge { 
   1.355 +      const Adaptor* _adaptor;
   1.356 +    public:
   1.357 +
   1.358 +      EdgeIt() { }
   1.359 +
   1.360 +      EdgeIt(Invalid i) : Edge(i) { }
   1.361 +
   1.362 +      explicit EdgeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
   1.363 +	_adaptor->first(static_cast<Edge&>(*this));
   1.364 +      }
   1.365 +
   1.366 +      EdgeIt(const Adaptor& adaptor, const Edge& e) : 
   1.367 +	Edge(e), _adaptor(&adaptor) { }
   1.368 +
   1.369 +      EdgeIt& operator++() { 
   1.370 +	_adaptor->next(*this);
   1.371 +	return *this; 
   1.372 +      }
   1.373 +
   1.374 +    };
   1.375 +
   1.376 +    class IncEdgeIt : public Edge { 
   1.377 +      friend class GraphAdaptorExtender;
   1.378 +      const Adaptor* _adaptor;
   1.379 +      bool direction;
   1.380 +    public:
   1.381 +
   1.382 +      IncEdgeIt() { }
   1.383 +
   1.384 +      IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
   1.385 +
   1.386 +      IncEdgeIt(const Adaptor& adaptor, const Node &n) : _adaptor(&adaptor) {
   1.387 +	_adaptor->firstInc(static_cast<Edge&>(*this), direction, n);
   1.388 +      }
   1.389 +
   1.390 +      IncEdgeIt(const Adaptor& adaptor, const Edge &e, const Node &n)
   1.391 +	: _adaptor(&adaptor), Edge(e) {
   1.392 +	direction = (_adaptor->u(e) == n);
   1.393 +      }
   1.394 +
   1.395 +      IncEdgeIt& operator++() {
   1.396 +	_adaptor->nextInc(*this, direction);
   1.397 +	return *this; 
   1.398 +      }
   1.399 +    };
   1.400 +
   1.401 +    /// \brief Base node of the iterator
   1.402 +    ///
   1.403 +    /// Returns the base node (ie. the source in this case) of the iterator
   1.404 +    Node baseNode(const OutArcIt &a) const {
   1.405 +      return Parent::source(a);
   1.406 +    }
   1.407 +    /// \brief Running node of the iterator
   1.408 +    ///
   1.409 +    /// Returns the running node (ie. the target in this case) of the
   1.410 +    /// iterator
   1.411 +    Node runningNode(const OutArcIt &a) const {
   1.412 +      return Parent::target(a);
   1.413 +    }
   1.414 +
   1.415 +    /// \brief Base node of the iterator
   1.416 +    ///
   1.417 +    /// Returns the base node (ie. the target in this case) of the iterator
   1.418 +    Node baseNode(const InArcIt &a) const {
   1.419 +      return Parent::target(a);
   1.420 +    }
   1.421 +    /// \brief Running node of the iterator
   1.422 +    ///
   1.423 +    /// Returns the running node (ie. the source in this case) of the
   1.424 +    /// iterator
   1.425 +    Node runningNode(const InArcIt &a) const {
   1.426 +      return Parent::source(a);
   1.427 +    }
   1.428 +
   1.429 +    /// Base node of the iterator
   1.430 +    ///
   1.431 +    /// Returns the base node of the iterator
   1.432 +    Node baseNode(const IncEdgeIt &e) const {
   1.433 +      return e.direction ? Parent::u(e) : Parent::v(e);
   1.434 +    }
   1.435 +    /// Running node of the iterator
   1.436 +    ///
   1.437 +    /// Returns the running node of the iterator
   1.438 +    Node runningNode(const IncEdgeIt &e) const {
   1.439 +      return e.direction ? Parent::v(e) : Parent::u(e);
   1.440 +    }
   1.441 +
   1.442 +  };
   1.443 +
   1.444 +}
   1.445 +
   1.446 +
   1.447 +#endif