test/unionfind_test.cc
author hegyi
Fri, 27 Oct 2006 15:19:33 +0000
changeset 2266 07b533060ec5
parent 2005 84ec2948eb1f
child 2308 cddae1c4fee6
permissions -rw-r--r--
NEWS updated to Rel0.6 - according to Alpar's instruction, at last
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     8
 *
alpar@906
     9
 * Permission to use, modify and distribute this software is granted
alpar@906
    10
 * provided that this copyright notice appears in all copies. For
alpar@906
    11
 * precise terms see the accompanying LICENSE file.
alpar@906
    12
 *
alpar@906
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    14
 * express or implied, and with no claim as to its suitability for any
alpar@906
    15
 * purpose.
alpar@906
    16
 *
alpar@906
    17
 */
alpar@906
    18
beckerjc@483
    19
#include <iostream>
beckerjc@483
    20
alpar@921
    21
#include <lemon/maps.h>
alpar@921
    22
#include <lemon/unionfind.h>
alpar@774
    23
#include "test_tools.h"
beckerjc@483
    24
alpar@921
    25
using namespace lemon;
beckerjc@483
    26
using namespace std;
beckerjc@483
    27
deba@2205
    28
typedef UnionFindEnum<int, StdMap<int, int> > UFE;
beckerjc@483
    29
beckerjc@483
    30
void print(UFE const &ufe) {
alpar@774
    31
  cout << "Print the classes of the structure:" << endl;
beckerjc@483
    32
  int i = 1;
deba@2205
    33
  for (UFE::ClassIt cit(ufe); cit != INVALID; ++cit) {
beckerjc@483
    34
    cout << "  " << i << " (" << cit << "):" << flush;
deba@2205
    35
    for (UFE::ItemIt iit(ufe, cit); iit != INVALID; ++iit) {
beckerjc@483
    36
      cout << " " << iit << flush;
beckerjc@483
    37
    }
beckerjc@483
    38
    cout << endl;
beckerjc@483
    39
    i++;
beckerjc@483
    40
  }
beckerjc@483
    41
  cout << "done" << endl;
beckerjc@483
    42
}
beckerjc@483
    43
beckerjc@483
    44
beckerjc@483
    45
int main() {
deba@2205
    46
  StdMap<int, int> base;
beckerjc@483
    47
  UFE U(base);
beckerjc@483
    48
alpar@1569
    49
  U.insert(1);
alpar@1569
    50
  U.insert(2);
beckerjc@483
    51
alpar@774
    52
  check(U.join(1,2),"Test failed.");
beckerjc@483
    53
beckerjc@483
    54
  U.insert(3);
beckerjc@483
    55
  U.insert(4);
beckerjc@483
    56
  U.insert(5);
beckerjc@483
    57
  U.insert(6);
beckerjc@483
    58
  U.insert(7);
beckerjc@483
    59
alpar@774
    60
  check(U.join(1,4),"Test failed.");
alpar@774
    61
  check(!U.join(2,4),"Test failed.");
alpar@774
    62
  check(U.join(3,5),"Test failed.");
beckerjc@483
    63
beckerjc@483
    64
  U.insert(8,5);
beckerjc@483
    65
alpar@774
    66
  check(U.size(4) == 3,"Test failed.");
alpar@774
    67
  check(U.size(5) == 3,"Test failed.");
alpar@774
    68
  check(U.size(6) == 1,"Test failed.");
alpar@774
    69
  check(U.size(2) == 3,"Test failed.");
beckerjc@483
    70
beckerjc@483
    71
  U.insert(9);
beckerjc@483
    72
  U.insert(10,9);
deba@2205
    73
alpar@1569
    74
  check(U.join(8,10),"Test failed.");
beckerjc@483
    75
alpar@1569
    76
  check(U.move(9,4),"Test failed.");
alpar@1569
    77
  check(!U.move(9,2),"Test failed.");
beckerjc@483
    78
alpar@1569
    79
  check(U.size(4) == 4,"Test failed.");
alpar@1569
    80
  check(U.size(9) == 4,"Test failed.");
beckerjc@483
    81
alpar@1569
    82
  check(U.move(5,6),"Test failed.");
beckerjc@483
    83
alpar@774
    84
  check(U.size(5) == 2,"Test failed.");
alpar@774
    85
  check(U.size(8) == 3,"Test failed.");
beckerjc@483
    86
alpar@774
    87
  check(U.move(7,10),"Test failed.");
alpar@774
    88
  check(U.size(7) == 4,"Test failed.");
beckerjc@483
    89
beckerjc@483
    90
  U.erase(9);
alpar@1569
    91
  U.erase(1);
beckerjc@483
    92
alpar@774
    93
  check(U.size(4) == 2,"Test failed.");
alpar@774
    94
  check(U.size(2) == 2,"Test failed.");
beckerjc@483
    95
alpar@1569
    96
  U.erase(6);
alpar@1569
    97
  U.split(8);
beckerjc@483
    98
alpar@774
    99
  check(U.size(4) == 2,"Test failed.");
alpar@774
   100
  check(U.size(3) == 1,"Test failed.");
alpar@774
   101
  check(U.size(2) == 2,"Test failed.");
beckerjc@483
   102
alpar@774
   103
  check(U.join(3,4),"Test failed.");
alpar@774
   104
  check(!U.join(2,4),"Test failed.");
beckerjc@483
   105
alpar@774
   106
  check(U.size(4) == 3,"Test failed.");
alpar@774
   107
  check(U.size(3) == 3,"Test failed.");
alpar@774
   108
  check(U.size(2) == 3,"Test failed.");
beckerjc@483
   109
beckerjc@483
   110
  U.makeRep(4);
beckerjc@483
   111
  U.makeRep(3);
beckerjc@483
   112
  U.makeRep(2);
beckerjc@483
   113
alpar@774
   114
  check(U.size(4) == 3,"Test failed.");
alpar@774
   115
  check(U.size(3) == 3,"Test failed.");
alpar@774
   116
  check(U.size(2) == 3,"Test failed.");
beckerjc@483
   117
beckerjc@483
   118
beckerjc@483
   119
  U.eraseClass(4);
alpar@1569
   120
  U.eraseClass(7);
beckerjc@483
   121
beckerjc@483
   122
}