COIN-OR::LEMON - Graph Library

Changeset 1037:3eaff8d04171 in lemon-0.x for src/work/deba/map_utils.h


Ignore:
Timestamp:
12/15/04 20:56:55 (19 years ago)
Author:
Balazs Dezso
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1427
Message:

graph_io under construction
This is a working version, but needs more improvments.

todo:

documention + fix the file format
improve the exception system

add some possible asserts

tutorials

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/work/deba/map_utils.h

    r1032 r1037  
    3636  template <
    3737    typename _Graph,
    38     typename _Map,
    39     template <typename, typename> class _InvMap = std::Map
     38    typename _Map
    4039  >
    4140  class InversableMap : protected _Map {
    4241
    4342  public:
     43    typedef _Graph Graph;
    4444
    45     typename _Map Map;
    46     typename _InvMap<Map::Value, Map::Key> InverseMap;
     45    typedef _Map Map;
     46    typedef typename _Map::Key Key;
     47    typedef typename _Map::Value Value;
     48    typedef std::map<Value, Key> InverseMap;
    4749   
    48     typename _Map::Key Key;
    49     typename _Map::Value Value;
    50     typename _Map::ConstReference ConstReference;
     50    typedef typename _Map::ConstReference ConstReference;
    5151
    5252    /// Constructor.
     
    6161    void set(const Key& key, const Value& val) {
    6262      Value oldval = Map::operator[](key);
    63       InverseMap::iterator it = invMap.find(oldval);
    64       if (it != invMap.end() && it->second == key) {
    65         invMap.erase(it);
     63      typename InverseMap::iterator it = inv_map.find(oldval);
     64      if (it != inv_map.end() && it->second == key) {
     65        inv_map.erase(it);
    6666      }     
    67       invMap.insert(make_pair(val, key));
     67      inv_map.insert(make_pair(val, key));
    6868      Map::set(key, val);
    6969    }
     
    7979    virtual void erase(const Key&) {
    8080      Value val = Map::operator[](key);
    81       InverseMap::iterator it = invMap.find(val);
    82       if (it != invMap.end() && it->second == key) {
     81      typename InverseMap::iterator it = inv_map.find(val);
     82      if (it != inv_map.end() && it->second == key) {
    8383        invMap.erase(it);
    8484      }
     
    8787
    8888    const InverseMap& inverse() const {
    89       return invMap;
     89      return inv_map;
    9090    }
    9191
    9292
    9393  private:
    94     InverseMap invMap;   
     94    InverseMap inv_map;   
    9595  };
     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 
    96164
    97165}
Note: See TracChangeset for help on using the changeset viewer.