src/work/johanna/unionfind_test.cc
author marci
Thu, 29 Apr 2004 15:58:34 +0000
changeset 472 052af4060f3e
parent 394 3a34c5626e52
child 481 54d8feda437b
permissions -rw-r--r--
preflow, maxflow
beckerjc@394
     1
#include <iostream>
beckerjc@394
     2
beckerjc@394
     3
#include <maps.h>
beckerjc@394
     4
#include <unionfind.h>
beckerjc@394
     5
beckerjc@394
     6
using namespace hugo;
beckerjc@394
     7
using namespace std;
beckerjc@394
     8
beckerjc@394
     9
bool passed = true;
beckerjc@394
    10
beckerjc@394
    11
void check(bool rc) {
beckerjc@394
    12
  passed = passed && rc;
beckerjc@394
    13
  if(!rc) {
beckerjc@394
    14
    cout << "Test failed!" << endl;
beckerjc@394
    15
  }
beckerjc@394
    16
}
beckerjc@394
    17
beckerjc@394
    18
template <typename T>
beckerjc@394
    19
class BaseMap : public StdMap<int,T> {};
beckerjc@394
    20
beckerjc@394
    21
typedef UnionFindEnum<int, BaseMap> UFE;
beckerjc@394
    22
beckerjc@394
    23
void print(UFE const &ufe) {
beckerjc@394
    24
  UFE::ClassIt cit;
beckerjc@394
    25
  cout << "printing the classes of the structure:" << endl;
beckerjc@394
    26
  int i = 1;
beckerjc@394
    27
  for (ufe.first(cit); ufe.valid(cit); ufe.next(cit)) {
beckerjc@394
    28
    cout << "  " << i << " (" << cit << "):" << flush;
beckerjc@394
    29
    UFE::ItemIt iit;
beckerjc@394
    30
    for (ufe.first(iit, cit); ufe.valid(iit); ufe.next(iit)) {
beckerjc@394
    31
      cout << " " << iit << flush;
beckerjc@394
    32
    }
beckerjc@394
    33
    cout << endl;
beckerjc@394
    34
    i++;
beckerjc@394
    35
  }
beckerjc@394
    36
  cout << "done" << endl;
beckerjc@394
    37
}
beckerjc@394
    38
beckerjc@394
    39
beckerjc@394
    40
