COIN-OR::LEMON - Graph Library

Changeset 361:ab0899df30d2 in lemon-0.x for src/work/klao/iter_map.h


Ignore:
Timestamp:
04/21/04 17:46:40 (20 years ago)
Author:
Mihaly Barasz
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@489
Message:

IterableMap? with template ValueType?. IterableBoolMap? as a specialization.
Range checking warnings...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/work/klao/iter_map.h

    r347 r361  
    1717  /// \todo Decide whether we need all the range checkings!!!
    1818
    19   template<typename KeyIntMap, uint8_t N>
     19  template<typename KeyIntMap, uint8_t N, typename Val = uint8_t>
    2020  class IterableMap {
    2121  public:
    2222
    2323    typedef typename KeyIntMap::KeyType KeyType;
    24     typedef uint8_t ValueType;
     24    typedef Val ValueType;
    2525
    2626    typedef typename std::vector<KeyType>::const_iterator iterator;
     
    3030    std::vector<KeyType> data;
    3131    size_t bounds[N];
     32    Val def_val;
    3233
    33     uint8_t find(size_t a) const {
    34       uint8_t n=0;
    35       for(; n<N && bounds[n]<=a; ++n);
    36       return n;
     34    Val find(size_t a) const {
     35      for(uint8_t n=0; n<N; ++n) {
     36        if(bounds[n] > a)
     37          return n;
     38      }
     39      return def_val;
    3740    }
    3841
     
    6770  public:
    6871   
    69     IterableMap(KeyIntMap &_base) : base(_base) {
     72    IterableMap(KeyIntMap &_base, Val d = N+1) : base(_base), def_val(d) {
    7073      memset(bounds, 0, sizeof(bounds));
    7174      //    for(int i=0; i<N; ++i) { bounds[i]=0; }
    7275    }
    7376
    74     uint8_t operator[](const KeyType& k) const {
     77    Val operator[](const KeyType& k) const {
    7578      return find(base[k]);
    7679    }
    7780
    78     void set(const KeyType& k, uint8_t n) {
     81    void set(const KeyType& k, Val n) {
     82      // FIXME: n < N ???
    7983      size_t a = base[k];
    80       if(a < bounds[N-1]) {
     84      if(a < bounds[N-1] && n < N) {
    8185        base.set(k, move(a, find(a), n));
    8286      }
    8387    }
    8488
    85     void insert(const KeyType& k, uint8_t n) {
    86       if(n<N) {
     89    void insert(const KeyType& k, Val n) {
     90      if(n < N) {
    8791        data.push_back(k);
    8892        base.set(k, move(bounds[N-1]++, N-1, n));
     
    9094    }
    9195
    92     iterator begin(uint8_t n) const {
     96    iterator begin(Val n) const {
    9397      if(n < N)
    9498        return data.begin() + (n ? bounds[n-1] : 0);
     
    97101    }
    98102
    99     iterator end(uint8_t n) const {
     103    iterator end(Val n) const {
    100104      if(n < N)
    101105        return data.begin() + bounds[n];
     
    104108    }
    105109
    106     size_t size(uint8_t n) const {
     110    size_t size(Val n) const {
    107111      if(n < N)
    108112        return bounds[n] - (n ? bounds[n-1] : 0);
     
    118122  };
    119123
     124
     125
     126
     127  template<typename KeyIntMap>
     128  class IterableBoolMap : public IterableMap<KeyIntMap, 2, bool> {
     129    typedef IterableMap<KeyIntMap, 2, bool> Parent;
     130
     131  public:
     132    IterableBoolMap(KeyIntMap &_base, bool d = false) : Parent(_base, d) {}
     133  };
     134
    120135}
    121136#endif
Note: See TracChangeset for help on using the changeset viewer.