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