int main() {
beckerjc@394
    41
  UFE::MapType base;
beckerjc@394
    42
  UFE U(base);
beckerjc@394
    43
beckerjc@394
    44
  print(U);
beckerjc@394
    45
beckerjc@394
    46
  cout << "Inserting 1..." << endl;
beckerjc@394
    47
  U.insert(1);
beckerjc@394
    48
  print(U);
beckerjc@394
    49
  
beckerjc@394
    50
  cout << "Inserting 2..." << endl;
beckerjc@394
    51
  U.insert(2);
beckerjc@394
    52
  print(U);
beckerjc@394
    53
  
beckerjc@394
    54
  cout << "Joining 1 and 2..." << endl;
beckerjc@394
    55
  check(U.join(1,2));
beckerjc@394
    56
  print(U);
beckerjc@394
    57
beckerjc@394
    58
  cout << "Inserting 3, 4, 5, 6, 7..." << endl;
beckerjc@394
    59
  U.insert(3);
beckerjc@394
    60
  U.insert(4);
beckerjc@394
    61
  U.insert(5);
beckerjc@394
    62
  U.insert(6);
beckerjc@394
    63
  U.insert(7);
beckerjc@394
    64
  print (U);
beckerjc@394
    65
beckerjc@394
    66
  cout << "Joining 1 - 4, 2 - 4 and 3 - 5 ..." << endl;
beckerjc@394
    67
  check(U.join(1,4));
beckerjc@394
    68
  check(!U.join(2,4));
beckerjc@394
    69
  check(U.join(3,5));
beckerjc@394
    70
  print(U);
beckerjc@394
    71
beckerjc@394
    72
  cout << "size of the class of 4: " << U.size(4) << endl;
beckerjc@394
    73
  check(U.size(4) == 3);
beckerjc@394
    74
  cout << "size of the class of 5: " << U.size(5) << endl;
beckerjc@394
    75
  check(U.size(5) == 2);
beckerjc@394
    76
  cout << "size of the class of 6: " << U.size(6) << endl;
beckerjc@394
    77
  check(U.size(6) == 1);
beckerjc@462
    78
  cout << "size of the class of 2: " << U.size(2) << endl;
beckerjc@462
    79
  check(U.size(2) == 3);
beckerjc@394
    80
beckerjc@394
    81
  cout <<"erase 1: " << endl;
beckerjc@394
    82
  U.erase(1);
beckerjc@394
    83
  print(U);
beckerjc@394
    84
beckerjc@462
    85
  cout << "size of the class of 4: " << U.size(4) << endl;
beckerjc@462
    86
  check(U.size(4) == 2);
beckerjc@462
    87
  cout << "size of the class of 2: " << U.size(2) << endl;
beckerjc@462
    88
  check(U.size(2) == 2);
beckerjc@462
    89
beckerjc@462
    90
beckerjc@394
    91
  cout <<"erase 1: " << endl;
beckerjc@394
    92
  U.erase(1);
beckerjc@394
    93
  print(U);
beckerjc@394
    94
beckerjc@394
    95
  cout <<"erase 6: " << endl;
beckerjc@394
    96
  U.erase(6);
beckerjc@394
    97
  print(U);
beckerjc@394
    98
beckerjc@394
    99
  cout << "split the class of 5: " << endl;
beckerjc@394
   100
  U.split(5);
beckerjc@394
   101
  print(U);
beckerjc@394
   102
beckerjc@462
   103
beckerjc@462
   104
  cout << "size of the class of 4: " << U.size(4) << endl;
beckerjc@462
   105
  check(U.size(4) == 2);
beckerjc@462
   106
  cout << "size of the class of 3: " << U.size(3) << endl;
beckerjc@462
   107
  check(U.size(3) == 1);
beckerjc@462
   108
  cout << "size of the class of 2: " << U.size(2) << endl;
beckerjc@462
   109
  check(U.size(2) == 2);
beckerjc@462
   110
beckerjc@462
   111
beckerjc@394
   112
  cout << "Joining 3 - 4 and 2 - 4 ..." << endl;
beckerjc@394
   113
  check(U.join(3,4));
beckerjc@394
   114
  check(!U.join(2,4));
beckerjc@394
   115
  print(U);
beckerjc@394
   116
beckerjc@462
   117
beckerjc@462
   118
  cout << "size of the class of 4: " << U.size(4) << endl;
beckerjc@462
   119
  check(U.size(4) == 3);
beckerjc@462
   120
  cout << "size of the class of 3: " << U.size(3) << endl;
beckerjc@462
   121
  check(U.size(3) == 3);
beckerjc@462
   122
  cout << "size of the class of 2: " << U.size(2) << endl;
beckerjc@462
   123
  check(U.size(2) == 3);
beckerjc@462
   124
beckerjc@462
   125
  cout << "makeRep(4)" << endl;
beckerjc@462
   126
  U.makeRep(4);
beckerjc@462
   127
  print(U);
beckerjc@462
   128
  cout << "makeRep(3)" << endl;
beckerjc@462
   129
  U.makeRep(3);
beckerjc@462
   130
  print(U);
beckerjc@462
   131
  cout << "makeRep(2)" << endl;
beckerjc@462
   132
  U.makeRep(2);
beckerjc@462
   133
  print(U);
beckerjc@462
   134
beckerjc@462
   135
  cout << "size of the class of 4: " << U.size(4) << endl;
beckerjc@462
   136
  check(U.size(4) == 3);
beckerjc@462
   137
  cout << "size of the class of 3: " << U.size(3) << endl;
beckerjc@462
   138
  check(U.size(3) == 3);
beckerjc@462
   139
  cout << "size of the class of 2: " << U.size(2) << endl;
beckerjc@462
   140
  check(U.size(2) == 3);
beckerjc@462
   141
beckerjc@462
   142
beckerjc@394
   143
  cout << "eraseClass 4 ..." << endl;
beckerjc@394
   144
  U.eraseClass(4);
beckerjc@394
   145
  print(U);
beckerjc@394
   146
beckerjc@394
   147
  cout << "eraseClass 7 ..." << endl;
beckerjc@394
   148
  U.eraseClass(7);
beckerjc@394
   149
  print(U);
beckerjc@394
   150
beckerjc@394
   151
  cout << "done" << endl;
beckerjc@394
   152
beckerjc@394
   153
  cout << (passed ? "All tests passed." : "Some of the tests failed!!!")
beckerjc@394
   154
       << endl;
beckerjc@394
   155
beckerjc@394
   156
  return passed ? 0 : 1;
beckerjc@394
   157
}