src/work/klao/iter_map_test.cc
author marci
Tue, 11 May 2004 12:09:06 +0000
changeset 605 b3c57602c516
parent 366 be6fe0ea99b5
child 618 e944d741f472
permissions -rw-r--r--
docs
klao@347
     1
#include <iter_map.h>
klao@347
     2
#include <maps.h>
klao@347
     3
klao@347
     4
#include <iostream>
klao@347
     5
klao@347
     6
using namespace hugo;
klao@347
     7
using namespace std;
klao@347
     8
klao@347
     9
const int N = 3;
klao@347
    10
klao@347
    11
typedef StdMap<int,int> BaseMap;
klao@347
    12
typedef IterableMap<BaseMap, N> TestMap;
klao@361
    13
typedef IterableBoolMap<BaseMap> TestBoolMap;
klao@347
    14
klao@347
    15
klao@361
    16
template<typename TM>
klao@361
    17
void print(TM const& m, int N = 3) {
klao@347
    18
  cout << "Size of the map: " << m.size() << endl;
klao@347
    19
  for(int i=0; i<N; ++i) {
klao@347
    20
    cout << "  Class " << i << ". (size=" << m.size(i) << "): " << flush;
klao@347
    21
    cout << "    ";
klao@361
    22
    for(typename TM::iterator j = m.begin(i); j!=m.end(i); ++j) {
klao@347
    23
      cout << " " << *j;
klao@347
    24
    }
klao@347
    25
    cout << endl;
klao@347
    26
  }
klao@347
    27
}
klao@347
    28
klao@366
    29
struct Int {
klao@366
    30
  int a;
klao@366
    31
klao@366
    32
  Int(int b = 5) : a(b) {}
klao@366
    33
  Int(Invalid) : a(-1) {}
klao@366
    34
klao@366
    35
  operator int() const { return a; }
klao@366
    36
klao@366
    37
  bool valid() { return a != -1; }
klao@366
    38
};
klao@366
    39
klao@366
    40
typedef StdMap<Int,int> BaseMap2;
klao@366
    41
typedef IterableBoolMap<BaseMap2> TestBoolMap2;
klao@366
    42
klao@366
    43
klao@347
    44
