src/work/klao/iter_map.h
changeset 538 d8863141824d
parent 365 9ca84022df34
child 555 995bc1f1a3ce
equal deleted inserted replaced
3:94b1993d70a4 4:f54bbb9398ec
    12 
    12 
    13 #include <invalid.h>
    13 #include <invalid.h>
    14 
    14 
    15 namespace hugo {
    15 namespace hugo {
    16 
    16 
       
    17   /// \brief A map with "small integers" as value set which can enumarate it
       
    18   /// value classes
    17 
    19 
    18   /// \todo Decide whether we need all the range checkings!!!
    20   /// \todo Decide whether we need all the range checkings!!!
       
    21 
       
    22   /// \todo Implement dynamic map behaviour. Is it necessary? Yes it is.
    19 
    23 
    20   template<typename KeyIntMap, uint8_t N, typename Val = uint8_t>
    24   template<typename KeyIntMap, uint8_t N, typename Val = uint8_t>
    21   class IterableMap {
    25   class IterableMap {
    22   public:
    26   public:
    23 
    27 
    82 
    86 
    83     void set(const KeyType& k, Val n) {
    87     void set(const KeyType& k, Val n) {
    84       // FIXME: range check?
    88       // FIXME: range check?
    85       size_t a = base[k];
    89       size_t a = base[k];
    86       if(a < bounds[N-1]) {
    90       if(a < bounds[N-1]) {
    87 	base.set(k, move(a, find(a), n));
    91 	move(a, find(a), n);
    88       }
    92       }
    89       else {
    93       else {
    90 	insert(k, n);
    94 	insert(k, n);
    91       }
    95       }
    92     }
    96     }
    93 
    97 
    94     void insert(const KeyType& k, Val n) {
    98     void insert(const KeyType& k, Val n) {
    95       data.push_back(k);
    99       data.push_back(k);
    96       base.set(k, move(bounds[N-1]++, N-1, n));
   100       base.set(k, move(bounds[N-1]++, N-1, n));
       
   101     }
       
   102 
       
   103     /// This func is not very usable, but necessary to implement 
       
   104     /// dynamic map behaviour.
       
   105     void remove(const KeyType& k) {
       
   106       size_t a = base[k];
       
   107       if(a < bounds[N-1]) {
       
   108 	move(a, find(a), N);
       
   109 	data.pop_back();
       
   110 	base.set(k, -1);
       
   111       }
    97     }
   112     }
    98 
   113 
    99     iterator begin(Val n) const {
   114     iterator begin(Val n) const {
   100       return data.begin() + (n ? bounds[n-1] : 0);
   115       return data.begin() + (n ? bounds[n-1] : 0);
   101     }
   116     }