# HG changeset patch # User klao # Date 1082562400 0 # Node ID ab0899df30d2968c4376cef296975dc1bbc10f0f # Parent 91fba31268d663ef9c657fc534277e149ae52ec3 IterableMap with template ValueType. IterableBoolMap as a specialization. Range checking warnings... diff -r 91fba31268d6 -r ab0899df30d2 src/work/klao/iter_map.h --- a/src/work/klao/iter_map.h Wed Apr 21 15:14:45 2004 +0000 +++ b/src/work/klao/iter_map.h Wed Apr 21 15:46:40 2004 +0000 @@ -16,12 +16,12 @@ /// \todo Decide whether we need all the range checkings!!! - template<typename KeyIntMap, uint8_t N> + template<typename KeyIntMap, uint8_t N, typename Val = uint8_t> class IterableMap { public: typedef typename KeyIntMap::KeyType KeyType; - typedef uint8_t ValueType; + typedef Val ValueType; typedef typename std::vector<KeyType>::const_iterator iterator; @@ -29,11 +29,14 @@ KeyIntMap &base; std::vector<KeyType> data; size_t bounds[N]; + Val def_val; - uint8_t find(size_t a) const { - uint8_t n=0; - for(; n<N && bounds[n]<=a; ++n); - return n; + Val find(size_t a) const { + for(uint8_t n=0; n<N; ++n) { + if(bounds[n] > a) + return n; + } + return def_val; } void half_swap(size_t &a, size_t b) { @@ -66,44 +69,45 @@ public: - IterableMap(KeyIntMap &_base) : base(_base) { + IterableMap(KeyIntMap &_base, Val d = N+1) : base(_base), def_val(d) { memset(bounds, 0, sizeof(bounds)); // for(int i=0; i<N; ++i) { bounds[i]=0; } } - uint8_t operator[](const KeyType& k) const { + Val operator[](const KeyType& k) const { return find(base[k]); } - void set(const KeyType& k, uint8_t n) { + void set(const KeyType& k, Val n) { + // FIXME: n < N ??? size_t a = base[k]; - if(a < bounds[N-1]) { + if(a < bounds[N-1] && n < N) { base.set(k, move(a, find(a), n)); } } - void insert(const KeyType& k, uint8_t n) { - if(n<N) { + void insert(const KeyType& k, Val n) { + if(n < N) { data.push_back(k); base.set(k, move(bounds[N-1]++, N-1, n)); } } - iterator begin(uint8_t n) const { + iterator begin(Val n) const { if(n < N) return data.begin() + (n ? bounds[n-1] : 0); else return data.end(); } - iterator end(uint8_t n) const { + iterator end(Val n) const { if(n < N) return data.begin() + bounds[n]; else return data.end(); } - size_t size(uint8_t n) const { + size_t size(Val n) const { if(n < N) return bounds[n] - (n ? bounds[n-1] : 0); else @@ -117,5 +121,16 @@ }; + + + + template<typename KeyIntMap> + class IterableBoolMap : public IterableMap<KeyIntMap, 2, bool> { + typedef IterableMap<KeyIntMap, 2, bool> Parent; + + public: + IterableBoolMap(KeyIntMap &_base, bool d = false) : Parent(_base, d) {} + }; + } #endif diff -r 91fba31268d6 -r ab0899df30d2 src/work/klao/iter_map_test.cc --- a/src/work/klao/iter_map_test.cc Wed Apr 21 15:14:45 2004 +0000 +++ b/src/work/klao/iter_map_test.cc Wed Apr 21 15:46:40 2004 +0000 @@ -10,14 +10,16 @@ typedef StdMap<int,int> BaseMap; typedef IterableMap<BaseMap, N> TestMap; +typedef IterableBoolMap<BaseMap> TestBoolMap; -void print(TestMap const& m) { +template<typename TM> +void print(TM const& m, int N = 3) { cout << "Size of the map: " << m.size() << endl; for(int i=0; i<N; ++i) { cout << " Class " << i << ". (size=" << m.size(i) << "): " << flush; cout << " "; - for(TestMap::iterator j = m.begin(i); j!=m.end(i); ++j) { + for(typename TM::iterator j = m.begin(i); j!=m.end(i); ++j) { cout << " " << *j; } cout << endl; @@ -28,90 +30,182 @@ int main() { - BaseMap base(344); - TestMap test(base); + { + BaseMap base(344); + TestMap test(base); - print(test); + print(test); - cout << "Inserting 12 to class 2...\n"; - test.insert(12,2); - print(test); + cout << "Inserting 12 to class 2...\n"; + test.insert(12,2); + print(test); - cout << "Inserting 22 to class 2...\n"; - test.insert(22,2); - print(test); + cout << "Inserting 22 to class 2...\n"; + test.insert(22,2); + print(test); - cout << "Testing some map values:\n"; - cout << " 12: " << int(test[12]) << endl; + cout << "Testing some map values:\n"; + cout << " 12: " << int(test[12]) << endl; - cout << "Inserting 10 to class 0...\n"; - test.insert(10,0); - print(test); + cout << "Inserting 10 to class 0...\n"; + test.insert(10,0); + print(test); - cout << "Testing some map values:\n"; - cout << " 12: " << int(test[12]) << endl; + cout << "Testing some map values:\n"; + cout << " 12: " << int(test[12]) << endl; - cout << "Inserting 11 to class 1...\n"; - test.insert(11,1); - print(test); + cout << "Inserting 11 to class 1...\n"; + test.insert(11,1); + print(test); - cout << "Testing some map values:\n"; - cout << " 12: " << int(test[12]) << endl; - cout << " 22: " << int(test[22]) << endl; - cout << " 10: " << int(test[10]) << endl; - cout << " 11: " << int(test[11]) << endl; - cout << " 42: " << int(test[42]) << endl; + cout << "Testing some map values:\n"; + cout << " 12: " << int(test[12]) << endl; + cout << " 22: " << int(test[22]) << endl; + cout << " 10: " << int(test[10]) << endl; + cout << " 11: " << int(test[11]) << endl; + cout << " 42: " << int(test[42]) << endl; - cout << "Inserting 21 to class 1...\n"; - test.insert(21,1); - print(test); + cout << "Inserting 21 to class 1...\n"; + test.insert(21,1); + print(test); - cout << "Inserting 20 to class 1...\n"; - test.insert(20,0); - print(test); + cout << "Inserting 20 to class 1...\n"; + test.insert(20,0); + print(test); - cout << "Testing some map values:\n"; - cout << " 12: " << int(test[12]) << endl; - cout << " 22: " << int(test[22]) << endl; - cout << " 10: " << int(test[10]) << endl; - cout << " 20: " << int(test[20]) << endl; - cout << " 11: " << int(test[11]) << endl; - cout << " 21: " << int(test[21]) << endl; - cout << " 42: " << int(test[42]) << endl; + cout << "Testing some map values:\n"; + cout << " 12: " << int(test[12]) << endl; + cout << " 22: " << int(test[22]) << endl; + cout << " 10: " << int(test[10]) << endl; + cout << " 20: " << int(test[20]) << endl; + cout << " 11: " << int(test[11]) << endl; + cout << " 21: " << int(test[21]) << endl; + cout << " 42: " << int(test[42]) << endl; - cout << "Setting 20 to class 2...\n"; - test.set(20,2); - print(test); + cout << "Setting 20 to class 2...\n"; + test.set(20,2); + print(test); - cout << "Setting 10 to class 1...\n"; - test.set(10,1); - print(test); + cout << "Setting 10 to class 1...\n"; + test.set(10,1); + print(test); - cout << "Setting 11 to class 1...\n"; - test.set(11,1); - print(test); + cout << "Setting 11 to class 1...\n"; + test.set(11,1); + print(test); - cout << "Setting 12 to class 1...\n"; - test.set(12,1); - print(test); + cout << "Setting 12 to class 1...\n"; + test.set(12,1); + print(test); - cout << "Setting 21 to class 2...\n"; - test.set(21,2); - print(test); + cout << "Setting 21 to class 2...\n"; + test.set(21,2); + print(test); - cout << "Setting 22 to class 2...\n"; - test.set(22,2); - print(test); + cout << "Setting 22 to class 2...\n"; + test.set(22,2); + print(test); - cout << "Testing some map values:\n"; - cout << " 12: " << int(test[12]) << endl; - cout << " 22: " << int(test[22]) << endl; - cout << " 10: " << int(test[10]) << endl; - cout << " 20: " << int(test[20]) << endl; - cout << " 11: " << int(test[11]) << endl; - cout << " 21: " << int(test[21]) << endl; - cout << " 42: " << int(test[42]) << endl; + cout << "Testing some map values:\n"; + cout << " 12: " << int(test[12]) << endl; + cout << " 22: " << int(test[22]) << endl; + cout << " 10: " << int(test[10]) << endl; + cout << " 20: " << int(test[20]) << endl; + cout << " 11: " << int(test[11]) << endl; + cout << " 21: " << int(test[21]) << endl; + cout << " 42: " << int(test[42]) << endl; + } + { + cout << "Testing the IterableBoolMap...\n"; + + BaseMap base(344); + TestBoolMap test(base); + + + print(test,2); + + cout << "Inserting 12 to class true...\n"; + test.insert(12,true); + print(test,2); + + + cout << "Inserting 22 to class true...\n"; + test.insert(22,true); + print(test,2); + + cout << "Testing some map values:\n"; + cout << " 12: " << test[12] << endl; + + cout << "Inserting 10 to class false...\n"; + test.insert(10,false); + print(test,2); + + cout << "Testing some map values:\n"; + cout << " 12: " << test[12] << endl; + + cout << "Inserting 11 to class false...\n"; + test.insert(11,false); + print(test,2); + + cout << "Testing some map values:\n"; + cout << " 12: " << test[12] << endl; + cout << " 22: " << test[22] << endl; + cout << " 10: " << test[10] << endl; + cout << " 11: " << test[11] << endl; + cout << " 42: " << test[42] << endl; + + cout << "Inserting 21 to class 1...\n"; + test.insert(21,1); + print(test,2); + + cout << "Inserting 20 to class 1...\n"; + test.insert(20,0); + print(test,2); + + cout << "Testing some map values:\n"; + cout << " 12: " << test[12] << endl; + cout << " 22: " << test[22] << endl; + cout << " 10: " << test[10] << endl; + cout << " 20: " << test[20] << endl; + cout << " 11: " << test[11] << endl; + cout << " 21: " << test[21] << endl; + cout << " 42: " << test[42] << endl; + + cout << "Setting 20 to class 2...\n"; + test.set(20,2); + print(test,2); + + cout << "Setting 10 to class 1...\n"; + test.set(10,1); + print(test,2); + + cout << "Setting 11 to class 1...\n"; + test.set(11,1); + print(test,2); + + cout << "Setting 12 to class 1...\n"; + test.set(12,1); + print(test,2); + + cout << "Setting 21 to class 2...\n"; + test.set(21,2); + print(test,2); + + cout << "Setting 22 to class 2...\n"; + test.set(22,2); + print(test,2); + + cout << "Testing some map values:\n"; + cout << " 12: " << test[12] << endl; + cout << " 22: " << test[22] << endl; + cout << " 10: " << test[10] << endl; + cout << " 20: " << test[20] << endl; + cout << " 11: " << test[11] << endl; + cout << " 21: " << test[21] << endl; + cout << " 42: " << test[42] << endl; + + } }