IterableMap with template ValueType. IterableBoolMap as a specialization.
Range checking warnings...
1.1 --- a/src/work/klao/iter_map.h Wed Apr 21 15:14:45 2004 +0000
1.2 +++ b/src/work/klao/iter_map.h Wed Apr 21 15:46:40 2004 +0000
1.3 @@ -16,12 +16,12 @@
1.4
1.5 /// \todo Decide whether we need all the range checkings!!!
1.6
1.7 - template<typename KeyIntMap, uint8_t N>
1.8 + template<typename KeyIntMap, uint8_t N, typename Val = uint8_t>
1.9 class IterableMap {
1.10 public:
1.11
1.12 typedef typename KeyIntMap::KeyType KeyType;
1.13 - typedef uint8_t ValueType;
1.14 + typedef Val ValueType;
1.15
1.16 typedef typename std::vector<KeyType>::const_iterator iterator;
1.17
1.18 @@ -29,11 +29,14 @@
1.19 KeyIntMap &base;
1.20 std::vector<KeyType> data;
1.21 size_t bounds[N];
1.22 + Val def_val;
1.23
1.24 - uint8_t find(size_t a) const {
1.25 - uint8_t n=0;
1.26 - for(; n<N && bounds[n]<=a; ++n);
1.27 - return n;
1.28 + Val find(size_t a) const {
1.29 + for(uint8_t n=0; n<N; ++n) {
1.30 + if(bounds[n] > a)
1.31 + return n;
1.32 + }
1.33 + return def_val;
1.34 }
1.35
1.36 void half_swap(size_t &a, size_t b) {
1.37 @@ -66,44 +69,45 @@
1.38
1.39 public:
1.40
1.41 - IterableMap(KeyIntMap &_base) : base(_base) {
1.42 + IterableMap(KeyIntMap &_base, Val d = N+1) : base(_base), def_val(d) {
1.43 memset(bounds, 0, sizeof(bounds));
1.44 // for(int i=0; i<N; ++i) { bounds[i]=0; }
1.45 }
1.46
1.47 - uint8_t operator[](const KeyType& k) const {
1.48 + Val operator[](const KeyType& k) const {
1.49 return find(base[k]);
1.50 }
1.51
1.52 - void set(const KeyType& k, uint8_t n) {
1.53 + void set(const KeyType& k, Val n) {
1.54 + // FIXME: n < N ???
1.55 size_t a = base[k];
1.56 - if(a < bounds[N-1]) {
1.57 + if(a < bounds[N-1] && n < N) {
1.58 base.set(k, move(a, find(a), n));
1.59 }
1.60 }
1.61
1.62 - void insert(const KeyType& k, uint8_t n) {
1.63 - if(n<N) {
1.64 + void insert(const KeyType& k, Val n) {
1.65 + if(n < N) {
1.66 data.push_back(k);
1.67 base.set(k, move(bounds[N-1]++, N-1, n));
1.68 }
1.69 }
1.70
1.71 - iterator begin(uint8_t n) const {
1.72 + iterator begin(Val n) const {
1.73 if(n < N)
1.74 return data.begin() + (n ? bounds[n-1] : 0);
1.75 else
1.76 return data.end();
1.77 }
1.78
1.79 - iterator end(uint8_t n) const {
1.80 + iterator end(Val n) const {
1.81 if(n < N)
1.82 return data.begin() + bounds[n];
1.83 else
1.84 return data.end();
1.85 }
1.86
1.87 - size_t size(uint8_t n) const {
1.88 + size_t size(Val n) const {
1.89 if(n < N)
1.90 return bounds[n] - (n ? bounds[n-1] : 0);
1.91 else
1.92 @@ -117,5 +121,16 @@
1.93
1.94 };
1.95
1.96 +
1.97 +
1.98 +
1.99 + template<typename KeyIntMap>
1.100 + class IterableBoolMap : public IterableMap<KeyIntMap, 2, bool> {
1.101 + typedef IterableMap<KeyIntMap, 2, bool> Parent;
1.102 +
1.103 + public:
1.104 + IterableBoolMap(KeyIntMap &_base, bool d = false) : Parent(_base, d) {}
1.105 + };
1.106 +
1.107 }
1.108 #endif
2.1 --- a/src/work/klao/iter_map_test.cc Wed Apr 21 15:14:45 2004 +0000
2.2 +++ b/src/work/klao/iter_map_test.cc Wed Apr 21 15:46:40 2004 +0000
2.3 @@ -10,14 +10,16 @@
2.4
2.5 typedef StdMap<int,int> BaseMap;
2.6 typedef IterableMap<BaseMap, N> TestMap;
2.7 +typedef IterableBoolMap<BaseMap> TestBoolMap;
2.8
2.9
2.10 -void print(TestMap const& m) {
2.11 +template<typename TM>
2.12 +void print(TM const& m, int N = 3) {
2.13 cout << "Size of the map: " << m.size() << endl;
2.14 for(int i=0; i<N; ++i) {
2.15 cout << " Class " << i << ". (size=" << m.size(i) << "): " << flush;
2.16 cout << " ";
2.17 - for(TestMap::iterator j = m.begin(i); j!=m.end(i); ++j) {
2.18 + for(typename TM::iterator j = m.begin(i); j!=m.end(i); ++j) {
2.19 cout << " " << *j;
2.20 }
2.21 cout << endl;
2.22 @@ -28,90 +30,182 @@
2.23
2.24 int main() {
2.25
2.26 - BaseMap base(344);
2.27 - TestMap test(base);
2.28 + {
2.29 + BaseMap base(344);
2.30 + TestMap test(base);
2.31
2.32
2.33 - print(test);
2.34 + print(test);
2.35
2.36 - cout << "Inserting 12 to class 2...\n";
2.37 - test.insert(12,2);
2.38 - print(test);
2.39 + cout << "Inserting 12 to class 2...\n";
2.40 + test.insert(12,2);
2.41 + print(test);
2.42
2.43
2.44 - cout << "Inserting 22 to class 2...\n";
2.45 - test.insert(22,2);
2.46 - print(test);
2.47 + cout << "Inserting 22 to class 2...\n";
2.48 + test.insert(22,2);
2.49 + print(test);
2.50
2.51 - cout << "Testing some map values:\n";
2.52 - cout << " 12: " << int(test[12]) << endl;
2.53 + cout << "Testing some map values:\n";
2.54 + cout << " 12: " << int(test[12]) << endl;
2.55
2.56 - cout << "Inserting 10 to class 0...\n";
2.57 - test.insert(10,0);
2.58 - print(test);
2.59 + cout << "Inserting 10 to class 0...\n";
2.60 + test.insert(10,0);
2.61 + print(test);
2.62
2.63 - cout << "Testing some map values:\n";
2.64 - cout << " 12: " << int(test[12]) << endl;
2.65 + cout << "Testing some map values:\n";
2.66 + cout << " 12: " << int(test[12]) << endl;
2.67
2.68 - cout << "Inserting 11 to class 1...\n";
2.69 - test.insert(11,1);
2.70 - print(test);
2.71 + cout << "Inserting 11 to class 1...\n";
2.72 + test.insert(11,1);
2.73 + print(test);
2.74
2.75 - cout << "Testing some map values:\n";
2.76 - cout << " 12: " << int(test[12]) << endl;
2.77 - cout << " 22: " << int(test[22]) << endl;
2.78 - cout << " 10: " << int(test[10]) << endl;
2.79 - cout << " 11: " << int(test[11]) << endl;
2.80 - cout << " 42: " << int(test[42]) << endl;
2.81 + cout << "Testing some map values:\n";
2.82 + cout << " 12: " << int(test[12]) << endl;
2.83 + cout << " 22: " << int(test[22]) << endl;
2.84 + cout << " 10: " << int(test[10]) << endl;
2.85 + cout << " 11: " << int(test[11]) << endl;
2.86 + cout << " 42: " << int(test[42]) << endl;
2.87
2.88 - cout << "Inserting 21 to class 1...\n";
2.89 - test.insert(21,1);
2.90 - print(test);
2.91 + cout << "Inserting 21 to class 1...\n";
2.92 + test.insert(21,1);
2.93 + print(test);
2.94
2.95 - cout << "Inserting 20 to class 1...\n";
2.96 - test.insert(20,0);
2.97 - print(test);
2.98 + cout << "Inserting 20 to class 1...\n";
2.99 + test.insert(20,0);
2.100 + print(test);
2.101
2.102 - cout << "Testing some map values:\n";
2.103 - cout << " 12: " << int(test[12]) << endl;
2.104 - cout << " 22: " << int(test[22]) << endl;
2.105 - cout << " 10: " << int(test[10]) << endl;
2.106 - cout << " 20: " << int(test[20]) << endl;
2.107 - cout << " 11: " << int(test[11]) << endl;
2.108 - cout << " 21: " << int(test[21]) << endl;
2.109 - cout << " 42: " << int(test[42]) << endl;
2.110 + cout << "Testing some map values:\n";
2.111 + cout << " 12: " << int(test[12]) << endl;
2.112 + cout << " 22: " << int(test[22]) << endl;
2.113 + cout << " 10: " << int(test[10]) << endl;
2.114 + cout << " 20: " << int(test[20]) << endl;
2.115 + cout << " 11: " << int(test[11]) << endl;
2.116 + cout << " 21: " << int(test[21]) << endl;
2.117 + cout << " 42: " << int(test[42]) << endl;
2.118
2.119 - cout << "Setting 20 to class 2...\n";
2.120 - test.set(20,2);
2.121 - print(test);
2.122 + cout << "Setting 20 to class 2...\n";
2.123 + test.set(20,2);
2.124 + print(test);
2.125
2.126 - cout << "Setting 10 to class 1...\n";
2.127 - test.set(10,1);
2.128 - print(test);
2.129 + cout << "Setting 10 to class 1...\n";
2.130 + test.set(10,1);
2.131 + print(test);
2.132
2.133 - cout << "Setting 11 to class 1...\n";
2.134 - test.set(11,1);
2.135 - print(test);
2.136 + cout << "Setting 11 to class 1...\n";
2.137 + test.set(11,1);
2.138 + print(test);
2.139
2.140 - cout << "Setting 12 to class 1...\n";
2.141 - test.set(12,1);
2.142 - print(test);
2.143 + cout << "Setting 12 to class 1...\n";
2.144 + test.set(12,1);
2.145 + print(test);
2.146
2.147 - cout << "Setting 21 to class 2...\n";
2.148 - test.set(21,2);
2.149 - print(test);
2.150 + cout << "Setting 21 to class 2...\n";
2.151 + test.set(21,2);
2.152 + print(test);
2.153
2.154 - cout << "Setting 22 to class 2...\n";
2.155 - test.set(22,2);
2.156 - print(test);
2.157 + cout << "Setting 22 to class 2...\n";
2.158 + test.set(22,2);
2.159 + print(test);
2.160
2.161 - cout << "Testing some map values:\n";
2.162 - cout << " 12: " << int(test[12]) << endl;
2.163 - cout << " 22: " << int(test[22]) << endl;
2.164 - cout << " 10: " << int(test[10]) << endl;
2.165 - cout << " 20: " << int(test[20]) << endl;
2.166 - cout << " 11: " << int(test[11]) << endl;
2.167 - cout << " 21: " << int(test[21]) << endl;
2.168 - cout << " 42: " << int(test[42]) << endl;
2.169 + cout << "Testing some map values:\n";
2.170 + cout << " 12: " << int(test[12]) << endl;
2.171 + cout << " 22: " << int(test[22]) << endl;
2.172 + cout << " 10: " << int(test[10]) << endl;
2.173 + cout << " 20: " << int(test[20]) << endl;
2.174 + cout << " 11: " << int(test[11]) << endl;
2.175 + cout << " 21: " << int(test[21]) << endl;
2.176 + cout << " 42: " << int(test[42]) << endl;
2.177 + }
2.178
2.179 + {
2.180 + cout << "Testing the IterableBoolMap...\n";
2.181 +
2.182 + BaseMap base(344);
2.183 + TestBoolMap test(base);
2.184 +
2.185 +
2.186 + print(test,2);
2.187 +
2.188 + cout << "Inserting 12 to class true...\n";
2.189 + test.insert(12,true);
2.190 + print(test,2);
2.191 +
2.192 +
2.193 + cout << "Inserting 22 to class true...\n";
2.194 + test.insert(22,true);
2.195 + print(test,2);
2.196 +
2.197 + cout << "Testing some map values:\n";
2.198 + cout << " 12: " << test[12] << endl;
2.199 +
2.200 + cout << "Inserting 10 to class false...\n";
2.201 + test.insert(10,false);
2.202 + print(test,2);
2.203 +
2.204 + cout << "Testing some map values:\n";
2.205 + cout << " 12: " << test[12] << endl;
2.206 +
2.207 + cout << "Inserting 11 to class false...\n";
2.208 + test.insert(11,false);
2.209 + print(test,2);
2.210 +
2.211 + cout << "Testing some map values:\n";
2.212 + cout << " 12: " << test[12] << endl;
2.213 + cout << " 22: " << test[22] << endl;
2.214 + cout << " 10: " << test[10] << endl;
2.215 + cout << " 11: " << test[11] << endl;
2.216 + cout << " 42: " << test[42] << endl;
2.217 +
2.218 + cout << "Inserting 21 to class 1...\n";
2.219 + test.insert(21,1);
2.220 + print(test,2);
2.221 +
2.222 + cout << "Inserting 20 to class 1...\n";
2.223 + test.insert(20,0);
2.224 + print(test,2);
2.225 +
2.226 + cout << "Testing some map values:\n";
2.227 + cout << " 12: " << test[12] << endl;
2.228 + cout << " 22: " << test[22] << endl;
2.229 + cout << " 10: " << test[10] << endl;
2.230 + cout << " 20: " << test[20] << endl;
2.231 + cout << " 11: " << test[11] << endl;
2.232 + cout << " 21: " << test[21] << endl;
2.233 + cout << " 42: " << test[42] << endl;
2.234 +
2.235 + cout << "Setting 20 to class 2...\n";
2.236 + test.set(20,2);
2.237 + print(test,2);
2.238 +
2.239 + cout << "Setting 10 to class 1...\n";
2.240 + test.set(10,1);
2.241 + print(test,2);
2.242 +
2.243 + cout << "Setting 11 to class 1...\n";
2.244 + test.set(11,1);
2.245 + print(test,2);
2.246 +
2.247 + cout << "Setting 12 to class 1...\n";
2.248 + test.set(12,1);
2.249 + print(test,2);
2.250 +
2.251 + cout << "Setting 21 to class 2...\n";
2.252 + test.set(21,2);
2.253 + print(test,2);
2.254 +
2.255 + cout << "Setting 22 to class 2...\n";
2.256 + test.set(22,2);
2.257 + print(test,2);
2.258 +
2.259 + cout << "Testing some map values:\n";
2.260 + cout << " 12: " << test[12] << endl;
2.261 + cout << " 22: " << test[22] << endl;
2.262 + cout << " 10: " << test[10] << endl;
2.263 + cout << " 20: " << test[20] << endl;
2.264 + cout << " 11: " << test[11] << endl;
2.265 + cout << " 21: " << test[21] << endl;
2.266 + cout << " 42: " << test[42] << endl;
2.267 +
2.268 + }
2.269 }