| 
alpar@906
 | 
     1  | 
/* -*- C++ -*-
  | 
| 
ladanyi@1435
 | 
     2  | 
 * test/unionfind_test.cc - Part of LEMON, a generic C++ optimization library
  | 
| 
alpar@906
 | 
     3  | 
 *
  | 
| 
alpar@1164
 | 
     4  | 
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
  | 
| 
alpar@1359
 | 
     5  | 
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
  | 
| 
alpar@906
 | 
     6  | 
 *
  | 
| 
alpar@906
 | 
     7  | 
 * Permission to use, modify and distribute this software is granted
  | 
| 
alpar@906
 | 
     8  | 
 * provided that this copyright notice appears in all copies. For
  | 
| 
alpar@906
 | 
     9  | 
 * precise terms see the accompanying LICENSE file.
  | 
| 
alpar@906
 | 
    10  | 
 *
  | 
| 
alpar@906
 | 
    11  | 
 * This software is provided "AS IS" with no warranty of any kind,
  | 
| 
alpar@906
 | 
    12  | 
 * express or implied, and with no claim as to its suitability for any
  | 
| 
alpar@906
 | 
    13  | 
 * purpose.
  | 
| 
alpar@906
 | 
    14  | 
 *
  | 
| 
alpar@906
 | 
    15  | 
 */
  | 
| 
alpar@906
 | 
    16  | 
  | 
| 
beckerjc@483
 | 
    17  | 
#include <iostream>
  | 
| 
beckerjc@483
 | 
    18  | 
  | 
| 
alpar@921
 | 
    19  | 
#include <lemon/maps.h>
  | 
| 
alpar@921
 | 
    20  | 
#include <lemon/unionfind.h>
  | 
| 
alpar@774
 | 
    21  | 
#include "test_tools.h"
  | 
| 
beckerjc@483
 | 
    22  | 
  | 
| 
alpar@921
 | 
    23  | 
using namespace lemon;
  | 
| 
beckerjc@483
 | 
    24  | 
using namespace std;
  | 
| 
beckerjc@483
 | 
    25  | 
  | 
| 
beckerjc@483
 | 
    26  | 
template <typename T>
  | 
| 
beckerjc@483
 | 
    27  | 
class BaseMap : public StdMap<int,T> {};
 | 
| 
beckerjc@483
 | 
    28  | 
  | 
| 
beckerjc@483
 | 
    29  | 
typedef UnionFindEnum<int, BaseMap> UFE;
  | 
| 
beckerjc@483
 | 
    30  | 
  | 
| 
beckerjc@483
 | 
    31  | 
void print(UFE const &ufe) {
 | 
| 
beckerjc@483
 | 
    32  | 
  UFE::ClassIt cit;
  | 
| 
alpar@774
 | 
    33  | 
  cout << "Print the classes of the structure:" << endl;
  | 
| 
beckerjc@483
 | 
    34  | 
  int i = 1;
  | 
| 
beckerjc@483
 | 
    35  | 
  for (ufe.first(cit); ufe.valid(cit); ufe.next(cit)) {
 | 
| 
beckerjc@483
 | 
    36  | 
    cout << "  " << i << " (" << cit << "):" << flush;
 | 
| 
beckerjc@483
 | 
    37  | 
    UFE::ItemIt iit;
  | 
| 
beckerjc@483
 | 
    38  | 
    for (ufe.first(iit, cit); ufe.valid(iit); ufe.next(iit)) {
 | 
| 
beckerjc@483
 | 
    39  | 
      cout << " " << iit << flush;
  | 
| 
beckerjc@483
 | 
    40  | 
    }
  | 
| 
beckerjc@483
 | 
    41  | 
    cout << endl;
  | 
| 
beckerjc@483
 | 
    42  | 
    i++;
  | 
| 
beckerjc@483
 | 
    43  | 
  }
  | 
| 
beckerjc@483
 | 
    44  | 
  cout << "done" << endl;
  | 
| 
beckerjc@483
 | 
    45  | 
}
  | 
| 
beckerjc@483
 | 
    46  | 
  | 
| 
beckerjc@483
 | 
    47  | 
  | 
| 
beckerjc@483
 | 
    48  | 
