src/lemon/sym_map.h
author marci
Fri, 01 Oct 2004 11:31:03 +0000
changeset 933 1b7c88fbb950
parent 919 6153d9cf78c6
permissions -rw-r--r--
NodeSubGraphWrapper, test, and ducumentation modifications.
alpar@919
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/sym_map.h - Part of LEMON, a generic C++ optimization library
alpar@919
     3
 *
alpar@919
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@919
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@919
     6
 *
alpar@919
     7
 * Permission to use, modify and distribute this software is granted
alpar@919
     8
 * provided that this copyright notice appears in all copies. For
alpar@919
     9
 * precise terms see the accompanying LICENSE file.
alpar@919
    10
 *
alpar@919
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@919
    12
 * express or implied, and with no claim as to its suitability for any
alpar@919
    13
 * purpose.
alpar@919
    14
 *
alpar@919
    15
 */
alpar@919
    16
alpar@921
    17
#ifndef LEMON_SYM_MAP_H
alpar@921
    18
#define LEMON_SYM_MAP_H
alpar@919
    19
alpar@919
    20
///\ingroup graphmaps
alpar@919
    21
///\file
alpar@919
    22
///\brief Graph maps that construates and destruates
alpar@919
    23
///their elements dynamically.
alpar@919
    24
alpar@921
    25
namespace lemon {
alpar@919
    26
alpar@919
    27
/// \addtogroup graphmaps
alpar@919
    28
/// @{
alpar@919
    29
alpar@919
    30
  /** The SymEdgeIt is wrapper class for the EdgeIt. It can be used to
alpar@919
    31
   *  iterate on the symmetric maps when all of the edge - reverse edge pair
alpar@919
    32
   *  has different parity.
alpar@919
    33
   */
alpar@919
    34
alpar@919
    35
alpar@919
    36
  template <typename Graph, typename Edge, typename EdgeIt>
alpar@919
    37
  class SymEdgeIt : public EdgeIt {
alpar@919
    38
  public:
alpar@919
    39
alpar@919
    40
    /** Default constructor.
alpar@919
    41
     */
alpar@919
    42
    SymEdgeIt() 
alpar@919
    43
      : EdgeIt() {}
alpar@919
    44
alpar@919
    45
    /** Graph initialized constructor.
alpar@919
    46
     */
alpar@919
    47
    SymEdgeIt(const Graph& graph) 
alpar@919
    48
      : EdgeIt(graph) {
alpar@919
    49
      while ( (EdgeIt::n & 1) && EdgeIt::n != -1) {
alpar@919
    50
	EdgeIt::operator++();
alpar@919
    51
      }
alpar@919
    52
    }
alpar@919
    53
alpar@919
    54
    /** Creating invelid SymEdgeIt.
alpar@919
    55
     */
alpar@919
    56
    SymEdgeIt(Invalid invalid) 
alpar@919
    57
      : EdgeIt(invalid) {}
alpar@919
    58
alpar@919
    59
    /** SymEdgeIt from the given Edge.
alpar@919
    60
     */
alpar@919
    61
    SymEdgeIt(const Graph& graph, const Edge& edge)
alpar@919
    62
      : EdgeIt(graph, edge) {
alpar@919
    63
      while ( (EdgeIt::n & 1) && EdgeIt::n != -1) {
alpar@919
    64
	EdgeIt::operator++();
alpar@919
    65
      }
alpar@919
    66
    }
alpar@919
    67
alpar@919
    68
    /** Increase operator.
alpar@919
    69
     */
alpar@919
    70
    SymEdgeIt& operator++() {
alpar@919
    71
      EdgeIt::operator++();
alpar@919
    72
      while ( (EdgeIt::n & 1) && EdgeIt::n != -1) {
alpar@919
    73
	EdgeIt::operator++();
alpar@919
    74
      }
alpar@919
    75
      return *this;
alpar@919
    76
    }
alpar@919
    77
  };
alpar@919
    78
alpar@919
    79
  /** The SymMap template class is graph map structure what
alpar@919
    80
   *  wraps an other map structure to use as symmetric map structure.
alpar@919
    81
   *
alpar@919
    82
   *  The template parameter is the MapRegistry that the maps
alpar@919
    83
   *  will belong to and the ValueType.
alpar@919
    84
   */
alpar@919
    85
  template <template <typename, typename> class DynMap, 
alpar@919
    86
	    typename MapRegistry, typename Value>
alpar@919
    87
  class SymMap : public DynMap<MapRegistry, Value>{
alpar@919
    88
alpar@919
    89
  private:
alpar@919
    90
alpar@919
    91
    typedef DynMap<MapRegistry, Value> MapImpl;
alpar@919
    92
alpar@919
    93
  public:
alpar@919
    94
		
alpar@919
    95
    /// The graph type of the maps. 
alpar@919
    96
    typedef typename MapRegistry::Graph Graph;
alpar@919
    97
alpar@919
    98
    typedef typename MapImpl::KeyType KeyType;
alpar@919
    99
alpar@919
   100
  public:
alpar@919
   101
alpar@919
   102
alpar@919
   103
    /** Graph and Registry initialized map constructor.
alpar@919
   104
     */
alpar@919
   105
    SymMap(const Graph& g, MapRegistry& r) : MapImpl(g, r) {}
alpar@919
   106
alpar@919
   107
    /** Constructor to use default value to initialize the map. 
alpar@919
   108
     */
alpar@919
   109
    SymMap(const Graph& g, MapRegistry& r, const Value& v) 
alpar@919
   110
      : MapImpl(g, r, v) {}
alpar@919
   111
alpar@919
   112
    /** Constructor to copy a map of the same map type.
alpar@919
   113
     */
alpar@919
   114
    SymMap(const SymMap& copy) 
alpar@919
   115
      : MapImpl(static_cast<const MapImpl&>(copy)) {}
alpar@919
   116
alpar@919
   117
    /** Assign operator to copy a map of the same map type.
alpar@919
   118
     */
alpar@919
   119
    SymMap& operator=(const SymMap& copy) {
alpar@919
   120
      MapImpl::operator=(static_cast<const MapImpl&>(copy));
alpar@919
   121
      return *this;
alpar@919
   122
    }
alpar@919
   123
alpar@919
   124
    /** Add a new key to the map. It called by the map registry.
alpar@919
   125
     */
alpar@919
   126
    void add(const KeyType& key) {
alpar@919
   127
      int id = MapImpl::getGraph()->id(key);
alpar@919
   128
      if (id & 1) return;
alpar@919
   129
      MapImpl::add(key);
alpar@919
   130
    }
alpar@919
   131
		
alpar@919
   132
    /** Erase a key from the map. It called by the map registry.
alpar@919
   133
     */
alpar@919
   134
    void erase(const KeyType& key) {
alpar@919
   135
      int id = MapImpl::getGraph()->id(key);
alpar@919
   136
      if (id & 1) return;
alpar@919
   137
      MapImpl::add(key);
alpar@919
   138
    }
alpar@919
   139
  };
alpar@919
   140
alpar@919
   141
  /// @}
alpar@919
   142
}
alpar@919
   143
alpar@919
   144
#endif