int main() {
klao@347
    45
klao@361
    46
  {
klao@361
    47
    BaseMap base(344);
klao@361
    48
    TestMap test(base);
klao@347
    49
klao@347
    50
klao@361
    51
    print(test);
klao@347
    52
klao@361
    53
    cout << "Inserting 12 to class 2...\n";
klao@361
    54
    test.insert(12,2);
klao@361
    55
    print(test);
klao@347
    56
klao@347
    57
klao@361
    58
    cout << "Inserting 22 to class 2...\n";
klao@361
    59
    test.insert(22,2);
klao@361
    60
    print(test);
klao@347
    61
klao@361
    62
    cout << "Testing some map values:\n";
klao@361
    63
    cout << " 12: " << int(test[12]) << endl;
klao@347
    64
klao@361
    65
    cout << "Inserting 10 to class 0...\n";
klao@361
    66
    test.insert(10,0);
klao@361
    67
    print(test);
klao@347
    68
klao@361
    69
    cout << "Testing some map values:\n";
klao@361
    70
    cout << " 12: " << int(test[12]) << endl;
klao@347
    71
klao@361
    72
    cout << "Inserting 11 to class 1...\n";
klao@361
    73
    test.insert(11,1);
klao@361
    74
    print(test);
klao@347
    75
klao@361
    76
    cout << "Testing some map values:\n";
klao@361
    77
    cout << " 12: " << int(test[12]) << endl;
klao@361
    78
    cout << " 22: " << int(test[22]) << endl;
klao@361
    79
    cout << " 10: " << int(test[10]) << endl;
klao@361
    80
    cout << " 11: " << int(test[11]) << endl;
klao@361
    81
    cout << " 42: " << int(test[42]) << endl;
klao@347
    82
klao@361
    83
    cout << "Inserting 21 to class 1...\n";
klao@361
    84
    test.insert(21,1);
klao@361
    85
    print(test);
klao@347
    86
klao@361
    87
    cout << "Inserting 20 to class 1...\n";
klao@361
    88
    test.insert(20,0);
klao@361
    89
    print(test);
klao@347
    90
klao@361
    91
    cout << "Testing some map values:\n";
klao@361
    92
    cout << " 12: " << int(test[12]) << endl;
klao@361
    93
    cout << " 22: " << int(test[22]) << endl;
klao@361
    94
    cout << " 10: " << int(test[10]) << endl;
klao@361
    95
    cout << " 20: " << int(test[20]) << endl;
klao@361
    96
    cout << " 11: " << int(test[11]) << endl;
klao@361
    97
    cout << " 21: " << int(test[21]) << endl;
klao@361
    98
    cout << " 42: " << int(test[42]) << endl;
klao@347
    99
klao@361
   100
    cout << "Setting 20 to class 2...\n";
klao@361
   101
    test.set(20,2);
klao@361
   102
    print(test);
klao@347
   103
  
klao@361
   104
    cout << "Setting 10 to class 1...\n";
klao@361
   105
    test.set(10,1);
klao@361
   106
    print(test);
klao@347
   107
  
klao@361
   108
    cout << "Setting 11 to class 1...\n";
klao@361
   109
    test.set(11,1);
klao@361
   110
    print(test);
klao@347
   111
  
klao@361
   112
    cout << "Setting 12 to class 1...\n";
klao@361
   113
    test.set(12,1);
klao@361
   114
    print(test);
klao@347
   115
  
klao@361
   116
    cout << "Setting 21 to class 2...\n";
klao@361
   117
    test.set(21,2);
klao@361
   118
    print(test);
klao@347
   119
  
klao@361
   120
    cout << "Setting 22 to class 2...\n";
klao@361
   121
    test.set(22,2);
klao@361
   122
    print(test);
klao@347
   123
  
klao@361
   124
    cout << "Testing some map values:\n";
klao@361
   125
    cout << " 12: " << int(test[12]) << endl;
klao@361
   126
    cout << " 22: " << int(test[22]) << endl;
klao@361
   127
    cout << " 10: " << int(test[10]) << endl;
klao@361
   128
    cout << " 20: " << int(test[20]) << endl;
klao@361
   129
    cout << " 11: " << int(test[11]) << endl;
klao@361
   130
    cout << " 21: " << int(test[21]) << endl;
klao@361
   131
    cout << " 42: " << int(test[42]) << endl;
klao@361
   132
  }
klao@347
   133
klao@361
   134
  {
klao@362
   135
    cout << "\n\n\nTesting the IterableBoolMap...\n";
klao@361
   136
klao@361
   137
    BaseMap base(344);
klao@362
   138
    TestBoolMap test(base,true);
klao@361
   139
klao@361
   140
klao@361
   141
    print(test,2);
klao@361
   142
klao@361
   143
    cout << "Inserting 12 to class true...\n";
klao@361
   144
    test.insert(12,true);
klao@361
   145
    print(test,2);
klao@361
   146
klao@361
   147
klao@361
   148
    cout << "Inserting 22 to class true...\n";
klao@361
   149
    test.insert(22,true);
klao@361
   150
    print(test,2);
klao@361
   151
klao@361
   152
    cout << "Testing some map values:\n";
klao@361
   153
    cout << " 12: " << test[12] << endl;
klao@361
   154
klao@361
   155
    cout << "Inserting 10 to class false...\n";
klao@361
   156
    test.insert(10,false);
klao@361
   157
    print(test,2);
klao@361
   158
klao@361
   159
    cout << "Testing some map values:\n";
klao@361
   160
    cout << " 12: " << test[12] << endl;
klao@361
   161
klao@361
   162
    cout << "Inserting 11 to class false...\n";
klao@361
   163
    test.insert(11,false);
klao@361
   164
    print(test,2);
klao@361
   165
klao@361
   166
    cout << "Testing some map values:\n";
klao@361
   167
    cout << " 12: " << test[12] << endl;
klao@361
   168
    cout << " 22: " << test[22] << endl;
klao@361
   169
    cout << " 10: " << test[10] << endl;
klao@361
   170
    cout << " 11: " << test[11] << endl;
klao@361
   171
    cout << " 42: " << test[42] << endl;
klao@361
   172
klao@362
   173
    cout << "Setting 10 to class true...\n";
klao@362
   174
    test.set(10,true);
klao@361
   175
    print(test,2);
klao@361
   176
  
klao@362
   177
    cout << "Setting 11 to class true...\n";
klao@361
   178
    test.set(11,1);
klao@361
   179
    print(test,2);
klao@361
   180
  
klao@362
   181
    cout << "Setting 12 to class false...\n";
klao@362
   182
    test.set(12,false);
klao@361
   183
    print(test,2);
klao@361
   184
  
klao@362
   185
    cout << "Setting 22 to class false...\n";
klao@362
   186
    test.set(22,false);
klao@361
   187
    print(test,2);
klao@361
   188
  
klao@361
   189
    cout << "Testing some map values:\n";
klao@361
   190
    cout << " 12: " << test[12] << endl;
klao@361
   191
    cout << " 22: " << test[22] << endl;
klao@361
   192
    cout << " 10: " << test[10] << endl;
klao@361
   193
    cout << " 11: " << test[11] << endl;
klao@361
   194
    cout << " 42: " << test[42] << endl;
klao@361
   195
klao@361
   196
  }
klao@366
   197
klao@366
   198
  {
klao@366
   199
    cout << "\n\n\nTest a masikfele iteralasra:\n";
klao@366
   200
klao@366
   201
    BaseMap2 base(344);
klao@366
   202
    TestBoolMap2 test(base,false);
klao@366
   203
klao@366
   204
    cout << "Inserting 12 to class true...\n";
klao@366
   205
    test.insert(12,true);
klao@366
   206
    print(test,2);
klao@366
   207
klao@366
   208
    cout << "Inserting 22 to class true...\n";
klao@366
   209
    test.insert(22,true);
klao@366
   210
    print(test,2);
klao@366
   211
klao@366
   212
    cout << "Inserting 10 to class false...\n";
klao@366
   213
    test.insert(10,false);
klao@366
   214
    print(test,2);
klao@366
   215
klao@366
   216
    cout << "Testing some map values:\n";
klao@366
   217
    cout << " 12: " << test[12] << endl;
klao@366
   218
    cout << " 22: " << test[22] << endl;
klao@366
   219
    cout << " 10: " << test[10] << endl;
klao@366
   220
    cout << " 42: " << test[42] << endl;
klao@366
   221
klao@366
   222
    cout << "The elements of the \"true\" class: ";
klao@366
   223
    Int a;
klao@366
   224
    for(test.first(a, true); a.valid(); test.next(a)) {
klao@366
   225
      cout << " " << a;
klao@366
   226
    }
klao@366
   227
    cout << endl;
klao@367
   228
klao@367
   229
    cout << "Removing 10 from the map...\n";
klao@367
   230
    test.remove(10);
klao@367
   231
    print(test,2);
klao@366
   232
  }
klao@347
   233
}