18 /// \todo Decide whether we need all the range checkings!!!
20 template<typename KeyIntMap, uint8_t N, typename Val = uint8_t>
24 typedef typename KeyIntMap::KeyType KeyType;
25 typedef Val ValueType;
27 typedef typename std::vector<KeyType>::const_iterator iterator;
31 std::vector<KeyType> data;
35 Val find(size_t a) const {
36 for(uint8_t n=0; n<N; ++n) {
43 void half_swap(size_t &a, size_t b) {
51 size_t move(size_t a, uint8_t m, uint8_t n) {
54 KeyType orig_key = data[a];
57 half_swap(a, bounds[m]++);
59 // FIXME: range check ide?
61 half_swap(a, --bounds[m]);
65 base.set(orig_key, a);
74 IterableMap(KeyIntMap &_base, Val d = N+1) : base(_base), def_val(d) {
75 memset(bounds, 0, sizeof(bounds));
76 // for(int i=0; i<N; ++i) { bounds[i]=0; }
79 Val operator[](const KeyType& k) const {
83 void set(const KeyType& k, Val n) {
84 // FIXME: range check?
87 base.set(k, move(a, find(a), n));
94 void insert(const KeyType& k, Val n) {
96 base.set(k, move(bounds[N-1]++, N-1, n));
99 iterator begin(Val n) const {
100 return data.begin() + (n ? bounds[n-1] : 0);
103 iterator end(Val n) const {
104 return data.begin() + bounds[n];
107 size_t size(Val n) const {
108 return bounds[n] - (n ? bounds[n-1] : 0);
111 size_t size() const {
112 // assert(bounds[N-1] == data.size());
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] ) {
129 /// For use as an iterator...
130 KeyType& next(KeyType &k) {
134 if( i < bounds[n] ) {
148 template<typename KeyIntMap>
149 class IterableBoolMap : public IterableMap<KeyIntMap, 2, bool> {
150 typedef IterableMap<KeyIntMap, 2, bool> Parent;
153 IterableBoolMap(KeyIntMap &_base, bool d = false) : Parent(_base, d) {}