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