COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/johanna/unionfind_test.cc @ 456:02c28d3cf97b

Last change on this file since 456:02c28d3cf97b was 394:3a34c5626e52, checked in by beckerjc, 20 years ago

New union-find structure with enumerable classes.

File size: 2.1 KB
Line 
1#include <iostream>
2
3#include <maps.h>
4#include <unionfind.h>
5
6using namespace hugo;
7using namespace std;
8
9bool passed = true;
10
11void check(bool rc) {
12  passed = passed && rc;
13  if(!rc) {
14    cout << "Test failed!" << endl;
15  }
16}
17
18template <typename T>
19class BaseMap : public StdMap<int,T> {};
20
21typedef UnionFindEnum<int, BaseMap> UFE;
22
23void print(UFE const &ufe) {
24  UFE::ClassIt cit;
25  cout << "printing the classes of the structure:" << endl;
26  int i = 1;
27  for (ufe.first(cit); ufe.valid(cit); ufe.next(cit)) {
28    cout << "  " << i << " (" << cit << "):" << flush;
29    UFE::ItemIt iit;
30    for (ufe.first(iit, cit); ufe.valid(iit); ufe.next(iit)) {
31      cout << " " << iit << flush;
32    }
33    cout << endl;
34    i++;
35  }
36  cout << "done" << endl;
37}
38
39
40int main() {
41  UFE::MapType base;
42  UFE U(base);
43
44  print(U);
45
46  cout << "Inserting 1..." << endl;
47  U.insert(1);
48  print(U);
49 
50  cout << "Inserting 2..." << endl;
51  U.insert(2);
52  print(U);
53 
54  cout << "Joining 1 and 2..." << endl;
55  check(U.join(1,2));
56  print(U);
57
58  cout << "Inserting 3, 4, 5, 6, 7..." << endl;
59  U.insert(3);
60  U.insert(4);
61  U.insert(5);
62  U.insert(6);
63  U.insert(7);
64  print (U);
65
66  cout << "Joining 1 - 4, 2 - 4 and 3 - 5 ..." << endl;
67  check(U.join(1,4));
68  check(!U.join(2,4));
69  check(U.join(3,5));
70  print(U);
71
72  cout << "size of the class of 4: " << U.size(4) << endl;
73  check(U.size(4) == 3);
74  cout << "size of the class of 5: " << U.size(5) << endl;
75  check(U.size(5) == 2);
76  cout << "size of the class of 6: " << U.size(6) << endl;
77  check(U.size(6) == 1);
78
79  cout <<"erase 1: " << endl;
80  U.erase(1);
81  print(U);
82
83  cout <<"erase 1: " << endl;
84  U.erase(1);
85  print(U);
86
87  cout <<"erase 6: " << endl;
88  U.erase(6);
89  print(U);
90
91  cout << "split the class of 5: " << endl;
92  U.split(5);
93  print(U);
94
95  cout << "Joining 3 - 4 and 2 - 4 ..." << endl;
96  check(U.join(3,4));
97  check(!U.join(2,4));
98  print(U);
99
100  cout << "eraseClass 4 ..." << endl;
101  U.eraseClass(4);
102  print(U);
103
104  cout << "eraseClass 7 ..." << endl;
105  U.eraseClass(7);
106  print(U);
107
108  cout << "done" << endl;
109
110  cout << (passed ? "All tests passed." : "Some of the tests failed!!!")
111       << endl;
112
113  return passed ? 0 : 1;
114}
Note: See TracBrowser for help on using the repository browser.