src/hugo/sym_map.h
author alpar
Mon, 13 Sep 2004 17:20:03 +0000
changeset 842 a4bb28813570
child 844 9bf990cb066d
permissions -rw-r--r--
Fix a DANGEROUS bug.
deba@822
     1
// -*- c++ -*-
deba@822
     2
#ifndef SYM_MAP_H
deba@822
     3
#define SYM_MAP_H
deba@822
     4
deba@822
     5
///\ingroup graphmaps
deba@822
     6
///\file
deba@822
     7
///\brief Graph maps that construates and destruates
deba@822
     8
///their elements dynamically.
deba@822
     9
deba@822
    10
namespace hugo {
deba@822
    11
deba@822
    12
/// \addtogroup graphmaps
deba@822
    13
/// @{
deba@822
    14
deba@822
    15
  /** The SymEdgeIt is wrapper class for the EdgeIt. It can be used to
deba@822
    16
   *  iterate on the symmetric maps when all of the edge - reverse edge pair
deba@822
    17
   *  has different parity.
deba@822
    18
   */
deba@822
    19
deba@822
    20
  template <typename Graph, typename Edge, typename EdgeIt>
deba@822
    21
  class SymEdgeIt : public EdgeIt {
deba@822
    22
  public:
deba@822
    23
deba@822
    24
    /** Default constructor.
deba@822
    25
     */
deba@822
    26
    SymEdgeIt() 
deba@822
    27
      : EdgeIt() {}
deba@822
    28
deba@822
    29
    /** Graph initialized constructor.
deba@822
    30
     */
deba@822
    31
    SymEdgeIt(const Graph& graph) 
deba@822
    32
      : EdgeIt(graph) {
deba@822
    33
      while ( (n & 1) && n != -1) {
deba@822
    34
	EdgeIt::operator++();
deba@822
    35
      }
deba@822
    36
    }
deba@822
    37
deba@822
    38
    /** Creating invelid SymEdgeIt.
deba@822
    39
     */
deba@822
    40
    SymEdgeIt(Invalid invalid) 
deba@822
    41
      : EdgeIt(invalid) {}
deba@822
    42
deba@822
    43
    /** SymEdgeIt from the given Edge.
deba@822
    44
     */
deba@822
    45
    SymEdgeIt(const Graph& graph, const Edge& edge)
deba@822
    46
      : EdgeIt(graph, edge) {
deba@822
    47
      while ( (n & 1) && n != -1) {
deba@822
    48
	EdgeIt::operator++();
deba@822
    49
      }
deba@822
    50
    }
deba@822
    51
deba@822
    52
    /** Increase operator.
deba@822
    53
     */
deba@822
    54
    SymEdgeIt& operator++() {
deba@822
    55
      EdgeIt::operator++();
deba@822
    56
      while ( (n & 1) && n != -1) {
deba@822
    57
	EdgeIt::operator++();
deba@822
    58
      }
deba@822
    59
      return *this;
deba@822
    60
    }
deba@822
    61
  };
deba@822
    62
deba@822
    63
  /** The SymMap template class is graph map structure what
deba@822
    64
   *  wraps an other map structure to use as symmetric map structure.
deba@822
    65
   *
deba@822
    66
   *  The template parameter is the MapRegistry that the maps
deba@822
    67
   *  will belong to and the ValueType.
deba@822
    68
   */
deba@822
    69
  template <template <typename, typename> class DynMap, 
deba@822
    70
	    typename MapRegistry, typename Value>
deba@822
    71
  class SymMap : public DynMap<MapRegistry, Value>{
deba@822
    72
deba@822
    73
  private:
deba@822
    74
deba@822
    75
    typedef DynMap<MapRegistry, Value> MapImpl;
deba@822
    76
deba@822
    77
  public:
deba@822
    78
		
deba@822
    79
    /// The graph type of the maps. 
deba@822
    80
    typedef typename MapRegistry::Graph Graph;
deba@822
    81
deba@822
    82
    typedef typename MapImpl::KeyType KeyType;
deba@822
    83
deba@822
    84
  public:
deba@822
    85
deba@822
    86
deba@822
    87
    /** Default constructor for the map.
deba@822
    88
     */
deba@822
    89
    SymMap() : MapImpl() {}
deba@822
    90
deba@822
    91
    /** Graph and Registry initialized map constructor.
deba@822
    92
     */
deba@822
    93
    SymMap(const Graph& g, MapRegistry& r) : MapImpl(g, r) {}
deba@822
    94
deba@822
    95
    /** Constructor to use default value to initialize the map. 
deba@822
    96
     */
deba@822
    97
    SymMap(const Graph& g, MapRegistry& r, const Value& v) 
deba@822
    98
      : MapImpl(g, r, v) {}
deba@822
    99
deba@822
   100
    /** Constructor to copy a map of the same map type.
deba@822
   101
     */
deba@822
   102
    SymMap(const SymMap& copy) 
deba@822
   103
      : MapImpl(static_cast<const MapImpl&>(copy)) {}
deba@822
   104
deba@822
   105
    /** Constructor to copy a map of an other map type.
deba@822
   106
     */
deba@822
   107
    template <typename CMap> SymMap(const CMap& copy) 
deba@822
   108
      : MapImpl(copy) {}
deba@822
   109
deba@822
   110
    /** Assign operator to copy a map of the same map type.
deba@822
   111
     */
deba@822
   112
    SymMap& operator=(const SymMap& copy) {
deba@822
   113
      MapImpl::operator=(static_cast<const MapImpl&>(copy));
deba@822
   114
      return *this;
deba@822
   115
    }
deba@822
   116
deba@822
   117
    /** Assign operator to copy a map of an other map type.
deba@822
   118
     */
deba@822
   119
    template <typename CMap> SymMap& operator=(const CMap& copy) {
deba@822
   120
      MapImpl::operator=(copy);
deba@822
   121
      return *this;
deba@822
   122
    }
deba@822
   123
   
deba@822
   124
    /**
deba@822
   125
     * The subscript operator. The map can be subscripted by the
deba@822
   126
     * actual keys of the graph. 
deba@822
   127
     */
deba@822
   128
    typename MapImpl::ReferenceType operator[](const KeyType& key) {
deba@822
   129
      int id = MapImpl::getGraph()->id(key);	
deba@822
   130
      return MapImpl::operator[](id >> 1);
deba@822
   131
    } 
deba@822
   132
		
deba@822
   133
    /**
deba@822
   134
     * The const subscript operator. The map can be subscripted by the
deba@822
   135
     * actual keys of the graph. 
deba@822
   136
     */
deba@822
   137
    typename MapImpl::ConstReferenceType operator[](const KeyType& key) const {
deba@822
   138
      int id = MapImpl::getGraph()->id(key);
deba@822
   139
      return MapImpl::operator[](id >> 1);
deba@822
   140
    }
deba@822
   141
	
deba@822
   142
    /** Setter function of the map. Equivalent with map[key] = val.
deba@822
   143
     *  This is a compatibility feature with the not dereferable maps.
deba@822
   144
     */
deba@822
   145
    void set(const KeyType& key, const typename MapImpl::ValueType& val) {
deba@822
   146
      int id = MapImpl::getGraph()->id(key);
deba@822
   147
      MapImpl::operator[](id >> 1) = val;
deba@822
   148
    }
deba@822
   149
		
deba@822
   150
    /** Add a new key to the map. It called by the map registry.
deba@822
   151
     */
deba@822
   152
    void add(const KeyType& key) {
deba@822
   153
      int id = MapImpl::getGraph()->id(key);
deba@822
   154
      if (id & 1) return;
deba@822
   155
      MapImpl::add(key);
deba@822
   156
    }
deba@822
   157
		
deba@822
   158
    /** Erase a key from the map. It called by the map registry.
deba@822
   159
     */
deba@822
   160
    void erase(const KeyType& key) {
deba@822
   161
      int id = MapImpl::getGraph()->id(key);
deba@822
   162
      if (id & 1) return;
deba@822
   163
      MapImpl::add(key);
deba@822
   164
    }
deba@822
   165
  };
deba@822
   166
deba@822
   167
  /// @}
deba@822
   168
}
deba@822
   169
deba@822
   170
#endif