[Lemon-commits] [lemon_svn] klao: r489 - hugo/trunk/src/work/klao
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:39:42 CET 2006
Author: klao
Date: Wed Apr 21 17:46:40 2004
New Revision: 489
Modified:
hugo/trunk/src/work/klao/iter_map.h
hugo/trunk/src/work/klao/iter_map_test.cc
Log:
IterableMap with template ValueType. IterableBoolMap as a specialization.
Range checking warnings...
Modified: hugo/trunk/src/work/klao/iter_map.h
==============================================================================
--- hugo/trunk/src/work/klao/iter_map.h (original)
+++ hugo/trunk/src/work/klao/iter_map.h Wed Apr 21 17:46:40 2004
@@ -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
Modified: hugo/trunk/src/work/klao/iter_map_test.cc
==============================================================================
--- hugo/trunk/src/work/klao/iter_map_test.cc (original)
+++ hugo/trunk/src/work/klao/iter_map_test.cc Wed Apr 21 17:46:40 2004
@@ -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);
+
+ 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 << "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 << "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 << "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 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 << "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 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 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 << "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);
- 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 << "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 << "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 << "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 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 << "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 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 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 << "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;
+ 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;
+
+ }
}
More information about the Lemon-commits
mailing list