COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/graph_utils.h @ 1552:5c7f270f8e25

Last change on this file since 1552:5c7f270f8e25 was 1552:5c7f270f8e25, checked in by Balazs Dezso, 14 years ago

Swap function for DescriptorMap?

File size: 32.0 KB
Line 
1/* -*- C++ -*-
2 * lemon/graph_utils.h - Part of LEMON, a generic C++ optimization library
3 *
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
6 *
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
10 *
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
13 * purpose.
14 *
15 */
16
17#ifndef LEMON_GRAPH_UTILS_H
18#define LEMON_GRAPH_UTILS_H
19
20#include <iterator>
21#include <vector>
22#include <map>
23
24#include <lemon/invalid.h>
25#include <lemon/utility.h>
26#include <lemon/maps.h>
27#include <lemon/bits/alteration_notifier.h>
28
29///\ingroup gutils
30///\file
31///\brief Graph utilities.
32///
33///
34
35
36namespace lemon {
37
38  /// \addtogroup gutils
39  /// @{
40
41  /// \brief Function to count the items in the graph.
42  ///
43  /// This function counts the items (nodes, edges etc) in the graph.
44  /// The complexity of the function is O(n) because
45  /// it iterates on all of the items.
46
47  template <typename Graph, typename ItemIt>
48  inline int countItems(const Graph& g) {
49    int num = 0;
50    for (ItemIt it(g); it != INVALID; ++it) {
51      ++num;
52    }
53    return num;
54  }
55
56  // Node counting:
57
58  template <typename Graph>
59  inline
60  typename enable_if<typename Graph::NodeNumTag, int>::type
61  _countNodes(const Graph &g) {
62    return g.nodeNum();
63  }
64
65  template <typename Graph>
66  inline int _countNodes(Wrap<Graph> w) {
67    return countItems<Graph, typename Graph::NodeIt>(w.value);
68  }
69
70  /// \brief Function to count the nodes in the graph.
71  ///
72  /// This function counts the nodes in the graph.
73  /// The complexity of the function is O(n) but for some
74  /// graph structures it is specialized to run in O(1).
75  ///
76  /// \todo refer how to specialize it
77
78  template <typename Graph>
79  inline int countNodes(const Graph& g) {
80    return _countNodes<Graph>(g);
81  }
82
83  // Edge counting:
84
85  template <typename Graph>
86  inline
87  typename enable_if<typename Graph::EdgeNumTag, int>::type
88  _countEdges(const Graph &g) {
89    return g.edgeNum();
90  }
91
92  template <typename Graph>
93  inline int _countEdges(Wrap<Graph> w) {
94    return countItems<Graph, typename Graph::EdgeIt>(w.value);
95  }
96
97  /// \brief Function to count the edges in the graph.
98  ///
99  /// This function counts the edges in the graph.
100  /// The complexity of the function is O(e) but for some
101  /// graph structures it is specialized to run in O(1).
102
103  template <typename Graph>
104  inline int countEdges(const Graph& g) {
105    return _countEdges<Graph>(g);
106  }
107
108  // Undirected edge counting:
109
110  template <typename Graph>
111  inline
112  typename enable_if<typename Graph::EdgeNumTag, int>::type
113  _countUndirEdges(const Graph &g) {
114    return g.undirEdgeNum();
115  }
116
117  template <typename Graph>
118  inline int _countUndirEdges(Wrap<Graph> w) {
119    return countItems<Graph, typename Graph::UndirEdgeIt>(w.value);
120  }
121
122  /// \brief Function to count the undirected edges in the graph.
123  ///
124  /// This function counts the undirected edges in the graph.
125  /// The complexity of the function is O(e) but for some
126  /// graph structures it is specialized to run in O(1).
127
128  template <typename Graph>
129  inline int countUndirEdges(const Graph& g) {
130    return _countUndirEdges<Graph>(g);
131  }
132
133
134
135  template <typename Graph, typename DegIt>
136  inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
137    int num = 0;
138    for (DegIt it(_g, _n); it != INVALID; ++it) {
139      ++num;
140    }
141    return num;
142  }
143
144  /// \brief Function to count the number of the out-edges from node \c n.
145  ///
146  /// This function counts the number of the out-edges from node \c n
147  /// in the graph. 
148  template <typename Graph>
149  inline int countOutEdges(const Graph& _g,  const typename Graph::Node& _n) {
150    return countNodeDegree<Graph, typename Graph::OutEdgeIt>(_g, _n);
151  }
152
153  /// \brief Function to count the number of the in-edges to node \c n.
154  ///
155  /// This function counts the number of the in-edges to node \c n
156  /// in the graph. 
157  template <typename Graph>
158  inline int countInEdges(const Graph& _g,  const typename Graph::Node& _n) {
159    return countNodeDegree<Graph, typename Graph::InEdgeIt>(_g, _n);
160  }
161
162
163  /// Finds an edge between two nodes of a graph.
164
165  /// Finds an edge from node \c u to node \c v in graph \c g.
166  ///
167  /// If \c prev is \ref INVALID (this is the default value), then
168  /// it finds the first edge from \c u to \c v. Otherwise it looks for
169  /// the next edge from \c u to \c v after \c prev.
170  /// \return The found edge or \ref INVALID if there is no such an edge.
171  ///
172  /// Thus you can iterate through each edge from \c u to \c v as it follows.
173  /// \code
174  /// for(Edge e=findEdge(g,u,v);e!=INVALID;e=findEdge(g,u,v,e)) {
175  ///   ...
176  /// }
177  /// \endcode
178  /// \todo We may want to use the "GraphBase"
179  /// interface here...
180  /// \bug Untested ...
181  template <typename Graph>
182  typename Graph::Edge findEdge(const Graph &g,
183                                typename Graph::Node u, typename Graph::Node v,
184                                typename Graph::Edge prev = INVALID)
185  {
186    typename Graph::OutEdgeIt e(g,prev);
187    //    if(prev==INVALID) g.first(e,u);
188    if(prev==INVALID) e=typename Graph::OutEdgeIt(g,u);
189    else ++e;
190    while(e!=INVALID && g.target(e)!=v) ++e;
191    return e;
192  }
193
194  /// \brief Copy a map.
195  ///
196  /// This function copies the \c source map to the \c target map. It uses the
197  /// given iterator to iterate on the data structure and it uses the \c ref
198  /// mapping to convert the source's keys to the target's keys.
199  template <typename Target, typename Source,
200            typename ItemIt, typename Ref>         
201  void copyMap(Target& target, const Source& source,
202               ItemIt it, const Ref& ref) {
203    for (; it != INVALID; ++it) {
204      target[ref[it]] = source[it];
205    }
206  }
207
208  /// \brief Copy the source map to the target map.
209  ///
210  /// Copy the \c source map to the \c target map. It uses the given iterator
211  /// to iterate on the data structure.
212  template <typename Target, typename Source,
213            typename ItemIt>       
214  void copyMap(Target& target, const Source& source, ItemIt it) {
215    for (; it != INVALID; ++it) {
216      target[it] = source[it];
217    }
218  }
219
220
221  /// \brief Class to copy a graph.
222  ///
223  /// Class to copy a graph to an other graph (duplicate a graph). The
224  /// simplest way of using it is through the \c copyGraph() function.
225  template <typename Target, typename Source>
226  class GraphCopy {
227  public:
228    typedef typename Source::Node Node;
229    typedef typename Source::NodeIt NodeIt;
230    typedef typename Source::Edge Edge;
231    typedef typename Source::EdgeIt EdgeIt;
232
233    typedef typename Source::template NodeMap<typename Target::Node>NodeRefMap;
234    typedef typename Source::template EdgeMap<typename Target::Edge>EdgeRefMap;
235
236    /// \brief Constructor for the GraphCopy.
237    ///
238    /// It copies the content of the \c _source graph into the
239    /// \c _target graph. It creates also two references, one beetween
240    /// the two nodeset and one beetween the two edgesets.
241    GraphCopy(Target& _target, const Source& _source)
242      : source(_source), target(_target),
243        nodeRefMap(_source), edgeRefMap(_source) {
244      for (NodeIt it(source); it != INVALID; ++it) {
245        nodeRefMap[it] = target.addNode();
246      }
247      for (EdgeIt it(source); it != INVALID; ++it) {
248        edgeRefMap[it] = target.addEdge(nodeRefMap[source.source(it)],
249                                        nodeRefMap[source.target(it)]);
250      }
251    }
252
253    /// \brief Copies the node references into the given map.
254    ///
255    /// Copies the node references into the given map.
256    template <typename NodeRef>
257    const GraphCopy& nodeRef(NodeRef& map) const {
258      for (NodeIt it(source); it != INVALID; ++it) {
259        map.set(it, nodeRefMap[it]);
260      }
261      return *this;
262    }
263
264    /// \brief Reverse and copies the node references into the given map.
265    ///
266    /// Reverse and copies the node references into the given map.
267    template <typename NodeRef>
268    const GraphCopy& nodeCrossRef(NodeRef& map) const {
269      for (NodeIt it(source); it != INVALID; ++it) {
270        map.set(nodeRefMap[it], it);
271      }
272      return *this;
273    }
274
275    /// \brief Copies the edge references into the given map.
276    ///
277    /// Copies the edge references into the given map.
278    template <typename EdgeRef>
279    const GraphCopy& edgeRef(EdgeRef& map) const {
280      for (EdgeIt it(source); it != INVALID; ++it) {
281        map.set(it, edgeRefMap[it]);
282      }
283      return *this;
284    }
285
286    /// \brief Reverse and copies the edge references into the given map.
287    ///
288    /// Reverse and copies the edge references into the given map.
289    template <typename EdgeRef>
290    const GraphCopy& edgeCrossRef(EdgeRef& map) const {
291      for (EdgeIt it(source); it != INVALID; ++it) {
292        map.set(edgeRefMap[it], it);
293      }
294      return *this;
295    }
296
297    /// \brief Make copy of the given map.
298    ///
299    /// Makes copy of the given map for the newly created graph.
300    /// The new map's key type is the target graph's node type,
301    /// and the copied map's key type is the source graph's node
302    /// type. 
303    template <typename TargetMap, typename SourceMap>
304    const GraphCopy& nodeMap(TargetMap& tMap, const SourceMap& sMap) const {
305      copyMap(tMap, sMap, NodeIt(source), nodeRefMap);
306      return *this;
307    }
308
309    /// \brief Make copy of the given map.
310    ///
311    /// Makes copy of the given map for the newly created graph.
312    /// The new map's key type is the target graph's edge type,
313    /// and the copied map's key type is the source graph's edge
314    /// type. 
315    template <typename TargetMap, typename SourceMap>
316    const GraphCopy& edgeMap(TargetMap& tMap, const SourceMap& sMap) const {
317      copyMap(tMap, sMap, EdgeIt(source), edgeRefMap);
318      return *this;
319    }
320
321    /// \brief Gives back the stored node references.
322    ///
323    /// Gives back the stored node references.
324    const NodeRefMap& nodeRef() const {
325      return nodeRefMap;
326    }
327
328    /// \brief Gives back the stored edge references.
329    ///
330    /// Gives back the stored edge references.
331    const EdgeRefMap& edgeRef() const {
332      return edgeRefMap;
333    }
334
335  private:
336   
337    const Source& source;
338    Target& target;
339
340    NodeRefMap nodeRefMap;
341    EdgeRefMap edgeRefMap;
342  };
343
344  /// \brief Copy a graph to an other graph.
345  ///
346  /// Copy a graph to an other graph.
347  /// The usage of the function:
348  ///
349  /// \code
350  /// copyGraph(trg, src).nodeRef(nr).edgeCrossRef(ecr);
351  /// \endcode
352  ///
353  /// After the copy the \c nr map will contain the mapping from the
354  /// source graph's nodes to the target graph's nodes and the \c ecr will
355  /// contain the mapping from the target graph's edges to the source's
356  /// edges.
357  template <typename Target, typename Source>
358  GraphCopy<Target, Source> copyGraph(Target& target, const Source& source) {
359    return GraphCopy<Target, Source>(target, source);
360  }
361
362  template <typename _Graph, typename _Item>
363  class ItemSetTraits {};
364 
365  template <typename _Graph>
366  class ItemSetTraits<_Graph, typename _Graph::Node> {
367  public:
368   
369    typedef _Graph Graph;
370
371    typedef typename Graph::Node Item;
372    typedef typename Graph::NodeIt ItemIt;
373
374    template <typename _Value>
375    class Map : public Graph::template NodeMap<_Value> {
376    public:
377      typedef typename Graph::template NodeMap<_Value> Parent;
378      typedef typename Parent::Value Value;
379
380      Map(const Graph& _graph) : Parent(_graph) {}
381      Map(const Graph& _graph, const Value& _value)
382        : Parent(_graph, _value) {}
383    };
384
385  };
386
387  template <typename _Graph>
388  class ItemSetTraits<_Graph, typename _Graph::Edge> {
389  public:
390   
391    typedef _Graph Graph;
392
393    typedef typename Graph::Edge Item;
394    typedef typename Graph::EdgeIt ItemIt;
395
396    template <typename _Value>
397    class Map : public Graph::template EdgeMap<_Value> {
398    public:
399      typedef typename Graph::template EdgeMap<_Value> Parent;
400      typedef typename Parent::Value Value;
401
402      Map(const Graph& _graph) : Parent(_graph) {}
403      Map(const Graph& _graph, const Value& _value)
404        : Parent(_graph, _value) {}
405    };
406
407  };
408
409  template <typename _Graph>
410  class ItemSetTraits<_Graph, typename _Graph::UndirEdge> {
411  public:
412   
413    typedef _Graph Graph;
414
415    typedef typename Graph::UndirEdge Item;
416    typedef typename Graph::UndirEdgeIt ItemIt;
417
418    template <typename _Value>
419    class Map : public Graph::template UndirEdgeMap<_Value> {
420    public:
421      typedef typename Graph::template UndirEdgeMap<_Value> Parent;
422      typedef typename Parent::Value Value;
423
424      Map(const Graph& _graph) : Parent(_graph) {}
425      Map(const Graph& _graph, const Value& _value)
426        : Parent(_graph, _value) {}
427    };
428
429  };
430
431  /// @}
432
433  /// \addtogroup graph_maps
434  /// @{
435
436  template <typename Map, typename Enable = void>
437  struct ReferenceMapTraits {
438    typedef typename Map::Value Value;
439    typedef typename Map::Value& Reference;
440    typedef const typename Map::Value& ConstReference;
441    typedef typename Map::Value* Pointer;
442    typedef const typename Map::Value* ConstPointer;
443  };
444
445  template <typename Map>
446  struct ReferenceMapTraits<
447    Map,
448    typename enable_if<typename Map::FullTypeTag, void>::type
449  > {
450    typedef typename Map::Value Value;
451    typedef typename Map::Reference Reference;
452    typedef typename Map::ConstReference ConstReference;
453    typedef typename Map::Pointer Pointer;
454    typedef typename Map::ConstPointer ConstPointer;
455  };
456
457  /// Provides an immutable and unique id for each item in the graph.
458
459  /// The IdMap class provides a unique and immutable id for each item of the
460  /// same type (e.g. node) in the graph. This id is <ul><li>\b unique:
461  /// different items (nodes) get different ids <li>\b immutable: the id of an
462  /// item (node) does not change (even if you delete other nodes).  </ul>
463  /// Through this map you get access (i.e. can read) the inner id values of
464  /// the items stored in the graph. This map can be inverted with its member
465  /// class \c InverseMap.
466  ///
467  template <typename _Graph, typename _Item>
468  class IdMap {
469  public:
470    typedef _Graph Graph;
471    typedef int Value;
472    typedef _Item Item;
473    typedef _Item Key;
474
475    typedef True NeedCopy;
476
477    /// \brief Constructor.
478    ///
479    /// Constructor for creating id map.
480    IdMap(const Graph& _graph) : graph(&_graph) {}
481
482    /// \brief Gives back the \e id of the item.
483    ///
484    /// Gives back the immutable and unique \e id of the map.
485    int operator[](const Item& item) const { return graph->id(item);}
486
487
488  private:
489    const Graph* graph;
490
491  public:
492
493    /// \brief The class represents the inverse of its owner (IdMap).
494    ///
495    /// The class represents the inverse of its owner (IdMap).
496    /// \see inverse()
497    class InverseMap {
498    public:
499
500      typedef True NeedCopy;
501
502      /// \brief Constructor.
503      ///
504      /// Constructor for creating an id-to-item map.
505      InverseMap(const Graph& _graph) : graph(&_graph) {}
506
507      /// \brief Constructor.
508      ///
509      /// Constructor for creating an id-to-item map.
510      InverseMap(const IdMap& idMap) : graph(idMap.graph) {}
511
512      /// \brief Gives back the given item from its id.
513      ///
514      /// Gives back the given item from its id.
515      ///
516      Item operator[](int id) const { return graph->fromId(id, Item());}
517    private:
518      const Graph* graph;
519    };
520
521    /// \brief Gives back the inverse of the map.
522    ///
523    /// Gives back the inverse of the IdMap.
524    InverseMap inverse() const { return InverseMap(*graph);}
525
526  };
527
528 
529  /// \brief General invertable graph-map type.
530
531  /// This type provides simple invertable graph-maps.
532  /// The InvertableMap wraps an arbitrary ReadWriteMap
533  /// and if a key is set to a new value then store it
534  /// in the inverse map.
535  /// \param _Graph The graph type.
536  /// \param _Map The map to extend with invertable functionality.
537  template <
538    typename _Graph,
539    typename _Item,
540    typename _Value,
541    typename _Map
542    = typename ItemSetTraits<_Graph, _Item>::template Map<_Value>::Parent
543  >
544  class InvertableMap : protected _Map {
545
546  public:
547 
548    typedef _Map Map;
549    typedef _Graph Graph;
550
551    /// The key type of InvertableMap (Node, Edge, UndirEdge).
552    typedef typename _Map::Key Key;
553    /// The value type of the InvertableMap.
554    typedef typename _Map::Value Value;
555
556    /// \brief Constructor.
557    ///
558    /// Construct a new InvertableMap for the graph.
559    ///
560    InvertableMap(const Graph& graph) : Map(graph) {}
561   
562    /// \brief The setter function of the map.
563    ///
564    /// Sets the mapped value.
565    void set(const Key& key, const Value& val) {
566      Value oldval = Map::operator[](key);
567      typename Container::iterator it = invMap.find(oldval);
568      if (it != invMap.end() && it->second == key) {
569        invMap.erase(it);
570      }     
571      invMap.insert(make_pair(val, key));
572      Map::set(key, val);
573    }
574
575    /// \brief The getter function of the map.
576    ///
577    /// It gives back the value associated with the key.
578    const Value operator[](const Key& key) const {
579      return Map::operator[](key);
580    }
581
582  protected:
583
584    /// \brief Add a new key to the map.
585    ///
586    /// Add a new key to the map. It is called by the
587    /// \c AlterationNotifier.
588    virtual void add(const Key& key) {
589      Map::add(key);
590    }
591
592    /// \brief Erase the key from the map.
593    ///
594    /// Erase the key to the map. It is called by the
595    /// \c AlterationNotifier.
596    virtual void erase(const Key& key) {
597      Value val = Map::operator[](key);
598      typename Container::iterator it = invMap.find(val);
599      if (it != invMap.end() && it->second == key) {
600        invMap.erase(it);
601      }
602      Map::erase(key);
603    }
604
605    /// \brief Clear the keys from the map and inverse map.
606    ///
607    /// Clear the keys from the map and inverse map. It is called by the
608    /// \c AlterationNotifier.
609    virtual void clear() {
610      invMap.clear();
611      Map::clear();
612    }
613
614  private:
615   
616    typedef std::map<Value, Key> Container;
617    Container invMap;   
618
619  public:
620
621    /// \brief The inverse map type.
622    ///
623    /// The inverse of this map. The subscript operator of the map
624    /// gives back always the item what was last assigned to the value.
625    class InverseMap {
626    public:
627      /// \brief Constructor of the InverseMap.
628      ///
629      /// Constructor of the InverseMap.
630      InverseMap(const InvertableMap& _inverted) : inverted(_inverted) {}
631
632      /// The value type of the InverseMap.
633      typedef typename InvertableMap::Key Value;
634      /// The key type of the InverseMap.
635      typedef typename InvertableMap::Value Key;
636
637      /// \brief Subscript operator.
638      ///
639      /// Subscript operator. It gives back always the item
640      /// what was last assigned to the value.
641      Value operator[](const Key& key) const {
642        typename Container::const_iterator it = inverted.invMap.find(key);
643        return it->second;
644      }
645     
646    private:
647      const InvertableMap& inverted;
648    };
649
650    /// \brief It gives back the just readeable inverse map.
651    ///
652    /// It gives back the just readeable inverse map.
653    InverseMap inverse() const {
654      return InverseMap(*this);
655    }
656
657
658   
659  };
660
661  /// \brief Provides a mutable, continuous and unique descriptor for each
662  /// item in the graph.
663  ///
664  /// The DescriptorMap class provides a unique and continuous (but mutable)
665  /// descriptor (id) for each item of the same type (e.g. node) in the
666  /// graph. This id is <ul><li>\b unique: different items (nodes) get
667  /// different ids <li>\b continuous: the range of the ids is the set of
668  /// integers between 0 and \c n-1, where \c n is the number of the items of
669  /// this type (e.g. nodes) (so the id of a node can change if you delete an
670  /// other node, i.e. this id is mutable).  </ul> This map can be inverted
671  /// with its member class \c InverseMap.
672  ///
673  /// \param _Graph The graph class the \c DescriptorMap belongs to.
674  /// \param _Item The Item is the Key of the Map. It may be Node, Edge or
675  /// UndirEdge.
676  /// \param _Map A ReadWriteMap mapping from the item type to integer.
677  template <
678    typename _Graph,   
679    typename _Item,
680    typename _Map
681    = typename ItemSetTraits<_Graph, _Item>::template Map<int>::Parent
682  >
683  class DescriptorMap : protected _Map {
684
685    typedef _Item Item;
686    typedef _Map Map;
687
688  public:
689    /// The graph class of DescriptorMap.
690    typedef _Graph Graph;
691
692    /// The key type of DescriptorMap (Node, Edge, UndirEdge).
693    typedef typename _Map::Key Key;
694    /// The value type of DescriptorMap.
695    typedef typename _Map::Value Value;
696
697    /// \brief Constructor.
698    ///
699    /// Constructor for descriptor map.
700    DescriptorMap(const Graph& _graph) : Map(_graph) {
701      build();
702    }
703
704  protected:
705
706    /// \brief Add a new key to the map.
707    ///
708    /// Add a new key to the map. It is called by the
709    /// \c AlterationNotifier.
710    virtual void add(const Item& item) {
711      Map::add(item);
712      Map::set(item, invMap.size());
713      invMap.push_back(item);
714    }
715
716    /// \brief Erase the key from the map.
717    ///
718    /// Erase the key to the map. It is called by the
719    /// \c AlterationNotifier.
720    virtual void erase(const Item& item) {
721      Map::set(invMap.back(), Map::operator[](item));
722      invMap[Map::operator[](item)] = invMap.back();
723      invMap.pop_back();
724      Map::erase(item);
725    }
726
727    /// \brief Build the unique map.
728    ///
729    /// Build the unique map. It is called by the
730    /// \c AlterationNotifier.
731    virtual void build() {
732      Map::build();
733      Item it;
734      const typename Map::Graph* graph = Map::getGraph();
735      for (graph->first(it); it != INVALID; graph->next(it)) {
736        Map::set(it, invMap.size());
737        invMap.push_back(it);   
738      }     
739    }
740   
741    /// \brief Clear the keys from the map.
742    ///
743    /// Clear the keys from the map. It is called by the
744    /// \c AlterationNotifier.
745    virtual void clear() {
746      invMap.clear();
747      Map::clear();
748    }
749
750  public:
751
752    /// \brief Swaps the position of the two items in the map.
753    ///
754    /// Swaps the position of the two items in the map.
755    void swap(const Item& p, const Item& q) {
756      int pi = Map::operator[](p);
757      int qi = Map::operator[](q);
758      Map::set(p, qi);
759      invMap[qi] = p;
760      Map::set(q, pi);
761      invMap[pi] = q;
762    }
763
764    /// \brief Gives back the \e descriptor of the item.
765    ///
766    /// Gives back the mutable and unique \e descriptor of the map.
767    int operator[](const Item& item) const {
768      return Map::operator[](item);
769    }
770   
771  private:
772
773    typedef std::vector<Item> Container;
774    Container invMap;
775
776  public:
777    /// \brief The inverse map type of DescriptorMap.
778    ///
779    /// The inverse map type of DescriptorMap.
780    class InverseMap {
781    public:
782      /// \brief Constructor of the InverseMap.
783      ///
784      /// Constructor of the InverseMap.
785      InverseMap(const DescriptorMap& _inverted)
786        : inverted(_inverted) {}
787
788
789      /// The value type of the InverseMap.
790      typedef typename DescriptorMap::Key Value;
791      /// The key type of the InverseMap.
792      typedef typename DescriptorMap::Value Key;
793
794      /// \brief Subscript operator.
795      ///
796      /// Subscript operator. It gives back the item
797      /// that the descriptor belongs to currently.
798      Value operator[](const Key& key) const {
799        return inverted.invMap[key];
800      }
801
802      /// \brief Size of the map.
803      ///
804      /// Returns the size of the map.
805      int size() const {
806        return inverted.invMap.size();
807      }
808     
809    private:
810      const DescriptorMap& inverted;
811    };
812
813    /// \brief Gives back the inverse of the map.
814    ///
815    /// Gives back the inverse of the map.
816    const InverseMap inverse() const {
817      return InverseMap(*this);
818    }
819  };
820
821  /// \brief Returns the source of the given edge.
822  ///
823  /// The SourceMap gives back the source Node of the given edge.
824  /// \author Balazs Dezso
825  template <typename Graph>
826  class SourceMap {
827  public:
828
829    typedef True NeedCopy;
830
831    typedef typename Graph::Node Value;
832    typedef typename Graph::Edge Key;
833
834    /// \brief Constructor
835    ///
836    /// Constructor
837    /// \param _graph The graph that the map belongs to.
838    SourceMap(const Graph& _graph) : graph(_graph) {}
839
840    /// \brief The subscript operator.
841    ///
842    /// The subscript operator.
843    /// \param edge The edge
844    /// \return The source of the edge
845    Value operator[](const Key& edge) {
846      return graph.source(edge);
847    }
848
849  private:
850    const Graph& graph;
851  };
852
853  /// \brief Returns a \ref SourceMap class
854  ///
855  /// This function just returns an \ref SourceMap class.
856  /// \relates SourceMap
857  template <typename Graph>
858  inline SourceMap<Graph> sourceMap(const Graph& graph) {
859    return SourceMap<Graph>(graph);
860  }
861
862  /// \brief Returns the target of the given edge.
863  ///
864  /// The TargetMap gives back the target Node of the given edge.
865  /// \author Balazs Dezso
866  template <typename Graph>
867  class TargetMap {
868  public:
869
870    typedef True NeedCopy;
871
872    typedef typename Graph::Node Value;
873    typedef typename Graph::Edge Key;
874
875    /// \brief Constructor
876    ///
877    /// Constructor
878    /// \param _graph The graph that the map belongs to.
879    TargetMap(const Graph& _graph) : graph(_graph) {}
880
881    /// \brief The subscript operator.
882    ///
883    /// The subscript operator.
884    /// \param e The edge
885    /// \return The target of the edge
886    Value operator[](const Key& e) {
887      return graph.target(e);
888    }
889
890  private:
891    const Graph& graph;
892  };
893
894  /// \brief Returns a \ref TargetMap class
895  ///
896  /// This function just returns a \ref TargetMap class.
897  /// \relates TargetMap
898  template <typename Graph>
899  inline TargetMap<Graph> targetMap(const Graph& graph) {
900    return TargetMap<Graph>(graph);
901  }
902
903  /// \brief Returns the "forward" directed edge view of an undirected edge.
904  ///
905  /// Returns the "forward" directed edge view of an undirected edge.
906  /// \author Balazs Dezso
907  template <typename Graph>
908  class ForwardMap {
909  public:
910
911    typedef True NeedCopy;
912
913    typedef typename Graph::Edge Value;
914    typedef typename Graph::UndirEdge Key;
915
916    /// \brief Constructor
917    ///
918    /// Constructor
919    /// \param _graph The graph that the map belongs to.
920    ForwardMap(const Graph& _graph) : graph(_graph) {}
921
922    /// \brief The subscript operator.
923    ///
924    /// The subscript operator.
925    /// \param key An undirected edge
926    /// \return The "forward" directed edge view of undirected edge
927    Value operator[](const Key& key) const {
928      return graph.edgeWithSource(key, graph.source(key));
929    }
930
931  private:
932    const Graph& graph;
933  };
934
935  /// \brief Returns a \ref ForwardMap class
936  ///
937  /// This function just returns an \ref ForwardMap class.
938  /// \relates ForwardMap
939  template <typename Graph>
940  inline ForwardMap<Graph> forwardMap(const Graph& graph) {
941    return ForwardMap<Graph>(graph);
942  }
943
944  /// \brief Returns the "backward" directed edge view of an undirected edge.
945  ///
946  /// Returns the "backward" directed edge view of an undirected edge.
947  /// \author Balazs Dezso
948  template <typename Graph>
949  class BackwardMap {
950  public:
951    typedef True NeedCopy;
952
953    typedef typename Graph::Edge Value;
954    typedef typename Graph::UndirEdge Key;
955
956    /// \brief Constructor
957    ///
958    /// Constructor
959    /// \param _graph The graph that the map belongs to.
960    BackwardMap(const Graph& _graph) : graph(_graph) {}
961
962    /// \brief The subscript operator.
963    ///
964    /// The subscript operator.
965    /// \param key An undirected edge
966    /// \return The "backward" directed edge view of undirected edge
967    Value operator[](const Key& key) const {
968      return graph.edgeWithSource(key, graph.target(key));
969    }
970
971  private:
972    const Graph& graph;
973  };
974
975  /// \brief Returns a \ref BackwardMap class
976
977  /// This function just returns a \ref BackwardMap class.
978  /// \relates BackwardMap
979  template <typename Graph>
980  inline BackwardMap<Graph> backwardMap(const Graph& graph) {
981    return BackwardMap<Graph>(graph);
982  }
983
984  template <typename _Graph>
985  class DegMapBase {
986  public:
987   
988    typedef _Graph Graph;
989
990  protected:
991
992    typedef typename Graph::template NodeMap<int> IntNodeMap;
993   
994  };
995
996  /// \brief Map of the node in-degrees.
997  ///
998  /// This map returns the in-degree of a node. Once it is constructed,
999  /// the degrees are stored in a standard NodeMap, so each query is done
1000  /// in constant time. On the other hand, the values are updated automatically
1001  /// whenever the graph changes.
1002  ///
1003  /// \sa OutDegMap
1004
1005  template <typename _Graph>
1006  class InDegMap 
1007    : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
1008
1009  public:
1010   
1011    typedef _Graph Graph;
1012    typedef int Value;
1013    typedef typename Graph::Node Key;
1014
1015  private:
1016
1017    class AutoNodeMap : public Graph::template NodeMap<int> {
1018    public:
1019
1020      typedef typename Graph::template NodeMap<int> Parent;
1021
1022      typedef typename Parent::Key Key;
1023      typedef typename Parent::Value Value;
1024     
1025      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
1026     
1027      void add(const Key& key) {
1028        Parent::add(key);
1029        Parent::set(key, 0);
1030      }
1031    };
1032
1033  public:
1034
1035    /// \brief Constructor.
1036    ///
1037    /// Constructor for creating in-degree map.
1038    InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
1039      AlterationNotifier<typename _Graph::Edge>
1040        ::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
1041     
1042      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1043        deg[it] = countInEdges(graph, it);
1044      }
1045    }
1046
1047    virtual ~InDegMap() {
1048      AlterationNotifier<typename _Graph::Edge>::
1049        ObserverBase::detach();
1050    }
1051   
1052    /// Gives back the in-degree of a Node.
1053    int operator[](const Key& key) const {
1054      return deg[key];
1055    }
1056
1057  protected:
1058   
1059    typedef typename Graph::Edge Edge;
1060
1061    virtual void add(const Edge& edge) {
1062      ++deg[graph.target(edge)];
1063    }
1064
1065    virtual void erase(const Edge& edge) {
1066      --deg[graph.target(edge)];
1067    }
1068
1069
1070    virtual void build() {
1071      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1072        deg[it] = countInEdges(graph, it);
1073      }     
1074    }
1075
1076    virtual void clear() {
1077      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1078        deg[it] = 0;
1079      }
1080    }
1081  private:
1082   
1083    const _Graph& graph;
1084    AutoNodeMap deg;
1085  };
1086
1087
1088  /// \brief Map of the node out-degrees.
1089  ///
1090  /// This map returns the out-degree of a node. Once it is constructed,
1091  /// the degrees are stored in a standard NodeMap, so each query is done
1092  /// in constant time. On the other hand, the values are updated automatically
1093  /// whenever the graph changes.
1094  ///
1095  /// \sa OutDegMap
1096
1097  template <typename _Graph>
1098  class OutDegMap 
1099    : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase {
1100
1101  public:
1102   
1103    typedef _Graph Graph;
1104    typedef int Value;
1105    typedef typename Graph::Node Key;
1106
1107  private:
1108
1109    class AutoNodeMap : public Graph::template NodeMap<int> {
1110    public:
1111
1112      typedef typename Graph::template NodeMap<int> Parent;
1113
1114      typedef typename Parent::Key Key;
1115      typedef typename Parent::Value Value;
1116     
1117      AutoNodeMap(const Graph& graph) : Parent(graph, 0) {}
1118     
1119      void add(const Key& key) {
1120        Parent::add(key);
1121        Parent::set(key, 0);
1122      }
1123    };
1124
1125  public:
1126
1127    /// \brief Constructor.
1128    ///
1129    /// Constructor for creating out-degree map.
1130    OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) {
1131      AlterationNotifier<typename _Graph::Edge>
1132        ::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge()));
1133     
1134      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1135        deg[it] = countOutEdges(graph, it);
1136      }
1137    }
1138
1139    virtual ~OutDegMap() {
1140      AlterationNotifier<typename _Graph::Edge>::
1141        ObserverBase::detach();
1142    }
1143   
1144    /// Gives back the in-degree of a Node.
1145    int operator[](const Key& key) const {
1146      return deg[key];
1147    }
1148
1149  protected:
1150   
1151    typedef typename Graph::Edge Edge;
1152
1153    virtual void add(const Edge& edge) {
1154      ++deg[graph.source(edge)];
1155    }
1156
1157    virtual void erase(const Edge& edge) {
1158      --deg[graph.source(edge)];
1159    }
1160
1161
1162    virtual void build() {
1163      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1164        deg[it] = countOutEdges(graph, it);
1165      }     
1166    }
1167
1168    virtual void clear() {
1169      for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) {
1170        deg[it] = 0;
1171      }
1172    }
1173  private:
1174   
1175    const _Graph& graph;
1176    AutoNodeMap deg;
1177  };
1178
1179  /// @}
1180
1181} //END OF NAMESPACE LEMON
1182
1183#endif
Note: See TracBrowser for help on using the repository browser.