src/hugo/default_map.h
author marci
Tue, 21 Sep 2004 21:28:43 +0000
changeset 894 68a18cd0505c
parent 822 88226d9fe821
child 897 ef09eee53b09
permissions -rw-r--r--
todo for real comparison
     1 // -*- c++ -*-
     2 #ifndef DEFAULT_MAP_H
     3 #define DEFAULT_MAP_H
     4 
     5 
     6 #include <hugo/array_map.h>
     7 #include <hugo/vector_map.h>
     8 
     9 ///\ingroup graphmaps
    10 ///\file
    11 ///\brief Graph maps that construates and destruates
    12 ///their elements dynamically.
    13 
    14 namespace hugo {
    15 
    16 /// \addtogroup graphmaps
    17 /// @{
    18 
    19   /** The ArrayMap template class is graph map structure what
    20    *  automatically updates the map when a key is added to or erased from
    21    *  the map. This map uses the VectorMap if the ValueType is a primitive
    22    *  type and the ArrayMap for the other cases.
    23    *
    24    *  The template parameter is the MapRegistry that the maps
    25    *  will belong to and the ValueType.
    26    */
    27 
    28 
    29   /** Macro to implement the DefaultMap.
    30    */
    31 #define DEFAULT_MAP_BODY(DynMap, Value) \
    32 { \
    33 \
    34 public: \
    35 \
    36 typedef DynMap<MapRegistry, Value> Parent; \
    37 \
    38 typedef typename MapRegistry::Graph Graph; \
    39 \
    40 DefaultMap() : Parent() {} \
    41 DefaultMap(const Graph& g, MapRegistry& r) : Parent(g, r) {} \
    42 DefaultMap(const Graph& g, MapRegistry& r, const Value& v) \
    43   : Parent(g, r, v) {} \
    44 DefaultMap(const DefaultMap& copy) \
    45   : Parent(static_cast<const Parent&>(copy)) {} \
    46 template <typename TT> \
    47 DefaultMap(const DefaultMap<MapRegistry, TT>& copy) { \
    48   Parent::MapBase::operator= \
    49     (static_cast<const typename Parent::MapBase&>(copy)); \
    50   if (Parent::getGraph()) { \
    51     for (typename Parent::KeyIt it(*Parent::getGraph()); it!=INVALID; ++it) {\
    52       Parent::add(it); \
    53       Parent::operator[](it) = copy[it]; \
    54     } \
    55   } \
    56 } \
    57 DefaultMap& operator=(const DefaultMap& copy) { \
    58   Parent::operator=(static_cast<const Parent&>(copy)); \
    59   return *this; \
    60 } \
    61 template <typename TT> \
    62 DefaultMap& operator=(const DefaultMap<MapRegistry, TT>& copy) { \
    63   Parent::clear(); \
    64   Parent::MapBase::operator=(copy); \
    65   if (Parent::getGraph()) { \
    66     for (typename Parent::KeyIt it(*Parent::getGraph()); it!=INVALID; ++it) {\
    67       Parent::add(it); \
    68       Parent::operator[](it) = copy[it]; \
    69     } \
    70   } \
    71   return *this; \
    72 } \
    73 };
    74 
    75 
    76   template <typename MapRegistry, typename Type>
    77   class DefaultMap : public ArrayMap<MapRegistry, Type> 
    78   DEFAULT_MAP_BODY(ArrayMap, Type);
    79 
    80   template <typename MapRegistry>
    81   class DefaultMap<MapRegistry, bool> 
    82     : public VectorMap<MapRegistry, bool> 
    83   DEFAULT_MAP_BODY(VectorMap, bool);
    84 
    85   template <typename MapRegistry>
    86   class DefaultMap<MapRegistry, char> 
    87     : public VectorMap<MapRegistry, char> 
    88   DEFAULT_MAP_BODY(VectorMap, char);
    89 
    90   template <typename MapRegistry>
    91   class DefaultMap<MapRegistry, int> 
    92     : public VectorMap<MapRegistry, int> 
    93   DEFAULT_MAP_BODY(VectorMap, int);
    94 
    95   template <typename MapRegistry>
    96   class DefaultMap<MapRegistry, short> 
    97     : public VectorMap<MapRegistry, short> 
    98   DEFAULT_MAP_BODY(VectorMap, short);
    99 
   100   template <typename MapRegistry>
   101   class DefaultMap<MapRegistry, long> 
   102     : public VectorMap<MapRegistry, long> 
   103   DEFAULT_MAP_BODY(VectorMap, long);
   104 
   105   template <typename MapRegistry>
   106   class DefaultMap<MapRegistry, float> 
   107     : public VectorMap<MapRegistry, float> 
   108   DEFAULT_MAP_BODY(VectorMap, float);
   109 
   110   template <typename MapRegistry>
   111   class DefaultMap<MapRegistry, double> 
   112     : public VectorMap<MapRegistry, double> 
   113   DEFAULT_MAP_BODY(VectorMap, double);
   114 
   115   template <typename MapRegistry>
   116   class DefaultMap<MapRegistry, long double> 
   117     : public VectorMap<MapRegistry, long double> 
   118   DEFAULT_MAP_BODY(VectorMap, long double);
   119 
   120   template <typename MapRegistry, typename Type>
   121   class DefaultMap<MapRegistry, Type*>
   122     : public VectorMap<MapRegistry, Type*> 
   123   DEFAULT_MAP_BODY(VectorMap, Type*);
   124 
   125 }
   126 
   127 #endif