src/work/deba/map_utils.h
changeset 1050 bcc0766a7b86
parent 1032 9e903d3a1ef6
child 1115 444f69240539
equal deleted inserted replaced
0:c3e5b0710ff9 1:af8e07b21ecf
    33   /// The InversableMap wraps an arbitrary ReadWriteMap 
    33   /// The InversableMap wraps an arbitrary ReadWriteMap 
    34   /// and if a key is setted to a new value then store it
    34   /// and if a key is setted to a new value then store it
    35   /// in the inverse map.
    35   /// in the inverse map.
    36   template <
    36   template <
    37     typename _Graph, 
    37     typename _Graph, 
    38     typename _Map, 
    38     typename _Map
    39     template <typename, typename> class _InvMap = std::Map
       
    40   >
    39   >
    41   class InversableMap : protected _Map {
    40   class InversableMap : protected _Map {
    42 
    41 
    43   public:
    42   public:
       
    43     typedef _Graph Graph;
    44 
    44 
    45     typename _Map Map;
    45     typedef _Map Map;
    46     typename _InvMap<Map::Value, Map::Key> InverseMap;
    46     typedef typename _Map::Key Key;
       
    47     typedef typename _Map::Value Value;
       
    48     typedef std::map<Value, Key> InverseMap;
    47     
    49     
    48     typename _Map::Key Key;
    50     typedef typename _Map::ConstReference ConstReference;
    49     typename _Map::Value Value;
       
    50     typename _Map::ConstReference ConstReference;
       
    51 
    51 
    52     /// Constructor.
    52     /// Constructor.
    53 
    53 
    54     /// Construct a new InversableMap for the graph.
    54     /// Construct a new InversableMap for the graph.
    55     ///
    55     ///
    58     /// The setter function of the map.
    58     /// The setter function of the map.
    59 
    59 
    60     /// It sets the map and the inverse map 
    60     /// It sets the map and the inverse map 
    61     void set(const Key& key, const Value& val) {
    61     void set(const Key& key, const Value& val) {
    62       Value oldval = Map::operator[](key);
    62       Value oldval = Map::operator[](key);
    63       InverseMap::iterator it = invMap.find(oldval);
    63       typename InverseMap::iterator it = inv_map.find(oldval);
    64       if (it != invMap.end() && it->second == key) {
    64       if (it != inv_map.end() && it->second == key) {
    65 	invMap.erase(it);
    65 	inv_map.erase(it);
    66       }      
    66       }      
    67       invMap.insert(make_pair(val, key));
    67       inv_map.insert(make_pair(val, key));
    68       Map::set(key, val);
    68       Map::set(key, val);
    69     }
    69     }
    70 
    70 
    71     ConstReference operator[](const Key&) const {
    71     ConstReference operator[](const Key&) const {
    72       return Map::operator[](key);
    72       return Map::operator[](key);
    76       Map::add(key);
    76       Map::add(key);
    77     }
    77     }
    78 
    78 
    79     virtual void erase(const Key&) {
    79     virtual void erase(const Key&) {
    80       Value val = Map::operator[](key);
    80       Value val = Map::operator[](key);
    81       InverseMap::iterator it = invMap.find(val);
    81       typename InverseMap::iterator it = inv_map.find(val);
    82       if (it != invMap.end() && it->second == key) {
    82       if (it != inv_map.end() && it->second == key) {
    83 	invMap.erase(it);
    83 	invMap.erase(it);
    84       }
    84       }
    85       Map::erase(key);
    85       Map::erase(key);
    86     }
    86     }
    87 
    87 
    88     const InverseMap& inverse() const {
    88     const InverseMap& inverse() const {
    89       return invMap;
    89       return inv_map;
    90     } 
    90     } 
    91 
    91 
    92 
    92 
    93   private:
    93   private:
    94     InverseMap invMap;    
    94     InverseMap inv_map;    
    95   };
    95   };
       
    96 
       
    97 
       
    98   // unique, continous, mutable
       
    99 
       
   100   template <
       
   101     typename _Graph,   
       
   102     typename _Item,
       
   103     typename _ItemIt,
       
   104     typename _Map
       
   105   >
       
   106   class DescriptorMap : protected _Map {
       
   107   public:
       
   108     typedef _Graph Graph;
       
   109     typedef _Item Item;
       
   110     typedef _ItemIt ItemIt;
       
   111     typedef _Map Map;
       
   112 
       
   113 
       
   114     typedef typename _Map::Key Key;
       
   115     typedef typename _Map::Value Value;
       
   116 
       
   117     typedef vector<Item> InverseMap;
       
   118 
       
   119     DescriptorMap(const Graph& _graph) : Map(_graph) {
       
   120       build();
       
   121     }
       
   122 
       
   123     virtual void add(const Item& item) {
       
   124       Map::add(item);
       
   125       Map::set(item, inv_map.size());
       
   126       inv_map.push_back(item);
       
   127     }
       
   128 
       
   129     virtual void erase(const Item& item) {
       
   130       Map::set(inv_map.back(), Map::operator[](item));
       
   131       inv_map[Map::operator[](item)] = inv_map.back();
       
   132       Map::erase(item);
       
   133     }
       
   134 
       
   135     virtual void build() {
       
   136       Map::build();
       
   137       for (ItemIt it(*Map::getGraph()); it != INVALID; ++it) {
       
   138 	Map::set(it, inv_map.size());
       
   139 	inv_map.push_back(it);	
       
   140       }      
       
   141     }
       
   142     
       
   143     virtual void clear() {
       
   144       inv_map.clear();
       
   145       Map::clear();
       
   146     }
       
   147 
       
   148     int operator[](const Item& item) const {
       
   149       return Map::operator[](item);
       
   150     }
       
   151 
       
   152     
       
   153     const InverseMap inverse() const {
       
   154       return inv_map;
       
   155     }
       
   156 
       
   157   private:
       
   158     vector<Item> inv_map;
       
   159   };
       
   160 
       
   161   // unique, immutable => IDMap
       
   162   
       
   163   
    96 
   164 
    97 }
   165 }
    98 
   166