int main() {
 | 
| 
beckerjc@483
 | 
    49  | 
  UFE::MapType base;
  | 
| 
beckerjc@483
 | 
    50  | 
  UFE U(base);
  | 
| 
beckerjc@483
 | 
    51  | 
  | 
| 
alpar@774
 | 
    52  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
    53  | 
  | 
| 
alpar@774
 | 
    54  | 
  cout << "Insert 1..." << endl;
  | 
| 
beckerjc@483
 | 
    55  | 
  U.insert(1);
  | 
| 
alpar@774
 | 
    56  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
    57  | 
  
  | 
| 
alpar@774
 | 
    58  | 
  cout << "Insert 2..." << endl;
  | 
| 
beckerjc@483
 | 
    59  | 
  U.insert(2);
  | 
| 
alpar@774
 | 
    60  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
    61  | 
  
  | 
| 
alpar@774
 | 
    62  | 
  cout << "Join 1 and 2..." << endl;
  | 
| 
alpar@774
 | 
    63  | 
  check(U.join(1,2),"Test failed.");
  | 
| 
alpar@774
 | 
    64  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
    65  | 
  | 
| 
alpar@774
 | 
    66  | 
  cout << "Insert 3, 4, 5, 6, 7..." << endl;
  | 
| 
beckerjc@483
 | 
    67  | 
  U.insert(3);
  | 
| 
beckerjc@483
 | 
    68  | 
  U.insert(4);
  | 
| 
beckerjc@483
 | 
    69  | 
  U.insert(5);
  | 
| 
beckerjc@483
 | 
    70  | 
  U.insert(6);
  | 
| 
beckerjc@483
 | 
    71  | 
  U.insert(7);
  | 
| 
alpar@774
 | 
    72  | 
//   print (U);
  | 
| 
beckerjc@483
 | 
    73  | 
  | 
| 
alpar@774
 | 
    74  | 
  cout << "Join 1 - 4, 2 - 4 and 3 - 5 ..." << endl;
  | 
| 
alpar@774
 | 
    75  | 
  check(U.join(1,4),"Test failed.");
  | 
| 
alpar@774
 | 
    76  | 
  check(!U.join(2,4),"Test failed.");
  | 
| 
alpar@774
 | 
    77  | 
  check(U.join(3,5),"Test failed.");
  | 
| 
alpar@774
 | 
    78  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
    79  | 
  | 
| 
alpar@774
 | 
    80  | 
  cout << "Insert 8 to the component of 5 ..." << endl;
  | 
| 
beckerjc@483
 | 
    81  | 
  U.insert(8,5);
  | 
| 
alpar@774
 | 
    82  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
    83  | 
  | 
| 
alpar@774
 | 
    84  | 
  cout << "Size of the class of 4: " << U.size(4) << endl;
  | 
| 
alpar@774
 | 
    85  | 
  check(U.size(4) == 3,"Test failed.");
  | 
| 
alpar@774
 | 
    86  | 
  cout << "Size of the class of 5: " << U.size(5) << endl;
  | 
| 
alpar@774
 | 
    87  | 
  check(U.size(5) == 3,"Test failed.");
  | 
| 
alpar@774
 | 
    88  | 
  cout << "Size of the class of 6: " << U.size(6) << endl;
  | 
| 
alpar@774
 | 
    89  | 
  check(U.size(6) == 1,"Test failed.");
  | 
| 
alpar@774
 | 
    90  | 
  cout << "Size of the class of 2: " << U.size(2) << endl;
  | 
| 
alpar@774
 | 
    91  | 
  check(U.size(2) == 3,"Test failed.");
  | 
| 
beckerjc@483
 | 
    92  | 
  | 
| 
alpar@774
 | 
    93  | 
  cout << "Insert 9 ..." << endl;
  | 
| 
beckerjc@483
 | 
    94  | 
  U.insert(9);
  | 
| 
alpar@774
 | 
    95  | 
//   print(U);
  | 
| 
alpar@774
 | 
    96  | 
  cout << "Insert 10 to the component of 9 ..." << endl;
  | 
| 
beckerjc@483
 | 
    97  | 
  U.insert(10,9);
  | 
| 
alpar@774
 | 
    98  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
    99  | 
  | 
| 
alpar@774
 | 
   100  | 
  cout << "Join 8 and 10..." << endl;
  | 
| 
alpar@774
 | 
   101  | 
  check(U.join(8,10),"Test failed.");
  | 
| 
alpar@774
 | 
   102  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
   103  | 
  | 
| 
beckerjc@483
 | 
   104  | 
  cout << "Move 9 to the class of 4 ..." << endl;
  | 
| 
alpar@774
 | 
   105  | 
  check(U.move(9,4),"Test failed.");
  | 
| 
alpar@774
 | 
   106  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
   107  | 
  | 
| 
beckerjc@483
 | 
   108  | 
  cout << "Move 9 to the class of 2 ..." << endl;
  | 
| 
alpar@774
 | 
   109  | 
  check(!U.move(9,2),"Test failed.");
  | 
| 
alpar@774
 | 
   110  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
   111  | 
  | 
| 
alpar@774
 | 
   112  | 
  cout << "Size of the class of 4: " << U.size(4) << endl;
  | 
| 
alpar@774
 | 
   113  | 
  check(U.size(4) == 4,"Test failed.");
  | 
| 
alpar@774
 | 
   114  | 
  cout << "Size of the class of 9: " << U.size(9) << endl;
  | 
| 
alpar@774
 | 
   115  | 
  check(U.size(9) == 4,"Test failed.");
  | 
| 
beckerjc@483
 | 
   116  | 
  
  | 
| 
beckerjc@483
 | 
   117  | 
  cout << "Move 5 to the class of 6 ..." << endl;
  | 
| 
alpar@774
 | 
   118  | 
  check(U.move(5,6),"Test failed.");
  | 
| 
alpar@774
 | 
   119  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
   120  | 
  | 
| 
alpar@774
 | 
   121  | 
  cout << "Size of the class of 5: " << U.size(5) << endl;
  | 
| 
alpar@774
 | 
   122  | 
  check(U.size(5) == 2,"Test failed.");
  | 
| 
alpar@774
 | 
   123  | 
  cout << "Size of the class of 8: " << U.size(8) << endl;
  | 
| 
alpar@774
 | 
   124  | 
  check(U.size(8) == 3,"Test failed.");
  | 
| 
beckerjc@483
 | 
   125  | 
  | 
| 
beckerjc@483
 | 
   126  | 
  cout << "Move 7 to the class of 10 ..." << endl;
  | 
| 
alpar@774
 | 
   127  | 
  check(U.move(7,10),"Test failed.");
  | 
| 
alpar@774
 | 
   128  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
   129  | 
  | 
| 
alpar@774
 | 
   130  | 
  cout << "Size of the class of 7: " << U.size(7) << endl;
  | 
| 
alpar@774
 | 
   131  | 
  check(U.size(7) == 4,"Test failed.");
  | 
| 
beckerjc@483
 | 
   132  | 
  | 
| 
alpar@774
 | 
   133  | 
  cout <<"Erase 9... " << endl;
  | 
| 
beckerjc@483
 | 
   134  | 
  U.erase(9);
  | 
| 
alpar@774
 | 
   135  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
   136  | 
  | 
| 
alpar@774
 | 
   137  | 
  cout <<"Erase 1... " << endl;
  | 
| 
beckerjc@483
 | 
   138  | 
  U.erase(1);
  | 
| 
alpar@774
 | 
   139  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
   140  | 
  | 
| 
alpar@774
 | 
   141  | 
  cout << "Size of the class of 4: " << U.size(4) << endl;
  | 
| 
alpar@774
 | 
   142  | 
  check(U.size(4) == 2,"Test failed.");
  | 
| 
alpar@774
 | 
   143  | 
  cout << "Size of the class of 2: " << U.size(2) << endl;
  | 
| 
alpar@774
 | 
   144  | 
  check(U.size(2) == 2,"Test failed.");
  | 
| 
beckerjc@483
 | 
   145  | 
  | 
| 
beckerjc@483
 | 
   146  | 
  | 
| 
alpar@774
 | 
   147  | 
  cout <<"Erase 1... " << endl;
  | 
| 
beckerjc@483
 | 
   148  | 
  U.erase(1);
  | 
| 
alpar@774
 | 
   149  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
   150  | 
  | 
| 
alpar@774
 | 
   151  | 
  cout <<"Erase 6... " << endl;
  | 
| 
beckerjc@483
 | 
   152  | 
  U.erase(6);
  | 
| 
alpar@774
 | 
   153  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
   154  | 
  | 
| 
alpar@774
 | 
   155  | 
  cout << "Split the class of 8... " << endl;
  | 
| 
beckerjc@483
 | 
   156  | 
  U.split(8);
  | 
| 
alpar@774
 | 
   157  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
   158  | 
  | 
| 
beckerjc@483
 | 
   159  | 
  | 
| 
alpar@774
 | 
   160  | 
  cout << "Size of the class of 4: " << U.size(4) << endl;
  | 
| 
alpar@774
 | 
   161  | 
  check(U.size(4) == 2,"Test failed.");
  | 
| 
alpar@774
 | 
   162  | 
  cout << "Size of the class of 3: " << U.size(3) << endl;
  | 
| 
alpar@774
 | 
   163  | 
  check(U.size(3) == 1,"Test failed.");
  | 
| 
alpar@774
 | 
   164  | 
  cout << "Size of the class of 2: " << U.size(2) << endl;
  | 
| 
alpar@774
 | 
   165  | 
  check(U.size(2) == 2,"Test failed.");
  | 
| 
beckerjc@483
 | 
   166  | 
  | 
| 
beckerjc@483
 | 
   167  | 
  | 
| 
alpar@774
 | 
   168  | 
  cout << "Join 3 - 4 and 2 - 4 ..." << endl;
  | 
| 
alpar@774
 | 
   169  | 
  check(U.join(3,4),"Test failed.");
  | 
| 
alpar@774
 | 
   170  | 
  check(!U.join(2,4),"Test failed.");
  | 
| 
alpar@774
 | 
   171  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
   172  | 
  | 
| 
beckerjc@483
 | 
   173  | 
  | 
| 
alpar@774
 | 
   174  | 
  cout << "Size of the class of 4: " << U.size(4) << endl;
  | 
| 
alpar@774
 | 
   175  | 
  check(U.size(4) == 3,"Test failed.");
  | 
| 
alpar@774
 | 
   176  | 
  cout << "Size of the class of 3: " << U.size(3) << endl;
  | 
| 
alpar@774
 | 
   177  | 
  check(U.size(3) == 3,"Test failed.");
  | 
| 
alpar@774
 | 
   178  | 
  cout << "Size of the class of 2: " << U.size(2) << endl;
  | 
| 
alpar@774
 | 
   179  | 
  check(U.size(2) == 3,"Test failed.");
  | 
| 
beckerjc@483
 | 
   180  | 
  | 
| 
alpar@793
 | 
   181  | 
  cout << "Calling makeRep(4)..." << endl;
  | 
| 
beckerjc@483
 | 
   182  | 
  U.makeRep(4);
  | 
| 
alpar@774
 | 
   183  | 
//   print(U);
  | 
| 
alpar@793
 | 
   184  | 
  cout << "Calling makeRep(3)..." << endl;
  | 
| 
beckerjc@483
 | 
   185  | 
  U.makeRep(3);
  | 
| 
alpar@774
 | 
   186  | 
//   print(U);
  | 
| 
alpar@793
 | 
   187  | 
  cout << "Calling makeRep(2)..." << endl;
  | 
| 
beckerjc@483
 | 
   188  | 
  U.makeRep(2);
  | 
| 
alpar@774
 | 
   189  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
   190  | 
  | 
| 
alpar@774
 | 
   191  | 
  cout << "Size of the class of 4: " << U.size(4) << endl;
  | 
| 
alpar@774
 | 
   192  | 
  check(U.size(4) == 3,"Test failed.");
  | 
| 
alpar@774
 | 
   193  | 
  cout << "Size of the class of 3: " << U.size(3) << endl;
  | 
| 
alpar@774
 | 
   194  | 
  check(U.size(3) == 3,"Test failed.");
  | 
| 
alpar@774
 | 
   195  | 
  cout << "Size of the class of 2: " << U.size(2) << endl;
  | 
| 
alpar@774
 | 
   196  | 
  check(U.size(2) == 3,"Test failed.");
  | 
| 
beckerjc@483
 | 
   197  | 
  | 
| 
beckerjc@483
 | 
   198  | 
  | 
| 
beckerjc@483
 | 
   199  | 
  cout << "eraseClass 4 ..." << endl;
  | 
| 
beckerjc@483
 | 
   200  | 
  U.eraseClass(4);
  | 
| 
alpar@774
 | 
   201  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
   202  | 
  | 
| 
beckerjc@483
 | 
   203  | 
  cout << "eraseClass 7 ..." << endl;
  | 
| 
beckerjc@483
 | 
   204  | 
  U.eraseClass(7);
  | 
| 
alpar@774
 | 
   205  | 
//   print(U);
  | 
| 
beckerjc@483
 | 
   206  | 
  | 
| 
alpar@774
 | 
   207  | 
  cout << "done." << endl;
  | 
| 
beckerjc@483
 | 
   208  | 
}
  |