[347] | 1 | // -*- c++ -*- // |
---|
| 2 | |
---|
| 3 | #ifndef HUGO_ITER_MAP |
---|
| 4 | #define HUGO_ITER_MAP |
---|
| 5 | |
---|
| 6 | #include <vector> |
---|
| 7 | #include <algorithm> |
---|
| 8 | // for uint8_t |
---|
| 9 | #include <stdint.h> |
---|
| 10 | // for memset |
---|
| 11 | #include <cstring> |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | namespace hugo { |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | /// \todo Decide whether we need all the range checkings!!! |
---|
| 18 | |
---|
[361] | 19 | template<typename KeyIntMap, uint8_t N, typename Val = uint8_t> |
---|
[347] | 20 | class IterableMap { |
---|
| 21 | public: |
---|
| 22 | |
---|
| 23 | typedef typename KeyIntMap::KeyType KeyType; |
---|
[361] | 24 | typedef Val ValueType; |
---|
[347] | 25 | |
---|
| 26 | typedef typename std::vector<KeyType>::const_iterator iterator; |
---|
| 27 | |
---|
| 28 | protected: |
---|
| 29 | KeyIntMap &base; |
---|
| 30 | std::vector<KeyType> data; |
---|
| 31 | size_t bounds[N]; |
---|
[361] | 32 | Val def_val; |
---|
[347] | 33 | |
---|
[361] | 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; |
---|
[347] | 40 | } |
---|
| 41 | |
---|
| 42 | void half_swap(size_t &a, size_t b) { |
---|
| 43 | if(a != b) { |
---|
| 44 | base.set(data[b],a); |
---|
| 45 | data[a] = data[b]; |
---|
| 46 | a = b; |
---|
| 47 | } |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | size_t move(size_t a, uint8_t m, uint8_t n) { |
---|
| 51 | if(m != n) { |
---|
| 52 | size_t orig_a = a; |
---|
| 53 | KeyType orig_key = data[a]; |
---|
| 54 | while(m > n) { |
---|
| 55 | --m; |
---|
| 56 | half_swap(a, bounds[m]++); |
---|
| 57 | } |
---|
[362] | 58 | // FIXME: range check ide? |
---|
[347] | 59 | while(m < n) { |
---|
| 60 | half_swap(a, --bounds[m]); |
---|
| 61 | ++m; |
---|
| 62 | } |
---|
| 63 | if(a != orig_a) { |
---|
| 64 | base.set(orig_key, a); |
---|
| 65 | data[a]=orig_key; |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | return a; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | public: |
---|
| 72 | |
---|
[361] | 73 | IterableMap(KeyIntMap &_base, Val d = N+1) : base(_base), def_val(d) { |
---|
[347] | 74 | memset(bounds, 0, sizeof(bounds)); |
---|
| 75 | // for(int i=0; i<N; ++i) { bounds[i]=0; } |
---|
| 76 | } |
---|
| 77 | |
---|
[361] | 78 | Val operator[](const KeyType& k) const { |
---|
[347] | 79 | return find(base[k]); |
---|
| 80 | } |
---|
| 81 | |
---|
[361] | 82 | void set(const KeyType& k, Val n) { |
---|
[362] | 83 | // FIXME: range check? |
---|
[347] | 84 | size_t a = base[k]; |
---|
[362] | 85 | if(a < bounds[N-1]) { |
---|
[347] | 86 | base.set(k, move(a, find(a), n)); |
---|
| 87 | } |
---|
[362] | 88 | else { |
---|
| 89 | insert(k, n); |
---|
| 90 | } |
---|
[347] | 91 | } |
---|
| 92 | |
---|
[361] | 93 | void insert(const KeyType& k, Val n) { |
---|
[362] | 94 | data.push_back(k); |
---|
| 95 | base.set(k, move(bounds[N-1]++, N-1, n)); |
---|
[347] | 96 | } |
---|
| 97 | |
---|
[361] | 98 | iterator begin(Val n) const { |
---|
[362] | 99 | return data.begin() + (n ? bounds[n-1] : 0); |
---|
[347] | 100 | } |
---|
| 101 | |
---|
[361] | 102 | iterator end(Val n) const { |
---|
[362] | 103 | return data.begin() + bounds[n]; |
---|
[347] | 104 | } |
---|
| 105 | |
---|
[361] | 106 | size_t size(Val n) const { |
---|
[362] | 107 | return bounds[n] - (n ? bounds[n-1] : 0); |
---|
[347] | 108 | } |
---|
| 109 | |
---|
| 110 | size_t size() const { |
---|
| 111 | // assert(bounds[N-1] == data.size()); |
---|
| 112 | return bounds[N-1]; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | }; |
---|
| 116 | |
---|
[361] | 117 | |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | template<typename KeyIntMap> |
---|
| 121 | class IterableBoolMap : public IterableMap<KeyIntMap, 2, bool> { |
---|
| 122 | typedef IterableMap<KeyIntMap, 2, bool> Parent; |
---|
| 123 | |
---|
| 124 | public: |
---|
| 125 | IterableBoolMap(KeyIntMap &_base, bool d = false) : Parent(_base, d) {} |
---|
| 126 | }; |
---|
| 127 | |
---|
[347] | 128 | } |
---|
| 129 | #endif |
---|