[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 | |
---|
[365] | 13 | #include <invalid.h> |
---|
[347] | 14 | |
---|
| 15 | namespace hugo { |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | /// \todo Decide whether we need all the range checkings!!! |
---|
| 19 | |
---|
[361] | 20 | template<typename KeyIntMap, uint8_t N, typename Val = uint8_t> |
---|
[347] | 21 | class IterableMap { |
---|
| 22 | public: |
---|
| 23 | |
---|
| 24 | typedef typename KeyIntMap::KeyType KeyType; |
---|
[361] | 25 | typedef Val ValueType; |
---|
[347] | 26 | |
---|
| 27 | typedef typename std::vector<KeyType>::const_iterator iterator; |
---|
| 28 | |
---|
| 29 | protected: |
---|
| 30 | KeyIntMap &base; |
---|
| 31 | std::vector<KeyType> data; |
---|
| 32 | size_t bounds[N]; |
---|
[361] | 33 | Val def_val; |
---|
[347] | 34 | |
---|
[361] | 35 | Val find(size_t a) const { |
---|
| 36 | for(uint8_t n=0; n<N; ++n) { |
---|
| 37 | if(bounds[n] > a) |
---|
| 38 | return n; |
---|
| 39 | } |
---|
| 40 | return def_val; |
---|
[347] | 41 | } |
---|
| 42 | |
---|
| 43 | void half_swap(size_t &a, size_t b) { |
---|
| 44 | if(a != b) { |
---|
| 45 | base.set(data[b],a); |
---|
| 46 | data[a] = data[b]; |
---|
| 47 | a = b; |
---|
| 48 | } |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | size_t move(size_t a, uint8_t m, uint8_t n) { |
---|
| 52 | if(m != n) { |
---|
| 53 | size_t orig_a = a; |
---|
| 54 | KeyType orig_key = data[a]; |
---|
| 55 | while(m > n) { |
---|
| 56 | --m; |
---|
| 57 | half_swap(a, bounds[m]++); |
---|
| 58 | } |
---|
[362] | 59 | // FIXME: range check ide? |
---|
[347] | 60 | while(m < n) { |
---|
| 61 | half_swap(a, --bounds[m]); |
---|
| 62 | ++m; |
---|
| 63 | } |
---|
| 64 | if(a != orig_a) { |
---|
| 65 | base.set(orig_key, a); |
---|
| 66 | data[a]=orig_key; |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | return a; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | public: |
---|
| 73 | |
---|
[361] | 74 | IterableMap(KeyIntMap &_base, Val d = N+1) : base(_base), def_val(d) { |
---|
[347] | 75 | memset(bounds, 0, sizeof(bounds)); |
---|
| 76 | // for(int i=0; i<N; ++i) { bounds[i]=0; } |
---|
| 77 | } |
---|
| 78 | |
---|
[361] | 79 | Val operator[](const KeyType& k) const { |
---|
[347] | 80 | return find(base[k]); |
---|
| 81 | } |
---|
| 82 | |
---|
[361] | 83 | void set(const KeyType& k, Val n) { |
---|
[362] | 84 | // FIXME: range check? |
---|
[347] | 85 | size_t a = base[k]; |
---|
[362] | 86 | if(a < bounds[N-1]) { |
---|
[347] | 87 | base.set(k, move(a, find(a), n)); |
---|
| 88 | } |
---|
[362] | 89 | else { |
---|
| 90 | insert(k, n); |
---|
| 91 | } |
---|
[347] | 92 | } |
---|
| 93 | |
---|
[361] | 94 | void insert(const KeyType& k, Val n) { |
---|
[362] | 95 | data.push_back(k); |
---|
| 96 | base.set(k, move(bounds[N-1]++, N-1, n)); |
---|
[347] | 97 | } |
---|
| 98 | |
---|
[361] | 99 | iterator begin(Val n) const { |
---|
[362] | 100 | return data.begin() + (n ? bounds[n-1] : 0); |
---|
[347] | 101 | } |
---|
| 102 | |
---|
[361] | 103 | iterator end(Val n) const { |
---|
[362] | 104 | return data.begin() + bounds[n]; |
---|
[347] | 105 | } |
---|
| 106 | |
---|
[361] | 107 | size_t size(Val n) const { |
---|
[362] | 108 | return bounds[n] - (n ? bounds[n-1] : 0); |
---|
[347] | 109 | } |
---|
| 110 | |
---|
| 111 | size_t size() const { |
---|
| 112 | // assert(bounds[N-1] == data.size()); |
---|
| 113 | return bounds[N-1]; |
---|
| 114 | } |
---|
| 115 | |
---|
[365] | 116 | |
---|
| 117 | /// For use as an iterator... |
---|
| 118 | KeyType& first(KeyType &k, Val n) { |
---|
| 119 | size_t i = (n ? bounds[n-1] : 0); |
---|
| 120 | if( i < bounds[n] ) { |
---|
| 121 | k = data[i]; |
---|
| 122 | } |
---|
| 123 | else { |
---|
| 124 | k = INVALID; |
---|
| 125 | } |
---|
| 126 | return k; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | /// For use as an iterator... |
---|
| 130 | KeyType& next(KeyType &k) { |
---|
| 131 | size_t i = base[k]; |
---|
| 132 | uint8_t n = find(i); |
---|
| 133 | ++i; |
---|
| 134 | if( i < bounds[n] ) { |
---|
| 135 | k = data[i]; |
---|
| 136 | } |
---|
| 137 | else { |
---|
| 138 | k = INVALID; |
---|
| 139 | } |
---|
| 140 | return k; |
---|
| 141 | } |
---|
| 142 | |
---|
[347] | 143 | }; |
---|
| 144 | |
---|
[361] | 145 | |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | template<typename KeyIntMap> |
---|
| 149 | class IterableBoolMap : public IterableMap<KeyIntMap, 2, bool> { |
---|
| 150 | typedef IterableMap<KeyIntMap, 2, bool> Parent; |
---|
| 151 | |
---|
| 152 | public: |
---|
| 153 | IterableBoolMap(KeyIntMap &_base, bool d = false) : Parent(_base, d) {} |
---|
| 154 | }; |
---|
| 155 | |
---|
[347] | 156 | } |
---|
| 157 | #endif |
---|