COIN-OR::LEMON - Graph Library

Ticket #69: bpgraphs.patch

File bpgraphs.patch, 184.3 KB (added by Balazs Dezso, 13 years ago)

Bipartite graphs

  • lemon/Makefile.am

    # HG changeset patch
    # User Balazs Dezso <deba@inf.elte.hu>
    # Date 1289748931 -3600
    # Node ID 1a48ab5320b3edcd33a4733d8e3fbe8c1727b4ac
    # Parent  1937b6455b7d0cff4f1e21bdd7c5a66b4c1af1cf
    Add concepts for bipartite graphs
    
    diff -r 1937b6455b7d -r 1a48ab5320b3 lemon/Makefile.am
    a b  
    143143        lemon/bits/vector_map.h
    144144
    145145concept_HEADERS += \
     146        lemon/concepts/bpgraph.h \
    146147        lemon/concepts/digraph.h \
    147148        lemon/concepts/graph.h \
    148149        lemon/concepts/graph_components.h \
  • new file lemon/concepts/bpgraph.h

    diff -r 1937b6455b7d -r 1a48ab5320b3 lemon/concepts/bpgraph.h
    - +  
     1/* -*- mode: C++; indent-tabs-mode: nil; -*-
     2 *
     3 * This file is a part of LEMON, a generic C++ optimization library.
     4 *
     5 * Copyright (C) 2003-2010
     6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8 *
     9 * Permission to use, modify and distribute this software is granted
     10 * provided that this copyright notice appears in all copies. For
     11 * precise terms see the accompanying LICENSE file.
     12 *
     13 * This software is provided "AS IS" with no warranty of any kind,
     14 * express or implied, and with no claim as to its suitability for any
     15 * purpose.
     16 *
     17 */
     18
     19///\ingroup graph_concepts
     20///\file
     21///\brief The concept of undirected graphs.
     22
     23#ifndef LEMON_CONCEPTS_BPGRAPH_H
     24#define LEMON_CONCEPTS_BPGRAPH_H
     25
     26#include <lemon/concepts/graph_components.h>
     27#include <lemon/concepts/maps.h>
     28#include <lemon/concept_check.h>
     29#include <lemon/core.h>
     30
     31namespace lemon {
     32  namespace concepts {
     33
     34    /// \ingroup graph_concepts
     35    ///
     36    /// \brief Class describing the concept of undirected bipartite graphs.
     37    ///
     38    /// This class describes the common interface of all undirected
     39    /// bipartite graphs.
     40    ///
     41    /// Like all concept classes, it only provides an interface
     42    /// without any sensible implementation. So any general algorithm for
     43    /// undirected bipartite graphs should compile with this class,
     44    /// but it will not run properly, of course.
     45    /// An actual graph implementation like \ref ListBpGraph or
     46    /// \ref SmartBpGraph may have additional functionality.
     47    ///
     48    /// The bipartite graphs also fulfill the concept of \ref Graph
     49    /// "undirected graphs". Bipartite graphs provide a bipartition of
     50    /// the node set, namely a red and blue set of the nodes. The
     51    /// nodes can be iterated with the RedIt and BlueIt in the two
     52    /// node sets. With RedMap and BlueMap values can be assigned to
     53    /// the nodes in the two sets.
     54    ///
     55    /// The edges of the graph cannot connect two nodes of the same
     56    /// set. The edges inherent orientation is from the red nodes to
     57    /// the blue nodes.
     58    ///
     59    /// \sa Graph
     60    class BpGraph {
     61    private:
     62      /// BpGraphs are \e not copy constructible. Use bpGraphCopy instead.
     63      BpGraph(const BpGraph&) {}
     64      /// \brief Assignment of a graph to another one is \e not allowed.
     65      /// Use bpGraphCopy instead.
     66      void operator=(const BpGraph&) {}
     67
     68    public:
     69      /// Default constructor.
     70      BpGraph() {}
     71
     72      /// \brief Undirected graphs should be tagged with \c UndirectedTag.
     73      ///
     74      /// Undirected graphs should be tagged with \c UndirectedTag.
     75      ///
     76      /// This tag helps the \c enable_if technics to make compile time
     77      /// specializations for undirected graphs.
     78      typedef True UndirectedTag;
     79
     80      /// The node type of the graph
     81
     82      /// This class identifies a node of the graph. It also serves
     83      /// as a base class of the node iterators,
     84      /// thus they convert to this type.
     85      class Node {
     86      public:
     87        /// Default constructor
     88
     89        /// Default constructor.
     90        /// \warning It sets the object to an undefined value.
     91        Node() { }
     92        /// Copy constructor.
     93
     94        /// Copy constructor.
     95        ///
     96        Node(const Node&) { }
     97
     98        /// %Invalid constructor \& conversion.
     99
     100        /// Initializes the object to be invalid.
     101        /// \sa Invalid for more details.
     102        Node(Invalid) { }
     103        /// Equality operator
     104
     105        /// Equality operator.
     106        ///
     107        /// Two iterators are equal if and only if they point to the
     108        /// same object or both are \c INVALID.
     109        bool operator==(Node) const { return true; }
     110
     111        /// Inequality operator
     112
     113        /// Inequality operator.
     114        bool operator!=(Node) const { return true; }
     115
     116        /// Artificial ordering operator.
     117
     118        /// Artificial ordering operator.
     119        ///
     120        /// \note This operator only has to define some strict ordering of
     121        /// the items; this order has nothing to do with the iteration
     122        /// ordering of the items.
     123        bool operator<(Node) const { return false; }
     124
     125      };
     126
     127      /// Class to represent red nodes.
     128
     129      /// This class represents the red nodes of the graph. It does
     130      /// not supposed to be used directly, because the nodes can be
     131      /// represented as Node instances. This class can be used as
     132      /// template parameter for special map classes.
     133      class RedNode : public Node {
     134      public:
     135        /// Default constructor
     136
     137        /// Default constructor.
     138        /// \warning It sets the object to an undefined value.
     139        RedNode() { }
     140        /// Copy constructor.
     141
     142        /// Copy constructor.
     143        ///
     144        RedNode(const RedNode&) : Node() { }
     145
     146        /// %Invalid constructor \& conversion.
     147
     148        /// Initializes the object to be invalid.
     149        /// \sa Invalid for more details.
     150        RedNode(Invalid) { }
     151
     152        /// Constructor for conversion from a node.
     153
     154        /// Constructor for conversion from a node. The conversion can
     155        /// be invalid, since the Node can be member of the blue
     156        /// set.
     157        RedNode(const Node&) {}
     158      };
     159
     160      /// Class to represent blue nodes.
     161
     162      /// This class represents the blue nodes of the graph. It does
     163      /// not supposed to be used directly, because the nodes can be
     164      /// represented as Node instances. This class can be used as
     165      /// template parameter for special map classes.
     166      class BlueNode : public Node {
     167      public:
     168        /// Default constructor
     169
     170        /// Default constructor.
     171        /// \warning It sets the object to an undefined value.
     172        BlueNode() { }
     173        /// Copy constructor.
     174
     175        /// Copy constructor.
     176        ///
     177        BlueNode(const BlueNode&) : Node() { }
     178
     179        /// %Invalid constructor \& conversion.
     180
     181        /// Initializes the object to be invalid.
     182        /// \sa Invalid for more details.
     183        BlueNode(Invalid) { }
     184
     185        /// Constructor for conversion from a node.
     186
     187        /// Constructor for conversion from a node. The conversion can
     188        /// be invalid, since the Node can be member of the red
     189        /// set.
     190        BlueNode(const Node&) {}
     191      };
     192
     193      /// Iterator class for the red nodes.
     194
     195      /// This iterator goes through each red node of the graph.
     196      /// Its usage is quite simple, for example, you can count the number
     197      /// of red nodes in a graph \c g of type \c %BpGraph like this:
     198      ///\code
     199      /// int count=0;
     200      /// for (BpGraph::RedNodeIt n(g); n!=INVALID; ++n) ++count;
     201      ///\endcode
     202      class RedIt : public Node {
     203      public:
     204        /// Default constructor
     205
     206        /// Default constructor.
     207        /// \warning It sets the iterator to an undefined value.
     208        RedIt() { }
     209        /// Copy constructor.
     210
     211        /// Copy constructor.
     212        ///
     213        RedIt(const RedIt& n) : Node(n) { }
     214        /// %Invalid constructor \& conversion.
     215
     216        /// Initializes the iterator to be invalid.
     217        /// \sa Invalid for more details.
     218        RedIt(Invalid) { }
     219        /// Sets the iterator to the first red node.
     220
     221        /// Sets the iterator to the first red node of the given
     222        /// digraph.
     223        explicit RedIt(const BpGraph&) { }
     224        /// Sets the iterator to the given red node.
     225
     226        /// Sets the iterator to the given red node of the given
     227        /// digraph.
     228        RedIt(const BpGraph&, const Node&) { }
     229        /// Next node.
     230
     231        /// Assign the iterator to the next red node.
     232        ///
     233        RedIt& operator++() { return *this; }
     234      };
     235
     236      /// Iterator class for the blue nodes.
     237
     238      /// This iterator goes through each blue node of the graph.
     239      /// Its usage is quite simple, for example, you can count the number
     240      /// of blue nodes in a graph \c g of type \c %BpGraph like this:
     241      ///\code
     242      /// int count=0;
     243      /// for (BpGraph::BlueNodeIt n(g); n!=INVALID; ++n) ++count;
     244      ///\endcode
     245      class BlueIt : public Node {
     246      public:
     247        /// Default constructor
     248
     249        /// Default constructor.
     250        /// \warning It sets the iterator to an undefined value.
     251        BlueIt() { }
     252        /// Copy constructor.
     253
     254        /// Copy constructor.
     255        ///
     256        BlueIt(const BlueIt& n) : Node(n) { }
     257        /// %Invalid constructor \& conversion.
     258
     259        /// Initializes the iterator to be invalid.
     260        /// \sa Invalid for more details.
     261        BlueIt(Invalid) { }
     262        /// Sets the iterator to the first blue node.
     263
     264        /// Sets the iterator to the first blue node of the given
     265        /// digraph.
     266        explicit BlueIt(const BpGraph&) { }
     267        /// Sets the iterator to the given blue node.
     268
     269        /// Sets the iterator to the given blue node of the given
     270        /// digraph.
     271        BlueIt(const BpGraph&, const Node&) { }
     272        /// Next node.
     273
     274        /// Assign the iterator to the next blue node.
     275        ///
     276        BlueIt& operator++() { return *this; }
     277      };
     278
     279      /// Iterator class for the nodes.
     280
     281      /// This iterator goes through each node of the graph.
     282      /// Its usage is quite simple, for example, you can count the number
     283      /// of nodes in a graph \c g of type \c %BpGraph like this:
     284      ///\code
     285      /// int count=0;
     286      /// for (BpGraph::NodeIt n(g); n!=INVALID; ++n) ++count;
     287      ///\endcode
     288      class NodeIt : public Node {
     289      public:
     290        /// Default constructor
     291
     292        /// Default constructor.
     293        /// \warning It sets the iterator to an undefined value.
     294        NodeIt() { }
     295        /// Copy constructor.
     296
     297        /// Copy constructor.
     298        ///
     299        NodeIt(const NodeIt& n) : Node(n) { }
     300        /// %Invalid constructor \& conversion.
     301
     302        /// Initializes the iterator to be invalid.
     303        /// \sa Invalid for more details.
     304        NodeIt(Invalid) { }
     305        /// Sets the iterator to the first node.
     306
     307        /// Sets the iterator to the first node of the given digraph.
     308        ///
     309        explicit NodeIt(const BpGraph&) { }
     310        /// Sets the iterator to the given node.
     311
     312        /// Sets the iterator to the given node of the given digraph.
     313        ///
     314        NodeIt(const BpGraph&, const Node&) { }
     315        /// Next node.
     316
     317        /// Assign the iterator to the next node.
     318        ///
     319        NodeIt& operator++() { return *this; }
     320      };
     321
     322
     323      /// The edge type of the graph
     324
     325      /// This class identifies an edge of the graph. It also serves
     326      /// as a base class of the edge iterators,
     327      /// thus they will convert to this type.
     328      class Edge {
     329      public:
     330        /// Default constructor
     331
     332        /// Default constructor.
     333        /// \warning It sets the object to an undefined value.
     334        Edge() { }
     335        /// Copy constructor.
     336
     337        /// Copy constructor.
     338        ///
     339        Edge(const Edge&) { }
     340        /// %Invalid constructor \& conversion.
     341
     342        /// Initializes the object to be invalid.
     343        /// \sa Invalid for more details.
     344        Edge(Invalid) { }
     345        /// Equality operator
     346
     347        /// Equality operator.
     348        ///
     349        /// Two iterators are equal if and only if they point to the
     350        /// same object or both are \c INVALID.
     351        bool operator==(Edge) const { return true; }
     352        /// Inequality operator
     353
     354        /// Inequality operator.
     355        bool operator!=(Edge) const { return true; }
     356
     357        /// Artificial ordering operator.
     358
     359        /// Artificial ordering operator.
     360        ///
     361        /// \note This operator only has to define some strict ordering of
     362        /// the edges; this order has nothing to do with the iteration
     363        /// ordering of the edges.
     364        bool operator<(Edge) const { return false; }
     365      };
     366
     367      /// Iterator class for the edges.
     368
     369      /// This iterator goes through each edge of the graph.
     370      /// Its usage is quite simple, for example, you can count the number
     371      /// of edges in a graph \c g of type \c %BpGraph as follows:
     372      ///\code
     373      /// int count=0;
     374      /// for(BpGraph::EdgeIt e(g); e!=INVALID; ++e) ++count;
     375      ///\endcode
     376      class EdgeIt : public Edge {
     377      public:
     378        /// Default constructor
     379
     380        /// Default constructor.
     381        /// \warning It sets the iterator to an undefined value.
     382        EdgeIt() { }
     383        /// Copy constructor.
     384
     385        /// Copy constructor.
     386        ///
     387        EdgeIt(const EdgeIt& e) : Edge(e) { }
     388        /// %Invalid constructor \& conversion.
     389
     390        /// Initializes the iterator to be invalid.
     391        /// \sa Invalid for more details.
     392        EdgeIt(Invalid) { }
     393        /// Sets the iterator to the first edge.
     394
     395        /// Sets the iterator to the first edge of the given graph.
     396        ///
     397        explicit EdgeIt(const BpGraph&) { }
     398        /// Sets the iterator to the given edge.
     399
     400        /// Sets the iterator to the given edge of the given graph.
     401        ///
     402        EdgeIt(const BpGraph&, const Edge&) { }
     403        /// Next edge
     404
     405        /// Assign the iterator to the next edge.
     406        ///
     407        EdgeIt& operator++() { return *this; }
     408      };
     409
     410      /// Iterator class for the incident edges of a node.
     411
     412      /// This iterator goes trough the incident undirected edges
     413      /// of a certain node of a graph.
     414      /// Its usage is quite simple, for example, you can compute the
     415      /// degree (i.e. the number of incident edges) of a node \c n
     416      /// in a graph \c g of type \c %BpGraph as follows.
     417      ///
     418      ///\code
     419      /// int count=0;
     420      /// for(BpGraph::IncEdgeIt e(g, n); e!=INVALID; ++e) ++count;
     421      ///\endcode
     422      ///
     423      /// \warning Loop edges will be iterated twice.
     424      class IncEdgeIt : public Edge {
     425      public:
     426        /// Default constructor
     427
     428        /// Default constructor.
     429        /// \warning It sets the iterator to an undefined value.
     430        IncEdgeIt() { }
     431        /// Copy constructor.
     432
     433        /// Copy constructor.
     434        ///
     435        IncEdgeIt(const IncEdgeIt& e) : Edge(e) { }
     436        /// %Invalid constructor \& conversion.
     437
     438        /// Initializes the iterator to be invalid.
     439        /// \sa Invalid for more details.
     440        IncEdgeIt(Invalid) { }
     441        /// Sets the iterator to the first incident edge.
     442
     443        /// Sets the iterator to the first incident edge of the given node.
     444        ///
     445        IncEdgeIt(const BpGraph&, const Node&) { }
     446        /// Sets the iterator to the given edge.
     447
     448        /// Sets the iterator to the given edge of the given graph.
     449        ///
     450        IncEdgeIt(const BpGraph&, const Edge&) { }
     451        /// Next incident edge
     452
     453        /// Assign the iterator to the next incident edge
     454        /// of the corresponding node.
     455        IncEdgeIt& operator++() { return *this; }
     456      };
     457
     458      /// The arc type of the graph
     459
     460      /// This class identifies a directed arc of the graph. It also serves
     461      /// as a base class of the arc iterators,
     462      /// thus they will convert to this type.
     463      class Arc {
     464      public:
     465        /// Default constructor
     466
     467        /// Default constructor.
     468        /// \warning It sets the object to an undefined value.
     469        Arc() { }
     470        /// Copy constructor.
     471
     472        /// Copy constructor.
     473        ///
     474        Arc(const Arc&) { }
     475        /// %Invalid constructor \& conversion.
     476
     477        /// Initializes the object to be invalid.
     478        /// \sa Invalid for more details.
     479        Arc(Invalid) { }
     480        /// Equality operator
     481
     482        /// Equality operator.
     483        ///
     484        /// Two iterators are equal if and only if they point to the
     485        /// same object or both are \c INVALID.
     486        bool operator==(Arc) const { return true; }
     487        /// Inequality operator
     488
     489        /// Inequality operator.
     490        bool operator!=(Arc) const { return true; }
     491
     492        /// Artificial ordering operator.
     493
     494        /// Artificial ordering operator.
     495        ///
     496        /// \note This operator only has to define some strict ordering of
     497        /// the arcs; this order has nothing to do with the iteration
     498        /// ordering of the arcs.
     499        bool operator<(Arc) const { return false; }
     500
     501        /// Converison to \c Edge
     502
     503        /// Converison to \c Edge.
     504        ///
     505        operator Edge() const { return Edge(); }
     506      };
     507
     508      /// Iterator class for the arcs.
     509
     510      /// This iterator goes through each directed arc of the graph.
     511      /// Its usage is quite simple, for example, you can count the number
     512      /// of arcs in a graph \c g of type \c %BpGraph as follows:
     513      ///\code
     514      /// int count=0;
     515      /// for(BpGraph::ArcIt a(g); a!=INVALID; ++a) ++count;
     516      ///\endcode
     517      class ArcIt : public Arc {
     518      public:
     519        /// Default constructor
     520
     521        /// Default constructor.
     522        /// \warning It sets the iterator to an undefined value.
     523        ArcIt() { }
     524        /// Copy constructor.
     525
     526        /// Copy constructor.
     527        ///
     528        ArcIt(const ArcIt& e) : Arc(e) { }
     529        /// %Invalid constructor \& conversion.
     530
     531        /// Initializes the iterator to be invalid.
     532        /// \sa Invalid for more details.
     533        ArcIt(Invalid) { }
     534        /// Sets the iterator to the first arc.
     535
     536        /// Sets the iterator to the first arc of the given graph.
     537        ///
     538        explicit ArcIt(const BpGraph &g) { ignore_unused_variable_warning(g); }
     539        /// Sets the iterator to the given arc.
     540
     541        /// Sets the iterator to the given arc of the given graph.
     542        ///
     543        ArcIt(const BpGraph&, const Arc&) { }
     544        /// Next arc
     545
     546        /// Assign the iterator to the next arc.
     547        ///
     548        ArcIt& operator++() { return *this; }
     549      };
     550
     551      /// Iterator class for the outgoing arcs of a node.
     552
     553      /// This iterator goes trough the \e outgoing directed arcs of a
     554      /// certain node of a graph.
     555      /// Its usage is quite simple, for example, you can count the number
     556      /// of outgoing arcs of a node \c n
     557      /// in a graph \c g of type \c %BpGraph as follows.
     558      ///\code
     559      /// int count=0;
     560      /// for (Digraph::OutArcIt a(g, n); a!=INVALID; ++a) ++count;
     561      ///\endcode
     562      class OutArcIt : public Arc {
     563      public:
     564        /// Default constructor
     565
     566        /// Default constructor.
     567        /// \warning It sets the iterator to an undefined value.
     568        OutArcIt() { }
     569        /// Copy constructor.
     570
     571        /// Copy constructor.
     572        ///
     573        OutArcIt(const OutArcIt& e) : Arc(e) { }
     574        /// %Invalid constructor \& conversion.
     575
     576        /// Initializes the iterator to be invalid.
     577        /// \sa Invalid for more details.
     578        OutArcIt(Invalid) { }
     579        /// Sets the iterator to the first outgoing arc.
     580
     581        /// Sets the iterator to the first outgoing arc of the given node.
     582        ///
     583        OutArcIt(const BpGraph& n, const Node& g) {
     584          ignore_unused_variable_warning(n);
     585          ignore_unused_variable_warning(g);
     586        }
     587        /// Sets the iterator to the given arc.
     588
     589        /// Sets the iterator to the given arc of the given graph.
     590        ///
     591        OutArcIt(const BpGraph&, const Arc&) { }
     592        /// Next outgoing arc
     593
     594        /// Assign the iterator to the next
     595        /// outgoing arc of the corresponding node.
     596        OutArcIt& operator++() { return *this; }
     597      };
     598
     599      /// Iterator class for the incoming arcs of a node.
     600
     601      /// This iterator goes trough the \e incoming directed arcs of a
     602      /// certain node of a graph.
     603      /// Its usage is quite simple, for example, you can count the number
     604      /// of incoming arcs of a node \c n
     605      /// in a graph \c g of type \c %BpGraph as follows.
     606      ///\code
     607      /// int count=0;
     608      /// for (Digraph::InArcIt a(g, n); a!=INVALID; ++a) ++count;
     609      ///\endcode
     610      class InArcIt : public Arc {
     611      public:
     612        /// Default constructor
     613
     614        /// Default constructor.
     615        /// \warning It sets the iterator to an undefined value.
     616        InArcIt() { }
     617        /// Copy constructor.
     618
     619        /// Copy constructor.
     620        ///
     621        InArcIt(const InArcIt& e) : Arc(e) { }
     622        /// %Invalid constructor \& conversion.
     623
     624        /// Initializes the iterator to be invalid.
     625        /// \sa Invalid for more details.
     626        InArcIt(Invalid) { }
     627        /// Sets the iterator to the first incoming arc.
     628
     629        /// Sets the iterator to the first incoming arc of the given node.
     630        ///
     631        InArcIt(const BpGraph& g, const Node& n) {
     632          ignore_unused_variable_warning(n);
     633          ignore_unused_variable_warning(g);
     634        }
     635        /// Sets the iterator to the given arc.
     636
     637        /// Sets the iterator to the given arc of the given graph.
     638        ///
     639        InArcIt(const BpGraph&, const Arc&) { }
     640        /// Next incoming arc
     641
     642        /// Assign the iterator to the next
     643        /// incoming arc of the corresponding node.
     644        InArcIt& operator++() { return *this; }
     645      };
     646
     647      /// \brief Standard graph map type for the nodes.
     648      ///
     649      /// Standard graph map type for the nodes.
     650      /// It conforms to the ReferenceMap concept.
     651      template<class T>
     652      class NodeMap : public ReferenceMap<Node, T, T&, const T&>
     653      {
     654      public:
     655
     656        /// Constructor
     657        explicit NodeMap(const BpGraph&) { }
     658        /// Constructor with given initial value
     659        NodeMap(const BpGraph&, T) { }
     660
     661      private:
     662        ///Copy constructor
     663        NodeMap(const NodeMap& nm) :
     664          ReferenceMap<Node, T, T&, const T&>(nm) { }
     665        ///Assignment operator
     666        template <typename CMap>
     667        NodeMap& operator=(const CMap&) {
     668          checkConcept<ReadMap<Node, T>, CMap>();
     669          return *this;
     670        }
     671      };
     672
     673      /// \brief Standard graph map type for the red nodes.
     674      ///
     675      /// Standard graph map type for the red nodes.
     676      /// It conforms to the ReferenceMap concept.
     677      template<class T>
     678      class RedMap : public ReferenceMap<Node, T, T&, const T&>
     679      {
     680      public:
     681
     682        /// Constructor
     683        explicit RedMap(const BpGraph&) { }
     684        /// Constructor with given initial value
     685        RedMap(const BpGraph&, T) { }
     686
     687      private:
     688        ///Copy constructor
     689        RedMap(const RedMap& nm) :
     690          ReferenceMap<Node, T, T&, const T&>(nm) { }
     691        ///Assignment operator
     692        template <typename CMap>
     693        RedMap& operator=(const CMap&) {
     694          checkConcept<ReadMap<Node, T>, CMap>();
     695          return *this;
     696        }
     697      };
     698
     699      /// \brief Standard graph map type for the blue nodes.
     700      ///
     701      /// Standard graph map type for the blue nodes.
     702      /// It conforms to the ReferenceMap concept.
     703      template<class T>
     704      class BlueMap : public ReferenceMap<Node, T, T&, const T&>
     705      {
     706      public:
     707
     708        /// Constructor
     709        explicit BlueMap(const BpGraph&) { }
     710        /// Constructor with given initial value
     711        BlueMap(const BpGraph&, T) { }
     712
     713      private:
     714        ///Copy constructor
     715        BlueMap(const BlueMap& nm) :
     716          ReferenceMap<Node, T, T&, const T&>(nm) { }
     717        ///Assignment operator
     718        template <typename CMap>
     719        BlueMap& operator=(const CMap&) {
     720          checkConcept<ReadMap<Node, T>, CMap>();
     721          return *this;
     722        }
     723      };
     724
     725      /// \brief Standard graph map type for the arcs.
     726      ///
     727      /// Standard graph map type for the arcs.
     728      /// It conforms to the ReferenceMap concept.
     729      template<class T>
     730      class ArcMap : public ReferenceMap<Arc, T, T&, const T&>
     731      {
     732      public:
     733
     734        /// Constructor
     735        explicit ArcMap(const BpGraph&) { }
     736        /// Constructor with given initial value
     737        ArcMap(const BpGraph&, T) { }
     738
     739      private:
     740        ///Copy constructor
     741        ArcMap(const ArcMap& em) :
     742          ReferenceMap<Arc, T, T&, const T&>(em) { }
     743        ///Assignment operator
     744        template <typename CMap>
     745        ArcMap& operator=(const CMap&) {
     746          checkConcept<ReadMap<Arc, T>, CMap>();
     747          return *this;
     748        }
     749      };
     750
     751      /// \brief Standard graph map type for the edges.
     752      ///
     753      /// Standard graph map type for the edges.
     754      /// It conforms to the ReferenceMap concept.
     755      template<class T>
     756      class EdgeMap : public ReferenceMap<Edge, T, T&, const T&>
     757      {
     758      public:
     759
     760        /// Constructor
     761        explicit EdgeMap(const BpGraph&) { }
     762        /// Constructor with given initial value
     763        EdgeMap(const BpGraph&, T) { }
     764
     765      private:
     766        ///Copy constructor
     767        EdgeMap(const EdgeMap& em) :
     768          ReferenceMap<Edge, T, T&, const T&>(em) {}
     769        ///Assignment operator
     770        template <typename CMap>
     771        EdgeMap& operator=(const CMap&) {
     772          checkConcept<ReadMap<Edge, T>, CMap>();
     773          return *this;
     774        }
     775      };
     776
     777      /// \brief Gives back %true for red nodes.
     778      ///
     779      /// Gives back %true for red nodes.
     780      bool red(const Node&) const { return true; }
     781
     782      /// \brief Gives back %true for blue nodes.
     783      ///
     784      /// Gives back %true for blue nodes.
     785      bool blue(const Node&) const { return true; }
     786
     787      /// \brief Gives back the red end node of the edge.
     788      ///
     789      /// Gives back the red end node of the edge.
     790      Node redNode(const Edge&) const { return Node(); }
     791
     792      /// \brief Gives back the blue end node of the edge.
     793      ///
     794      /// Gives back the blue end node of the edge.
     795      Node blueNode(const Edge&) const { return Node(); }
     796
     797      /// \brief The first node of the edge.
     798      ///
     799      /// It is a synonim for the \c redNode().
     800      Node u(Edge) const { return INVALID; }
     801
     802      /// \brief The second node of the edge.
     803      ///
     804      /// It is a synonim for the \c blueNode().
     805      Node v(Edge) const { return INVALID; }
     806
     807      /// \brief The source node of the arc.
     808      ///
     809      /// Returns the source node of the given arc.
     810      Node source(Arc) const { return INVALID; }
     811
     812      /// \brief The target node of the arc.
     813      ///
     814      /// Returns the target node of the given arc.
     815      Node target(Arc) const { return INVALID; }
     816
     817      /// \brief The ID of the node.
     818      ///
     819      /// Returns the ID of the given node.
     820      int id(Node) const { return -1; }
     821
     822      /// \brief The red ID of the node.
     823      ///
     824      /// Returns the red ID of the given node.
     825      int redId(Node) const { return -1; }
     826
     827      /// \brief The red ID of the node.
     828      ///
     829      /// Returns the red ID of the given node.
     830      int id(RedNode) const { return -1; }
     831
     832      /// \brief The blue ID of the node.
     833      ///
     834      /// Returns the blue ID of the given node.
     835      int blueId(Node) const { return -1; }
     836
     837      /// \brief The blue ID of the node.
     838      ///
     839      /// Returns the blue ID of the given node.
     840      int id(BlueNode) const { return -1; }
     841
     842      /// \brief The ID of the edge.
     843      ///
     844      /// Returns the ID of the given edge.
     845      int id(Edge) const { return -1; }
     846
     847      /// \brief The ID of the arc.
     848      ///
     849      /// Returns the ID of the given arc.
     850      int id(Arc) const { return -1; }
     851
     852      /// \brief The node with the given ID.
     853      ///
     854      /// Returns the node with the given ID.
     855      /// \pre The argument should be a valid node ID in the graph.
     856      Node nodeFromId(int) const { return INVALID; }
     857
     858      /// \brief The edge with the given ID.
     859      ///
     860      /// Returns the edge with the given ID.
     861      /// \pre The argument should be a valid edge ID in the graph.
     862      Edge edgeFromId(int) const { return INVALID; }
     863
     864      /// \brief The arc with the given ID.
     865      ///
     866      /// Returns the arc with the given ID.
     867      /// \pre The argument should be a valid arc ID in the graph.
     868      Arc arcFromId(int) const { return INVALID; }
     869
     870      /// \brief An upper bound on the node IDs.
     871      ///
     872      /// Returns an upper bound on the node IDs.
     873      int maxNodeId() const { return -1; }
     874
     875      /// \brief An upper bound on the red IDs.
     876      ///
     877      /// Returns an upper bound on the red IDs.
     878      int maxRedId() const { return -1; }
     879
     880      /// \brief An upper bound on the blue IDs.
     881      ///
     882      /// Returns an upper bound on the blue IDs.
     883      int maxBlueId() const { return -1; }
     884
     885      /// \brief An upper bound on the edge IDs.
     886      ///
     887      /// Returns an upper bound on the edge IDs.
     888      int maxEdgeId() const { return -1; }
     889
     890      /// \brief An upper bound on the arc IDs.
     891      ///
     892      /// Returns an upper bound on the arc IDs.
     893      int maxArcId() const { return -1; }
     894
     895      /// \brief The direction of the arc.
     896      ///
     897      /// Returns \c true if the given arc goes from a red node to a blue node.
     898      bool direction(Arc) const { return true; }
     899
     900      /// \brief Direct the edge.
     901      ///
     902      /// Direct the given edge. The returned arc
     903      /// represents the given edge and its direction comes
     904      /// from the bool parameter. If it is \c true, then the source of the node
     905      /// will be a red node.
     906      Arc direct(Edge, bool) const {
     907        return INVALID;
     908      }
     909
     910      /// \brief Direct the edge.
     911      ///
     912      /// Direct the given edge. The returned arc represents the given
     913      /// edge and its source node is the given node.
     914      Arc direct(Edge, Node) const {
     915        return INVALID;
     916      }
     917
     918      /// \brief The oppositely directed arc.
     919      ///
     920      /// Returns the oppositely directed arc representing the same edge.
     921      Arc oppositeArc(Arc) const { return INVALID; }
     922
     923      /// \brief The opposite node on the edge.
     924      ///
     925      /// Returns the opposite node on the given edge.
     926      Node oppositeNode(Node, Edge) const { return INVALID; }
     927
     928      void first(Node&) const {}
     929      void next(Node&) const {}
     930
     931      void firstRed(Node&) const {}
     932      void nextRed(Node&) const {}
     933
     934      void firstBlue(Node&) const {}
     935      void nextBlue(Node&) const {}
     936
     937      void first(Edge&) const {}
     938      void next(Edge&) const {}
     939
     940      void first(Arc&) const {}
     941      void next(Arc&) const {}
     942
     943      void firstOut(Arc&, Node) const {}
     944      void nextOut(Arc&) const {}
     945
     946      void firstIn(Arc&, Node) const {}
     947      void nextIn(Arc&) const {}
     948
     949      void firstInc(Edge &, bool &, const Node &) const {}
     950      void nextInc(Edge &, bool &) const {}
     951
     952      // The second parameter is dummy.
     953      Node fromId(int, Node) const { return INVALID; }
     954      // The second parameter is dummy.
     955      Edge fromId(int, Edge) const { return INVALID; }
     956      // The second parameter is dummy.
     957      Arc fromId(int, Arc) const { return INVALID; }
     958
     959      // Dummy parameter.
     960      int maxId(Node) const { return -1; }
     961      // Dummy parameter.
     962      int maxId(RedNode) const { return -1; }
     963      // Dummy parameter.
     964      int maxId(BlueNode) const { return -1; }
     965      // Dummy parameter.
     966      int maxId(Edge) const { return -1; }
     967      // Dummy parameter.
     968      int maxId(Arc) const { return -1; }
     969
     970      /// \brief The base node of the iterator.
     971      ///
     972      /// Returns the base node of the given incident edge iterator.
     973      Node baseNode(IncEdgeIt) const { return INVALID; }
     974
     975      /// \brief The running node of the iterator.
     976      ///
     977      /// Returns the running node of the given incident edge iterator.
     978      Node runningNode(IncEdgeIt) const { return INVALID; }
     979
     980      /// \brief The base node of the iterator.
     981      ///
     982      /// Returns the base node of the given outgoing arc iterator
     983      /// (i.e. the source node of the corresponding arc).
     984      Node baseNode(OutArcIt) const { return INVALID; }
     985
     986      /// \brief The running node of the iterator.
     987      ///
     988      /// Returns the running node of the given outgoing arc iterator
     989      /// (i.e. the target node of the corresponding arc).
     990      Node runningNode(OutArcIt) const { return INVALID; }
     991
     992      /// \brief The base node of the iterator.
     993      ///
     994      /// Returns the base node of the given incomming arc iterator
     995      /// (i.e. the target node of the corresponding arc).
     996      Node baseNode(InArcIt) const { return INVALID; }
     997
     998      /// \brief The running node of the iterator.
     999      ///
     1000      /// Returns the running node of the given incomming arc iterator
     1001      /// (i.e. the source node of the corresponding arc).
     1002      Node runningNode(InArcIt) const { return INVALID; }
     1003
     1004      template <typename _BpGraph>
     1005      struct Constraints {
     1006        void constraints() {
     1007          checkConcept<BaseBpGraphComponent, _BpGraph>();
     1008          checkConcept<IterableBpGraphComponent<>, _BpGraph>();
     1009          checkConcept<IDableBpGraphComponent<>, _BpGraph>();
     1010          checkConcept<MappableBpGraphComponent<>, _BpGraph>();
     1011        }
     1012      };
     1013
     1014    };
     1015
     1016  }
     1017
     1018}
     1019
     1020#endif
  • lemon/concepts/graph.h

    diff -r 1937b6455b7d -r 1a48ab5320b3 lemon/concepts/graph.h
    a b  
    7272    /// \sa Digraph
    7373    class Graph {
    7474    private:
    75       /// Graphs are \e not copy constructible. Use DigraphCopy instead.
     75      /// Graphs are \e not copy constructible. Use GraphCopy instead.
    7676      Graph(const Graph&) {}
    7777      /// \brief Assignment of a graph to another one is \e not allowed.
    78       /// Use DigraphCopy instead.
     78      /// Use GraphCopy instead.
    7979      void operator=(const Graph&) {}
    8080
    8181    public:
  • lemon/concepts/graph_components.h

    diff -r 1937b6455b7d -r 1a48ab5320b3 lemon/concepts/graph_components.h
    a b  
    294294
    295295    };
    296296
     297    /// \brief Base skeleton class for undirected bipartite graphs.
     298    ///
     299    /// This class describes the base interface of undirected
     300    /// bipartite graph types.  All bipartite graph %concepts have to
     301    /// conform to this class.  It extends the interface of \ref
     302    /// BaseGraphComponent with an \c Edge type and functions to get
     303    /// the end nodes of edges, to convert from arcs to edges and to
     304    /// get both direction of edges.
     305    class BaseBpGraphComponent : public BaseGraphComponent {
     306    public:
     307
     308      typedef BaseBpGraphComponent BpGraph;
     309
     310      typedef BaseDigraphComponent::Node Node;
     311      typedef BaseDigraphComponent::Arc Arc;
     312
     313      /// \brief Class to represent red nodes.
     314      ///
     315      /// This class represents the red nodes of the graph. It does
     316      /// not supposed to be used directly, because the nodes can be
     317      /// represented as Node instances. This class can be used as
     318      /// template parameter for special map classes.
     319      class RedNode : public Node {
     320        typedef Node Parent;
     321
     322      public:
     323        /// \brief Default constructor.
     324        ///
     325        /// Default constructor.
     326        /// \warning The default constructor is not required to set
     327        /// the item to some well-defined value. So you should consider it
     328        /// as uninitialized.
     329        RedNode() {}
     330
     331        /// \brief Copy constructor.
     332        ///
     333        /// Copy constructor.
     334        RedNode(const RedNode &) : Parent() {}
     335
     336        /// \brief Constructor for conversion from \c INVALID.
     337        ///
     338        /// Constructor for conversion from \c INVALID.
     339        /// It initializes the item to be invalid.
     340        /// \sa Invalid for more details.
     341        RedNode(Invalid) {}
     342
     343        /// \brief Constructor for conversion from a node.
     344        ///
     345        /// Constructor for conversion from a node. The conversion can
     346        /// be invalid, since the Node can be member of the blue
     347        /// set.
     348        RedNode(const Node&) {}
     349      };
     350
     351      /// \brief Class to represent blue nodes.
     352      ///
     353      /// This class represents the blue nodes of the graph. It does
     354      /// not supposed to be used directly, because the nodes can be
     355      /// represented as Node instances. This class can be used as
     356      /// template parameter for special map classes.
     357      class BlueNode : public Node {
     358        typedef Node Parent;
     359
     360      public:
     361        /// \brief Default constructor.
     362        ///
     363        /// Default constructor.
     364        /// \warning The default constructor is not required to set
     365        /// the item to some well-defined value. So you should consider it
     366        /// as uninitialized.
     367        BlueNode() {}
     368
     369        /// \brief Copy constructor.
     370        ///
     371        /// Copy constructor.
     372        BlueNode(const BlueNode &) : Parent() {}
     373
     374        /// \brief Constructor for conversion from \c INVALID.
     375        ///
     376        /// Constructor for conversion from \c INVALID.
     377        /// It initializes the item to be invalid.
     378        /// \sa Invalid for more details.
     379        BlueNode(Invalid) {}
     380
     381        /// \brief Constructor for conversion from a node.
     382        ///
     383        /// Constructor for conversion from a node. The conversion can
     384        /// be invalid, since the Node can be member of the red
     385        /// set.
     386        BlueNode(const Node&) {}
     387      };
     388
     389      /// \brief Gives back %true for red nodes.
     390      ///
     391      /// Gives back %true for red nodes.
     392      bool red(const Node&) const { return true; }
     393
     394      /// \brief Gives back %true for blue nodes.
     395      ///
     396      /// Gives back %true for blue nodes.
     397      bool blue(const Node&) const { return true; }
     398
     399      /// \brief Gives back the red end node of the edge.
     400      ///
     401      /// Gives back the red end node of the edge.
     402      Node redNode(const Edge&) const { return Node(); }
     403
     404      /// \brief Gives back the blue end node of the edge.
     405      ///
     406      /// Gives back the blue end node of the edge.
     407      Node blueNode(const Edge&) const { return Node(); }
     408
     409      template <typename _BpGraph>
     410      struct Constraints {
     411        typedef typename _BpGraph::Node Node;
     412        typedef typename _BpGraph::RedNode RedNode;
     413        typedef typename _BpGraph::BlueNode BlueNode;
     414        typedef typename _BpGraph::Arc Arc;
     415        typedef typename _BpGraph::Edge Edge;
     416
     417        void constraints() {
     418          checkConcept<BaseGraphComponent, _BpGraph>();
     419          checkConcept<GraphItem<'n'>, RedNode>();
     420          checkConcept<GraphItem<'n'>, BlueNode>();
     421          {
     422            Node n;
     423            RedNode rn = n;
     424            BlueNode bn = bn;
     425            Edge e;
     426            bool b;
     427            b = bpgraph.red(n);
     428            b = bpgraph.blue(n);
     429            n = bpgraph.redNode(e);
     430            n = bpgraph.blueNode(e);
     431            rn = n;
     432            bn = n;
     433          }
     434        }
     435
     436        const _BpGraph& bpgraph;
     437      };
     438
     439    };
     440
    297441    /// \brief Skeleton class for \e idable directed graphs.
    298442    ///
    299443    /// This class describes the interface of \e idable directed graphs.
     
    424568      };
    425569    };
    426570
     571    /// \brief Skeleton class for \e idable undirected bipartite graphs.
     572    ///
     573    /// This class describes the interface of \e idable undirected
     574    /// bipartite graphs. It extends \ref IDableGraphComponent with
     575    /// the core ID functions of undirected bipartite graphs. Beside
     576    /// the regular node ids, this class also provides ids within the
     577    /// the red and blue sets of the nodes. This concept is part of
     578    /// the BpGraph concept.
     579    template <typename BAS = BaseBpGraphComponent>
     580    class IDableBpGraphComponent : public IDableGraphComponent<BAS> {
     581    public:
     582
     583      typedef BAS Base;
     584      typedef IDableGraphComponent<BAS> Parent;
     585      typedef typename Base::Node Node;
     586      typedef typename Base::RedNode RedNode;
     587      typedef typename Base::BlueNode BlueNode;
     588
     589      using Parent::id;
     590
     591      /// \brief Return a unique integer id for the given node in the red set.
     592      ///
     593      /// Return a unique integer id for the given node in the red set.
     594      int redId(const Node&) const { return -1; }
     595
     596      /// \brief Return the same value as redId().
     597      ///
     598      /// Return the same value as redId().
     599      int id(const RedNode&) const { return -1; }
     600
     601      /// \brief Return a unique integer id for the given node in the blue set.
     602      ///
     603      /// Return a unique integer id for the given node in the blue set.
     604      int blueId(const Node&) const { return -1; }
     605
     606      /// \brief Return the same value as blueId().
     607      ///
     608      /// Return the same value as blueId().
     609      int id(const BlueNode&) const { return -1; }
     610
     611      /// \brief Return an integer greater or equal to the maximum
     612      /// node id in the red set.
     613      ///
     614      /// Return an integer greater or equal to the maximum
     615      /// node id in the red set.
     616      int maxRedId() const { return -1; }
     617
     618      /// \brief Return an integer greater or equal to the maximum
     619      /// node id in the blue set.
     620      ///
     621      /// Return an integer greater or equal to the maximum
     622      /// node id in the blue set.
     623      int maxBlueId() const { return -1; }
     624
     625      template <typename _BpGraph>
     626      struct Constraints {
     627
     628        void constraints() {
     629          checkConcept<IDableGraphComponent<Base>, _BpGraph>();
     630          typename _BpGraph::Node node;
     631          typename _BpGraph::RedNode red;
     632          typename _BpGraph::BlueNode blue;
     633          int rid = bpgraph.redId(node);
     634          int bid = bpgraph.blueId(node);
     635          rid = bpgraph.id(red);
     636          bid = bpgraph.id(blue);
     637          rid = bpgraph.maxRedId();
     638          bid = bpgraph.maxBlueId();
     639          ignore_unused_variable_warning(rid);
     640          ignore_unused_variable_warning(bid);
     641        }
     642
     643        const _BpGraph& bpgraph;
     644      };
     645    };
     646
    427647    /// \brief Concept class for \c NodeIt, \c ArcIt and \c EdgeIt types.
    428648    ///
    429649    /// This class describes the concept of \c NodeIt, \c ArcIt and
     
    8891109      };
    8901110    };
    8911111
     1112    /// \brief Skeleton class for iterable undirected bipartite graphs.
     1113    ///
     1114    /// This class describes the interface of iterable undirected
     1115    /// bipartite graphs. It extends \ref IterableGraphComponent with
     1116    /// the core iterable interface of undirected bipartite graphs.
     1117    /// This concept is part of the BpGraph concept.
     1118    template <typename BAS = BaseBpGraphComponent>
     1119    class IterableBpGraphComponent : public IterableGraphComponent<BAS> {
     1120    public:
     1121
     1122      typedef BAS Base;
     1123      typedef typename Base::Node Node;
     1124      typedef typename Base::Arc Arc;
     1125      typedef typename Base::Edge Edge;
     1126
     1127
     1128      typedef IterableBpGraphComponent BpGraph;
     1129
     1130      /// \name Base Iteration
     1131      ///
     1132      /// This interface provides functions for iteration on red and blue nodes.
     1133      ///
     1134      /// @{
     1135
     1136      /// \brief Return the first red node.
     1137      ///
     1138      /// This function gives back the first red node in the iteration order.
     1139      void firstRed(Node&) const {}
     1140
     1141      /// \brief Return the next red node.
     1142      ///
     1143      /// This function gives back the next red node in the iteration order.
     1144      void nextRed(Node&) const {}
     1145
     1146      /// \brief Return the first blue node.
     1147      ///
     1148      /// This function gives back the first blue node in the iteration order.
     1149      void firstBlue(Node&) const {}
     1150
     1151      /// \brief Return the next blue node.
     1152      ///
     1153      /// This function gives back the next blue node in the iteration order.
     1154      void nextBlue(Node&) const {}
     1155
     1156
     1157      /// @}
     1158
     1159      /// \name Class Based Iteration
     1160      ///
     1161      /// This interface provides iterator classes for red and blue nodes.
     1162      ///
     1163      /// @{
     1164
     1165      /// \brief This iterator goes through each red node.
     1166      ///
     1167      /// This iterator goes through each red node.
     1168      typedef GraphItemIt<BpGraph, Node> RedIt;
     1169
     1170      /// \brief This iterator goes through each blue node.
     1171      ///
     1172      /// This iterator goes through each blue node.
     1173      typedef GraphItemIt<BpGraph, Node> BlueIt;
     1174
     1175      /// @}
     1176
     1177      template <typename _BpGraph>
     1178      struct Constraints {
     1179        void constraints() {
     1180          checkConcept<IterableGraphComponent<Base>, _BpGraph>();
     1181
     1182          typename _BpGraph::Node node(INVALID);
     1183          bpgraph.firstRed(node);
     1184          bpgraph.nextRed(node);
     1185          bpgraph.firstBlue(node);
     1186          bpgraph.nextBlue(node);
     1187
     1188          checkConcept<GraphItemIt<_BpGraph, typename _BpGraph::Node>,
     1189            typename _BpGraph::RedIt>();
     1190          checkConcept<GraphItemIt<_BpGraph, typename _BpGraph::Node>,
     1191            typename _BpGraph::BlueIt>();
     1192        }
     1193
     1194        const _BpGraph& bpgraph;
     1195      };
     1196    };
     1197
    8921198    /// \brief Skeleton class for alterable directed graphs.
    8931199    ///
    8941200    /// This class describes the interface of alterable directed
     
    9141220      typedef AlterationNotifier<AlterableDigraphComponent, Arc>
    9151221      ArcNotifier;
    9161222
     1223      mutable NodeNotifier node_notifier;
     1224      mutable ArcNotifier arc_notifier;
     1225
    9171226      /// \brief Return the node alteration notifier.
    9181227      ///
    9191228      /// This function gives back the node alteration notifier.
    9201229      NodeNotifier& notifier(Node) const {
    921          return NodeNotifier();
     1230        return node_notifier;
    9221231      }
    9231232
    9241233      /// \brief Return the arc alteration notifier.
    9251234      ///
    9261235      /// This function gives back the arc alteration notifier.
    9271236      ArcNotifier& notifier(Arc) const {
    928         return ArcNotifier();
     1237        return arc_notifier;
    9291238      }
    9301239
    9311240      template <typename _Digraph>
     
    9601269    public:
    9611270
    9621271      typedef BAS Base;
     1272      typedef AlterableDigraphComponent<Base> Parent;
    9631273      typedef typename Base::Edge Edge;
    9641274
    9651275
     
    9671277      typedef AlterationNotifier<AlterableGraphComponent, Edge>
    9681278      EdgeNotifier;
    9691279
     1280      mutable EdgeNotifier edge_notifier;
     1281
     1282      using Parent::notifier;
     1283
    9701284      /// \brief Return the edge alteration notifier.
    9711285      ///
    9721286      /// This function gives back the edge alteration notifier.
    9731287      EdgeNotifier& notifier(Edge) const {
    974         return EdgeNotifier();
     1288        return edge_notifier;
    9751289      }
    9761290
    9771291      template <typename _Graph>
     
    9871301      };
    9881302    };
    9891303
     1304    /// \brief Skeleton class for alterable undirected bipartite graphs.
     1305    ///
     1306    /// This class describes the interface of alterable undirected
     1307    /// bipartite graphs. It extends \ref AlterableGraphComponent with
     1308    /// the alteration notifier interface of bipartite graphs. It
     1309    /// implements an observer-notifier pattern for the red and blue
     1310    /// nodes. More obsevers can be registered into the notifier and
     1311    /// whenever an alteration occured in the graph all the observers
     1312    /// will be notified about it.
     1313    template <typename BAS = BaseBpGraphComponent>
     1314    class AlterableBpGraphComponent : public AlterableGraphComponent<BAS> {
     1315    public:
     1316
     1317      typedef BAS Base;
     1318      typedef AlterableGraphComponent<Base> Parent;
     1319      typedef typename Base::RedNode RedNode;
     1320      typedef typename Base::BlueNode BlueNode;
     1321
     1322
     1323      /// Red node alteration notifier class.
     1324      typedef AlterationNotifier<AlterableBpGraphComponent, RedNode>
     1325      RedNodeNotifier;
     1326
     1327      /// Blue node alteration notifier class.
     1328      typedef AlterationNotifier<AlterableBpGraphComponent, BlueNode>
     1329      BlueNodeNotifier;
     1330
     1331      mutable RedNodeNotifier red_node_notifier;
     1332      mutable BlueNodeNotifier blue_node_notifier;
     1333
     1334      using Parent::notifier;
     1335
     1336      /// \brief Return the red node alteration notifier.
     1337      ///
     1338      /// This function gives back the red node alteration notifier.
     1339      RedNodeNotifier& notifier(RedNode) const {
     1340        return red_node_notifier;
     1341      }
     1342
     1343      /// \brief Return the blue node alteration notifier.
     1344      ///
     1345      /// This function gives back the blue node alteration notifier.
     1346      BlueNodeNotifier& notifier(BlueNode) const {
     1347        return blue_node_notifier;
     1348      }
     1349
     1350      template <typename _BpGraph>
     1351      struct Constraints {
     1352        void constraints() {
     1353          checkConcept<AlterableGraphComponent<Base>, _BpGraph>();
     1354          typename _BpGraph::RedNodeNotifier& rnn
     1355            = bpgraph.notifier(typename _BpGraph::RedNode());
     1356          typename _BpGraph::BlueNodeNotifier& bnn
     1357            = bpgraph.notifier(typename _BpGraph::BlueNode());
     1358          ignore_unused_variable_warning(rnn);
     1359          ignore_unused_variable_warning(bnn);
     1360        }
     1361
     1362        const _BpGraph& bpgraph;
     1363      };
     1364    };
     1365
    9901366    /// \brief Concept class for standard graph maps.
    9911367    ///
    9921368    /// This class describes the concept of standard graph maps, i.e.
     
    12871663      };
    12881664    };
    12891665
     1666    /// \brief Skeleton class for mappable undirected bipartite graphs.
     1667    ///
     1668    /// This class describes the interface of mappable undirected
     1669    /// bipartite graphs.  It extends \ref MappableGraphComponent with
     1670    /// the standard graph map class for red and blue nodes (\c
     1671    /// RedMap and BlueMap). This concept is part of the BpGraph concept.
     1672    template <typename BAS = BaseBpGraphComponent>
     1673    class MappableBpGraphComponent : public MappableGraphComponent<BAS>  {
     1674    public:
     1675
     1676      typedef BAS Base;
     1677      typedef typename Base::Node Node;
     1678
     1679      typedef MappableBpGraphComponent BpGraph;
     1680
     1681      /// \brief Standard graph map for the red nodes.
     1682      ///
     1683      /// Standard graph map for the red nodes.
     1684      /// It conforms to the ReferenceMap concept.
     1685      template <typename V>
     1686      class RedMap : public GraphMap<MappableBpGraphComponent, Node, V> {
     1687        typedef GraphMap<MappableBpGraphComponent, Node, V> Parent;
     1688
     1689      public:
     1690        /// \brief Construct a new map.
     1691        ///
     1692        /// Construct a new map for the graph.
     1693        explicit RedMap(const MappableBpGraphComponent& graph)
     1694          : Parent(graph) {}
     1695
     1696        /// \brief Construct a new map with default value.
     1697        ///
     1698        /// Construct a new map for the graph and initalize the values.
     1699        RedMap(const MappableBpGraphComponent& graph, const V& value)
     1700          : Parent(graph, value) {}
     1701
     1702      private:
     1703        /// \brief Copy constructor.
     1704        ///
     1705        /// Copy Constructor.
     1706        RedMap(const RedMap& nm) : Parent(nm) {}
     1707
     1708        /// \brief Assignment operator.
     1709        ///
     1710        /// Assignment operator.
     1711        template <typename CMap>
     1712        RedMap& operator=(const CMap&) {
     1713          checkConcept<ReadMap<Node, V>, CMap>();
     1714          return *this;
     1715        }
     1716
     1717      };
     1718
     1719      /// \brief Standard graph map for the blue nodes.
     1720      ///
     1721      /// Standard graph map for the blue nodes.
     1722      /// It conforms to the ReferenceMap concept.
     1723      template <typename V>
     1724      class BlueMap : public GraphMap<MappableBpGraphComponent, Node, V> {
     1725        typedef GraphMap<MappableBpGraphComponent, Node, V> Parent;
     1726
     1727      public:
     1728        /// \brief Construct a new map.
     1729        ///
     1730        /// Construct a new map for the graph.
     1731        explicit BlueMap(const MappableBpGraphComponent& graph)
     1732          : Parent(graph) {}
     1733
     1734        /// \brief Construct a new map with default value.
     1735        ///
     1736        /// Construct a new map for the graph and initalize the values.
     1737        BlueMap(const MappableBpGraphComponent& graph, const V& value)
     1738          : Parent(graph, value) {}
     1739
     1740      private:
     1741        /// \brief Copy constructor.
     1742        ///
     1743        /// Copy Constructor.
     1744        BlueMap(const BlueMap& nm) : Parent(nm) {}
     1745
     1746        /// \brief Assignment operator.
     1747        ///
     1748        /// Assignment operator.
     1749        template <typename CMap>
     1750        BlueMap& operator=(const CMap&) {
     1751          checkConcept<ReadMap<Node, V>, CMap>();
     1752          return *this;
     1753        }
     1754
     1755      };
     1756
     1757
     1758      template <typename _BpGraph>
     1759      struct Constraints {
     1760
     1761        struct Dummy {
     1762          int value;
     1763          Dummy() : value(0) {}
     1764          Dummy(int _v) : value(_v) {}
     1765        };
     1766
     1767        void constraints() {
     1768          checkConcept<MappableGraphComponent<Base>, _BpGraph>();
     1769
     1770          { // int map test
     1771            typedef typename _BpGraph::template RedMap<int> IntRedMap;
     1772            checkConcept<GraphMap<_BpGraph, typename _BpGraph::Node, int>,
     1773              IntRedMap >();
     1774          } { // bool map test
     1775            typedef typename _BpGraph::template RedMap<bool> BoolRedMap;
     1776            checkConcept<GraphMap<_BpGraph, typename _BpGraph::Node, bool>,
     1777              BoolRedMap >();
     1778          } { // Dummy map test
     1779            typedef typename _BpGraph::template RedMap<Dummy> DummyRedMap;
     1780            checkConcept<GraphMap<_BpGraph, typename _BpGraph::Node, Dummy>,
     1781              DummyRedMap >();
     1782          }
     1783
     1784          { // int map test
     1785            typedef typename _BpGraph::template BlueMap<int> IntBlueMap;
     1786            checkConcept<GraphMap<_BpGraph, typename _BpGraph::Node, int>,
     1787              IntBlueMap >();
     1788          } { // bool map test
     1789            typedef typename _BpGraph::template BlueMap<bool> BoolBlueMap;
     1790            checkConcept<GraphMap<_BpGraph, typename _BpGraph::Node, bool>,
     1791              BoolBlueMap >();
     1792          } { // Dummy map test
     1793            typedef typename _BpGraph::template BlueMap<Dummy> DummyBlueMap;
     1794            checkConcept<GraphMap<_BpGraph, typename _BpGraph::Node, Dummy>,
     1795              DummyBlueMap >();
     1796          }
     1797        }
     1798
     1799        const _BpGraph& bpgraph;
     1800      };
     1801    };
     1802
    12901803    /// \brief Skeleton class for extendable directed graphs.
    12911804    ///
    12921805    /// This class describes the interface of extendable directed graphs.
     
    13751888      };
    13761889    };
    13771890
     1891    /// \brief Skeleton class for extendable undirected bipartite graphs.
     1892    ///
     1893    /// This class describes the interface of extendable undirected
     1894    /// bipartite graphs. It extends \ref BaseGraphComponent with
     1895    /// functions for adding nodes and edges to the graph. This
     1896    /// concept requires \ref AlterableBpGraphComponent.
     1897    template <typename BAS = BaseBpGraphComponent>
     1898    class ExtendableBpGraphComponent : public BAS {
     1899    public:
     1900
     1901      typedef BAS Base;
     1902      typedef typename Base::Node Node;
     1903      typedef typename Base::Edge Edge;
     1904
     1905      /// \brief Add a new red node to the digraph.
     1906      ///
     1907      /// This function adds a red new node to the digraph.
     1908      Node addRedNode() {
     1909        return INVALID;
     1910      }
     1911
     1912      /// \brief Add a new blue node to the digraph.
     1913      ///
     1914      /// This function adds a blue new node to the digraph.
     1915      Node addBlueNode() {
     1916        return INVALID;
     1917      }
     1918
     1919      /// \brief Add a new edge connecting the given two nodes.
     1920      ///
     1921      /// This function adds a new edge connecting the given two nodes
     1922      /// of the graph. The first node has to be a red node, and the
     1923      /// second one a blue node.
     1924      Edge addEdge(const Node&, const Node&) {
     1925        return INVALID;
     1926      }
     1927
     1928      template <typename _BpGraph>
     1929      struct Constraints {
     1930        void constraints() {
     1931          checkConcept<Base, _BpGraph>();
     1932          typename _BpGraph::Node red_node, blue_node;
     1933          red_node = bpgraph.addRedNode();
     1934          blue_node = bpgraph.addBlueNode();
     1935          typename _BpGraph::Edge edge;
     1936          edge = bpgraph.addEdge(red_node, blue_node);
     1937        }
     1938
     1939        _BpGraph& bpgraph;
     1940      };
     1941    };
     1942
    13781943    /// \brief Skeleton class for erasable directed graphs.
    13791944    ///
    13801945    /// This class describes the interface of erasable directed graphs.
     
    14532018      };
    14542019    };
    14552020
     2021    /// \brief Skeleton class for erasable undirected graphs.
     2022    ///
     2023    /// This class describes the interface of erasable undirected
     2024    /// bipartite graphs. It extends \ref BaseBpGraphComponent with
     2025    /// functions for removing nodes and edges from the graph. This
     2026    /// concept requires \ref AlterableBpGraphComponent.
     2027    template <typename BAS = BaseBpGraphComponent>
     2028    class ErasableBpGraphComponent : public ErasableGraphComponent<BAS> {};
     2029
    14562030    /// \brief Skeleton class for clearable directed graphs.
    14572031    ///
    14582032    /// This class describes the interface of clearable directed graphs.
     
    14882062    /// the graph.
    14892063    /// This concept requires \ref AlterableGraphComponent.
    14902064    template <typename BAS = BaseGraphComponent>
    1491     class ClearableGraphComponent : public ClearableDigraphComponent<BAS> {
    1492     public:
     2065    class ClearableGraphComponent : public ClearableDigraphComponent<BAS> {};
    14932066
    1494       typedef BAS Base;
    1495 
    1496       /// \brief Erase all nodes and edges from the graph.
    1497       ///
    1498       /// This function erases all nodes and edges from the graph.
    1499       void clear() {}
    1500 
    1501       template <typename _Graph>
    1502       struct Constraints {
    1503         void constraints() {
    1504           checkConcept<Base, _Graph>();
    1505           graph.clear();
    1506         }
    1507 
    1508         _Graph& graph;
    1509       };
    1510     };
     2067    /// \brief Skeleton class for clearable undirected biparite graphs.
     2068    ///
     2069    /// This class describes the interface of clearable undirected
     2070    /// bipartite graphs. It extends \ref BaseBpGraphComponent with a
     2071    /// function for clearing the graph.  This concept requires \ref
     2072    /// AlterableBpGraphComponent.
     2073    template <typename BAS = BaseBpGraphComponent>
     2074    class ClearableBpGraphComponent : public ClearableGraphComponent<BAS> {};
    15112075
    15122076  }
    15132077
  • test/CMakeLists.txt

    diff -r 1937b6455b7d -r 1a48ab5320b3 test/CMakeLists.txt
    a b  
    1111  adaptors_test
    1212  bellman_ford_test
    1313  bfs_test
     14  bpgraph_test
    1415  circulation_test
    1516  connectivity_test
    1617  counter_test
  • test/Makefile.am

    diff -r 1937b6455b7d -r 1a48ab5320b3 test/Makefile.am
    a b  
    1313        test/adaptors_test \
    1414        test/bellman_ford_test \
    1515        test/bfs_test \
     16        test/bpgraph_test \
    1617        test/circulation_test \
    1718        test/connectivity_test \
    1819        test/counter_test \
     
    6364test_adaptors_test_SOURCES = test/adaptors_test.cc
    6465test_bellman_ford_test_SOURCES = test/bellman_ford_test.cc
    6566test_bfs_test_SOURCES = test/bfs_test.cc
     67test_bpgraph_test_SOURCES = test/bpgraph_test.cc
    6668test_circulation_test_SOURCES = test/circulation_test.cc
    6769test_counter_test_SOURCES = test/counter_test.cc
    6870test_connectivity_test_SOURCES = test/connectivity_test.cc
  • new file test/bpgraph_test.cc

    diff -r 1937b6455b7d -r 1a48ab5320b3 test/bpgraph_test.cc
    - +  
     1/* -*- mode: C++; indent-tabs-mode: nil; -*-
     2 *
     3 * This file is a part of LEMON, a generic C++ optimization library.
     4 *
     5 * Copyright (C) 2003-2010
     6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8 *
     9 * Permission to use, modify and distribute this software is granted
     10 * provided that this copyright notice appears in all copies. For
     11 * precise terms see the accompanying LICENSE file.
     12 *
     13 * This software is provided "AS IS" with no warranty of any kind,
     14 * express or implied, and with no claim as to its suitability for any
     15 * purpose.
     16 *
     17 */
     18
     19#include <lemon/concepts/bpgraph.h>
     20//#include <lemon/list_graph.h>
     21//#include <lemon/smart_graph.h>
     22//#include <lemon/full_graph.h>
     23//#include <lemon/grid_graph.h>
     24//#include <lemon/hypercube_graph.h>
     25
     26#include "test_tools.h"
     27#include "graph_test.h"
     28
     29using namespace lemon;
     30using namespace lemon::concepts;
     31
     32void checkConcepts() {
     33  { // Checking graph components
     34    checkConcept<BaseBpGraphComponent, BaseBpGraphComponent >();
     35
     36    checkConcept<IDableBpGraphComponent<>,
     37      IDableBpGraphComponent<> >();
     38
     39    checkConcept<IterableBpGraphComponent<>,
     40      IterableBpGraphComponent<> >();
     41
     42    checkConcept<AlterableBpGraphComponent<>,
     43      AlterableBpGraphComponent<> >();
     44
     45    checkConcept<MappableBpGraphComponent<>,
     46      MappableBpGraphComponent<> >();
     47
     48    checkConcept<ExtendableBpGraphComponent<>,
     49      ExtendableBpGraphComponent<> >();
     50
     51    checkConcept<ErasableBpGraphComponent<>,
     52      ErasableBpGraphComponent<> >();
     53
     54    checkConcept<ClearableGraphComponent<>,
     55      ClearableGraphComponent<> >();
     56
     57  }
     58  { // Checking skeleton graph
     59    checkConcept<BpGraph, BpGraph>();
     60  }
     61}
     62
     63void checkGraphs() {
     64}
     65
     66int main() {
     67  checkConcepts();
     68  checkGraphs();
     69  return 0;
     70}
  • lemon/bits/graph_extender.h

    # HG changeset patch
    # User Balazs Dezso <deba@inf.elte.hu>
    # Date 1289761583 -3600
    # Node ID 13bca1f55d6121d9df328aa3b1f89e0f8c90d518
    # Parent  1a48ab5320b3edcd33a4733d8e3fbe8c1727b4ac
    SmartBpGraph implementation
    
    diff -r 1a48ab5320b3 -r 13bca1f55d61 lemon/bits/graph_extender.h
    a b  
    746746
    747747  };
    748748
     749  // \ingroup _graphbits
     750  //
     751  // \brief Extender for the BpGraphs
     752  template <typename Base>
     753  class BpGraphExtender : public Base {
     754    typedef Base Parent;
     755
     756  public:
     757
     758    typedef BpGraphExtender BpGraph;
     759
     760    typedef True UndirectedTag;
     761
     762    typedef typename Parent::Node Node;
     763    typedef typename Parent::Arc Arc;
     764    typedef typename Parent::Edge Edge;
     765
     766    // BpGraph extension
     767
     768    class RedNode : public Node {
     769    public:
     770      RedNode() {}
     771      RedNode(const RedNode& node) : Node(node) {}
     772      RedNode(Invalid) : Node(INVALID){}
     773      RedNode(const Node& node) : Node(node) {}
     774    };
     775    class BlueNode : public Node {
     776    public:
     777      BlueNode() {}
     778      BlueNode(const BlueNode& node) : Node(node) {}
     779      BlueNode(Invalid) : Node(INVALID){}
     780      BlueNode(const Node& node) : Node(node) {}
     781    };
     782
     783    using Parent::first;
     784    using Parent::next;
     785
     786    void first(RedNode& node) const {
     787      Parent::firstRed(node);
     788    }
     789   
     790    void next(RedNode& node) const {
     791      Parent::nextRed(node);
     792    }
     793
     794    void first(BlueNode& node) const {
     795      Parent::firstBlue(node);
     796    }
     797
     798    void next(BlueNode& node) const {
     799      Parent::nextBlue(node);
     800    }
     801
     802    using Parent::id;
     803
     804    int id(const RedNode& node) const {
     805      return Parent::redId(node);
     806    }
     807
     808    int id(const BlueNode& node) const {
     809      return Parent::blueId(node);
     810    }
     811
     812    int maxId(Node) const {
     813      return Parent::maxNodeId();
     814    }
     815
     816    int maxId(RedNode) const {
     817      return Parent::maxRedId();
     818    }
     819
     820    int maxId(BlueNode) const {
     821      return Parent::maxBlueId();
     822    }
     823
     824    int maxId(Arc) const {
     825      return Parent::maxArcId();
     826    }
     827
     828    int maxId(Edge) const {
     829      return Parent::maxEdgeId();
     830    }
     831
     832    static Node fromId(int id, Node) {
     833      return Parent::nodeFromId(id);
     834    }
     835
     836    static Arc fromId(int id, Arc) {
     837      return Parent::arcFromId(id);
     838    }
     839
     840    static Edge fromId(int id, Edge) {
     841      return Parent::edgeFromId(id);
     842    }
     843
     844    Node oppositeNode(const Node &n, const Edge &e) const {
     845      if( n == Parent::u(e))
     846        return Parent::v(e);
     847      else if( n == Parent::v(e))
     848        return Parent::u(e);
     849      else
     850        return INVALID;
     851    }
     852
     853    Arc oppositeArc(const Arc &arc) const {
     854      return Parent::direct(arc, !Parent::direction(arc));
     855    }
     856
     857    using Parent::direct;
     858    Arc direct(const Edge &edge, const Node &node) const {
     859      return Parent::direct(edge, Parent::u(edge) == node);
     860    }
     861
     862    // Alterable extension
     863
     864    typedef AlterationNotifier<BpGraphExtender, Node> NodeNotifier;
     865    typedef AlterationNotifier<BpGraphExtender, RedNode> RedNodeNotifier;
     866    typedef AlterationNotifier<BpGraphExtender, BlueNode> BlueNodeNotifier;
     867    typedef AlterationNotifier<BpGraphExtender, Arc> ArcNotifier;
     868    typedef AlterationNotifier<BpGraphExtender, Edge> EdgeNotifier;
     869
     870
     871  protected:
     872
     873    mutable NodeNotifier node_notifier;
     874    mutable RedNodeNotifier red_node_notifier;
     875    mutable BlueNodeNotifier blue_node_notifier;
     876    mutable ArcNotifier arc_notifier;
     877    mutable EdgeNotifier edge_notifier;
     878
     879  public:
     880
     881    NodeNotifier& notifier(Node) const {
     882      return node_notifier;
     883    }
     884
     885    RedNodeNotifier& notifier(RedNode) const {
     886      return red_node_notifier;
     887    }
     888
     889    BlueNodeNotifier& notifier(BlueNode) const {
     890      return blue_node_notifier;
     891    }
     892
     893    ArcNotifier& notifier(Arc) const {
     894      return arc_notifier;
     895    }
     896
     897    EdgeNotifier& notifier(Edge) const {
     898      return edge_notifier;
     899    }
     900
     901
     902
     903    class NodeIt : public Node {
     904      const BpGraph* _graph;
     905    public:
     906
     907      NodeIt() {}
     908
     909      NodeIt(Invalid i) : Node(i) { }
     910
     911      explicit NodeIt(const BpGraph& graph) : _graph(&graph) {
     912        _graph->first(static_cast<Node&>(*this));
     913      }
     914
     915      NodeIt(const BpGraph& graph, const Node& node)
     916        : Node(node), _graph(&graph) {}
     917
     918      NodeIt& operator++() {
     919        _graph->next(*this);
     920        return *this;
     921      }
     922
     923    };
     924
     925    class RedIt : public Node {
     926      const BpGraph* _graph;
     927    public:
     928
     929      RedIt() {}
     930
     931      RedIt(Invalid i) : Node(i) { }
     932
     933      explicit RedIt(const BpGraph& graph) : _graph(&graph) {
     934        _graph->firstRed(static_cast<Node&>(*this));
     935      }
     936
     937      RedIt(const BpGraph& graph, const Node& node)
     938        : Node(node), _graph(&graph) {
     939        LEMON_DEBUG(_graph->red(node), "Node has to be red.");
     940      }
     941
     942      RedIt& operator++() {
     943        _graph->nextRed(*this);
     944        return *this;
     945      }
     946
     947    };
     948
     949    class BlueIt : public Node {
     950      const BpGraph* _graph;
     951    public:
     952
     953      BlueIt() {}
     954
     955      BlueIt(Invalid i) : Node(i) { }
     956
     957      explicit BlueIt(const BpGraph& graph) : _graph(&graph) {
     958        _graph->firstBlue(static_cast<Node&>(*this));
     959      }
     960
     961      BlueIt(const BpGraph& graph, const Node& node)
     962        : Node(node), _graph(&graph) {
     963        LEMON_DEBUG(_graph->blue(node), "Node has to be blue.");
     964      }
     965
     966      BlueIt& operator++() {
     967        _graph->nextBlue(*this);
     968        return *this;
     969      }
     970
     971    };
     972
     973
     974    class ArcIt : public Arc {
     975      const BpGraph* _graph;
     976    public:
     977
     978      ArcIt() { }
     979
     980      ArcIt(Invalid i) : Arc(i) { }
     981
     982      explicit ArcIt(const BpGraph& graph) : _graph(&graph) {
     983        _graph->first(static_cast<Arc&>(*this));
     984      }
     985
     986      ArcIt(const BpGraph& graph, const Arc& arc) :
     987        Arc(arc), _graph(&graph) { }
     988
     989      ArcIt& operator++() {
     990        _graph->next(*this);
     991        return *this;
     992      }
     993
     994    };
     995
     996
     997    class OutArcIt : public Arc {
     998      const BpGraph* _graph;
     999    public:
     1000
     1001      OutArcIt() { }
     1002
     1003      OutArcIt(Invalid i) : Arc(i) { }
     1004
     1005      OutArcIt(const BpGraph& graph, const Node& node)
     1006        : _graph(&graph) {
     1007        _graph->firstOut(*this, node);
     1008      }
     1009
     1010      OutArcIt(const BpGraph& graph, const Arc& arc)
     1011        : Arc(arc), _graph(&graph) {}
     1012
     1013      OutArcIt& operator++() {
     1014        _graph->nextOut(*this);
     1015        return *this;
     1016      }
     1017
     1018    };
     1019
     1020
     1021    class InArcIt : public Arc {
     1022      const BpGraph* _graph;
     1023    public:
     1024
     1025      InArcIt() { }
     1026
     1027      InArcIt(Invalid i) : Arc(i) { }
     1028
     1029      InArcIt(const BpGraph& graph, const Node& node)
     1030        : _graph(&graph) {
     1031        _graph->firstIn(*this, node);
     1032      }
     1033
     1034      InArcIt(const BpGraph& graph, const Arc& arc) :
     1035        Arc(arc), _graph(&graph) {}
     1036
     1037      InArcIt& operator++() {
     1038        _graph->nextIn(*this);
     1039        return *this;
     1040      }
     1041
     1042    };
     1043
     1044
     1045    class EdgeIt : public Parent::Edge {
     1046      const BpGraph* _graph;
     1047    public:
     1048
     1049      EdgeIt() { }
     1050
     1051      EdgeIt(Invalid i) : Edge(i) { }
     1052
     1053      explicit EdgeIt(const BpGraph& graph) : _graph(&graph) {
     1054        _graph->first(static_cast<Edge&>(*this));
     1055      }
     1056
     1057      EdgeIt(const BpGraph& graph, const Edge& edge) :
     1058        Edge(edge), _graph(&graph) { }
     1059
     1060      EdgeIt& operator++() {
     1061        _graph->next(*this);
     1062        return *this;
     1063      }
     1064
     1065    };
     1066
     1067    class IncEdgeIt : public Parent::Edge {
     1068      friend class BpGraphExtender;
     1069      const BpGraph* _graph;
     1070      bool _direction;
     1071    public:
     1072
     1073      IncEdgeIt() { }
     1074
     1075      IncEdgeIt(Invalid i) : Edge(i), _direction(false) { }
     1076
     1077      IncEdgeIt(const BpGraph& graph, const Node &node) : _graph(&graph) {
     1078        _graph->firstInc(*this, _direction, node);
     1079      }
     1080
     1081      IncEdgeIt(const BpGraph& graph, const Edge &edge, const Node &node)
     1082        : _graph(&graph), Edge(edge) {
     1083        _direction = (_graph->source(edge) == node);
     1084      }
     1085
     1086      IncEdgeIt& operator++() {
     1087        _graph->nextInc(*this, _direction);
     1088        return *this;
     1089      }
     1090    };
     1091
     1092    // \brief Base node of the iterator
     1093    //
     1094    // Returns the base node (ie. the source in this case) of the iterator
     1095    Node baseNode(const OutArcIt &arc) const {
     1096      return Parent::source(static_cast<const Arc&>(arc));
     1097    }
     1098    // \brief Running node of the iterator
     1099    //
     1100    // Returns the running node (ie. the target in this case) of the
     1101    // iterator
     1102    Node runningNode(const OutArcIt &arc) const {
     1103      return Parent::target(static_cast<const Arc&>(arc));
     1104    }
     1105
     1106    // \brief Base node of the iterator
     1107    //
     1108    // Returns the base node (ie. the target in this case) of the iterator
     1109    Node baseNode(const InArcIt &arc) const {
     1110      return Parent::target(static_cast<const Arc&>(arc));
     1111    }
     1112    // \brief Running node of the iterator
     1113    //
     1114    // Returns the running node (ie. the source in this case) of the
     1115    // iterator
     1116    Node runningNode(const InArcIt &arc) const {
     1117      return Parent::source(static_cast<const Arc&>(arc));
     1118    }
     1119
     1120    // Base node of the iterator
     1121    //
     1122    // Returns the base node of the iterator
     1123    Node baseNode(const IncEdgeIt &edge) const {
     1124      return edge._direction ? u(edge) : v(edge);
     1125    }
     1126    // Running node of the iterator
     1127    //
     1128    // Returns the running node of the iterator
     1129    Node runningNode(const IncEdgeIt &edge) const {
     1130      return edge._direction ? v(edge) : u(edge);
     1131    }
     1132
     1133    // Mappable extension
     1134
     1135    template <typename _Value>
     1136    class NodeMap
     1137      : public MapExtender<DefaultMap<BpGraph, Node, _Value> > {
     1138      typedef MapExtender<DefaultMap<BpGraph, Node, _Value> > Parent;
     1139
     1140    public:
     1141      explicit NodeMap(const BpGraph& bpgraph)
     1142        : Parent(bpgraph) {}
     1143      NodeMap(const BpGraph& bpgraph, const _Value& value)
     1144        : Parent(bpgraph, value) {}
     1145
     1146    private:
     1147      NodeMap& operator=(const NodeMap& cmap) {
     1148        return operator=<NodeMap>(cmap);
     1149      }
     1150
     1151      template <typename CMap>
     1152      NodeMap& operator=(const CMap& cmap) {
     1153        Parent::operator=(cmap);
     1154        return *this;
     1155      }
     1156
     1157    };
     1158
     1159    template <typename _Value>
     1160    class RedMap
     1161      : public MapExtender<DefaultMap<BpGraph, RedNode, _Value> > {
     1162      typedef MapExtender<DefaultMap<BpGraph, RedNode, _Value> > Parent;
     1163
     1164    public:
     1165      explicit RedMap(const BpGraph& bpgraph)
     1166        : Parent(bpgraph) {}
     1167      RedMap(const BpGraph& bpgraph, const _Value& value)
     1168        : Parent(bpgraph, value) {}
     1169
     1170    private:
     1171      RedMap& operator=(const RedMap& cmap) {
     1172        return operator=<RedMap>(cmap);
     1173      }
     1174
     1175      template <typename CMap>
     1176      RedMap& operator=(const CMap& cmap) {
     1177        Parent::operator=(cmap);
     1178        return *this;
     1179      }
     1180
     1181    };
     1182
     1183    template <typename _Value>
     1184    class BlueMap
     1185      : public MapExtender<DefaultMap<BpGraph, BlueNode, _Value> > {
     1186      typedef MapExtender<DefaultMap<BpGraph, BlueNode, _Value> > Parent;
     1187
     1188    public:
     1189      explicit BlueMap(const BpGraph& bpgraph)
     1190        : Parent(bpgraph) {}
     1191      BlueMap(const BpGraph& bpgraph, const _Value& value)
     1192        : Parent(bpgraph, value) {}
     1193
     1194    private:
     1195      BlueMap& operator=(const BlueMap& cmap) {
     1196        return operator=<BlueMap>(cmap);
     1197      }
     1198
     1199      template <typename CMap>
     1200      BlueMap& operator=(const CMap& cmap) {
     1201        Parent::operator=(cmap);
     1202        return *this;
     1203      }
     1204
     1205    };
     1206
     1207    template <typename _Value>
     1208    class ArcMap
     1209      : public MapExtender<DefaultMap<BpGraph, Arc, _Value> > {
     1210      typedef MapExtender<DefaultMap<BpGraph, Arc, _Value> > Parent;
     1211
     1212    public:
     1213      explicit ArcMap(const BpGraph& graph)
     1214        : Parent(graph) {}
     1215      ArcMap(const BpGraph& graph, const _Value& value)
     1216        : Parent(graph, value) {}
     1217
     1218    private:
     1219      ArcMap& operator=(const ArcMap& cmap) {
     1220        return operator=<ArcMap>(cmap);
     1221      }
     1222
     1223      template <typename CMap>
     1224      ArcMap& operator=(const CMap& cmap) {
     1225        Parent::operator=(cmap);
     1226        return *this;
     1227      }
     1228    };
     1229
     1230
     1231    template <typename _Value>
     1232    class EdgeMap
     1233      : public MapExtender<DefaultMap<BpGraph, Edge, _Value> > {
     1234      typedef MapExtender<DefaultMap<BpGraph, Edge, _Value> > Parent;
     1235
     1236    public:
     1237      explicit EdgeMap(const BpGraph& graph)
     1238        : Parent(graph) {}
     1239
     1240      EdgeMap(const BpGraph& graph, const _Value& value)
     1241        : Parent(graph, value) {}
     1242
     1243    private:
     1244      EdgeMap& operator=(const EdgeMap& cmap) {
     1245        return operator=<EdgeMap>(cmap);
     1246      }
     1247
     1248      template <typename CMap>
     1249      EdgeMap& operator=(const CMap& cmap) {
     1250        Parent::operator=(cmap);
     1251        return *this;
     1252      }
     1253
     1254    };
     1255
     1256    // Alteration extension
     1257
     1258    Node addRedNode() {
     1259      Node node = Parent::addRedNode();
     1260      notifier(RedNode()).add(node);
     1261      notifier(Node()).add(node);
     1262      return node;
     1263    }
     1264
     1265    Node addBlueNode() {
     1266      Node node = Parent::addBlueNode();
     1267      notifier(BlueNode()).add(node);
     1268      notifier(Node()).add(node);
     1269      return node;
     1270    }
     1271
     1272    Edge addEdge(const Node& from, const Node& to) {
     1273      Edge edge = Parent::addEdge(from, to);
     1274      notifier(Edge()).add(edge);
     1275      std::vector<Arc> av;
     1276      av.push_back(Parent::direct(edge, true));
     1277      av.push_back(Parent::direct(edge, false));
     1278      notifier(Arc()).add(av);
     1279      return edge;
     1280    }
     1281
     1282    void clear() {
     1283      notifier(Arc()).clear();
     1284      notifier(Edge()).clear();
     1285      notifier(Node()).clear();
     1286      notifier(BlueNode()).clear();
     1287      notifier(RedNode()).clear();
     1288      Parent::clear();
     1289    }
     1290
     1291    template <typename BpGraph, typename NodeRefMap, typename EdgeRefMap>
     1292    void build(const BpGraph& graph, NodeRefMap& nodeRef,
     1293               EdgeRefMap& edgeRef) {
     1294      Parent::build(graph, nodeRef, edgeRef);
     1295      notifier(RedNode()).build();
     1296      notifier(BlueNode()).build();
     1297      notifier(Node()).build();
     1298      notifier(Edge()).build();
     1299      notifier(Arc()).build();
     1300    }
     1301
     1302    void erase(const Node& node) {
     1303      Arc arc;
     1304      Parent::firstOut(arc, node);
     1305      while (arc != INVALID ) {
     1306        erase(arc);
     1307        Parent::firstOut(arc, node);
     1308      }
     1309
     1310      Parent::firstIn(arc, node);
     1311      while (arc != INVALID ) {
     1312        erase(arc);
     1313        Parent::firstIn(arc, node);
     1314      }
     1315
     1316      if (Parent::red(node)) {
     1317        notifier(RedNode()).erase(node);
     1318      } else {
     1319        notifier(BlueNode()).erase(node);       
     1320      }
     1321
     1322      notifier(Node()).erase(node);
     1323      Parent::erase(node);
     1324    }
     1325
     1326    void erase(const Edge& edge) {
     1327      std::vector<Arc> av;
     1328      av.push_back(Parent::direct(edge, true));
     1329      av.push_back(Parent::direct(edge, false));
     1330      notifier(Arc()).erase(av);
     1331      notifier(Edge()).erase(edge);
     1332      Parent::erase(edge);
     1333    }
     1334
     1335    BpGraphExtender() {
     1336      red_node_notifier.setContainer(*this);
     1337      blue_node_notifier.setContainer(*this);
     1338      node_notifier.setContainer(*this);
     1339      arc_notifier.setContainer(*this);
     1340      edge_notifier.setContainer(*this);
     1341    }
     1342
     1343    ~BpGraphExtender() {
     1344      edge_notifier.clear();
     1345      arc_notifier.clear();
     1346      node_notifier.clear();
     1347      blue_node_notifier.clear();
     1348      red_node_notifier.clear();
     1349    }
     1350
     1351  };
     1352
    7491353}
    7501354
    7511355#endif
  • lemon/bits/traits.h

    diff -r 1a48ab5320b3 -r 13bca1f55d61 lemon/bits/traits.h
    a b  
    151151
    152152  };
    153153
     154  template <typename GR, typename Enable = void>
     155  struct RedNodeNotifierIndicator {
     156    typedef InvalidType Type;
     157  };
     158  template <typename GR>
     159  struct RedNodeNotifierIndicator<
     160    GR,
     161    typename enable_if<typename GR::RedNodeNotifier::Notifier, void>::type
     162  > {
     163    typedef typename GR::RedNodeNotifier Type;
     164  };
     165
     166  template <typename GR>
     167  class ItemSetTraits<GR, typename GR::RedNode> {
     168  public:
     169
     170    typedef GR BpGraph;
     171    typedef GR Graph;
     172    typedef GR Digraph;
     173
     174    typedef typename GR::RedNode Item;
     175    typedef typename GR::RedIt ItemIt;
     176
     177    typedef typename RedNodeNotifierIndicator<GR>::Type ItemNotifier;
     178
     179    template <typename V>
     180    class Map : public GR::template RedMap<V> {
     181      typedef typename GR::template RedMap<V> Parent;
     182
     183    public:
     184      typedef typename GR::template RedMap<V> Type;
     185      typedef typename Parent::Value Value;
     186
     187      Map(const GR& _bpgraph) : Parent(_bpgraph) {}
     188      Map(const GR& _bpgraph, const Value& _value)
     189        : Parent(_bpgraph, _value) {}
     190
     191     };
     192
     193  };
     194
     195  template <typename GR, typename Enable = void>
     196  struct BlueNodeNotifierIndicator {
     197    typedef InvalidType Type;
     198  };
     199  template <typename GR>
     200  struct BlueNodeNotifierIndicator<
     201    GR,
     202    typename enable_if<typename GR::BlueNodeNotifier::Notifier, void>::type
     203  > {
     204    typedef typename GR::BlueNodeNotifier Type;
     205  };
     206
     207  template <typename GR>
     208  class ItemSetTraits<GR, typename GR::BlueNode> {
     209  public:
     210
     211    typedef GR BpGraph;
     212    typedef GR Graph;
     213    typedef GR Digraph;
     214
     215    typedef typename GR::BlueNode Item;
     216    typedef typename GR::BlueIt ItemIt;
     217
     218    typedef typename BlueNodeNotifierIndicator<GR>::Type ItemNotifier;
     219
     220    template <typename V>
     221    class Map : public GR::template BlueMap<V> {
     222      typedef typename GR::template BlueMap<V> Parent;
     223
     224    public:
     225      typedef typename GR::template BlueMap<V> Type;
     226      typedef typename Parent::Value Value;
     227
     228      Map(const GR& _bpgraph) : Parent(_bpgraph) {}
     229      Map(const GR& _bpgraph, const Value& _value)
     230        : Parent(_bpgraph, _value) {}
     231
     232     };
     233
     234  };
     235
    154236  template <typename Map, typename Enable = void>
    155237  struct MapTraits {
    156238    typedef False ReferenceMapTag;
  • lemon/core.h

    diff -r 1a48ab5320b3 -r 13bca1f55d61 lemon/core.h
    a b  
    148148  typedef typename Graph::template EdgeMap<int> IntEdgeMap;             \
    149149  typedef typename Graph::template EdgeMap<double> DoubleEdgeMap
    150150
     151  ///Create convenience typedefs for the bipartite graph types and iterators
     152
     153  ///This \c \#define creates the same convenient type definitions as defined
     154  ///by \ref GRAPH_TYPEDEFS(BpGraph) and ten more, namely it creates
     155  ///\c RedNode, \c RedIt, \c BoolRedMap, \c IntRedMap, \c DoubleRedMap,
     156  ///\c BlueNode, \c BlueIt, \c BoolBlueMap, \c IntBlueMap, \c DoubleBlueMap.
     157  ///
     158  ///\note If the graph type is a dependent type, ie. the graph type depend
     159  ///on a template parameter, then use \c TEMPLATE_BPGRAPH_TYPEDEFS()
     160  ///macro.
     161#define BPGRAPH_TYPEDEFS(BpGraph)                                       \
     162  GRAPH_TYPEDEFS(BpGraph);                                              \
     163  typedef BpGraph::RedNode RedNode;                                     \
     164  typedef BpGraph::RedIt RedIt;                                         \
     165  typedef BpGraph::RedMap<bool> BoolRedMap;                             \
     166  typedef BpGraph::RedMap<int> IntRedMap;                               \
     167  typedef BpGraph::RedMap<double> DoubleRedMap                          \
     168  typedef BpGraph::BlueNode BlueNode;                                   \
     169  typedef BpGraph::BlueIt BlueIt;                                       \
     170  typedef BpGraph::BlueMap<bool> BoolBlueMap;                           \
     171  typedef BpGraph::BlueMap<int> IntBlueMap;                             \
     172  typedef BpGraph::BlueMap<double> DoubleBlueMap
     173
     174  ///Create convenience typedefs for the bipartite graph types and iterators
     175
     176  ///\see BPGRAPH_TYPEDEFS
     177  ///
     178  ///\note Use this macro, if the graph type is a dependent type,
     179  ///ie. the graph type depend on a template parameter.
     180#define TEMPLATE_BPGRAPH_TYPEDEFS(BpGraph)                              \
     181  TEMPLATE_GRAPH_TYPEDEFS(BpGraph);                                     \
     182  typedef typename BpGraph::RedNode RedNode;                            \
     183  typedef typename BpGraph::RedIt RedIt;                                \
     184  typedef typename BpGraph::template RedMap<bool> BoolRedMap;           \
     185  typedef typename BpGraph::template RedMap<int> IntRedMap;             \
     186  typedef typename BpGraph::template RedMap<double> DoubleRedMap;       \
     187  typedef typename BpGraph::BlueNode BlueNode;                          \
     188  typedef typename BpGraph::BlueIt BlueIt;                              \
     189  typedef typename BpGraph::template BlueMap<bool> BoolBlueMap;         \
     190  typedef typename BpGraph::template BlueMap<int> IntBlueMap;           \
     191  typedef typename BpGraph::template BlueMap<double> DoubleBlueMap
     192
    151193  /// \brief Function to count the items in a graph.
    152194  ///
    153195  /// This function counts the items (nodes, arcs etc.) in a graph.
     
    199241    return _core_bits::CountNodesSelector<Graph>::count(g);
    200242  }
    201243
     244  namespace _graph_utils_bits {
     245   
     246    template <typename Graph, typename Enable = void>
     247    struct CountRedNodesSelector {
     248      static int count(const Graph &g) {
     249        return countItems<Graph, typename Graph::RedNode>(g);
     250      }
     251    };
     252
     253    template <typename Graph>
     254    struct CountRedNodesSelector<
     255      Graph, typename
     256      enable_if<typename Graph::NodeNumTag, void>::type>
     257    {
     258      static int count(const Graph &g) {
     259        return g.redNum();
     260      }
     261    };   
     262  }
     263
     264  /// \brief Function to count the red nodes in the graph.
     265  ///
     266  /// This function counts the red nodes in the graph.
     267  /// The complexity of the function is O(n) but for some
     268  /// graph structures it is specialized to run in O(1).
     269  ///
     270  /// If the graph contains a \e redNum() member function and a
     271  /// \e NodeNumTag tag then this function calls directly the member
     272  /// function to query the cardinality of the node set.
     273  template <typename Graph>
     274  inline int countRedNodes(const Graph& g) {
     275    return _graph_utils_bits::CountRedNodesSelector<Graph>::count(g);
     276  }
     277
     278  namespace _graph_utils_bits {
     279   
     280    template <typename Graph, typename Enable = void>
     281    struct CountBlueNodesSelector {
     282      static int count(const Graph &g) {
     283        return countItems<Graph, typename Graph::BlueNode>(g);
     284      }
     285    };
     286
     287    template <typename Graph>
     288    struct CountBlueNodesSelector<
     289      Graph, typename
     290      enable_if<typename Graph::NodeNumTag, void>::type>
     291    {
     292      static int count(const Graph &g) {
     293        return g.blueNum();
     294      }
     295    };   
     296  }
     297
     298  /// \brief Function to count the blue nodes in the graph.
     299  ///
     300  /// This function counts the blue nodes in the graph.
     301  /// The complexity of the function is O(n) but for some
     302  /// graph structures it is specialized to run in O(1).
     303  ///
     304  /// If the graph contains a \e blueNum() member function and a
     305  /// \e NodeNumTag tag then this function calls directly the member
     306  /// function to query the cardinality of the node set.
     307  template <typename Graph>
     308  inline int countBlueNodes(const Graph& g) {
     309    return _graph_utils_bits::CountBlueNodesSelector<Graph>::count(g);
     310  }
     311
    202312  // Arc counting:
    203313
    204314  namespace _core_bits {
     
    12571367
    12581368    /// The Digraph type
    12591369    typedef GR Digraph;
    1260 
     1370   
    12611371  protected:
    12621372
    12631373    class AutoNodeMap : public ItemSetTraits<GR, Node>::template Map<Arc>::Type
  • lemon/smart_graph.h

    diff -r 1a48ab5320b3 -r 13bca1f55d61 lemon/smart_graph.h
    a b  
    405405    std::vector<NodeT> nodes;
    406406    std::vector<ArcT> arcs;
    407407
    408     int first_free_arc;
    409 
    410408  public:
    411409
    412410    typedef SmartGraphBase Graph;
     
    811809    };
    812810  };
    813811
     812  class SmartBpGraphBase {
     813
     814  protected:
     815
     816    struct NodeT {
     817      int first_out;
     818      int partition_next;
     819      int partition_index;
     820      bool red;
     821    };
     822
     823    struct ArcT {
     824      int target;
     825      int next_out;
     826    };
     827
     828    std::vector<NodeT> nodes;
     829    std::vector<ArcT> arcs;
     830
     831    int first_red, first_blue;
     832
     833  public:
     834
     835    typedef SmartBpGraphBase Graph;
     836
     837    class Node;
     838    class Arc;
     839    class Edge;
     840
     841    class Node {
     842      friend class SmartBpGraphBase;
     843    protected:
     844
     845      int _id;
     846      explicit Node(int id) { _id = id;}
     847
     848    public:
     849      Node() {}
     850      Node (Invalid) { _id = -1; }
     851      bool operator==(const Node& node) const {return _id == node._id;}
     852      bool operator!=(const Node& node) const {return _id != node._id;}
     853      bool operator<(const Node& node) const {return _id < node._id;}
     854    };
     855
     856    class Edge {
     857      friend class SmartBpGraphBase;
     858    protected:
     859
     860      int _id;
     861      explicit Edge(int id) { _id = id;}
     862
     863    public:
     864      Edge() {}
     865      Edge (Invalid) { _id = -1; }
     866      bool operator==(const Edge& arc) const {return _id == arc._id;}
     867      bool operator!=(const Edge& arc) const {return _id != arc._id;}
     868      bool operator<(const Edge& arc) const {return _id < arc._id;}
     869    };
     870
     871    class Arc {
     872      friend class SmartBpGraphBase;
     873    protected:
     874
     875      int _id;
     876      explicit Arc(int id) { _id = id;}
     877
     878    public:
     879      operator Edge() const {
     880        return _id != -1 ? edgeFromId(_id / 2) : INVALID;
     881      }
     882
     883      Arc() {}
     884      Arc (Invalid) { _id = -1; }
     885      bool operator==(const Arc& arc) const {return _id == arc._id;}
     886      bool operator!=(const Arc& arc) const {return _id != arc._id;}
     887      bool operator<(const Arc& arc) const {return _id < arc._id;}
     888    };
     889
     890
     891
     892    SmartBpGraphBase()
     893      : nodes(), arcs(), first_red(-1), first_blue(-1) {}
     894
     895    typedef True NodeNumTag;
     896    typedef True EdgeNumTag;
     897    typedef True ArcNumTag;
     898
     899    int nodeNum() const { return nodes.size(); }
     900    int redNum() const {
     901      return first_red == -1 ? 0 : nodes[first_red].partition_index + 1;
     902    }
     903    int blueNum() const {
     904      return first_blue == -1 ? 0 : nodes[first_blue].partition_index + 1;
     905    }
     906    int edgeNum() const { return arcs.size() / 2; }
     907    int arcNum() const { return arcs.size(); }
     908
     909    int maxNodeId() const { return nodes.size()-1; }
     910    int maxRedId() const {
     911      return first_red == -1 ? -1 : nodes[first_red].partition_index;
     912    }
     913    int maxBlueId() const {
     914      return first_blue == -1 ? -1 : nodes[first_blue].partition_index;
     915    }
     916    int maxEdgeId() const { return arcs.size() / 2 - 1; }
     917    int maxArcId() const { return arcs.size()-1; }
     918
     919    bool red(Node n) const { return nodes[n._id].red; }
     920    bool blue(Node n) const { return !nodes[n._id].red; }
     921
     922    Node source(Arc a) const { return Node(arcs[a._id ^ 1].target); }
     923    Node target(Arc a) const { return Node(arcs[a._id].target); }
     924
     925    Node redNode(Edge e) const { return Node(arcs[2 * e._id].target); }
     926    Node blueNode(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
     927
     928    Node u(Edge e) const { return redNode(e); }
     929    Node v(Edge e) const { return blueNode(e); }
     930
     931    static bool direction(Arc a) {
     932      return (a._id & 1) == 1;
     933    }
     934
     935    static Arc direct(Edge e, bool d) {
     936      return Arc(e._id * 2 + (d ? 1 : 0));
     937    }
     938
     939    void first(Node& node) const {
     940      node._id = nodes.size() - 1;
     941    }
     942
     943    static void next(Node& node) {
     944      --node._id;
     945    }
     946
     947    void firstRed(Node& node) const {
     948      node._id = first_red;
     949    }
     950
     951    void nextRed(Node& node) const {
     952      node._id = nodes[node._id].partition_next;
     953    }
     954
     955    void firstBlue(Node& node) const {
     956      node._id = first_blue;
     957    }
     958
     959    void nextBlue(Node& node) const {
     960      node._id = nodes[node._id].partition_next;
     961    }
     962
     963    void first(Arc& arc) const {
     964      arc._id = arcs.size() - 1;
     965    }
     966
     967    static void next(Arc& arc) {
     968      --arc._id;
     969    }
     970
     971    void first(Edge& arc) const {
     972      arc._id = arcs.size() / 2 - 1;
     973    }
     974
     975    static void next(Edge& arc) {
     976      --arc._id;
     977    }
     978
     979    void firstOut(Arc &arc, const Node& v) const {
     980      arc._id = nodes[v._id].first_out;
     981    }
     982    void nextOut(Arc &arc) const {
     983      arc._id = arcs[arc._id].next_out;
     984    }
     985
     986    void firstIn(Arc &arc, const Node& v) const {
     987      arc._id = ((nodes[v._id].first_out) ^ 1);
     988      if (arc._id == -2) arc._id = -1;
     989    }
     990    void nextIn(Arc &arc) const {
     991      arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1);
     992      if (arc._id == -2) arc._id = -1;
     993    }
     994
     995    void firstInc(Edge &arc, bool& d, const Node& v) const {
     996      int de = nodes[v._id].first_out;
     997      if (de != -1) {
     998        arc._id = de / 2;
     999        d = ((de & 1) == 1);
     1000      } else {
     1001        arc._id = -1;
     1002        d = true;
     1003      }
     1004    }
     1005    void nextInc(Edge &arc, bool& d) const {
     1006      int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out);
     1007      if (de != -1) {
     1008        arc._id = de / 2;
     1009        d = ((de & 1) == 1);
     1010      } else {
     1011        arc._id = -1;
     1012        d = true;
     1013      }
     1014    }
     1015
     1016    static int id(Node v) { return v._id; }
     1017    int redId(Node v) const {
     1018      LEMON_DEBUG(nodes[v._id].red, "Node has to be red");
     1019      return nodes[v._id].partition_index;
     1020    }
     1021    int blueId(Node v) const {
     1022      LEMON_DEBUG(nodes[v._id].red, "Node has to be blue");
     1023      return nodes[v._id].partition_index;
     1024    }
     1025    static int id(Arc e) { return e._id; }
     1026    static int id(Edge e) { return e._id; }
     1027
     1028    static Node nodeFromId(int id) { return Node(id);}
     1029    static Arc arcFromId(int id) { return Arc(id);}
     1030    static Edge edgeFromId(int id) { return Edge(id);}
     1031
     1032    bool valid(Node n) const {
     1033      return n._id >= 0 && n._id < static_cast<int>(nodes.size());
     1034    }
     1035    bool valid(Arc a) const {
     1036      return a._id >= 0 && a._id < static_cast<int>(arcs.size());
     1037    }
     1038    bool valid(Edge e) const {
     1039      return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size());
     1040    }
     1041
     1042    Node addRedNode() {
     1043      int n = nodes.size();
     1044      nodes.push_back(NodeT());
     1045      nodes[n].first_out = -1;
     1046      nodes[n].red = true;
     1047      if (first_red == -1) {
     1048        nodes[n].partition_index = 0;
     1049      } else {
     1050        nodes[n].partition_index = nodes[first_red].partition_index + 1;
     1051      }
     1052      nodes[n].partition_next = first_red;
     1053      first_red = n;
     1054
     1055      return Node(n);
     1056    }
     1057
     1058    Node addBlueNode() {
     1059      int n = nodes.size();
     1060      nodes.push_back(NodeT());
     1061      nodes[n].first_out = -1;
     1062      nodes[n].red = false;
     1063      if (first_blue == -1) {
     1064        nodes[n].partition_index = 0;
     1065      } else {
     1066        nodes[n].partition_index = nodes[first_blue].partition_index + 1;
     1067      }
     1068      nodes[n].partition_next = first_blue;
     1069      first_blue = n;
     1070
     1071      return Node(n);
     1072    }
     1073
     1074    Edge addEdge(Node u, Node v) {
     1075      int n = arcs.size();
     1076      arcs.push_back(ArcT());
     1077      arcs.push_back(ArcT());
     1078
     1079      arcs[n].target = u._id;
     1080      arcs[n | 1].target = v._id;
     1081
     1082      arcs[n].next_out = nodes[v._id].first_out;
     1083      nodes[v._id].first_out = n;
     1084
     1085      arcs[n | 1].next_out = nodes[u._id].first_out;
     1086      nodes[u._id].first_out = (n | 1);
     1087
     1088      return Edge(n / 2);
     1089    }
     1090
     1091    void clear() {
     1092      arcs.clear();
     1093      nodes.clear();
     1094      first_red = -1;
     1095      first_blue = -1;
     1096    }
     1097
     1098  };
     1099
     1100  typedef BpGraphExtender<SmartBpGraphBase> ExtendedSmartBpGraphBase;
     1101
     1102  /// \ingroup graphs
     1103  ///
     1104  /// \brief A smart undirected graph class.
     1105  ///
     1106  /// \ref SmartBpGraph is a simple and fast graph implementation.
     1107  /// It is also quite memory efficient but at the price
     1108  /// that it does not support node and edge deletion
     1109  /// (except for the Snapshot feature).
     1110  ///
     1111  /// This type fully conforms to the \ref concepts::Graph "Graph concept"
     1112  /// and it also provides some additional functionalities.
     1113  /// Most of its member functions and nested classes are documented
     1114  /// only in the concept class.
     1115  ///
     1116  /// This class provides constant time counting for nodes, edges and arcs.
     1117  ///
     1118  /// \sa concepts::Graph
     1119  /// \sa SmartDigraph
     1120  class SmartBpGraph : public ExtendedSmartBpGraphBase {
     1121    typedef ExtendedSmartBpGraphBase Parent;
     1122
     1123  private:
     1124    /// Graphs are \e not copy constructible. Use GraphCopy instead.
     1125    SmartBpGraph(const SmartBpGraph &) : ExtendedSmartBpGraphBase() {};
     1126    /// \brief Assignment of a graph to another one is \e not allowed.
     1127    /// Use GraphCopy instead.
     1128    void operator=(const SmartBpGraph &) {}
     1129
     1130  public:
     1131
     1132    /// Constructor
     1133
     1134    /// Constructor.
     1135    ///
     1136    SmartBpGraph() {}
     1137
     1138    /// \brief Add a new red node to the graph.
     1139    ///
     1140    /// This function adds a red new node to the graph.
     1141    /// \return The new node.
     1142    Node addRedNode() { return Parent::addRedNode(); }
     1143
     1144    /// \brief Add a new blue node to the graph.
     1145    ///
     1146    /// This function adds a blue new node to the graph.
     1147    /// \return The new node.
     1148    Node addBlueNode() { return Parent::addBlueNode(); }
     1149
     1150    /// \brief Add a new edge to the graph.
     1151    ///
     1152    /// This function adds a new edge to the graph between nodes
     1153    /// \c u and \c v with inherent orientation from node \c u to
     1154    /// node \c v.
     1155    /// \return The new edge.
     1156    Edge addEdge(Node red, Node blue) {
     1157      LEMON_DEBUG(Parent::red(red) && Parent::blue(blue),
     1158                  "Edge has to be formed by a red and a blue nodes");
     1159      return Parent::addEdge(red, blue);
     1160    }
     1161
     1162    /// \brief Node validity check
     1163    ///
     1164    /// This function gives back \c true if the given node is valid,
     1165    /// i.e. it is a real node of the graph.
     1166    ///
     1167    /// \warning A removed node (using Snapshot) could become valid again
     1168    /// if new nodes are added to the graph.
     1169    bool valid(Node n) const { return Parent::valid(n); }
     1170
     1171    /// \brief Edge validity check
     1172    ///
     1173    /// This function gives back \c true if the given edge is valid,
     1174    /// i.e. it is a real edge of the graph.
     1175    ///
     1176    /// \warning A removed edge (using Snapshot) could become valid again
     1177    /// if new edges are added to the graph.
     1178    bool valid(Edge e) const { return Parent::valid(e); }
     1179
     1180    /// \brief Arc validity check
     1181    ///
     1182    /// This function gives back \c true if the given arc is valid,
     1183    /// i.e. it is a real arc of the graph.
     1184    ///
     1185    /// \warning A removed arc (using Snapshot) could become valid again
     1186    /// if new edges are added to the graph.
     1187    bool valid(Arc a) const { return Parent::valid(a); }
     1188
     1189    ///Clear the graph.
     1190
     1191    ///This function erases all nodes and arcs from the graph.
     1192    ///
     1193    void clear() {
     1194      Parent::clear();
     1195    }
     1196
     1197    /// Reserve memory for nodes.
     1198
     1199    /// Using this function, it is possible to avoid superfluous memory
     1200    /// allocation: if you know that the graph you want to build will
     1201    /// be large (e.g. it will contain millions of nodes and/or edges),
     1202    /// then it is worth reserving space for this amount before starting
     1203    /// to build the graph.
     1204    /// \sa reserveEdge()
     1205    void reserveNode(int n) { nodes.reserve(n); };
     1206
     1207    /// Reserve memory for edges.
     1208
     1209    /// Using this function, it is possible to avoid superfluous memory
     1210    /// allocation: if you know that the graph you want to build will
     1211    /// be large (e.g. it will contain millions of nodes and/or edges),
     1212    /// then it is worth reserving space for this amount before starting
     1213    /// to build the graph.
     1214    /// \sa reserveNode()
     1215    void reserveEdge(int m) { arcs.reserve(2 * m); };
     1216
     1217  public:
     1218
     1219    class Snapshot;
     1220
     1221  protected:
     1222
     1223    void saveSnapshot(Snapshot &s)
     1224    {
     1225      s._graph = this;
     1226      s.node_num = nodes.size();
     1227      s.arc_num = arcs.size();
     1228    }
     1229
     1230    void restoreSnapshot(const Snapshot &s)
     1231    {
     1232      while(s.arc_num<arcs.size()) {
     1233        int n=arcs.size()-1;
     1234        Edge arc=edgeFromId(n/2);
     1235        Parent::notifier(Edge()).erase(arc);
     1236        std::vector<Arc> dir;
     1237        dir.push_back(arcFromId(n));
     1238        dir.push_back(arcFromId(n-1));
     1239        Parent::notifier(Arc()).erase(dir);
     1240        nodes[arcs[n-1].target].first_out=arcs[n].next_out;
     1241        nodes[arcs[n].target].first_out=arcs[n-1].next_out;
     1242        arcs.pop_back();
     1243        arcs.pop_back();
     1244      }
     1245      while(s.node_num<nodes.size()) {
     1246        int n=nodes.size()-1;
     1247        Node node = nodeFromId(n);
     1248        if (Parent::red(node)) {
     1249          first_red = nodes[n].partition_next;
     1250          Parent::notifier(RedNode()).erase(node);         
     1251        } else {
     1252          first_blue = nodes[n].partition_next;
     1253          Parent::notifier(BlueNode()).erase(node);
     1254        }
     1255        Parent::notifier(Node()).erase(node);
     1256        nodes.pop_back();
     1257      }
     1258    }
     1259
     1260  public:
     1261
     1262    ///Class to make a snapshot of the graph and to restore it later.
     1263
     1264    ///Class to make a snapshot of the graph and to restore it later.
     1265    ///
     1266    ///The newly added nodes and edges can be removed using the
     1267    ///restore() function. This is the only way for deleting nodes and/or
     1268    ///edges from a SmartBpGraph structure.
     1269    ///
     1270    ///\note After a state is restored, you cannot restore a later state,
     1271    ///i.e. you cannot add the removed nodes and edges again using
     1272    ///another Snapshot instance.
     1273    ///
     1274    ///\warning The validity of the snapshot is not stored due to
     1275    ///performance reasons. If you do not use the snapshot correctly,
     1276    ///it can cause broken program, invalid or not restored state of
     1277    ///the graph or no change.
     1278    class Snapshot
     1279    {
     1280      SmartBpGraph *_graph;
     1281    protected:
     1282      friend class SmartBpGraph;
     1283      unsigned int node_num;
     1284      unsigned int arc_num;
     1285    public:
     1286      ///Default constructor.
     1287
     1288      ///Default constructor.
     1289      ///You have to call save() to actually make a snapshot.
     1290      Snapshot() : _graph(0) {}
     1291      ///Constructor that immediately makes a snapshot
     1292
     1293      /// This constructor immediately makes a snapshot of the given graph.
     1294      ///
     1295      Snapshot(SmartBpGraph &gr) {
     1296        gr.saveSnapshot(*this);
     1297      }
     1298
     1299      ///Make a snapshot.
     1300
     1301      ///This function makes a snapshot of the given graph.
     1302      ///It can be called more than once. In case of a repeated
     1303      ///call, the previous snapshot gets lost.
     1304      void save(SmartBpGraph &gr)
     1305      {
     1306        gr.saveSnapshot(*this);
     1307      }
     1308
     1309      ///Undo the changes until the last snapshot.
     1310
     1311      ///This function undos the changes until the last snapshot
     1312      ///created by save() or Snapshot(SmartBpGraph&).
     1313      void restore()
     1314      {
     1315        _graph->restoreSnapshot(*this);
     1316      }
     1317    };
     1318  };
     1319
    8141320} //namespace lemon
    8151321
    8161322
  • test/bpgraph_test.cc

    diff -r 1a48ab5320b3 -r 13bca1f55d61 test/bpgraph_test.cc
    a b  
    1818
    1919#include <lemon/concepts/bpgraph.h>
    2020//#include <lemon/list_graph.h>
    21 //#include <lemon/smart_graph.h>
     21#include <lemon/smart_graph.h>
    2222//#include <lemon/full_graph.h>
    23 //#include <lemon/grid_graph.h>
    24 //#include <lemon/hypercube_graph.h>
    2523
    2624#include "test_tools.h"
    2725#include "graph_test.h"
     
    2927using namespace lemon;
    3028using namespace lemon::concepts;
    3129
     30template <class BpGraph>
     31void checkBpGraphBuild() {
     32  TEMPLATE_BPGRAPH_TYPEDEFS(BpGraph);
     33
     34  BpGraph G;
     35  checkGraphNodeList(G, 0);
     36  checkGraphRedNodeList(G, 0);
     37  checkGraphBlueNodeList(G, 0);
     38  checkGraphEdgeList(G, 0);
     39  checkGraphArcList(G, 0);
     40
     41  G.reserveNode(3);
     42  G.reserveEdge(3);
     43
     44  Node
     45    rn1 = G.addRedNode();
     46  checkGraphNodeList(G, 1);
     47  checkGraphRedNodeList(G, 1);
     48  checkGraphBlueNodeList(G, 0);
     49  checkGraphEdgeList(G, 0);
     50  checkGraphArcList(G, 0);
     51
     52  Node
     53    bn1 = G.addBlueNode(),
     54    bn2 = G.addBlueNode();
     55  checkGraphNodeList(G, 3);
     56  checkGraphRedNodeList(G, 1);
     57  checkGraphBlueNodeList(G, 2);
     58  checkGraphEdgeList(G, 0);
     59  checkGraphArcList(G, 0);
     60
     61  Edge e1 = G.addEdge(rn1, bn2);
     62  check(G.redNode(e1) == rn1 && G.blueNode(e1) == bn2, "Wrong edge");
     63  check(G.u(e1) == rn1 && G.v(e1) == bn2, "Wrong edge");
     64
     65  checkGraphNodeList(G, 3);
     66  checkGraphRedNodeList(G, 1);
     67  checkGraphBlueNodeList(G, 2);
     68  checkGraphEdgeList(G, 1);
     69  checkGraphArcList(G, 2);
     70
     71  checkGraphIncEdgeArcLists(G, rn1, 1);
     72  checkGraphIncEdgeArcLists(G, bn1, 0);
     73  checkGraphIncEdgeArcLists(G, bn2, 1);
     74
     75  checkGraphConEdgeList(G, 1);
     76  checkGraphConArcList(G, 2);
     77
     78  Edge
     79    e2 = G.addEdge(rn1, bn1),
     80    e3 = G.addEdge(rn1, bn2);
     81
     82  checkGraphNodeList(G, 3);
     83  checkGraphRedNodeList(G, 1);
     84  checkGraphBlueNodeList(G, 2);
     85  checkGraphEdgeList(G, 3);
     86  checkGraphArcList(G, 6);
     87
     88  checkGraphIncEdgeArcLists(G, rn1, 3);
     89  checkGraphIncEdgeArcLists(G, bn1, 1);
     90  checkGraphIncEdgeArcLists(G, bn2, 2);
     91
     92  checkGraphConEdgeList(G, 3);
     93  checkGraphConArcList(G, 6);
     94
     95  checkArcDirections(G);
     96
     97  checkNodeIds(G);
     98  checkRedNodeIds(G);
     99  checkBlueNodeIds(G);
     100  checkArcIds(G);
     101  checkEdgeIds(G);
     102
     103  checkGraphNodeMap(G);
     104  checkGraphRedMap(G);
     105  checkGraphBlueMap(G);
     106  checkGraphArcMap(G);
     107  checkGraphEdgeMap(G);
     108}
     109
     110template <class Graph>
     111void checkBpGraphSnapshot() {
     112  TEMPLATE_BPGRAPH_TYPEDEFS(Graph);
     113
     114  Graph G;
     115  Node
     116    n1 = G.addRedNode(),
     117    n2 = G.addBlueNode(),
     118    n3 = G.addBlueNode();
     119  Edge
     120    e1 = G.addEdge(n1, n2),
     121    e2 = G.addEdge(n1, n3);
     122
     123  checkGraphNodeList(G, 3);
     124  checkGraphRedNodeList(G, 1);
     125  checkGraphBlueNodeList(G, 2);
     126  checkGraphEdgeList(G, 2);
     127  checkGraphArcList(G, 4);
     128
     129  typename Graph::Snapshot snapshot(G);
     130
     131  Node n4 = G.addRedNode();
     132  G.addEdge(n4, n2);
     133  G.addEdge(n4, n3);
     134
     135  checkGraphNodeList(G, 4);
     136  checkGraphRedNodeList(G, 2);
     137  checkGraphBlueNodeList(G, 2);
     138  checkGraphEdgeList(G, 4);
     139  checkGraphArcList(G, 8);
     140
     141  snapshot.restore();
     142
     143  checkGraphNodeList(G, 3);
     144  checkGraphRedNodeList(G, 1);
     145  checkGraphBlueNodeList(G, 2);
     146  checkGraphEdgeList(G, 2);
     147  checkGraphArcList(G, 4);
     148
     149  checkGraphIncEdgeArcLists(G, n1, 2);
     150  checkGraphIncEdgeArcLists(G, n2, 1);
     151  checkGraphIncEdgeArcLists(G, n3, 1);
     152
     153  checkGraphConEdgeList(G, 2);
     154  checkGraphConArcList(G, 4);
     155
     156  checkNodeIds(G);
     157  checkRedNodeIds(G);
     158  checkBlueNodeIds(G);
     159  checkArcIds(G);
     160  checkEdgeIds(G);
     161
     162  checkGraphNodeMap(G);
     163  checkGraphRedMap(G);
     164  checkGraphBlueMap(G);
     165  checkGraphArcMap(G);
     166  checkGraphEdgeMap(G);
     167
     168  G.addRedNode();
     169  snapshot.save(G);
     170
     171  G.addEdge(G.addRedNode(), G.addBlueNode());
     172
     173  snapshot.restore();
     174  snapshot.save(G);
     175
     176  checkGraphNodeList(G, 4);
     177  checkGraphRedNodeList(G, 2);
     178  checkGraphBlueNodeList(G, 2);
     179  checkGraphEdgeList(G, 2);
     180  checkGraphArcList(G, 4);
     181
     182  G.addEdge(G.addRedNode(), G.addBlueNode());
     183
     184  snapshot.restore();
     185
     186  checkGraphNodeList(G, 4);
     187  checkGraphRedNodeList(G, 2);
     188  checkGraphBlueNodeList(G, 2);
     189  checkGraphEdgeList(G, 2);
     190  checkGraphArcList(G, 4);
     191}
     192
     193template <typename Graph>
     194void checkBpGraphValidity() {
     195  TEMPLATE_GRAPH_TYPEDEFS(Graph);
     196  Graph g;
     197
     198  Node
     199    n1 = g.addRedNode(),
     200    n2 = g.addBlueNode(),
     201    n3 = g.addBlueNode();
     202
     203  Edge
     204    e1 = g.addEdge(n1, n2),
     205    e2 = g.addEdge(n1, n3);
     206
     207  check(g.valid(n1), "Wrong validity check");
     208  check(g.valid(e1), "Wrong validity check");
     209  check(g.valid(g.direct(e1, true)), "Wrong validity check");
     210
     211  check(!g.valid(g.nodeFromId(-1)), "Wrong validity check");
     212  check(!g.valid(g.edgeFromId(-1)), "Wrong validity check");
     213  check(!g.valid(g.arcFromId(-1)), "Wrong validity check");
     214}
     215
    32216void checkConcepts() {
    33217  { // Checking graph components
    34218    checkConcept<BaseBpGraphComponent, BaseBpGraphComponent >();
     
    51235    checkConcept<ErasableBpGraphComponent<>,
    52236      ErasableBpGraphComponent<> >();
    53237
    54     checkConcept<ClearableGraphComponent<>,
    55       ClearableGraphComponent<> >();
     238    checkConcept<ClearableBpGraphComponent<>,
     239      ClearableBpGraphComponent<> >();
    56240
    57241  }
    58242  { // Checking skeleton graph
    59243    checkConcept<BpGraph, BpGraph>();
    60244  }
     245  { // Checking SmartBpGraph
     246    checkConcept<BpGraph, SmartBpGraph>();
     247    checkConcept<AlterableBpGraphComponent<>, SmartBpGraph>();
     248    checkConcept<ExtendableBpGraphComponent<>, SmartBpGraph>();
     249    checkConcept<ClearableBpGraphComponent<>, SmartBpGraph>();
     250  }
    61251}
    62252
    63253void checkGraphs() {
     254  { // Checking SmartGraph
     255    checkBpGraphBuild<SmartBpGraph>();
     256    checkBpGraphSnapshot<SmartBpGraph>();
     257    checkBpGraphValidity<SmartBpGraph>();
     258  }
    64259}
    65260
    66261int main() {
  • test/graph_test.h

    diff -r 1a48ab5320b3 -r 13bca1f55d61 test/graph_test.h
    a b  
    4141  }
    4242
    4343  template<class Graph>
     44  void checkGraphRedNodeList(const Graph &G, int cnt)
     45  {
     46    typename Graph::RedIt n(G);
     47    for(int i=0;i<cnt;i++) {
     48      check(n!=INVALID,"Wrong red Node list linking.");
     49      ++n;
     50    }
     51    check(n==INVALID,"Wrong red Node list linking.");
     52    check(countRedNodes(G)==cnt,"Wrong red Node number.");
     53  }
     54
     55  template<class Graph>
     56  void checkGraphBlueNodeList(const Graph &G, int cnt)
     57  {
     58    typename Graph::BlueIt n(G);
     59    for(int i=0;i<cnt;i++) {
     60      check(n!=INVALID,"Wrong blue Node list linking.");
     61      ++n;
     62    }
     63    check(n==INVALID,"Wrong blue Node list linking.");
     64    check(countBlueNodes(G)==cnt,"Wrong blue Node number.");
     65  }
     66
     67  template<class Graph>
    4468  void checkGraphArcList(const Graph &G, int cnt)
    4569  {
    4670    typename Graph::ArcIt e(G);
     
    166190
    167191  template <typename Graph>
    168192  void checkNodeIds(const Graph& G) {
     193    typedef typename Graph::Node Node;
    169194    std::set<int> values;
    170195    for (typename Graph::NodeIt n(G); n != INVALID; ++n) {
    171196      check(G.nodeFromId(G.id(n)) == n, "Wrong id");
     
    173198      check(G.id(n) <= G.maxNodeId(), "Wrong maximum id");
    174199      values.insert(G.id(n));
    175200    }
     201    check(G.maxId(Node()) <= G.maxNodeId(), "Wrong maximum id");
     202  }
     203
     204  template <typename Graph>
     205  void checkRedNodeIds(const Graph& G) {
     206    typedef typename Graph::RedNode RedNode;
     207    std::set<int> values;
     208    for (typename Graph::RedIt n(G); n != INVALID; ++n) {
     209      check(G.red(n), "Wrong partition");
     210      check(G.redId(n) == G.id(RedNode(n)), "Wrong id");
     211      check(values.find(G.redId(n)) == values.end(), "Wrong id");
     212      check(G.redId(n) <= G.maxRedId(), "Wrong maximum id");
     213      values.insert(G.id(n));
     214    }
     215    check(G.maxId(RedNode()) == G.maxRedId(), "Wrong maximum id");
     216  }
     217
     218  template <typename Graph>
     219  void checkBlueNodeIds(const Graph& G) {
     220    typedef typename Graph::BlueNode BlueNode;
     221    std::set<int> values;
     222    for (typename Graph::BlueIt n(G); n != INVALID; ++n) {
     223      check(G.blue(n), "Wrong partition");
     224      check(G.blueId(n) == G.id(BlueNode(n)), "Wrong id");
     225      check(values.find(G.blueId(n)) == values.end(), "Wrong id");
     226      check(G.blueId(n) <= G.maxBlueId(), "Wrong maximum id");
     227      values.insert(G.id(n));
     228    }
     229    check(G.maxId(BlueNode()) == G.maxBlueId(), "Wrong maximum id");
    176230  }
    177231
    178232  template <typename Graph>
    179233  void checkArcIds(const Graph& G) {
     234    typedef typename Graph::Arc Arc;
    180235    std::set<int> values;
    181236    for (typename Graph::ArcIt a(G); a != INVALID; ++a) {
    182237      check(G.arcFromId(G.id(a)) == a, "Wrong id");
     
    184239      check(G.id(a) <= G.maxArcId(), "Wrong maximum id");
    185240      values.insert(G.id(a));
    186241    }
     242    check(G.maxId(Arc()) <= G.maxArcId(), "Wrong maximum id");
    187243  }
    188244
    189245  template <typename Graph>
    190246  void checkEdgeIds(const Graph& G) {
     247    typedef typename Graph::Edge Edge;
    191248    std::set<int> values;
    192249    for (typename Graph::EdgeIt e(G); e != INVALID; ++e) {
    193250      check(G.edgeFromId(G.id(e)) == e, "Wrong id");
     
    195252      check(G.id(e) <= G.maxEdgeId(), "Wrong maximum id");
    196253      values.insert(G.id(e));
    197254    }
     255    check(G.maxId(Edge()) <= G.maxEdgeId(), "Wrong maximum id");
    198256  }
    199257
    200258  template <typename Graph>
     
    228286  }
    229287
    230288  template <typename Graph>
     289  void checkGraphRedMap(const Graph& G) {
     290    typedef typename Graph::Node Node;
     291    typedef typename Graph::RedIt RedIt;
     292
     293    typedef typename Graph::template RedMap<int> IntRedMap;
     294    IntRedMap map(G, 42);
     295    for (RedIt it(G); it != INVALID; ++it) {
     296      check(map[it] == 42, "Wrong map constructor.");
     297    }
     298    int s = 0;
     299    for (RedIt it(G); it != INVALID; ++it) {
     300      map[it] = 0;
     301      check(map[it] == 0, "Wrong operator[].");
     302      map.set(it, s);
     303      check(map[it] == s, "Wrong set.");
     304      ++s;
     305    }
     306    s = s * (s - 1) / 2;
     307    for (RedIt it(G); it != INVALID; ++it) {
     308      s -= map[it];
     309    }
     310    check(s == 0, "Wrong sum.");
     311
     312    // map = constMap<Node>(12);
     313    // for (NodeIt it(G); it != INVALID; ++it) {
     314    //   check(map[it] == 12, "Wrong operator[].");
     315    // }
     316  }
     317
     318  template <typename Graph>
     319  void checkGraphBlueMap(const Graph& G) {
     320    typedef typename Graph::Node Node;
     321    typedef typename Graph::BlueIt BlueIt;
     322
     323    typedef typename Graph::template BlueMap<int> IntBlueMap;
     324    IntBlueMap map(G, 42);
     325    for (BlueIt it(G); it != INVALID; ++it) {
     326      check(map[it] == 42, "Wrong map constructor.");
     327    }
     328    int s = 0;
     329    for (BlueIt it(G); it != INVALID; ++it) {
     330      map[it] = 0;
     331      check(map[it] == 0, "Wrong operator[].");
     332      map.set(it, s);
     333      check(map[it] == s, "Wrong set.");
     334      ++s;
     335    }
     336    s = s * (s - 1) / 2;
     337    for (BlueIt it(G); it != INVALID; ++it) {
     338      s -= map[it];
     339    }
     340    check(s == 0, "Wrong sum.");
     341
     342    // map = constMap<Node>(12);
     343    // for (NodeIt it(G); it != INVALID; ++it) {
     344    //   check(map[it] == 12, "Wrong operator[].");
     345    // }
     346  }
     347
     348  template <typename Graph>
    231349  void checkGraphArcMap(const Graph& G) {
    232350    typedef typename Graph::Arc Arc;
    233351    typedef typename Graph::ArcIt ArcIt;
  • lemon/bits/graph_extender.h

    # HG changeset patch
    # User Balazs Dezso <deba@inf.elte.hu>
    # Date 1289771312 -3600
    # Node ID 261a074fead9e48f4b12a6f201a9d1ade3c1a2a4
    # Parent  13bca1f55d6121d9df328aa3b1f89e0f8c90d518
    FullBpGraph implementation
    
    diff -r 13bca1f55d61 -r 261a074fead9 lemon/bits/graph_extender.h
    a b  
    841841      return Parent::edgeFromId(id);
    842842    }
    843843
     844    Node u(Edge e) const { return redNode(e); }
     845    Node v(Edge e) const { return blueNode(e); }
     846
    844847    Node oppositeNode(const Node &n, const Edge &e) const {
    845       if( n == Parent::u(e))
    846         return Parent::v(e);
    847       else if( n == Parent::v(e))
    848         return Parent::u(e);
     848      if( n == u(e))
     849        return v(e);
     850      else if( n == v(e))
     851        return u(e);
    849852      else
    850853        return INVALID;
    851854    }
     
    856859
    857860    using Parent::direct;
    858861    Arc direct(const Edge &edge, const Node &node) const {
    859       return Parent::direct(edge, Parent::u(edge) == node);
     862      return Parent::direct(edge, Parent::redNode(edge) == node);
    860863    }
    861864
    862865    // Alterable extension
  • lemon/core.h

    diff -r 13bca1f55d61 -r 261a074fead9 lemon/core.h
    a b  
    164164  typedef BpGraph::RedIt RedIt;                                         \
    165165  typedef BpGraph::RedMap<bool> BoolRedMap;                             \
    166166  typedef BpGraph::RedMap<int> IntRedMap;                               \
    167   typedef BpGraph::RedMap<double> DoubleRedMap                          \
     167  typedef BpGraph::RedMap<double> DoubleRedMap;                         \
    168168  typedef BpGraph::BlueNode BlueNode;                                   \
    169169  typedef BpGraph::BlueIt BlueIt;                                       \
    170170  typedef BpGraph::BlueMap<bool> BoolBlueMap;                           \
  • lemon/full_graph.h

    diff -r 13bca1f55d61 -r 261a074fead9 lemon/full_graph.h
    a b  
    621621
    622622  };
    623623
     624  class FullBpGraphBase {
     625
     626  protected:
     627
     628    int _red_num, _blue_num;
     629    int _node_num, _edge_num;
     630
     631  public:
     632
     633    typedef FullBpGraphBase Graph;
     634
     635    class Node;
     636    class Arc;
     637    class Edge;
     638
     639    class Node {
     640      friend class FullBpGraphBase;
     641    protected:
     642
     643      int _id;
     644      explicit Node(int id) { _id = id;}
     645
     646    public:
     647      Node() {}
     648      Node (Invalid) { _id = -1; }
     649      bool operator==(const Node& node) const {return _id == node._id;}
     650      bool operator!=(const Node& node) const {return _id != node._id;}
     651      bool operator<(const Node& node) const {return _id < node._id;}
     652    };
     653
     654    class Edge {
     655      friend class FullBpGraphBase;
     656    protected:
     657
     658      int _id;
     659      explicit Edge(int id) { _id = id;}
     660
     661    public:
     662      Edge() {}
     663      Edge (Invalid) { _id = -1; }
     664      bool operator==(const Edge& arc) const {return _id == arc._id;}
     665      bool operator!=(const Edge& arc) const {return _id != arc._id;}
     666      bool operator<(const Edge& arc) const {return _id < arc._id;}
     667    };
     668
     669    class Arc {
     670      friend class FullBpGraphBase;
     671    protected:
     672
     673      int _id;
     674      explicit Arc(int id) { _id = id;}
     675
     676    public:
     677      operator Edge() const {
     678        return _id != -1 ? edgeFromId(_id / 2) : INVALID;
     679      }
     680
     681      Arc() {}
     682      Arc (Invalid) { _id = -1; }
     683      bool operator==(const Arc& arc) const {return _id == arc._id;}
     684      bool operator!=(const Arc& arc) const {return _id != arc._id;}
     685      bool operator<(const Arc& arc) const {return _id < arc._id;}
     686    };
     687
     688
     689  protected:
     690
     691    FullBpGraphBase()
     692      : _red_num(0), _blue_num(0), _node_num(0), _edge_num(0) {}
     693
     694    void construct(int redNum, int blueNum) {
     695      _red_num = redNum; _blue_num = blueNum;
     696      _node_num = redNum + blueNum; _edge_num = redNum * blueNum;
     697    }
     698
     699  public:
     700
     701    typedef True NodeNumTag;
     702    typedef True EdgeNumTag;
     703    typedef True ArcNumTag;
     704
     705    int nodeNum() const { return _node_num; }
     706    int redNum() const { return _red_num; }
     707    int blueNum() const { return _blue_num; }
     708    int edgeNum() const { return _edge_num; }
     709    int arcNum() const { return 2 * _edge_num; }
     710
     711    int maxNodeId() const { return _node_num - 1; }
     712    int maxRedId() const { return _red_num - 1; }
     713    int maxBlueId() const { return _blue_num - 1; }
     714    int maxEdgeId() const { return _edge_num - 1; }
     715    int maxArcId() const { return 2 * _edge_num - 1; }
     716
     717    bool red(Node n) const { return n._id < _red_num; }
     718    bool blue(Node n) const { return n._id >= _red_num; }
     719
     720    Node source(Arc a) const {
     721      if (a._id & 1) {
     722        return Node((a._id >> 1) % _red_num);
     723      } else {
     724        return Node((a._id >> 1) / _red_num + _red_num);
     725      }
     726    }
     727    Node target(Arc a) const {
     728      if (a._id & 1) {
     729        return Node((a._id >> 1) / _red_num + _red_num);
     730      } else {
     731        return Node((a._id >> 1) % _red_num);
     732      }
     733    }
     734
     735    Node redNode(Edge e) const {
     736      return Node(e._id % _red_num);
     737    }
     738    Node blueNode(Edge e) const {
     739      return Node(e._id / _red_num + _red_num);
     740    }
     741
     742    static bool direction(Arc a) {
     743      return (a._id & 1) == 1;
     744    }
     745
     746    static Arc direct(Edge e, bool d) {
     747      return Arc(e._id * 2 + (d ? 1 : 0));
     748    }
     749
     750    void first(Node& node) const {
     751      node._id = _node_num - 1;
     752    }
     753
     754    static void next(Node& node) {
     755      --node._id;
     756    }
     757
     758    void firstRed(Node& node) const {
     759      node._id = _red_num - 1;
     760    }
     761
     762    static void nextRed(Node& node) {
     763      --node._id;
     764    }
     765
     766    void firstBlue(Node& node) const {
     767      if (_red_num == _node_num) node._id = -1;
     768      else node._id = _node_num - 1;
     769    }
     770
     771    void nextBlue(Node& node) const {
     772      if (node._id == _red_num) node._id = -1;
     773      else --node._id;
     774    }
     775
     776    void first(Arc& arc) const {
     777      arc._id = 2 * _edge_num - 1;
     778    }
     779
     780    static void next(Arc& arc) {
     781      --arc._id;
     782    }
     783
     784    void first(Edge& arc) const {
     785      arc._id = _edge_num - 1;
     786    }
     787
     788    static void next(Edge& arc) {
     789      --arc._id;
     790    }
     791
     792    void firstOut(Arc &a, const Node& v) const {
     793      if (v._id < _red_num) {
     794        a._id = 2 * (v._id + _red_num * (_blue_num - 1)) + 1;
     795      } else {
     796        a._id = 2 * (_red_num - 1 + _red_num * (v._id - _red_num));
     797      }
     798    }
     799    void nextOut(Arc &a) const {
     800      if (a._id & 1) {
     801        a._id -= 2 * _red_num;
     802        if (a._id < 0) a._id = -1;
     803      } else {
     804        if (a._id % (2 * _red_num) == 0) a._id = -1;
     805        else a._id -= 2;
     806      }
     807    }
     808
     809    void firstIn(Arc &a, const Node& v) const {
     810      if (v._id < _red_num) {
     811        a._id = 2 * (v._id + _red_num * (_blue_num - 1));
     812      } else {
     813        a._id = 2 * (_red_num - 1 + _red_num * (v._id - _red_num)) + 1;
     814      }
     815    }
     816    void nextIn(Arc &a) const {
     817      if (a._id & 1) {
     818        if (a._id % (2 * _red_num) == 1) a._id = -1;
     819        else a._id -= 2;
     820      } else {
     821        a._id -= 2 * _red_num;
     822        if (a._id < 0) a._id = -1;
     823      }
     824    }
     825
     826    void firstInc(Edge &e, bool& d, const Node& v) const {
     827      if (v._id < _red_num) {
     828        d = true;
     829        e._id = v._id + _red_num * (_blue_num - 1);
     830      } else {
     831        d = false;
     832        e._id = _red_num - 1 + _red_num * (v._id - _red_num);
     833      }
     834    }
     835    void nextInc(Edge &e, bool& d) const {
     836      if (d) {
     837        e._id -= _red_num;
     838        if (e._id < 0) e._id = -1;
     839      } else {
     840        if (e._id % _red_num == 0) e._id = -1;
     841        else --e._id;
     842      }
     843    }
     844
     845    static int id(Node v) { return v._id; }
     846    int redId(Node v) const {
     847      LEMON_DEBUG(v._id < _red_num, "Node has to be red");
     848      return v._id;
     849    }
     850    int blueId(Node v) const {
     851      LEMON_DEBUG(v._id >= _red_num, "Node has to be blue");
     852      return v._id - _red_num;
     853    }
     854    static int id(Arc e) { return e._id; }
     855    static int id(Edge e) { return e._id; }
     856   
     857    static Node nodeFromId(int id) { return Node(id);}
     858    static Arc arcFromId(int id) { return Arc(id);}
     859    static Edge edgeFromId(int id) { return Edge(id);}
     860
     861    bool valid(Node n) const {
     862      return n._id >= 0 && n._id < _node_num;
     863    }
     864    bool valid(Arc a) const {
     865      return a._id >= 0 && a._id < 2 * _edge_num;
     866    }
     867    bool valid(Edge e) const {
     868      return e._id >= 0 && e._id < _edge_num;
     869    }
     870
     871    Node redNode(int index) const {
     872      return Node(index);
     873    }
     874
     875    int redIndex(Node n) const {
     876      return n._id;
     877    }
     878
     879    Node blueNode(int index) const {
     880      return Node(index + _red_num);
     881    }
     882
     883    int blueIndex(Node n) const {
     884      return n._id - _red_num;
     885    }
     886       
     887    void clear() {
     888      _red_num = 0; _blue_num = 0;
     889      _node_num = 0; _edge_num = 0;
     890    }
     891
     892    Edge edge(const Node& u, const Node& v) const {
     893      if (u._id < _red_num) {
     894        if (v._id < _red_num) {
     895          return Edge(-1);
     896        } else {
     897          return Edge(u._id + _red_num * (v._id - _red_num));
     898        }
     899      } else {
     900        if (v._id < _red_num) {
     901          return Edge(v._id + _red_num * (u._id - _red_num));
     902        } else {
     903          return Edge(-1);
     904        }
     905      }
     906    }
     907
     908    Arc arc(const Node& u, const Node& v) const {
     909      if (u._id < _red_num) {
     910        if (v._id < _red_num) {
     911          return Arc(-1);
     912        } else {
     913          return Arc(2 * (u._id + _red_num * (v._id - _red_num)) + 1);
     914        }
     915      } else {
     916        if (v._id < _red_num) {
     917          return Arc(2 * (v._id + _red_num * (u._id - _red_num)));
     918        } else {
     919          return Arc(-1);
     920        }
     921      }
     922    }
     923
     924    typedef True FindEdgeTag;
     925    typedef True FindArcTag;
     926
     927    Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
     928      return prev != INVALID ? INVALID : edge(u, v);
     929    }
     930
     931    Arc findArc(Node s, Node t, Arc prev = INVALID) const {
     932      return prev != INVALID ? INVALID : arc(s, t);
     933    }
     934
     935  };
     936
     937  typedef BpGraphExtender<FullBpGraphBase> ExtendedFullBpGraphBase;
     938
     939  /// \ingroup graphs
     940  ///
     941  /// \brief An undirected full bipartite graph class.
     942  ///
     943  /// FullBpGraph is a simple and fast implmenetation of undirected
     944  /// full bipartite graphs. It contains an edge between every
     945  /// red-blue pairs of nodes, therefore the number of edges is
     946  /// <tt>nr*nb</tt>.  This class is completely static and it needs
     947  /// constant memory space.  Thus you can neither add nor delete
     948  /// nodes or edges, however the structure can be resized using
     949  /// resize().
     950  ///
     951  /// This type fully conforms to the \ref concepts::BpGraph "BpGraph concept".
     952  /// Most of its member functions and nested classes are documented
     953  /// only in the concept class.
     954  ///
     955  /// This class provides constant time counting for nodes, edges and arcs.
     956  ///
     957  /// \sa FullGraph
     958  class FullBpGraph : public ExtendedFullBpGraphBase {
     959  public:
     960
     961    typedef ExtendedFullBpGraphBase Parent;
     962
     963    /// \brief Default constructor.
     964    ///
     965    /// Default constructor. The number of nodes and edges will be zero.
     966    FullBpGraph() { construct(0, 0); }
     967
     968    /// \brief Constructor
     969    ///
     970    /// Constructor.
     971    /// \param redNum The number of the red nodes.
     972    /// \param blueNum The number of the blue nodes.
     973    FullBpGraph(int redNum, int blueNum) { construct(redNum, blueNum); }
     974
     975    /// \brief Resizes the graph
     976    ///
     977    /// This function resizes the graph. It fully destroys and
     978    /// rebuilds the structure, therefore the maps of the graph will be
     979    /// reallocated automatically and the previous values will be lost.
     980    void resize(int redNum, int blueNum) {
     981      Parent::notifier(Arc()).clear();
     982      Parent::notifier(Edge()).clear();
     983      Parent::notifier(Node()).clear();
     984      Parent::notifier(BlueNode()).clear();
     985      Parent::notifier(RedNode()).clear();
     986      construct(redNum, blueNum);
     987      Parent::notifier(RedNode()).build();
     988      Parent::notifier(BlueNode()).build();
     989      Parent::notifier(Node()).build();
     990      Parent::notifier(Edge()).build();
     991      Parent::notifier(Arc()).build();
     992    }
     993
     994    /// \brief Returns the red node with the given index.
     995    ///
     996    /// Returns the red node with the given index. Since this
     997    /// structure is completely static, the red nodes can be indexed
     998    /// with integers from the range <tt>[0..redNum()-1]</tt>.
     999    /// \sa redIndex()
     1000    Node redNode(int index) const { return Parent::redNode(index); }
     1001
     1002    /// \brief Returns the index of the given red node.
     1003    ///
     1004    /// Returns the index of the given red node. Since this structure
     1005    /// is completely static, the red nodes can be indexed with
     1006    /// integers from the range <tt>[0..redNum()-1]</tt>.
     1007    ///
     1008    /// \sa operator()()
     1009    int redIndex(Node node) const { return Parent::redIndex(node); }
     1010
     1011    /// \brief Returns the blue node with the given index.
     1012    ///
     1013    /// Returns the blue node with the given index. Since this
     1014    /// structure is completely static, the blue nodes can be indexed
     1015    /// with integers from the range <tt>[0..blueNum()-1]</tt>.
     1016    /// \sa blueIndex()
     1017    Node blueNode(int index) const { return Parent::blueNode(index); }
     1018
     1019    /// \brief Returns the index of the given blue node.
     1020    ///
     1021    /// Returns the index of the given blue node. Since this structure
     1022    /// is completely static, the blue nodes can be indexed with
     1023    /// integers from the range <tt>[0..blueNum()-1]</tt>.
     1024    ///
     1025    /// \sa operator()()
     1026    int blueIndex(Node node) const { return Parent::blueIndex(node); }
     1027
     1028    /// \brief Returns the edge which connects the given nodes.
     1029    ///
     1030    /// Returns the edge which connects the given nodes.
     1031    Edge edge(const Node& u, const Node& v) const {
     1032      return Parent::edge(u, v);
     1033    }
     1034
     1035    /// \brief Returns the arc which connects the given nodes.
     1036    ///
     1037    /// Returns the arc which connects the given nodes.
     1038    Arc arc(const Node& u, const Node& v) const {
     1039      return Parent::arc(u, v);
     1040    }
     1041
     1042    /// \brief Number of nodes.
     1043    int nodeNum() const { return Parent::nodeNum(); }
     1044    /// \brief Number of red nodes.
     1045    int redNum() const { return Parent::redNum(); }
     1046    /// \brief Number of blue nodes.
     1047    int blueNum() const { return Parent::blueNum(); }
     1048    /// \brief Number of arcs.
     1049    int arcNum() const { return Parent::arcNum(); }
     1050    /// \brief Number of edges.
     1051    int edgeNum() const { return Parent::edgeNum(); }
     1052  };
     1053
    6241054
    6251055} //namespace lemon
    6261056
  • lemon/smart_graph.h

    diff -r 13bca1f55d61 -r 261a074fead9 lemon/smart_graph.h
    a b  
    925925    Node redNode(Edge e) const { return Node(arcs[2 * e._id].target); }
    926926    Node blueNode(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
    927927
    928     Node u(Edge e) const { return redNode(e); }
    929     Node v(Edge e) const { return blueNode(e); }
    930 
    931928    static bool direction(Arc a) {
    932929      return (a._id & 1) == 1;
    933930    }
     
    11011098
    11021099  /// \ingroup graphs
    11031100  ///
    1104   /// \brief A smart undirected graph class.
     1101  /// \brief A smart undirected bipartite graph class.
    11051102  ///
    1106   /// \ref SmartBpGraph is a simple and fast graph implementation.
     1103  /// \ref SmartBpGraph is a simple and fast bipartite graph implementation.
    11071104  /// It is also quite memory efficient but at the price
    11081105  /// that it does not support node and edge deletion
    11091106  /// (except for the Snapshot feature).
    11101107  ///
    1111   /// This type fully conforms to the \ref concepts::Graph "Graph concept"
     1108  /// This type fully conforms to the \ref concepts::BpGraph "BpGraph concept"
    11121109  /// and it also provides some additional functionalities.
    11131110  /// Most of its member functions and nested classes are documented
    11141111  /// only in the concept class.
    11151112  ///
    11161113  /// This class provides constant time counting for nodes, edges and arcs.
    11171114  ///
    1118   /// \sa concepts::Graph
    1119   /// \sa SmartDigraph
     1115  /// \sa concepts::BpGraph
     1116  /// \sa SmartGraph
    11201117  class SmartBpGraph : public ExtendedSmartBpGraphBase {
    11211118    typedef ExtendedSmartBpGraphBase Parent;
    11221119
  • test/bpgraph_test.cc

    diff -r 13bca1f55d61 -r 261a074fead9 test/bpgraph_test.cc
    a b  
    1919#include <lemon/concepts/bpgraph.h>
    2020//#include <lemon/list_graph.h>
    2121#include <lemon/smart_graph.h>
    22 //#include <lemon/full_graph.h>
     22#include <lemon/full_graph.h>
    2323
    2424#include "test_tools.h"
    2525#include "graph_test.h"
     
    250250  }
    251251}
    252252
     253void checkFullBpGraph(int redNum, int blueNum) {
     254  typedef FullBpGraph BpGraph;
     255  BPGRAPH_TYPEDEFS(BpGraph);
     256
     257  BpGraph G(redNum, blueNum);
     258  checkGraphNodeList(G, redNum + blueNum);
     259  checkGraphRedNodeList(G, redNum);
     260  checkGraphBlueNodeList(G, blueNum);
     261  checkGraphEdgeList(G, redNum * blueNum);
     262  checkGraphArcList(G, 2 * redNum * blueNum);
     263
     264  G.resize(redNum, blueNum);
     265  checkGraphNodeList(G, redNum + blueNum);
     266  checkGraphRedNodeList(G, redNum);
     267  checkGraphBlueNodeList(G, blueNum);
     268  checkGraphEdgeList(G, redNum * blueNum);
     269  checkGraphArcList(G, 2 * redNum * blueNum);
     270
     271  for (RedIt n(G); n != INVALID; ++n) {
     272    checkGraphOutArcList(G, n, blueNum);
     273    checkGraphInArcList(G, n, blueNum);
     274    checkGraphIncEdgeList(G, n, blueNum);
     275  }
     276
     277  for (BlueIt n(G); n != INVALID; ++n) {
     278    checkGraphOutArcList(G, n, redNum);
     279    checkGraphInArcList(G, n, redNum);
     280    checkGraphIncEdgeList(G, n, redNum);
     281  }
     282
     283  checkGraphConArcList(G, 2 * redNum * blueNum);
     284  checkGraphConEdgeList(G, redNum * blueNum);
     285
     286  checkArcDirections(G);
     287
     288  checkNodeIds(G);
     289  checkRedNodeIds(G);
     290  checkBlueNodeIds(G);
     291  checkArcIds(G);
     292  checkEdgeIds(G);
     293
     294  checkGraphNodeMap(G);
     295  checkGraphRedMap(G);
     296  checkGraphBlueMap(G);
     297  checkGraphArcMap(G);
     298  checkGraphEdgeMap(G);
     299
     300  for (int i = 0; i < G.redNum(); ++i) {
     301    check(G.red(G.redNode(i)), "Wrong node");
     302    check(G.redIndex(G.redNode(i)) == i, "Wrong index");
     303  }
     304
     305  for (int i = 0; i < G.blueNum(); ++i) {
     306    check(G.blue(G.blueNode(i)), "Wrong node");
     307    check(G.blueIndex(G.blueNode(i)) == i, "Wrong index");
     308  }
     309
     310  for (NodeIt u(G); u != INVALID; ++u) {
     311    for (NodeIt v(G); v != INVALID; ++v) {
     312      Edge e = G.edge(u, v);
     313      Arc a = G.arc(u, v);
     314      if (G.red(u) == G.red(v)) {
     315        check(e == INVALID, "Wrong edge lookup");
     316        check(a == INVALID, "Wrong arc lookup");
     317      } else {
     318        check((G.u(e) == u && G.v(e) == v) ||
     319              (G.u(e) == v && G.v(e) == u), "Wrong edge lookup");
     320        check(G.source(a) == u && G.target(a) == v, "Wrong arc lookup");
     321      }
     322    }
     323  }
     324
     325}
     326
    253327void checkGraphs() {
    254328  { // Checking SmartGraph
    255329    checkBpGraphBuild<SmartBpGraph>();
    256330    checkBpGraphSnapshot<SmartBpGraph>();
    257331    checkBpGraphValidity<SmartBpGraph>();
    258332  }
     333  { // Checking FullBpGraph
     334    checkFullBpGraph(6, 8);
     335    checkFullBpGraph(7, 4);
     336  }
    259337}
    260338
    261339int main() {
  • lemon/list_graph.h

    # HG changeset patch
    # User Balazs Dezso <deba@inf.elte.hu>
    # Date 1289810768 -3600
    # Node ID 1c825ca10e1f6228b4c1adde25272502ce5cdb6a
    # Parent  261a074fead9e48f4b12a6f201a9d1ade3c1a2a4
    ListBpGraph implementation
    
    diff -r 261a074fead9 -r 1c825ca10e1f lemon/list_graph.h
    a b  
    15991599  };
    16001600
    16011601  /// @}
     1602
     1603  class ListBpGraphBase {
     1604
     1605  protected:
     1606
     1607    struct NodeT {
     1608      int first_out;
     1609      int prev, next;
     1610      int partition_prev, partition_next;
     1611      int partition_index;
     1612      bool red;
     1613    };
     1614
     1615    struct ArcT {
     1616      int target;
     1617      int prev_out, next_out;
     1618    };
     1619
     1620    std::vector<NodeT> nodes;
     1621
     1622    int first_node, first_red, first_blue;
     1623    int max_red, max_blue;
     1624
     1625    int first_free_red, first_free_blue;
     1626
     1627    std::vector<ArcT> arcs;
     1628
     1629    int first_free_arc;
     1630
     1631  public:
     1632
     1633    typedef ListBpGraphBase BpGraph;
     1634
     1635    class Node {
     1636      friend class ListBpGraphBase;
     1637    protected:
     1638
     1639      int id;
     1640      explicit Node(int pid) { id = pid;}
     1641
     1642    public:
     1643      Node() {}
     1644      Node (Invalid) { id = -1; }
     1645      bool operator==(const Node& node) const {return id == node.id;}
     1646      bool operator!=(const Node& node) const {return id != node.id;}
     1647      bool operator<(const Node& node) const {return id < node.id;}
     1648    };
     1649
     1650    class Edge {
     1651      friend class ListBpGraphBase;
     1652    protected:
     1653
     1654      int id;
     1655      explicit Edge(int pid) { id = pid;}
     1656
     1657    public:
     1658      Edge() {}
     1659      Edge (Invalid) { id = -1; }
     1660      bool operator==(const Edge& edge) const {return id == edge.id;}
     1661      bool operator!=(const Edge& edge) const {return id != edge.id;}
     1662      bool operator<(const Edge& edge) const {return id < edge.id;}
     1663    };
     1664
     1665    class Arc {
     1666      friend class ListBpGraphBase;
     1667    protected:
     1668
     1669      int id;
     1670      explicit Arc(int pid) { id = pid;}
     1671
     1672    public:
     1673      operator Edge() const {
     1674        return id != -1 ? edgeFromId(id / 2) : INVALID;
     1675      }
     1676
     1677      Arc() {}
     1678      Arc (Invalid) { id = -1; }
     1679      bool operator==(const Arc& arc) const {return id == arc.id;}
     1680      bool operator!=(const Arc& arc) const {return id != arc.id;}
     1681      bool operator<(const Arc& arc) const {return id < arc.id;}
     1682    };
     1683
     1684    ListBpGraphBase()
     1685      : nodes(), first_node(-1),
     1686        first_red(-1), first_blue(-1),
     1687        max_red(-1), max_blue(-1),
     1688        first_free_red(-1), first_free_blue(-1),
     1689        arcs(), first_free_arc(-1) {}
     1690
     1691
     1692    bool red(Node n) const { return nodes[n.id].red; }
     1693    bool blue(Node n) const { return !nodes[n.id].red; }
     1694
     1695    int maxNodeId() const { return nodes.size()-1; }
     1696    int maxRedId() const { return max_red; }
     1697    int maxBlueId() const { return max_blue; }
     1698    int maxEdgeId() const { return arcs.size() / 2 - 1; }
     1699    int maxArcId() const { return arcs.size()-1; }
     1700
     1701    Node source(Arc e) const { return Node(arcs[e.id ^ 1].target); }
     1702    Node target(Arc e) const { return Node(arcs[e.id].target); }
     1703
     1704    Node redNode(Edge e) const { return Node(arcs[2 * e.id].target); }
     1705    Node blueNode(Edge e) const { return Node(arcs[2 * e.id + 1].target); }
     1706
     1707    static bool direction(Arc e) {
     1708      return (e.id & 1) == 1;
     1709    }
     1710
     1711    static Arc direct(Edge e, bool d) {
     1712      return Arc(e.id * 2 + (d ? 1 : 0));
     1713    }
     1714
     1715    void first(Node& node) const {
     1716      node.id = first_node;
     1717    }
     1718
     1719    void next(Node& node) const {
     1720      node.id = nodes[node.id].next;
     1721    }
     1722
     1723    void firstRed(Node& node) const {
     1724      node.id = first_red;
     1725    }
     1726
     1727    void nextRed(Node& node) const {
     1728      node.id = nodes[node.id].partition_next;
     1729    }
     1730
     1731    void firstBlue(Node& node) const {
     1732      node.id = first_blue;
     1733    }
     1734
     1735    void nextBlue(Node& node) const {
     1736      node.id = nodes[node.id].partition_next;
     1737    }   
     1738
     1739    void first(Arc& e) const {
     1740      int n = first_node;
     1741      while (n != -1 && nodes[n].first_out == -1) {
     1742        n = nodes[n].next;
     1743      }
     1744      e.id = (n == -1) ? -1 : nodes[n].first_out;
     1745    }
     1746
     1747    void next(Arc& e) const {
     1748      if (arcs[e.id].next_out != -1) {
     1749        e.id = arcs[e.id].next_out;
     1750      } else {
     1751        int n = nodes[arcs[e.id ^ 1].target].next;
     1752        while(n != -1 && nodes[n].first_out == -1) {
     1753          n = nodes[n].next;
     1754        }
     1755        e.id = (n == -1) ? -1 : nodes[n].first_out;
     1756      }
     1757    }
     1758
     1759    void first(Edge& e) const {
     1760      int n = first_node;
     1761      while (n != -1) {
     1762        e.id = nodes[n].first_out;
     1763        while ((e.id & 1) != 1) {
     1764          e.id = arcs[e.id].next_out;
     1765        }
     1766        if (e.id != -1) {
     1767          e.id /= 2;
     1768          return;
     1769        }
     1770        n = nodes[n].next;
     1771      }
     1772      e.id = -1;
     1773    }
     1774
     1775    void next(Edge& e) const {
     1776      int n = arcs[e.id * 2].target;
     1777      e.id = arcs[(e.id * 2) | 1].next_out;
     1778      while ((e.id & 1) != 1) {
     1779        e.id = arcs[e.id].next_out;
     1780      }
     1781      if (e.id != -1) {
     1782        e.id /= 2;
     1783        return;
     1784      }
     1785      n = nodes[n].next;
     1786      while (n != -1) {
     1787        e.id = nodes[n].first_out;
     1788        while ((e.id & 1) != 1) {
     1789          e.id = arcs[e.id].next_out;
     1790        }
     1791        if (e.id != -1) {
     1792          e.id /= 2;
     1793          return;
     1794        }
     1795        n = nodes[n].next;
     1796      }
     1797      e.id = -1;
     1798    }
     1799
     1800    void firstOut(Arc &e, const Node& v) const {
     1801      e.id = nodes[v.id].first_out;
     1802    }
     1803    void nextOut(Arc &e) const {
     1804      e.id = arcs[e.id].next_out;
     1805    }
     1806
     1807    void firstIn(Arc &e, const Node& v) const {
     1808      e.id = ((nodes[v.id].first_out) ^ 1);
     1809      if (e.id == -2) e.id = -1;
     1810    }
     1811    void nextIn(Arc &e) const {
     1812      e.id = ((arcs[e.id ^ 1].next_out) ^ 1);
     1813      if (e.id == -2) e.id = -1;
     1814    }
     1815
     1816    void firstInc(Edge &e, bool& d, const Node& v) const {
     1817      int a = nodes[v.id].first_out;
     1818      if (a != -1 ) {
     1819        e.id = a / 2;
     1820        d = ((a & 1) == 1);
     1821      } else {
     1822        e.id = -1;
     1823        d = true;
     1824      }
     1825    }
     1826    void nextInc(Edge &e, bool& d) const {
     1827      int a = (arcs[(e.id * 2) | (d ? 1 : 0)].next_out);
     1828      if (a != -1 ) {
     1829        e.id = a / 2;
     1830        d = ((a & 1) == 1);
     1831      } else {
     1832        e.id = -1;
     1833        d = true;
     1834      }
     1835    }
     1836
     1837    static int id(Node v) { return v.id; }
     1838    int redId(Node v) const {
     1839      LEMON_DEBUG(nodes[v.id].red, "Node has to be red");
     1840      return nodes[v.id].partition_index;
     1841    }
     1842    int blueId(Node v) const {
     1843      LEMON_DEBUG(!nodes[v.id].red, "Node has to be blue");
     1844      return nodes[v.id].partition_index;
     1845    }
     1846    static int id(Arc e) { return e.id; }
     1847    static int id(Edge e) { return e.id; }
     1848
     1849    static Node nodeFromId(int id) { return Node(id);}
     1850    static Arc arcFromId(int id) { return Arc(id);}
     1851    static Edge edgeFromId(int id) { return Edge(id);}
     1852
     1853    bool valid(Node n) const {
     1854      return n.id >= 0 && n.id < static_cast<int>(nodes.size()) &&
     1855        nodes[n.id].prev != -2;
     1856    }
     1857
     1858    bool valid(Arc a) const {
     1859      return a.id >= 0 && a.id < static_cast<int>(arcs.size()) &&
     1860        arcs[a.id].prev_out != -2;
     1861    }
     1862
     1863    bool valid(Edge e) const {
     1864      return e.id >= 0 && 2 * e.id < static_cast<int>(arcs.size()) &&
     1865        arcs[2 * e.id].prev_out != -2;
     1866    }
     1867
     1868    Node addRedNode() {
     1869      int n;
     1870
     1871      if(first_free_red==-1) {
     1872        n = nodes.size();
     1873        nodes.push_back(NodeT());
     1874        nodes[n].partition_index = ++max_red;
     1875        nodes[n].red = true;
     1876      } else {
     1877        n = first_free_red;
     1878        first_free_red = nodes[n].next;
     1879      }
     1880
     1881      nodes[n].next = first_node;
     1882      if (first_node != -1) nodes[first_node].prev = n;
     1883      first_node = n;
     1884      nodes[n].prev = -1;
     1885
     1886      nodes[n].partition_next = first_red;
     1887      if (first_red != -1) nodes[first_red].partition_prev = n;
     1888      first_red = n;
     1889      nodes[n].partition_prev = -1;
     1890
     1891      nodes[n].first_out = -1;
     1892
     1893      return Node(n);
     1894    }
     1895
     1896    Node addBlueNode() {
     1897      int n;
     1898
     1899      if(first_free_blue==-1) {
     1900        n = nodes.size();
     1901        nodes.push_back(NodeT());
     1902        nodes[n].partition_index = ++max_blue;
     1903        nodes[n].red = false;
     1904      } else {
     1905        n = first_free_blue;
     1906        first_free_blue = nodes[n].next;
     1907      }
     1908
     1909      nodes[n].next = first_node;
     1910      if (first_node != -1) nodes[first_node].prev = n;
     1911      first_node = n;
     1912      nodes[n].prev = -1;
     1913
     1914      nodes[n].partition_next = first_blue;
     1915      if (first_blue != -1) nodes[first_blue].partition_prev = n;
     1916      first_blue = n;
     1917      nodes[n].partition_prev = -1;
     1918
     1919      nodes[n].first_out = -1;
     1920
     1921      return Node(n);
     1922    }
     1923
     1924    Edge addEdge(Node u, Node v) {
     1925      int n;
     1926
     1927      if (first_free_arc == -1) {
     1928        n = arcs.size();
     1929        arcs.push_back(ArcT());
     1930        arcs.push_back(ArcT());
     1931      } else {
     1932        n = first_free_arc;
     1933        first_free_arc = arcs[n].next_out;
     1934      }
     1935
     1936      arcs[n].target = u.id;
     1937      arcs[n | 1].target = v.id;
     1938
     1939      arcs[n].next_out = nodes[v.id].first_out;
     1940      if (nodes[v.id].first_out != -1) {
     1941        arcs[nodes[v.id].first_out].prev_out = n;
     1942      }
     1943      arcs[n].prev_out = -1;
     1944      nodes[v.id].first_out = n;
     1945
     1946      arcs[n | 1].next_out = nodes[u.id].first_out;
     1947      if (nodes[u.id].first_out != -1) {
     1948        arcs[nodes[u.id].first_out].prev_out = (n | 1);
     1949      }
     1950      arcs[n | 1].prev_out = -1;
     1951      nodes[u.id].first_out = (n | 1);
     1952
     1953      return Edge(n / 2);
     1954    }
     1955
     1956    void erase(const Node& node) {
     1957      int n = node.id;
     1958
     1959      if(nodes[n].next != -1) {
     1960        nodes[nodes[n].next].prev = nodes[n].prev;
     1961      }
     1962
     1963      if(nodes[n].prev != -1) {
     1964        nodes[nodes[n].prev].next = nodes[n].next;
     1965      } else {
     1966        first_node = nodes[n].next;
     1967      }
     1968
     1969      if (nodes[n].partition_next != -1) {
     1970        nodes[nodes[n].partition_next].partition_prev = nodes[n].partition_prev;
     1971      }
     1972
     1973      if (nodes[n].partition_prev != -1) {
     1974        nodes[nodes[n].partition_prev].partition_next = nodes[n].partition_next;
     1975      } else {
     1976        if (nodes[n].red) {
     1977          first_red = nodes[n].partition_next;
     1978        } else {
     1979          first_blue = nodes[n].partition_next;
     1980        }
     1981      }
     1982
     1983      if (nodes[n].red) {
     1984        nodes[n].next = first_free_red;
     1985        first_free_red = n;
     1986      } else {
     1987        nodes[n].next = first_free_blue;
     1988        first_free_blue = n;
     1989      }
     1990      nodes[n].prev = -2;
     1991    }
     1992
     1993    void erase(const Edge& edge) {
     1994      int n = edge.id * 2;
     1995
     1996      if (arcs[n].next_out != -1) {
     1997        arcs[arcs[n].next_out].prev_out = arcs[n].prev_out;
     1998      }
     1999
     2000      if (arcs[n].prev_out != -1) {
     2001        arcs[arcs[n].prev_out].next_out = arcs[n].next_out;
     2002      } else {
     2003        nodes[arcs[n | 1].target].first_out = arcs[n].next_out;
     2004      }
     2005
     2006      if (arcs[n | 1].next_out != -1) {
     2007        arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out;
     2008      }
     2009
     2010      if (arcs[n | 1].prev_out != -1) {
     2011        arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out;
     2012      } else {
     2013        nodes[arcs[n].target].first_out = arcs[n | 1].next_out;
     2014      }
     2015
     2016      arcs[n].next_out = first_free_arc;
     2017      first_free_arc = n;
     2018      arcs[n].prev_out = -2;
     2019      arcs[n | 1].prev_out = -2;
     2020
     2021    }
     2022
     2023    void clear() {
     2024      arcs.clear();
     2025      nodes.clear();
     2026      first_node = first_free_arc = first_red = first_blue =
     2027        max_red = max_blue = first_free_red = first_free_blue = -1;
     2028    }
     2029
     2030  protected:
     2031
     2032    void changeRed(Edge e, Node n) {
     2033      LEMON_DEBUG(nodes[n].red, "Node has to be red");
     2034      if(arcs[(2 * e.id) | 1].next_out != -1) {
     2035        arcs[arcs[(2 * e.id) | 1].next_out].prev_out =
     2036          arcs[(2 * e.id) | 1].prev_out;
     2037      }
     2038      if(arcs[(2 * e.id) | 1].prev_out != -1) {
     2039        arcs[arcs[(2 * e.id) | 1].prev_out].next_out =
     2040          arcs[(2 * e.id) | 1].next_out;
     2041      } else {
     2042        nodes[arcs[2 * e.id].target].first_out =
     2043          arcs[(2 * e.id) | 1].next_out;
     2044      }
     2045
     2046      if (nodes[n.id].first_out != -1) {
     2047        arcs[nodes[n.id].first_out].prev_out = ((2 * e.id) | 1);
     2048      }
     2049      arcs[2 * e.id].target = n.id;
     2050      arcs[(2 * e.id) | 1].prev_out = -1;
     2051      arcs[(2 * e.id) | 1].next_out = nodes[n.id].first_out;
     2052      nodes[n.id].first_out = ((2 * e.id) | 1);
     2053    }
     2054
     2055    void changeBlue(Edge e, Node n) {
     2056      LEMON_DEBUG(nodes[n].red, "Node has to be blue");
     2057      if(arcs[2 * e.id].next_out != -1) {
     2058        arcs[arcs[2 * e.id].next_out].prev_out = arcs[2 * e.id].prev_out;
     2059      }
     2060      if(arcs[2 * e.id].prev_out != -1) {
     2061        arcs[arcs[2 * e.id].prev_out].next_out =
     2062          arcs[2 * e.id].next_out;
     2063      } else {
     2064        nodes[arcs[(2 * e.id) | 1].target].first_out =
     2065          arcs[2 * e.id].next_out;
     2066      }
     2067
     2068      if (nodes[n.id].first_out != -1) {
     2069        arcs[nodes[n.id].first_out].prev_out = 2 * e.id;
     2070      }
     2071      arcs[(2 * e.id) | 1].target = n.id;
     2072      arcs[2 * e.id].prev_out = -1;
     2073      arcs[2 * e.id].next_out = nodes[n.id].first_out;
     2074      nodes[n.id].first_out = 2 * e.id;
     2075    }
     2076
     2077  };
     2078
     2079  typedef BpGraphExtender<ListBpGraphBase> ExtendedListBpGraphBase;
     2080
     2081
     2082  /// \addtogroup graphs
     2083  /// @{
     2084
     2085  ///A general undirected graph structure.
     2086
     2087  ///\ref ListBpGraph is a versatile and fast undirected graph
     2088  ///implementation based on linked lists that are stored in
     2089  ///\c std::vector structures.
     2090  ///
     2091  ///This type fully conforms to the \ref concepts::BpGraph "BpGraph concept"
     2092  ///and it also provides several useful additional functionalities.
     2093  ///Most of its member functions and nested classes are documented
     2094  ///only in the concept class.
     2095  ///
     2096  ///This class provides only linear time counting for nodes, edges and arcs.
     2097  ///
     2098  ///\sa concepts::BpGraph
     2099  ///\sa ListDigraph
     2100  class ListBpGraph : public ExtendedListBpGraphBase {
     2101    typedef ExtendedListBpGraphBase Parent;
     2102
     2103  private:
     2104    /// BpGraphs are \e not copy constructible. Use BpGraphCopy instead.
     2105    ListBpGraph(const ListBpGraph &) :ExtendedListBpGraphBase()  {};
     2106    /// \brief Assignment of a graph to another one is \e not allowed.
     2107    /// Use BpGraphCopy instead.
     2108    void operator=(const ListBpGraph &) {}
     2109  public:
     2110    /// Constructor
     2111
     2112    /// Constructor.
     2113    ///
     2114    ListBpGraph() {}
     2115
     2116    typedef Parent::OutArcIt IncEdgeIt;
     2117
     2118    /// \brief Add a new red node to the graph.
     2119    ///
     2120    /// This function adds a red new node to the graph.
     2121    /// \return The new node.
     2122    Node addRedNode() { return Parent::addRedNode(); }
     2123
     2124    /// \brief Add a new blue node to the graph.
     2125    ///
     2126    /// This function adds a blue new node to the graph.
     2127    /// \return The new node.
     2128    Node addBlueNode() { return Parent::addBlueNode(); }
     2129
     2130    /// \brief Add a new edge to the graph.
     2131    ///
     2132    /// This function adds a new edge to the graph between nodes
     2133    /// \c u and \c v with inherent orientation from node \c u to
     2134    /// node \c v.
     2135    /// \return The new edge.
     2136    Edge addEdge(Node u, Node v) {
     2137      return Parent::addEdge(u, v);
     2138    }
     2139
     2140    ///\brief Erase a node from the graph.
     2141    ///
     2142    /// This function erases the given node along with its incident arcs
     2143    /// from the graph.
     2144    ///
     2145    /// \note All iterators referencing the removed node or the incident
     2146    /// edges are invalidated, of course.
     2147    void erase(Node n) { Parent::erase(n); }
     2148
     2149    ///\brief Erase an edge from the graph.
     2150    ///
     2151    /// This function erases the given edge from the graph.
     2152    ///
     2153    /// \note All iterators referencing the removed edge are invalidated,
     2154    /// of course.
     2155    void erase(Edge e) { Parent::erase(e); }
     2156    /// Node validity check
     2157
     2158    /// This function gives back \c true if the given node is valid,
     2159    /// i.e. it is a real node of the graph.
     2160    ///
     2161    /// \warning A removed node could become valid again if new nodes are
     2162    /// added to the graph.
     2163    bool valid(Node n) const { return Parent::valid(n); }
     2164    /// Edge validity check
     2165
     2166    /// This function gives back \c true if the given edge is valid,
     2167    /// i.e. it is a real edge of the graph.
     2168    ///
     2169    /// \warning A removed edge could become valid again if new edges are
     2170    /// added to the graph.
     2171    bool valid(Edge e) const { return Parent::valid(e); }
     2172    /// Arc validity check
     2173
     2174    /// This function gives back \c true if the given arc is valid,
     2175    /// i.e. it is a real arc of the graph.
     2176    ///
     2177    /// \warning A removed arc could become valid again if new edges are
     2178    /// added to the graph.
     2179    bool valid(Arc a) const { return Parent::valid(a); }
     2180
     2181    /// \brief Change the red node of an edge.
     2182    ///
     2183    /// This function changes the red node of the given edge \c e to \c n.
     2184    ///
     2185    ///\note \c EdgeIt and \c ArcIt iterators referencing the
     2186    ///changed edge are invalidated and all other iterators whose
     2187    ///base node is the changed node are also invalidated.
     2188    ///
     2189    ///\warning This functionality cannot be used together with the
     2190    ///Snapshot feature.
     2191    void changeRed(Edge e, Node n) {
     2192      Parent::changeRed(e,n);
     2193    }
     2194    /// \brief Change the blue node of an edge.
     2195    ///
     2196    /// This function changes the blue node of the given edge \c e to \c n.
     2197    ///
     2198    ///\note \c EdgeIt iterators referencing the changed edge remain
     2199    ///valid, but \c ArcIt iterators referencing the changed edge and
     2200    ///all other iterators whose base node is the changed node are also
     2201    ///invalidated.
     2202    ///
     2203    ///\warning This functionality cannot be used together with the
     2204    ///Snapshot feature.
     2205    void changeBlue(Edge e, Node n) {
     2206      Parent::changeBlue(e,n);
     2207    }
     2208
     2209    ///Clear the graph.
     2210
     2211    ///This function erases all nodes and arcs from the graph.
     2212    ///
     2213    ///\note All iterators of the graph are invalidated, of course.
     2214    void clear() {
     2215      Parent::clear();
     2216    }
     2217
     2218    /// Reserve memory for nodes.
     2219
     2220    /// Using this function, it is possible to avoid superfluous memory
     2221    /// allocation: if you know that the graph you want to build will
     2222    /// be large (e.g. it will contain millions of nodes and/or edges),
     2223    /// then it is worth reserving space for this amount before starting
     2224    /// to build the graph.
     2225    /// \sa reserveEdge()
     2226    void reserveNode(int n) { nodes.reserve(n); };
     2227
     2228    /// Reserve memory for edges.
     2229
     2230    /// Using this function, it is possible to avoid superfluous memory
     2231    /// allocation: if you know that the graph you want to build will
     2232    /// be large (e.g. it will contain millions of nodes and/or edges),
     2233    /// then it is worth reserving space for this amount before starting
     2234    /// to build the graph.
     2235    /// \sa reserveNode()
     2236    void reserveEdge(int m) { arcs.reserve(2 * m); };
     2237
     2238    /// \brief Class to make a snapshot of the graph and restore
     2239    /// it later.
     2240    ///
     2241    /// Class to make a snapshot of the graph and restore it later.
     2242    ///
     2243    /// The newly added nodes and edges can be removed
     2244    /// using the restore() function.
     2245    ///
     2246    /// \note After a state is restored, you cannot restore a later state,
     2247    /// i.e. you cannot add the removed nodes and edges again using
     2248    /// another Snapshot instance.
     2249    ///
     2250    /// \warning Node and edge deletions and other modifications
     2251    /// (e.g. changing the end-nodes of edges or contracting nodes)
     2252    /// cannot be restored. These events invalidate the snapshot.
     2253    /// However, the edges and nodes that were added to the graph after
     2254    /// making the current snapshot can be removed without invalidating it.
     2255    class Snapshot {
     2256    protected:
     2257
     2258      typedef Parent::NodeNotifier NodeNotifier;
     2259
     2260      class NodeObserverProxy : public NodeNotifier::ObserverBase {
     2261      public:
     2262
     2263        NodeObserverProxy(Snapshot& _snapshot)
     2264          : snapshot(_snapshot) {}
     2265
     2266        using NodeNotifier::ObserverBase::attach;
     2267        using NodeNotifier::ObserverBase::detach;
     2268        using NodeNotifier::ObserverBase::attached;
     2269
     2270      protected:
     2271
     2272        virtual void add(const Node& node) {
     2273          snapshot.addNode(node);
     2274        }
     2275        virtual void add(const std::vector<Node>& nodes) {
     2276          for (int i = nodes.size() - 1; i >= 0; ++i) {
     2277            snapshot.addNode(nodes[i]);
     2278          }
     2279        }
     2280        virtual void erase(const Node& node) {
     2281          snapshot.eraseNode(node);
     2282        }
     2283        virtual void erase(const std::vector<Node>& nodes) {
     2284          for (int i = 0; i < int(nodes.size()); ++i) {
     2285            snapshot.eraseNode(nodes[i]);
     2286          }
     2287        }
     2288        virtual void build() {
     2289          Node node;
     2290          std::vector<Node> nodes;
     2291          for (notifier()->first(node); node != INVALID;
     2292               notifier()->next(node)) {
     2293            nodes.push_back(node);
     2294          }
     2295          for (int i = nodes.size() - 1; i >= 0; --i) {
     2296            snapshot.addNode(nodes[i]);
     2297          }
     2298        }
     2299        virtual void clear() {
     2300          Node node;
     2301          for (notifier()->first(node); node != INVALID;
     2302               notifier()->next(node)) {
     2303            snapshot.eraseNode(node);
     2304          }
     2305        }
     2306
     2307        Snapshot& snapshot;
     2308      };
     2309
     2310      class EdgeObserverProxy : public EdgeNotifier::ObserverBase {
     2311      public:
     2312
     2313        EdgeObserverProxy(Snapshot& _snapshot)
     2314          : snapshot(_snapshot) {}
     2315
     2316        using EdgeNotifier::ObserverBase::attach;
     2317        using EdgeNotifier::ObserverBase::detach;
     2318        using EdgeNotifier::ObserverBase::attached;
     2319
     2320      protected:
     2321
     2322        virtual void add(const Edge& edge) {
     2323          snapshot.addEdge(edge);
     2324        }
     2325        virtual void add(const std::vector<Edge>& edges) {
     2326          for (int i = edges.size() - 1; i >= 0; ++i) {
     2327            snapshot.addEdge(edges[i]);
     2328          }
     2329        }
     2330        virtual void erase(const Edge& edge) {
     2331          snapshot.eraseEdge(edge);
     2332        }
     2333        virtual void erase(const std::vector<Edge>& edges) {
     2334          for (int i = 0; i < int(edges.size()); ++i) {
     2335            snapshot.eraseEdge(edges[i]);
     2336          }
     2337        }
     2338        virtual void build() {
     2339          Edge edge;
     2340          std::vector<Edge> edges;
     2341          for (notifier()->first(edge); edge != INVALID;
     2342               notifier()->next(edge)) {
     2343            edges.push_back(edge);
     2344          }
     2345          for (int i = edges.size() - 1; i >= 0; --i) {
     2346            snapshot.addEdge(edges[i]);
     2347          }
     2348        }
     2349        virtual void clear() {
     2350          Edge edge;
     2351          for (notifier()->first(edge); edge != INVALID;
     2352               notifier()->next(edge)) {
     2353            snapshot.eraseEdge(edge);
     2354          }
     2355        }
     2356
     2357        Snapshot& snapshot;
     2358      };
     2359
     2360      ListBpGraph *graph;
     2361
     2362      NodeObserverProxy node_observer_proxy;
     2363      EdgeObserverProxy edge_observer_proxy;
     2364
     2365      std::list<Node> added_nodes;
     2366      std::list<Edge> added_edges;
     2367
     2368
     2369      void addNode(const Node& node) {
     2370        added_nodes.push_front(node);
     2371      }
     2372      void eraseNode(const Node& node) {
     2373        std::list<Node>::iterator it =
     2374          std::find(added_nodes.begin(), added_nodes.end(), node);
     2375        if (it == added_nodes.end()) {
     2376          clear();
     2377          edge_observer_proxy.detach();
     2378          throw NodeNotifier::ImmediateDetach();
     2379        } else {
     2380          added_nodes.erase(it);
     2381        }
     2382      }
     2383
     2384      void addEdge(const Edge& edge) {
     2385        added_edges.push_front(edge);
     2386      }
     2387      void eraseEdge(const Edge& edge) {
     2388        std::list<Edge>::iterator it =
     2389          std::find(added_edges.begin(), added_edges.end(), edge);
     2390        if (it == added_edges.end()) {
     2391          clear();
     2392          node_observer_proxy.detach();
     2393          throw EdgeNotifier::ImmediateDetach();
     2394        } else {
     2395          added_edges.erase(it);
     2396        }
     2397      }
     2398
     2399      void attach(ListBpGraph &_graph) {
     2400        graph = &_graph;
     2401        node_observer_proxy.attach(graph->notifier(Node()));
     2402        edge_observer_proxy.attach(graph->notifier(Edge()));
     2403      }
     2404
     2405      void detach() {
     2406        node_observer_proxy.detach();
     2407        edge_observer_proxy.detach();
     2408      }
     2409
     2410      bool attached() const {
     2411        return node_observer_proxy.attached();
     2412      }
     2413
     2414      void clear() {
     2415        added_nodes.clear();
     2416        added_edges.clear();
     2417      }
     2418
     2419    public:
     2420
     2421      /// \brief Default constructor.
     2422      ///
     2423      /// Default constructor.
     2424      /// You have to call save() to actually make a snapshot.
     2425      Snapshot()
     2426        : graph(0), node_observer_proxy(*this),
     2427          edge_observer_proxy(*this) {}
     2428
     2429      /// \brief Constructor that immediately makes a snapshot.
     2430      ///
     2431      /// This constructor immediately makes a snapshot of the given graph.
     2432      Snapshot(ListBpGraph &gr)
     2433        : node_observer_proxy(*this),
     2434          edge_observer_proxy(*this) {
     2435        attach(gr);
     2436      }
     2437
     2438      /// \brief Make a snapshot.
     2439      ///
     2440      /// This function makes a snapshot of the given graph.
     2441      /// It can be called more than once. In case of a repeated
     2442      /// call, the previous snapshot gets lost.
     2443      void save(ListBpGraph &gr) {
     2444        if (attached()) {
     2445          detach();
     2446          clear();
     2447        }
     2448        attach(gr);
     2449      }
     2450
     2451      /// \brief Undo the changes until the last snapshot.
     2452      ///
     2453      /// This function undos the changes until the last snapshot
     2454      /// created by save() or Snapshot(ListBpGraph&).
     2455      ///
     2456      /// \warning This method invalidates the snapshot, i.e. repeated
     2457      /// restoring is not supported unless you call save() again.
     2458      void restore() {
     2459        detach();
     2460        for(std::list<Edge>::iterator it = added_edges.begin();
     2461            it != added_edges.end(); ++it) {
     2462          graph->erase(*it);
     2463        }
     2464        for(std::list<Node>::iterator it = added_nodes.begin();
     2465            it != added_nodes.end(); ++it) {
     2466          graph->erase(*it);
     2467        }
     2468        clear();
     2469      }
     2470
     2471      /// \brief Returns \c true if the snapshot is valid.
     2472      ///
     2473      /// This function returns \c true if the snapshot is valid.
     2474      bool valid() const {
     2475        return attached();
     2476      }
     2477    };
     2478  };
     2479
     2480  /// @}
    16022481} //namespace lemon
    16032482
    16042483
  • lemon/smart_graph.h

    diff -r 261a074fead9 -r 1c825ca10e1f lemon/smart_graph.h
    a b  
    10161016      return nodes[v._id].partition_index;
    10171017    }
    10181018    int blueId(Node v) const {
    1019       LEMON_DEBUG(nodes[v._id].red, "Node has to be blue");
     1019      LEMON_DEBUG(!nodes[v._id].red, "Node has to be blue");
    10201020      return nodes[v._id].partition_index;
    10211021    }
    10221022    static int id(Arc e) { return e._id; }
  • test/bpgraph_test.cc

    diff -r 261a074fead9 -r 1c825ca10e1f test/bpgraph_test.cc
    a b  
    1717 */
    1818
    1919#include <lemon/concepts/bpgraph.h>
    20 //#include <lemon/list_graph.h>
     20#include <lemon/list_graph.h>
    2121#include <lemon/smart_graph.h>
    2222#include <lemon/full_graph.h>
    2323
     
    107107  checkGraphEdgeMap(G);
    108108}
    109109
    110 template <class Graph>
     110template <class BpGraph>
     111void checkBpGraphErase() {
     112  TEMPLATE_BPGRAPH_TYPEDEFS(BpGraph);
     113
     114  BpGraph G;
     115  Node
     116    n1 = G.addRedNode(), n2 = G.addBlueNode(),
     117    n3 = G.addBlueNode(), n4 = G.addRedNode();
     118  Edge
     119    e1 = G.addEdge(n1, n2), e2 = G.addEdge(n1, n3),
     120    e3 = G.addEdge(n4, n2), e4 = G.addEdge(n4, n3);
     121
     122  // Check edge deletion
     123  G.erase(e2);
     124
     125  checkGraphNodeList(G, 4);
     126  checkGraphRedNodeList(G, 2);
     127  checkGraphBlueNodeList(G, 2);
     128  checkGraphEdgeList(G, 3);
     129  checkGraphArcList(G, 6);
     130
     131  checkGraphIncEdgeArcLists(G, n1, 1);
     132  checkGraphIncEdgeArcLists(G, n2, 2);
     133  checkGraphIncEdgeArcLists(G, n3, 1);
     134  checkGraphIncEdgeArcLists(G, n4, 2);
     135
     136  checkGraphConEdgeList(G, 3);
     137  checkGraphConArcList(G, 6);
     138
     139  // Check node deletion
     140  G.erase(n3);
     141
     142  checkGraphNodeList(G, 3);
     143  checkGraphRedNodeList(G, 2);
     144  checkGraphBlueNodeList(G, 1);
     145  checkGraphEdgeList(G, 2);
     146  checkGraphArcList(G, 4);
     147
     148  checkGraphIncEdgeArcLists(G, n1, 1);
     149  checkGraphIncEdgeArcLists(G, n2, 2);
     150  checkGraphIncEdgeArcLists(G, n4, 1);
     151
     152  checkGraphConEdgeList(G, 2);
     153  checkGraphConArcList(G, 4);
     154
     155}
     156
     157template <class BpGraph>
     158void checkBpGraphAlter() {
     159  TEMPLATE_BPGRAPH_TYPEDEFS(BpGraph);
     160
     161  BpGraph G;
     162  Node
     163    n1 = G.addRedNode(), n2 = G.addBlueNode(),
     164    n3 = G.addBlueNode(), n4 = G.addRedNode();
     165  Edge
     166    e1 = G.addEdge(n1, n2), e2 = G.addEdge(n1, n3),
     167    e3 = G.addEdge(n4, n2), e4 = G.addEdge(n4, n3);
     168
     169  G.changeRed(e2, n4);
     170  check(G.redNode(e2) == n4, "Wrong red node");
     171  check(G.blueNode(e2) == n3, "Wrong blue node");
     172
     173  checkGraphNodeList(G, 4);
     174  checkGraphRedNodeList(G, 2);
     175  checkGraphBlueNodeList(G, 2);
     176  checkGraphEdgeList(G, 4);
     177  checkGraphArcList(G, 8);
     178
     179  checkGraphIncEdgeArcLists(G, n1, 1);
     180  checkGraphIncEdgeArcLists(G, n2, 2);
     181  checkGraphIncEdgeArcLists(G, n3, 2);
     182  checkGraphIncEdgeArcLists(G, n4, 3);
     183
     184  checkGraphConEdgeList(G, 4);
     185  checkGraphConArcList(G, 8);
     186
     187  G.changeBlue(e2, n2);
     188  check(G.redNode(e2) == n4, "Wrong red node");
     189  check(G.blueNode(e2) == n2, "Wrong blue node");
     190
     191  checkGraphNodeList(G, 4);
     192  checkGraphRedNodeList(G, 2);
     193  checkGraphBlueNodeList(G, 2);
     194  checkGraphEdgeList(G, 4);
     195  checkGraphArcList(G, 8);
     196
     197  checkGraphIncEdgeArcLists(G, n1, 1);
     198  checkGraphIncEdgeArcLists(G, n2, 3);
     199  checkGraphIncEdgeArcLists(G, n3, 1);
     200  checkGraphIncEdgeArcLists(G, n4, 3);
     201
     202  checkGraphConEdgeList(G, 4);
     203  checkGraphConArcList(G, 8);
     204}
     205
     206
     207template <class BpGraph>
    111208void checkBpGraphSnapshot() {
    112   TEMPLATE_BPGRAPH_TYPEDEFS(Graph);
     209  TEMPLATE_BPGRAPH_TYPEDEFS(BpGraph);
    113210
    114   Graph G;
     211  BpGraph G;
    115212  Node
    116213    n1 = G.addRedNode(),
    117214    n2 = G.addBlueNode(),
     
    126223  checkGraphEdgeList(G, 2);
    127224  checkGraphArcList(G, 4);
    128225
    129   typename Graph::Snapshot snapshot(G);
     226  typename BpGraph::Snapshot snapshot(G);
    130227
    131228  Node n4 = G.addRedNode();
    132229  G.addEdge(n4, n2);
     
    190287  checkGraphArcList(G, 4);
    191288}
    192289
    193 template <typename Graph>
     290template <typename BpGraph>
    194291void checkBpGraphValidity() {
    195   TEMPLATE_GRAPH_TYPEDEFS(Graph);
    196   Graph g;
     292  TEMPLATE_BPGRAPH_TYPEDEFS(BpGraph);
     293  BpGraph g;
    197294
    198295  Node
    199296    n1 = g.addRedNode(),
     
    325422}
    326423
    327424void checkGraphs() {
     425  { // Checking ListGraph
     426    checkBpGraphBuild<ListBpGraph>();
     427    checkBpGraphErase<ListBpGraph>();
     428    checkBpGraphAlter<ListBpGraph>();
     429    checkBpGraphSnapshot<ListBpGraph>();
     430    checkBpGraphValidity<ListBpGraph>();
     431  }
    328432  { // Checking SmartGraph
    329433    checkBpGraphBuild<SmartBpGraph>();
    330434    checkBpGraphSnapshot<SmartBpGraph>();
  • lemon/core.h

    # HG changeset patch
    # User Balazs Dezso <deba@inf.elte.hu>
    # Date 1289865576 -3600
    # Node ID 353635c555b0038dbab145d2a20987528b062ecb
    # Parent  1c825ca10e1f6228b4c1adde25272502ce5cdb6a
    Implementation of BpGraphCopy
    
    diff -r 1c825ca10e1f -r 353635c555b0 lemon/core.h
    a b  
    555555      }
    556556    };
    557557
     558    template <typename BpGraph, typename Enable = void>
     559    struct BpGraphCopySelector {
     560      template <typename From, typename NodeRefMap, typename EdgeRefMap>
     561      static void copy(const From& from, BpGraph &to,
     562                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
     563        to.clear();
     564        for (typename From::RedIt it(from); it != INVALID; ++it) {
     565          nodeRefMap[it] = to.addRedNode();
     566        }
     567        for (typename From::BlueIt it(from); it != INVALID; ++it) {
     568          nodeRefMap[it] = to.addBlueNode();
     569        }
     570        for (typename From::EdgeIt it(from); it != INVALID; ++it) {
     571          edgeRefMap[it] = to.addEdge(nodeRefMap[from.redNode(it)],
     572                                      nodeRefMap[from.blueNode(it)]);
     573        }
     574      }
     575    };
     576
     577    template <typename BpGraph>
     578    struct BpGraphCopySelector<
     579      BpGraph,
     580      typename enable_if<typename BpGraph::BuildTag, void>::type>
     581    {
     582      template <typename From, typename NodeRefMap, typename EdgeRefMap>
     583      static void copy(const From& from, BpGraph &to,
     584                       NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
     585        to.build(from, nodeRefMap, edgeRefMap);
     586      }
     587    };
     588
    558589  }
    559590
    560591  /// Check whether a graph is undirected.
     
    11011132    return GraphCopy<From, To>(from, to);
    11021133  }
    11031134
     1135  /// \brief Class to copy a bipartite graph.
     1136  ///
     1137  /// Class to copy a bipartite graph to another graph (duplicate a
     1138  /// graph). The simplest way of using it is through the
     1139  /// \c bpGraphCopy() function.
     1140  ///
     1141  /// This class not only make a copy of a bipartite graph, but it can
     1142  /// create references and cross references between the nodes, edges
     1143  /// and arcs of the two graphs, and it can copy maps for using with
     1144  /// the newly created graph.
     1145  ///
     1146  /// To make a copy from a graph, first an instance of BpGraphCopy
     1147  /// should be created, then the data belongs to the graph should
     1148  /// assigned to copy. In the end, the \c run() member should be
     1149  /// called.
     1150  ///
     1151  /// The next code copies a graph with several data:
     1152  ///\code
     1153  ///  BpGraphCopy<OrigBpGraph, NewBpGraph> cg(orig_graph, new_graph);
     1154  ///  // Create references for the nodes
     1155  ///  OrigBpGraph::NodeMap<NewBpGraph::Node> nr(orig_graph);
     1156  ///  cg.nodeRef(nr);
     1157  ///  // Create cross references (inverse) for the edges
     1158  ///  NewBpGraph::EdgeMap<OrigBpGraph::Edge> ecr(new_graph);
     1159  ///  cg.edgeCrossRef(ecr);
     1160  ///  // Copy a red map
     1161  ///  OrigBpGraph::RedMap<double> ormap(orig_graph);
     1162  ///  NewBpGraph::RedMap<double> nrmap(new_graph);
     1163  ///  cg.edgeMap(ormap, nrmap);
     1164  ///  // Copy a node
     1165  ///  OrigBpGraph::Node on;
     1166  ///  NewBpGraph::Node nn;
     1167  ///  cg.node(on, nn);
     1168  ///  // Execute copying
     1169  ///  cg.run();
     1170  ///\endcode
     1171  template <typename From, typename To>
     1172  class BpGraphCopy {
     1173  private:
     1174
     1175    typedef typename From::Node Node;
     1176    typedef typename From::RedNode RedNode;
     1177    typedef typename From::BlueNode BlueNode;
     1178    typedef typename From::NodeIt NodeIt;
     1179    typedef typename From::Arc Arc;
     1180    typedef typename From::ArcIt ArcIt;
     1181    typedef typename From::Edge Edge;
     1182    typedef typename From::EdgeIt EdgeIt;
     1183
     1184    typedef typename To::Node TNode;
     1185    typedef typename To::Arc TArc;
     1186    typedef typename To::Edge TEdge;
     1187
     1188    typedef typename From::template NodeMap<TNode> NodeRefMap;
     1189    typedef typename From::template EdgeMap<TEdge> EdgeRefMap;
     1190
     1191    struct ArcRefMap {
     1192      ArcRefMap(const From& from, const To& to, const EdgeRefMap& edge_ref)
     1193        : _from(from), _to(to), _edge_ref(edge_ref) {}
     1194
     1195      typedef typename From::Arc Key;
     1196      typedef typename To::Arc Value;
     1197
     1198      Value operator[](const Key& key) const {
     1199        return _to.direct(_edge_ref[key], _from.direction(key));
     1200      }
     1201
     1202      const From& _from;
     1203      const To& _to;
     1204      const EdgeRefMap& _edge_ref;
     1205    };
     1206
     1207  public:
     1208
     1209    /// \brief Constructor of BpGraphCopy.
     1210    ///
     1211    /// Constructor of BpGraphCopy for copying the content of the
     1212    /// \c from graph into the \c to graph.
     1213    BpGraphCopy(const From& from, To& to)
     1214      : _from(from), _to(to) {}
     1215
     1216    /// \brief Destructor of BpGraphCopy
     1217    ///
     1218    /// Destructor of BpGraphCopy.
     1219    ~BpGraphCopy() {
     1220      for (int i = 0; i < int(_node_maps.size()); ++i) {
     1221        delete _node_maps[i];
     1222      }
     1223      for (int i = 0; i < int(_red_maps.size()); ++i) {
     1224        delete _red_maps[i];
     1225      }
     1226      for (int i = 0; i < int(_blue_maps.size()); ++i) {
     1227        delete _blue_maps[i];
     1228      }
     1229      for (int i = 0; i < int(_arc_maps.size()); ++i) {
     1230        delete _arc_maps[i];
     1231      }
     1232      for (int i = 0; i < int(_edge_maps.size()); ++i) {
     1233        delete _edge_maps[i];
     1234      }
     1235    }
     1236
     1237    /// \brief Copy the node references into the given map.
     1238    ///
     1239    /// This function copies the node references into the given map.
     1240    /// The parameter should be a map, whose key type is the Node type of
     1241    /// the source graph, while the value type is the Node type of the
     1242    /// destination graph.
     1243    template <typename NodeRef>
     1244    BpGraphCopy& nodeRef(NodeRef& map) {
     1245      _node_maps.push_back(new _core_bits::RefCopy<From, Node,
     1246                           NodeRefMap, NodeRef>(map));
     1247      return *this;
     1248    }
     1249
     1250    /// \brief Copy the node cross references into the given map.
     1251    ///
     1252    /// This function copies the node cross references (reverse references)
     1253    /// into the given map. The parameter should be a map, whose key type
     1254    /// is the Node type of the destination graph, while the value type is
     1255    /// the Node type of the source graph.
     1256    template <typename NodeCrossRef>
     1257    BpGraphCopy& nodeCrossRef(NodeCrossRef& map) {
     1258      _node_maps.push_back(new _core_bits::CrossRefCopy<From, Node,
     1259                           NodeRefMap, NodeCrossRef>(map));
     1260      return *this;
     1261    }
     1262
     1263    /// \brief Make a copy of the given node map.
     1264    ///
     1265    /// This function makes a copy of the given node map for the newly
     1266    /// created graph.
     1267    /// The key type of the new map \c tmap should be the Node type of the
     1268    /// destination graph, and the key type of the original map \c map
     1269    /// should be the Node type of the source graph.
     1270    template <typename FromMap, typename ToMap>
     1271    BpGraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
     1272      _node_maps.push_back(new _core_bits::MapCopy<From, Node,
     1273                           NodeRefMap, FromMap, ToMap>(map, tmap));
     1274      return *this;
     1275    }
     1276
     1277    /// \brief Make a copy of the given node.
     1278    ///
     1279    /// This function makes a copy of the given node.
     1280    BpGraphCopy& node(const Node& node, TNode& tnode) {
     1281      _node_maps.push_back(new _core_bits::ItemCopy<From, Node,
     1282                           NodeRefMap, TNode>(node, tnode));
     1283      return *this;
     1284    }
     1285
     1286    /// \brief Copy the red node references into the given map.
     1287    ///
     1288    /// This function copies the red node references into the given
     1289    /// map.  The parameter should be a map, whose key type is the
     1290    /// Node type of the source graph with the red item set, while the
     1291    /// value type is the Node type of the destination graph.
     1292    template <typename RedRef>
     1293    BpGraphCopy& redRef(RedRef& map) {
     1294      _red_maps.push_back(new _core_bits::RefCopy<From, RedNode,
     1295                          NodeRefMap, RedRef>(map));
     1296      return *this;
     1297    }
     1298
     1299    /// \brief Copy the red node cross references into the given map.
     1300    ///
     1301    /// This function copies the red node cross references (reverse
     1302    /// references) into the given map. The parameter should be a map,
     1303    /// whose key type is the Node type of the destination graph with
     1304    /// the red item set, while the value type is the Node type of the
     1305    /// source graph.
     1306    template <typename RedCrossRef>
     1307    BpGraphCopy& redCrossRef(RedCrossRef& map) {
     1308      _red_maps.push_back(new _core_bits::CrossRefCopy<From, RedNode,
     1309                          NodeRefMap, RedCrossRef>(map));
     1310      return *this;
     1311    }
     1312
     1313    /// \brief Make a copy of the given red node map.
     1314    ///
     1315    /// This function makes a copy of the given red node map for the newly
     1316    /// created graph.
     1317    /// The key type of the new map \c tmap should be the Node type of
     1318    /// the destination graph with the red items, and the key type of
     1319    /// the original map \c map should be the Node type of the source
     1320    /// graph.
     1321    template <typename FromMap, typename ToMap>
     1322    BpGraphCopy& redMap(const FromMap& map, ToMap& tmap) {
     1323      _red_maps.push_back(new _core_bits::MapCopy<From, RedNode,
     1324                           NodeRefMap, FromMap, ToMap>(map, tmap));
     1325      return *this;
     1326    }
     1327
     1328    /// \brief Copy the blue node references into the given map.
     1329    ///
     1330    /// This function copies the blue node references into the given
     1331    /// map.  The parameter should be a map, whose key type is the
     1332    /// Node type of the source graph with the blue item set, while the
     1333    /// value type is the Node type of the destination graph.
     1334    template <typename BlueRef>
     1335    BpGraphCopy& blueRef(BlueRef& map) {
     1336      _blue_maps.push_back(new _core_bits::RefCopy<From, BlueNode,
     1337                           NodeRefMap, BlueRef>(map));
     1338      return *this;
     1339    }
     1340
     1341    /// \brief Copy the blue node cross references into the given map.
     1342    ///
     1343    /// This function copies the blue node cross references (reverse
     1344    /// references) into the given map. The parameter should be a map,
     1345    /// whose key type is the Node type of the destination graph with
     1346    /// the blue item set, while the value type is the Node type of the
     1347    /// source graph.
     1348    template <typename BlueCrossRef>
     1349    BpGraphCopy& blueCrossRef(BlueCrossRef& map) {
     1350      _blue_maps.push_back(new _core_bits::CrossRefCopy<From, BlueNode,
     1351                           NodeRefMap, BlueCrossRef>(map));
     1352      return *this;
     1353    }
     1354
     1355    /// \brief Make a copy of the given blue node map.
     1356    ///
     1357    /// This function makes a copy of the given blue node map for the newly
     1358    /// created graph.
     1359    /// The key type of the new map \c tmap should be the Node type of
     1360    /// the destination graph with the blue items, and the key type of
     1361    /// the original map \c map should be the Node type of the source
     1362    /// graph.
     1363    template <typename FromMap, typename ToMap>
     1364    BpGraphCopy& blueMap(const FromMap& map, ToMap& tmap) {
     1365      _blue_maps.push_back(new _core_bits::MapCopy<From, BlueNode,
     1366                           NodeRefMap, FromMap, ToMap>(map, tmap));
     1367      return *this;
     1368    }
     1369
     1370    /// \brief Copy the arc references into the given map.
     1371    ///
     1372    /// This function copies the arc references into the given map.
     1373    /// The parameter should be a map, whose key type is the Arc type of
     1374    /// the source graph, while the value type is the Arc type of the
     1375    /// destination graph.
     1376    template <typename ArcRef>
     1377    BpGraphCopy& arcRef(ArcRef& map) {
     1378      _arc_maps.push_back(new _core_bits::RefCopy<From, Arc,
     1379                          ArcRefMap, ArcRef>(map));
     1380      return *this;
     1381    }
     1382
     1383    /// \brief Copy the arc cross references into the given map.
     1384    ///
     1385    /// This function copies the arc cross references (reverse references)
     1386    /// into the given map. The parameter should be a map, whose key type
     1387    /// is the Arc type of the destination graph, while the value type is
     1388    /// the Arc type of the source graph.
     1389    template <typename ArcCrossRef>
     1390    BpGraphCopy& arcCrossRef(ArcCrossRef& map) {
     1391      _arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc,
     1392                          ArcRefMap, ArcCrossRef>(map));
     1393      return *this;
     1394    }
     1395
     1396    /// \brief Make a copy of the given arc map.
     1397    ///
     1398    /// This function makes a copy of the given arc map for the newly
     1399    /// created graph.
     1400    /// The key type of the new map \c tmap should be the Arc type of the
     1401    /// destination graph, and the key type of the original map \c map
     1402    /// should be the Arc type of the source graph.
     1403    template <typename FromMap, typename ToMap>
     1404    BpGraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
     1405      _arc_maps.push_back(new _core_bits::MapCopy<From, Arc,
     1406                          ArcRefMap, FromMap, ToMap>(map, tmap));
     1407      return *this;
     1408    }
     1409
     1410    /// \brief Make a copy of the given arc.
     1411    ///
     1412    /// This function makes a copy of the given arc.
     1413    BpGraphCopy& arc(const Arc& arc, TArc& tarc) {
     1414      _arc_maps.push_back(new _core_bits::ItemCopy<From, Arc,
     1415                          ArcRefMap, TArc>(arc, tarc));
     1416      return *this;
     1417    }
     1418
     1419    /// \brief Copy the edge references into the given map.
     1420    ///
     1421    /// This function copies the edge references into the given map.
     1422    /// The parameter should be a map, whose key type is the Edge type of
     1423    /// the source graph, while the value type is the Edge type of the
     1424    /// destination graph.
     1425    template <typename EdgeRef>
     1426    BpGraphCopy& edgeRef(EdgeRef& map) {
     1427      _edge_maps.push_back(new _core_bits::RefCopy<From, Edge,
     1428                           EdgeRefMap, EdgeRef>(map));
     1429      return *this;
     1430    }
     1431
     1432    /// \brief Copy the edge cross references into the given map.
     1433    ///
     1434    /// This function copies the edge cross references (reverse references)
     1435    /// into the given map. The parameter should be a map, whose key type
     1436    /// is the Edge type of the destination graph, while the value type is
     1437    /// the Edge type of the source graph.
     1438    template <typename EdgeCrossRef>
     1439    BpGraphCopy& edgeCrossRef(EdgeCrossRef& map) {
     1440      _edge_maps.push_back(new _core_bits::CrossRefCopy<From,
     1441                           Edge, EdgeRefMap, EdgeCrossRef>(map));
     1442      return *this;
     1443    }
     1444
     1445    /// \brief Make a copy of the given edge map.
     1446    ///
     1447    /// This function makes a copy of the given edge map for the newly
     1448    /// created graph.
     1449    /// The key type of the new map \c tmap should be the Edge type of the
     1450    /// destination graph, and the key type of the original map \c map
     1451    /// should be the Edge type of the source graph.
     1452    template <typename FromMap, typename ToMap>
     1453    BpGraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
     1454      _edge_maps.push_back(new _core_bits::MapCopy<From, Edge,
     1455                           EdgeRefMap, FromMap, ToMap>(map, tmap));
     1456      return *this;
     1457    }
     1458
     1459    /// \brief Make a copy of the given edge.
     1460    ///
     1461    /// This function makes a copy of the given edge.
     1462    BpGraphCopy& edge(const Edge& edge, TEdge& tedge) {
     1463      _edge_maps.push_back(new _core_bits::ItemCopy<From, Edge,
     1464                           EdgeRefMap, TEdge>(edge, tedge));
     1465      return *this;
     1466    }
     1467
     1468    /// \brief Execute copying.
     1469    ///
     1470    /// This function executes the copying of the graph along with the
     1471    /// copying of the assigned data.
     1472    void run() {
     1473      NodeRefMap nodeRefMap(_from);
     1474      EdgeRefMap edgeRefMap(_from);
     1475      ArcRefMap arcRefMap(_from, _to, edgeRefMap);
     1476      _core_bits::BpGraphCopySelector<To>::
     1477        copy(_from, _to, nodeRefMap, edgeRefMap);
     1478      for (int i = 0; i < int(_node_maps.size()); ++i) {
     1479        _node_maps[i]->copy(_from, nodeRefMap);
     1480      }
     1481      for (int i = 0; i < int(_red_maps.size()); ++i) {
     1482        _red_maps[i]->copy(_from, nodeRefMap);
     1483      }
     1484      for (int i = 0; i < int(_blue_maps.size()); ++i) {
     1485        _blue_maps[i]->copy(_from, nodeRefMap);
     1486      }
     1487      for (int i = 0; i < int(_edge_maps.size()); ++i) {
     1488        _edge_maps[i]->copy(_from, edgeRefMap);
     1489      }
     1490      for (int i = 0; i < int(_arc_maps.size()); ++i) {
     1491        _arc_maps[i]->copy(_from, arcRefMap);
     1492      }
     1493    }
     1494
     1495  private:
     1496
     1497    const From& _from;
     1498    To& _to;
     1499
     1500    std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* >
     1501      _node_maps;
     1502
     1503    std::vector<_core_bits::MapCopyBase<From, RedNode, NodeRefMap>* >
     1504      _red_maps;
     1505
     1506    std::vector<_core_bits::MapCopyBase<From, BlueNode, NodeRefMap>* >
     1507      _blue_maps;
     1508
     1509    std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* >
     1510      _arc_maps;
     1511
     1512    std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* >
     1513      _edge_maps;
     1514
     1515  };
     1516
     1517  /// \brief Copy a graph to another graph.
     1518  ///
     1519  /// This function copies a graph to another graph.
     1520  /// The complete usage of it is detailed in the BpGraphCopy class,
     1521  /// but a short example shows a basic work:
     1522  ///\code
     1523  /// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run();
     1524  ///\endcode
     1525  ///
     1526  /// After the copy the \c nr map will contain the mapping from the
     1527  /// nodes of the \c from graph to the nodes of the \c to graph and
     1528  /// \c ecr will contain the mapping from the edges of the \c to graph
     1529  /// to the edges of the \c from graph.
     1530  ///
     1531  /// \see BpGraphCopy
     1532  template <typename From, typename To>
     1533  BpGraphCopy<From, To>
     1534  bpGraphCopy(const From& from, To& to) {
     1535    return BpGraphCopy<From, To>(from, to);
     1536  }
     1537
    11041538  namespace _core_bits {
    11051539
    11061540    template <typename Graph, typename Enable = void>
  • test/graph_copy_test.cc

    diff -r 1c825ca10e1f -r 353635c555b0 test/graph_copy_test.cc
    a b  
    209209  check(countArcs(from) == countArcs(to), "Wrong copy.");
    210210}
    211211
     212template <typename GR>
     213void bpgraph_copy_test() {
     214  const int nn = 10;
     215
     216  // Build a graph
     217  SmartBpGraph from;
     218  SmartBpGraph::NodeMap<int> fnm(from);
     219  SmartBpGraph::RedMap<int> frnm(from);
     220  SmartBpGraph::BlueMap<int> fbnm(from);
     221  SmartBpGraph::ArcMap<int> fam(from);
     222  SmartBpGraph::EdgeMap<int> fem(from);
     223  SmartBpGraph::Node fn = INVALID;
     224  SmartBpGraph::Arc fa = INVALID;
     225  SmartBpGraph::Edge fe = INVALID;
     226
     227  std::vector<SmartBpGraph::Node> frnv;
     228  for (int i = 0; i < nn; ++i) {
     229    SmartBpGraph::Node node = from.addRedNode();
     230    frnv.push_back(node);
     231    fnm[node] = i * i;
     232    frnm[node] = i + i;
     233    if (i == 0) fn = node;
     234  }
     235
     236  std::vector<SmartBpGraph::Node> fbnv;
     237  for (int i = 0; i < nn; ++i) {
     238    SmartBpGraph::Node node = from.addBlueNode();
     239    fbnv.push_back(node);
     240    fnm[node] = i * i;
     241    fbnm[node] = i + i;
     242  }
     243
     244  for (int i = 0; i < nn; ++i) {
     245    for (int j = 0; j < nn; ++j) {
     246      SmartBpGraph::Edge edge = from.addEdge(frnv[i], fbnv[j]);
     247      fem[edge] = i * i + j * j;
     248      fam[from.direct(edge, true)] = i + j * j;
     249      fam[from.direct(edge, false)] = i * i + j;
     250      if (i == 0 && j == 0) fa = from.direct(edge, true);
     251      if (i == 0 && j == 0) fe = edge;
     252    }
     253  }
     254
     255  // Test graph copy
     256  GR to;
     257  typename GR::template NodeMap<int> tnm(to);
     258  typename GR::template RedMap<int> trnm(to);
     259  typename GR::template BlueMap<int> tbnm(to);
     260  typename GR::template ArcMap<int> tam(to);
     261  typename GR::template EdgeMap<int> tem(to);
     262  typename GR::Node tn;
     263  typename GR::Arc ta;
     264  typename GR::Edge te;
     265
     266  SmartBpGraph::NodeMap<typename GR::Node> nr(from);
     267  SmartBpGraph::RedMap<typename GR::Node> rnr(from);
     268  SmartBpGraph::BlueMap<typename GR::Node> bnr(from);
     269  SmartBpGraph::ArcMap<typename GR::Arc> ar(from);
     270  SmartBpGraph::EdgeMap<typename GR::Edge> er(from);
     271
     272  typename GR::template NodeMap<SmartBpGraph::Node> ncr(to);
     273  typename GR::template RedMap<SmartBpGraph::Node> rncr(to);
     274  typename GR::template BlueMap<SmartBpGraph::Node> bncr(to);
     275  typename GR::template ArcMap<SmartBpGraph::Arc> acr(to);
     276  typename GR::template EdgeMap<SmartBpGraph::Edge> ecr(to);
     277
     278  bpGraphCopy(from, to).
     279    nodeMap(fnm, tnm).redMap(frnm, trnm).blueMap(fbnm, tbnm).
     280    arcMap(fam, tam).edgeMap(fem, tem).
     281    nodeRef(nr).redRef(rnr).blueRef(bnr).
     282    arcRef(ar).edgeRef(er).
     283    nodeCrossRef(ncr).redCrossRef(rncr).blueCrossRef(bncr).
     284    arcCrossRef(acr).edgeCrossRef(ecr).
     285    node(fn, tn).arc(fa, ta).edge(fe, te).run();
     286
     287  check(countNodes(from) == countNodes(to), "Wrong copy.");
     288  check(countRedNodes(from) == countRedNodes(to), "Wrong copy.");
     289  check(countBlueNodes(from) == countBlueNodes(to), "Wrong copy.");
     290  check(countEdges(from) == countEdges(to), "Wrong copy.");
     291  check(countArcs(from) == countArcs(to), "Wrong copy.");
     292
     293  for (SmartBpGraph::NodeIt it(from); it != INVALID; ++it) {
     294    check(ncr[nr[it]] == it, "Wrong copy.");
     295    check(fnm[it] == tnm[nr[it]], "Wrong copy.");
     296    if (from.red(it)) {
     297      check(rnr[it] == nr[it], "Wrong copy.");
     298      check(rncr[rnr[it]] == it, "Wrong copy.");
     299      check(frnm[it] == trnm[rnr[it]], "Wrong copy.");
     300      check(to.red(rnr[it]), "Wrong copy.");
     301    } else {
     302      check(bnr[it] == nr[it], "Wrong copy.");
     303      check(bncr[bnr[it]] == it, "Wrong copy.");
     304      check(fbnm[it] == tbnm[bnr[it]], "Wrong copy.");
     305      check(to.blue(bnr[it]), "Wrong copy.");
     306    }
     307  }
     308
     309  for (SmartBpGraph::ArcIt it(from); it != INVALID; ++it) {
     310    check(acr[ar[it]] == it, "Wrong copy.");
     311    check(fam[it] == tam[ar[it]], "Wrong copy.");
     312    check(nr[from.source(it)] == to.source(ar[it]), "Wrong copy.");
     313    check(nr[from.target(it)] == to.target(ar[it]), "Wrong copy.");
     314  }
     315
     316  for (SmartBpGraph::EdgeIt it(from); it != INVALID; ++it) {
     317    check(ecr[er[it]] == it, "Wrong copy.");
     318    check(fem[it] == tem[er[it]], "Wrong copy.");
     319    check(nr[from.u(it)] == to.u(er[it]) || nr[from.u(it)] == to.v(er[it]),
     320          "Wrong copy.");
     321    check(nr[from.v(it)] == to.u(er[it]) || nr[from.v(it)] == to.v(er[it]),
     322          "Wrong copy.");
     323    check((from.u(it) != from.v(it)) == (to.u(er[it]) != to.v(er[it])),
     324          "Wrong copy.");
     325  }
     326
     327  for (typename GR::NodeIt it(to); it != INVALID; ++it) {
     328    check(nr[ncr[it]] == it, "Wrong copy.");
     329  }
     330  for (typename GR::RedIt it(to); it != INVALID; ++it) {
     331    check(rncr[it] == ncr[it], "Wrong copy.");
     332    check(rnr[rncr[it]] == it, "Wrong copy.");
     333  }
     334  for (typename GR::BlueIt it(to); it != INVALID; ++it) {
     335    check(bncr[it] == ncr[it], "Wrong copy.");
     336    check(bnr[bncr[it]] == it, "Wrong copy.");
     337  }
     338  for (typename GR::ArcIt it(to); it != INVALID; ++it) {
     339    check(ar[acr[it]] == it, "Wrong copy.");
     340  }
     341  for (typename GR::EdgeIt it(to); it != INVALID; ++it) {
     342    check(er[ecr[it]] == it, "Wrong copy.");
     343  }
     344  check(tn == nr[fn], "Wrong copy.");
     345  check(ta == ar[fa], "Wrong copy.");
     346  check(te == er[fe], "Wrong copy.");
     347
     348  // Test repeated copy
     349  bpGraphCopy(from, to).run();
     350 
     351  check(countNodes(from) == countNodes(to), "Wrong copy.");
     352  check(countRedNodes(from) == countRedNodes(to), "Wrong copy.");
     353  check(countBlueNodes(from) == countBlueNodes(to), "Wrong copy.");
     354  check(countEdges(from) == countEdges(to), "Wrong copy.");
     355  check(countArcs(from) == countArcs(to), "Wrong copy.");
     356}
     357
    212358
    213359int main() {
    214360  digraph_copy_test<SmartDigraph>();
     
    216362  digraph_copy_test<StaticDigraph>();
    217363  graph_copy_test<SmartGraph>();
    218364  graph_copy_test<ListGraph>();
     365  bpgraph_copy_test<SmartBpGraph>();
     366  bpgraph_copy_test<ListBpGraph>();
    219367
    220368  return 0;
    221369}
  • lemon/smart_graph.h

    # HG changeset patch
    # User Balazs Dezso <deba@inf.elte.hu>
    # Date 1289891951 -3600
    # Node ID 41a2fc90ece074af5d01218bc33bcd66eff08642
    # Parent  353635c555b0038dbab145d2a20987528b062ecb
    Use member variables to store the highest IDs in partitions
    
    diff -r 353635c555b0 -r 41a2fc90ece0 lemon/smart_graph.h
    a b  
    829829    std::vector<ArcT> arcs;
    830830
    831831    int first_red, first_blue;
     832    int max_red, max_blue;
    832833
    833834  public:
    834835
     
    890891
    891892
    892893    SmartBpGraphBase()
    893       : nodes(), arcs(), first_red(-1), first_blue(-1) {}
     894      : nodes(), arcs(), first_red(-1), first_blue(-1),
     895        max_red(-1), max_blue(-1) {}
    894896
    895897    typedef True NodeNumTag;
    896898    typedef True EdgeNumTag;
    897899    typedef True ArcNumTag;
    898900
    899901    int nodeNum() const { return nodes.size(); }
    900     int redNum() const {
    901       return first_red == -1 ? 0 : nodes[first_red].partition_index + 1;
    902     }
    903     int blueNum() const {
    904       return first_blue == -1 ? 0 : nodes[first_blue].partition_index + 1;
    905     }
     902    int redNum() const { return max_red + 1; }
     903    int blueNum() const { return max_blue + 1; }
    906904    int edgeNum() const { return arcs.size() / 2; }
    907905    int arcNum() const { return arcs.size(); }
    908906
    909907    int maxNodeId() const { return nodes.size()-1; }
    910     int maxRedId() const {
    911       return first_red == -1 ? -1 : nodes[first_red].partition_index;
    912     }
    913     int maxBlueId() const {
    914       return first_blue == -1 ? -1 : nodes[first_blue].partition_index;
    915     }
     908    int maxRedId() const { return max_red; }
     909    int maxBlueId() const { return max_blue; }
    916910    int maxEdgeId() const { return arcs.size() / 2 - 1; }
    917911    int maxArcId() const { return arcs.size()-1; }
    918912
     
    10411035      nodes.push_back(NodeT());
    10421036      nodes[n].first_out = -1;
    10431037      nodes[n].red = true;
    1044       if (first_red == -1) {
    1045         nodes[n].partition_index = 0;
    1046       } else {
    1047         nodes[n].partition_index = nodes[first_red].partition_index + 1;
    1048       }
     1038      nodes[n].partition_index = ++max_red;
    10491039      nodes[n].partition_next = first_red;
    10501040      first_red = n;
    10511041
     
    10571047      nodes.push_back(NodeT());
    10581048      nodes[n].first_out = -1;
    10591049      nodes[n].red = false;
    1060       if (first_blue == -1) {
    1061         nodes[n].partition_index = 0;
    1062       } else {
    1063         nodes[n].partition_index = nodes[first_blue].partition_index + 1;
    1064       }
     1050      nodes[n].partition_index = ++max_blue;
    10651051      nodes[n].partition_next = first_blue;
    10661052      first_blue = n;
    10671053
     
    10901076      nodes.clear();
    10911077      first_red = -1;
    10921078      first_blue = -1;
     1079      max_blue = -1;
     1080      max_red = -1;
    10931081    }
    10941082
    10951083  };
     
    12441232        Node node = nodeFromId(n);
    12451233        if (Parent::red(node)) {
    12461234          first_red = nodes[n].partition_next;
     1235          if (first_red != -1) {
     1236            max_red = nodes[first_red].partition_index;
     1237          } else {
     1238            max_red = -1;
     1239          }
    12471240          Parent::notifier(RedNode()).erase(node);         
    12481241        } else {
    12491242          first_blue = nodes[n].partition_next;
     1243          if (first_blue != -1) {
     1244            max_blue = nodes[first_blue].partition_index;
     1245          } else {
     1246            max_blue = -1;
     1247          }
    12501248          Parent::notifier(BlueNode()).erase(node);
    12511249        }
    12521250        Parent::notifier(Node()).erase(node);