src/work/klao/iter_map.h
changeset 361 ab0899df30d2
parent 347 e4ab32225f1c
child 362 6c2e8a1f380a
     1.1 --- a/src/work/klao/iter_map.h	Wed Apr 21 15:14:45 2004 +0000
     1.2 +++ b/src/work/klao/iter_map.h	Wed Apr 21 15:46:40 2004 +0000
     1.3 @@ -16,12 +16,12 @@
     1.4  
     1.5    /// \todo Decide whether we need all the range checkings!!!
     1.6  
     1.7 -  template<typename KeyIntMap, uint8_t N>
     1.8 +  template<typename KeyIntMap, uint8_t N, typename Val = uint8_t>
     1.9    class IterableMap {
    1.10    public:
    1.11  
    1.12      typedef typename KeyIntMap::KeyType KeyType;
    1.13 -    typedef uint8_t ValueType;
    1.14 +    typedef Val ValueType;
    1.15  
    1.16      typedef typename std::vector<KeyType>::const_iterator iterator;
    1.17  
    1.18 @@ -29,11 +29,14 @@
    1.19      KeyIntMap &base;
    1.20      std::vector<KeyType> data;
    1.21      size_t bounds[N];
    1.22 +    Val def_val;
    1.23  
    1.24 -    uint8_t find(size_t a) const {
    1.25 -      uint8_t n=0;
    1.26 -      for(; n<N && bounds[n]<=a; ++n);
    1.27 -      return n;
    1.28 +    Val find(size_t a) const {
    1.29 +      for(uint8_t n=0; n<N; ++n) {
    1.30 +	if(bounds[n] > a)
    1.31 +	  return n;
    1.32 +      }
    1.33 +      return def_val;
    1.34      }
    1.35  
    1.36      void half_swap(size_t &a, size_t b) {
    1.37 @@ -66,44 +69,45 @@
    1.38  
    1.39    public:
    1.40      
    1.41 -    IterableMap(KeyIntMap &_base) : base(_base) {
    1.42 +    IterableMap(KeyIntMap &_base, Val d = N+1) : base(_base), def_val(d) {
    1.43        memset(bounds, 0, sizeof(bounds));
    1.44        //    for(int i=0; i<N; ++i) { bounds[i]=0; }
    1.45      }
    1.46  
    1.47 -    uint8_t operator[](const KeyType& k) const {
    1.48 +    Val operator[](const KeyType& k) const {
    1.49        return find(base[k]);
    1.50      }
    1.51  
    1.52 -    void set(const KeyType& k, uint8_t n) {
    1.53 +    void set(const KeyType& k, Val n) {
    1.54 +      // FIXME: n < N ???
    1.55        size_t a = base[k];
    1.56 -      if(a < bounds[N-1]) {
    1.57 +      if(a < bounds[N-1] && n < N) {
    1.58  	base.set(k, move(a, find(a), n));
    1.59        }
    1.60      }
    1.61  
    1.62 -    void insert(const KeyType& k, uint8_t n) {
    1.63 -      if(n<N) {
    1.64 +    void insert(const KeyType& k, Val n) {
    1.65 +      if(n < N) {
    1.66  	data.push_back(k);
    1.67  	base.set(k, move(bounds[N-1]++, N-1, n));
    1.68        }
    1.69      }
    1.70  
    1.71 -    iterator begin(uint8_t n) const {
    1.72 +    iterator begin(Val n) const {
    1.73        if(n < N)
    1.74  	return data.begin() + (n ? bounds[n-1] : 0);
    1.75        else
    1.76  	return data.end();
    1.77      }
    1.78  
    1.79 -    iterator end(uint8_t n) const {
    1.80 +    iterator end(Val n) const {
    1.81        if(n < N)
    1.82  	return data.begin() + bounds[n];
    1.83        else
    1.84  	return data.end();
    1.85      }
    1.86  
    1.87 -    size_t size(uint8_t n) const {
    1.88 +    size_t size(Val n) const {
    1.89        if(n < N)
    1.90  	return bounds[n] - (n ? bounds[n-1] : 0);
    1.91        else
    1.92 @@ -117,5 +121,16 @@
    1.93  
    1.94    };
    1.95  
    1.96 +
    1.97 +
    1.98 +
    1.99 +  template<typename KeyIntMap>
   1.100 +  class IterableBoolMap : public IterableMap<KeyIntMap, 2, bool> {
   1.101 +    typedef IterableMap<KeyIntMap, 2, bool> Parent;
   1.102 +
   1.103 +  public:
   1.104 +    IterableBoolMap(KeyIntMap &_base, bool d = false) : Parent(_base, d) {}
   1.105 +  };
   1.106 +
   1.107  }
   1.108  #endif