klao@347: // -*- c++ -*- // klao@347: klao@347: #ifndef HUGO_ITER_MAP klao@347: #define HUGO_ITER_MAP klao@347: klao@347: #include klao@347: #include klao@347: // for uint8_t klao@347: #include klao@347: // for memset klao@347: #include klao@347: klao@347: klao@347: namespace hugo { klao@347: klao@347: klao@347: /// \todo Decide whether we need all the range checkings!!! klao@347: klao@361: template klao@347: class IterableMap { klao@347: public: klao@347: klao@347: typedef typename KeyIntMap::KeyType KeyType; klao@361: typedef Val ValueType; klao@347: klao@347: typedef typename std::vector::const_iterator iterator; klao@347: klao@347: protected: klao@347: KeyIntMap &base; klao@347: std::vector data; klao@347: size_t bounds[N]; klao@361: Val def_val; klao@347: klao@361: Val find(size_t a) const { klao@361: for(uint8_t n=0; n a) klao@361: return n; klao@361: } klao@361: return def_val; klao@347: } klao@347: klao@347: void half_swap(size_t &a, size_t b) { klao@347: if(a != b) { klao@347: base.set(data[b],a); klao@347: data[a] = data[b]; klao@347: a = b; klao@347: } klao@347: } klao@347: klao@347: size_t move(size_t a, uint8_t m, uint8_t n) { klao@347: if(m != n) { klao@347: size_t orig_a = a; klao@347: KeyType orig_key = data[a]; klao@347: while(m > n) { klao@347: --m; klao@347: half_swap(a, bounds[m]++); klao@347: } klao@362: // FIXME: range check ide? klao@347: while(m < n) { klao@347: half_swap(a, --bounds[m]); klao@347: ++m; klao@347: } klao@347: if(a != orig_a) { klao@347: base.set(orig_key, a); klao@347: data[a]=orig_key; klao@347: } klao@347: } klao@347: return a; klao@347: } klao@347: klao@347: public: klao@347: klao@361: IterableMap(KeyIntMap &_base, Val d = N+1) : base(_base), def_val(d) { klao@347: memset(bounds, 0, sizeof(bounds)); klao@347: // for(int i=0; i klao@361: class IterableBoolMap : public IterableMap { klao@361: typedef IterableMap Parent; klao@361: klao@361: public: klao@361: IterableBoolMap(KeyIntMap &_base, bool d = false) : Parent(_base, d) {} klao@361: }; klao@361: klao@347: } klao@347: #endif