1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/bits/edge_set_extender.h Wed Feb 22 18:26:56 2006 +0000
1.3 @@ -0,0 +1,606 @@
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 +
1.23 +namespace lemon {
1.24 +
1.25 + template <typename Base>
1.26 + class EdgeSetExtender : public Base {
1.27 + public:
1.28 +
1.29 + typedef Base Parent;
1.30 + typedef EdgeSetExtender Graph;
1.31 +
1.32 + // Base extensions
1.33 +
1.34 + typedef typename Parent::Node Node;
1.35 + typedef typename Parent::Edge Edge;
1.36 +
1.37 + int maxId(Node) const {
1.38 + return Parent::maxNodeId();
1.39 + }
1.40 +
1.41 + int maxId(Edge) const {
1.42 + return Parent::maxEdgeId();
1.43 + }
1.44 +
1.45 + Node fromId(int id, Node) const {
1.46 + return Parent::nodeFromId(id);
1.47 + }
1.48 +
1.49 + Edge fromId(int id, Edge) const {
1.50 + return Parent::edgeFromId(id);
1.51 + }
1.52 +
1.53 + Node oppositeNode(const Node &n, const Edge &e) const {
1.54 + if (n == Parent::source(e))
1.55 + return Parent::target(e);
1.56 + else if(n==Parent::target(e))
1.57 + return Parent::source(e);
1.58 + else
1.59 + return INVALID;
1.60 + }
1.61 +
1.62 +
1.63 + // Alteration notifier extensions
1.64 +
1.65 + /// The edge observer registry.
1.66 + typedef AlterationNotifier<Edge> EdgeNotifier;
1.67 +
1.68 + protected:
1.69 +
1.70 + mutable EdgeNotifier edge_notifier;
1.71 +
1.72 + public:
1.73 +
1.74 + /// \brief Gives back the edge alteration notifier.
1.75 + ///
1.76 + /// Gives back the edge alteration notifier.
1.77 + EdgeNotifier& getNotifier(Edge) const {
1.78 + return edge_notifier;
1.79 + }
1.80 +
1.81 + // Iterable extensions
1.82 +
1.83 + class NodeIt : public Node {
1.84 + const Graph* graph;
1.85 + public:
1.86 +
1.87 + NodeIt() {}
1.88 +
1.89 + NodeIt(Invalid i) : Node(i) { }
1.90 +
1.91 + explicit NodeIt(const Graph& _graph) : graph(&_graph) {
1.92 + _graph.first(*static_cast<Node*>(this));
1.93 + }
1.94 +
1.95 + NodeIt(const Graph& _graph, const Node& node)
1.96 + : Node(node), graph(&_graph) {}
1.97 +
1.98 + NodeIt& operator++() {
1.99 + graph->next(*this);
1.100 + return *this;
1.101 + }
1.102 +
1.103 + };
1.104 +
1.105 +
1.106 + class EdgeIt : public Edge {
1.107 + const Graph* graph;
1.108 + public:
1.109 +
1.110 + EdgeIt() { }
1.111 +
1.112 + EdgeIt(Invalid i) : Edge(i) { }
1.113 +
1.114 + explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
1.115 + _graph.first(*static_cast<Edge*>(this));
1.116 + }
1.117 +
1.118 + EdgeIt(const Graph& _graph, const Edge& e) :
1.119 + Edge(e), graph(&_graph) { }
1.120 +
1.121 + EdgeIt& operator++() {
1.122 + graph->next(*this);
1.123 + return *this;
1.124 + }
1.125 +
1.126 + };
1.127 +
1.128 +
1.129 + class OutEdgeIt : public Edge {
1.130 + const Graph* graph;
1.131 + public:
1.132 +
1.133 + OutEdgeIt() { }
1.134 +
1.135 + OutEdgeIt(Invalid i) : Edge(i) { }
1.136 +
1.137 + OutEdgeIt(const Graph& _graph, const Node& node)
1.138 + : graph(&_graph) {
1.139 + _graph.firstOut(*this, node);
1.140 + }
1.141 +
1.142 + OutEdgeIt(const Graph& _graph, const Edge& edge)
1.143 + : Edge(edge), graph(&_graph) {}
1.144 +
1.145 + OutEdgeIt& operator++() {
1.146 + graph->nextOut(*this);
1.147 + return *this;
1.148 + }
1.149 +
1.150 + };
1.151 +
1.152 +
1.153 + class InEdgeIt : public Edge {
1.154 + const Graph* graph;
1.155 + public:
1.156 +
1.157 + InEdgeIt() { }
1.158 +
1.159 + InEdgeIt(Invalid i) : Edge(i) { }
1.160 +
1.161 + InEdgeIt(const Graph& _graph, const Node& node)
1.162 + : graph(&_graph) {
1.163 + _graph.firstIn(*this, node);
1.164 + }
1.165 +
1.166 + InEdgeIt(const Graph& _graph, const Edge& edge) :
1.167 + Edge(edge), graph(&_graph) {}
1.168 +
1.169 + InEdgeIt& operator++() {
1.170 + graph->nextIn(*this);
1.171 + return *this;
1.172 + }
1.173 +
1.174 + };
1.175 +
1.176 + /// \brief Base node of the iterator
1.177 + ///
1.178 + /// Returns the base node (ie. the source in this case) of the iterator
1.179 + Node baseNode(const OutEdgeIt &e) const {
1.180 + return Parent::source((Edge)e);
1.181 + }
1.182 + /// \brief Running node of the iterator
1.183 + ///
1.184 + /// Returns the running node (ie. the target in this case) of the
1.185 + /// iterator
1.186 + Node runningNode(const OutEdgeIt &e) const {
1.187 + return Parent::target((Edge)e);
1.188 + }
1.189 +
1.190 + /// \brief Base node of the iterator
1.191 + ///
1.192 + /// Returns the base node (ie. the target in this case) of the iterator
1.193 + Node baseNode(const InEdgeIt &e) const {
1.194 + return Parent::target((Edge)e);
1.195 + }
1.196 + /// \brief Running node of the iterator
1.197 + ///
1.198 + /// Returns the running node (ie. the source in this case) of the
1.199 + /// iterator
1.200 + Node runningNode(const InEdgeIt &e) const {
1.201 + return Parent::source((Edge)e);
1.202 + }
1.203 +
1.204 + using Parent::first;
1.205 +
1.206 + // Mappable extension
1.207 +
1.208 + template <typename _Value>
1.209 + class EdgeMap
1.210 + : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
1.211 + public:
1.212 + typedef EdgeSetExtender Graph;
1.213 + typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
1.214 +
1.215 + EdgeMap(const Graph& _g)
1.216 + : Parent(_g) {}
1.217 + EdgeMap(const Graph& _g, const _Value& _v)
1.218 + : Parent(_g, _v) {}
1.219 +
1.220 + EdgeMap& operator=(const EdgeMap& cmap) {
1.221 + return operator=<EdgeMap>(cmap);
1.222 + }
1.223 +
1.224 + template <typename CMap>
1.225 + EdgeMap& operator=(const CMap& cmap) {
1.226 + checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
1.227 + const typename Parent::Graph* graph = Parent::getGraph();
1.228 + Edge it;
1.229 + for (graph->first(it); it != INVALID; graph->next(it)) {
1.230 + Parent::set(it, cmap[it]);
1.231 + }
1.232 + return *this;
1.233 + }
1.234 + };
1.235 +
1.236 +
1.237 + // Alteration extension
1.238 +
1.239 + Edge addEdge(const Node& from, const Node& to) {
1.240 + Edge edge = Parent::addEdge(from, to);
1.241 + getNotifier(Edge()).add(edge);
1.242 + return edge;
1.243 + }
1.244 +
1.245 + void clear() {
1.246 + getNotifier(Edge()).clear();
1.247 + Parent::clear();
1.248 + }
1.249 +
1.250 + void erase(const Edge& edge) {
1.251 + getNotifier(Edge()).erase(edge);
1.252 + Parent::erase(edge);
1.253 + }
1.254 +
1.255 +
1.256 + ~EdgeSetExtender() {
1.257 + edge_notifier.clear();
1.258 + }
1.259 +
1.260 + };
1.261 +
1.262 +
1.263 + template <typename Base>
1.264 + class UEdgeSetExtender : public Base {
1.265 +
1.266 + public:
1.267 +
1.268 + typedef Base Parent;
1.269 + typedef UEdgeSetExtender Graph;
1.270 +
1.271 + typedef typename Parent::Node Node;
1.272 + typedef typename Parent::Edge Edge;
1.273 + typedef typename Parent::UEdge UEdge;
1.274 +
1.275 +
1.276 + int maxId(Node) const {
1.277 + return Parent::maxNodeId();
1.278 + }
1.279 +
1.280 + int maxId(Edge) const {
1.281 + return Parent::maxEdgeId();
1.282 + }
1.283 +
1.284 + int maxId(UEdge) const {
1.285 + return Parent::maxUEdgeId();
1.286 + }
1.287 +
1.288 + Node fromId(int id, Node) const {
1.289 + return Parent::nodeFromId(id);
1.290 + }
1.291 +
1.292 + Edge fromId(int id, Edge) const {
1.293 + return Parent::edgeFromId(id);
1.294 + }
1.295 +
1.296 + UEdge fromId(int id, UEdge) const {
1.297 + return Parent::uEdgeFromId(id);
1.298 + }
1.299 +
1.300 + Node oppositeNode(const Node &n, const UEdge &e) const {
1.301 + if( n == Parent::source(e))
1.302 + return Parent::target(e);
1.303 + else if( n == Parent::target(e))
1.304 + return Parent::source(e);
1.305 + else
1.306 + return INVALID;
1.307 + }
1.308 +
1.309 + Edge oppositeEdge(const Edge &e) const {
1.310 + return Parent::direct(e, !Parent::direction(e));
1.311 + }
1.312 +
1.313 + using Parent::direct;
1.314 + Edge direct(const UEdge &ue, const Node &s) const {
1.315 + return Parent::direct(ue, Parent::source(ue) == s);
1.316 + }
1.317 +
1.318 + typedef AlterationNotifier<Edge> EdgeNotifier;
1.319 + typedef AlterationNotifier<UEdge> UEdgeNotifier;
1.320 +
1.321 +
1.322 + protected:
1.323 +
1.324 + mutable EdgeNotifier edge_notifier;
1.325 + mutable UEdgeNotifier uedge_notifier;
1.326 +
1.327 + public:
1.328 +
1.329 + EdgeNotifier& getNotifier(Edge) const {
1.330 + return edge_notifier;
1.331 + }
1.332 +
1.333 + UEdgeNotifier& getNotifier(UEdge) const {
1.334 + return uedge_notifier;
1.335 + }
1.336 +
1.337 +
1.338 + class NodeIt : public Node {
1.339 + const Graph* graph;
1.340 + public:
1.341 +
1.342 + NodeIt() {}
1.343 +
1.344 + NodeIt(Invalid i) : Node(i) { }
1.345 +
1.346 + explicit NodeIt(const Graph& _graph) : graph(&_graph) {
1.347 + _graph.first(*static_cast<Node*>(this));
1.348 + }
1.349 +
1.350 + NodeIt(const Graph& _graph, const Node& node)
1.351 + : Node(node), graph(&_graph) {}
1.352 +
1.353 + NodeIt& operator++() {
1.354 + graph->next(*this);
1.355 + return *this;
1.356 + }
1.357 +
1.358 + };
1.359 +
1.360 +
1.361 + class EdgeIt : public Edge {
1.362 + const Graph* graph;
1.363 + public:
1.364 +
1.365 + EdgeIt() { }
1.366 +
1.367 + EdgeIt(Invalid i) : Edge(i) { }
1.368 +
1.369 + explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
1.370 + _graph.first(*static_cast<Edge*>(this));
1.371 + }
1.372 +
1.373 + EdgeIt(const Graph& _graph, const Edge& e) :
1.374 + Edge(e), graph(&_graph) { }
1.375 +
1.376 + EdgeIt& operator++() {
1.377 + graph->next(*this);
1.378 + return *this;
1.379 + }
1.380 +
1.381 + };
1.382 +
1.383 +
1.384 + class OutEdgeIt : public Edge {
1.385 + const Graph* graph;
1.386 + public:
1.387 +
1.388 + OutEdgeIt() { }
1.389 +
1.390 + OutEdgeIt(Invalid i) : Edge(i) { }
1.391 +
1.392 + OutEdgeIt(const Graph& _graph, const Node& node)
1.393 + : graph(&_graph) {
1.394 + _graph.firstOut(*this, node);
1.395 + }
1.396 +
1.397 + OutEdgeIt(const Graph& _graph, const Edge& edge)
1.398 + : Edge(edge), graph(&_graph) {}
1.399 +
1.400 + OutEdgeIt& operator++() {
1.401 + graph->nextOut(*this);
1.402 + return *this;
1.403 + }
1.404 +
1.405 + };
1.406 +
1.407 +
1.408 + class InEdgeIt : public Edge {
1.409 + const Graph* graph;
1.410 + public:
1.411 +
1.412 + InEdgeIt() { }
1.413 +
1.414 + InEdgeIt(Invalid i) : Edge(i) { }
1.415 +
1.416 + InEdgeIt(const Graph& _graph, const Node& node)
1.417 + : graph(&_graph) {
1.418 + _graph.firstIn(*this, node);
1.419 + }
1.420 +
1.421 + InEdgeIt(const Graph& _graph, const Edge& edge) :
1.422 + Edge(edge), graph(&_graph) {}
1.423 +
1.424 + InEdgeIt& operator++() {
1.425 + graph->nextIn(*this);
1.426 + return *this;
1.427 + }
1.428 +
1.429 + };
1.430 +
1.431 +
1.432 + class UEdgeIt : public Parent::UEdge {
1.433 + const Graph* graph;
1.434 + public:
1.435 +
1.436 + UEdgeIt() { }
1.437 +
1.438 + UEdgeIt(Invalid i) : UEdge(i) { }
1.439 +
1.440 + explicit UEdgeIt(const Graph& _graph) : graph(&_graph) {
1.441 + _graph.first(*static_cast<UEdge*>(this));
1.442 + }
1.443 +
1.444 + UEdgeIt(const Graph& _graph, const UEdge& e) :
1.445 + UEdge(e), graph(&_graph) { }
1.446 +
1.447 + UEdgeIt& operator++() {
1.448 + graph->next(*this);
1.449 + return *this;
1.450 + }
1.451 +
1.452 + };
1.453 +
1.454 + class IncEdgeIt : public Parent::UEdge {
1.455 + friend class UEdgeSetExtender;
1.456 + const Graph* graph;
1.457 + bool direction;
1.458 + public:
1.459 +
1.460 + IncEdgeIt() { }
1.461 +
1.462 + IncEdgeIt(Invalid i) : UEdge(i), direction(false) { }
1.463 +
1.464 + IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
1.465 + _graph.firstInc(*this, direction, n);
1.466 + }
1.467 +
1.468 + IncEdgeIt(const Graph& _graph, const UEdge &ue, const Node &n)
1.469 + : graph(&_graph), UEdge(ue) {
1.470 + direction = (_graph.source(ue) == n);
1.471 + }
1.472 +
1.473 + IncEdgeIt& operator++() {
1.474 + graph->nextInc(*this, direction);
1.475 + return *this;
1.476 + }
1.477 + };
1.478 +
1.479 + /// \brief Base node of the iterator
1.480 + ///
1.481 + /// Returns the base node (ie. the source in this case) of the iterator
1.482 + Node baseNode(const OutEdgeIt &e) const {
1.483 + return Parent::source((Edge)e);
1.484 + }
1.485 + /// \brief Running node of the iterator
1.486 + ///
1.487 + /// Returns the running node (ie. the target in this case) of the
1.488 + /// iterator
1.489 + Node runningNode(const OutEdgeIt &e) const {
1.490 + return Parent::target((Edge)e);
1.491 + }
1.492 +
1.493 + /// \brief Base node of the iterator
1.494 + ///
1.495 + /// Returns the base node (ie. the target in this case) of the iterator
1.496 + Node baseNode(const InEdgeIt &e) const {
1.497 + return Parent::target((Edge)e);
1.498 + }
1.499 + /// \brief Running node of the iterator
1.500 + ///
1.501 + /// Returns the running node (ie. the source in this case) of the
1.502 + /// iterator
1.503 + Node runningNode(const InEdgeIt &e) const {
1.504 + return Parent::source((Edge)e);
1.505 + }
1.506 +
1.507 + /// Base node of the iterator
1.508 + ///
1.509 + /// Returns the base node of the iterator
1.510 + Node baseNode(const IncEdgeIt &e) const {
1.511 + return e.direction ? source(e) : target(e);
1.512 + }
1.513 + /// Running node of the iterator
1.514 + ///
1.515 + /// Returns the running node of the iterator
1.516 + Node runningNode(const IncEdgeIt &e) const {
1.517 + return e.direction ? target(e) : source(e);
1.518 + }
1.519 +
1.520 +
1.521 + template <typename _Value>
1.522 + class EdgeMap
1.523 + : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
1.524 + public:
1.525 + typedef UEdgeSetExtender Graph;
1.526 + typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
1.527 +
1.528 + EdgeMap(const Graph& _g)
1.529 + : Parent(_g) {}
1.530 + EdgeMap(const Graph& _g, const _Value& _v)
1.531 + : Parent(_g, _v) {}
1.532 +
1.533 + EdgeMap& operator=(const EdgeMap& cmap) {
1.534 + return operator=<EdgeMap>(cmap);
1.535 + }
1.536 +
1.537 + template <typename CMap>
1.538 + EdgeMap& operator=(const CMap& cmap) {
1.539 + checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
1.540 + const typename Parent::Graph* graph = Parent::getGraph();
1.541 + Edge it;
1.542 + for (graph->first(it); it != INVALID; graph->next(it)) {
1.543 + Parent::set(it, cmap[it]);
1.544 + }
1.545 + return *this;
1.546 + }
1.547 + };
1.548 +
1.549 +
1.550 + template <typename _Value>
1.551 + class UEdgeMap
1.552 + : public IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > {
1.553 + public:
1.554 + typedef UEdgeSetExtender Graph;
1.555 + typedef IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > Parent;
1.556 +
1.557 + UEdgeMap(const Graph& _g)
1.558 + : Parent(_g) {}
1.559 + UEdgeMap(const Graph& _g, const _Value& _v)
1.560 + : Parent(_g, _v) {}
1.561 +
1.562 + UEdgeMap& operator=(const UEdgeMap& cmap) {
1.563 + return operator=<UEdgeMap>(cmap);
1.564 + }
1.565 +
1.566 + template <typename CMap>
1.567 + UEdgeMap& operator=(const CMap& cmap) {
1.568 + checkConcept<concept::ReadMap<UEdge, _Value>, CMap>();
1.569 + const typename Parent::Graph* graph = Parent::getGraph();
1.570 + UEdge it;
1.571 + for (graph->first(it); it != INVALID; graph->next(it)) {
1.572 + Parent::set(it, cmap[it]);
1.573 + }
1.574 + return *this;
1.575 + }
1.576 + };
1.577 +
1.578 +
1.579 + // Alteration extension
1.580 +
1.581 + UEdge addEdge(const Node& from, const Node& to) {
1.582 + UEdge uedge = Parent::addEdge(from, to);
1.583 + getNotifier(UEdge()).add(uedge);
1.584 + getNotifier(Edge()).add(Parent::direct(uedge, true));
1.585 + getNotifier(Edge()).add(Parent::direct(uedge, false));
1.586 + return uedge;
1.587 + }
1.588 +
1.589 + void clear() {
1.590 + getNotifier(Edge()).clear();
1.591 + getNotifier(UEdge()).clear();
1.592 + Parent::clear();
1.593 + }
1.594 +
1.595 + void erase(const UEdge& uedge) {
1.596 + getNotifier(Edge()).erase(Parent::direct(uedge, true));
1.597 + getNotifier(Edge()).erase(Parent::direct(uedge, false));
1.598 + getNotifier(UEdge()).erase(uedge);
1.599 + Parent::erase(uedge);
1.600 + }
1.601 +
1.602 +
1.603 + ~UEdgeSetExtender() {
1.604 + getNotifier(Edge()).clear();
1.605 + getNotifier(UEdge()).clear();
1.606 + }
1.607 +
1.608 + };
1.609 +}