test/unionfind_test.cc
author klao
Fri, 03 Mar 2006 21:49:39 +0000
changeset 1997 b7a70cdb5520
parent 1875 98698b69a902
child 2005 84ec2948eb1f
permissions -rw-r--r--
Bugfix: an ugly artefact of the 'id' -> 'label' renaming
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
beckerjc@483
    28
template <typename T>
beckerjc@483
    29
class BaseMap : public StdMap<int,T> {};
beckerjc@483
    30
beckerjc@483
    31
typedef UnionFindEnum<int, BaseMap> UFE;
beckerjc@483
    32
beckerjc@483
    33
void print(UFE const &ufe) {
beckerjc@483
    34
  UFE::ClassIt cit;
alpar@774
    35
  cout << "Print the classes of the structure:" << endl;
beckerjc@483
    36
  int i = 1;
beckerjc@483
    37
  for (ufe.first(cit); ufe.valid(cit); ufe.next(cit)) {
beckerjc@483
    38
    cout << "  " << i << " (" << cit << "):" << flush;
beckerjc@483
    39
    UFE::ItemIt iit;
beckerjc@483
    40
    for (ufe.first(iit, cit); ufe.valid(iit); ufe.next(iit)) {
beckerjc@483
    41
      cout << " " << iit << flush;
beckerjc@483
    42
    }
beckerjc@483
    43
    cout << endl;
beckerjc@483
    44
    i++;
beckerjc@483
    45
  }
beckerjc@483
    46
  cout << "done" << endl;
beckerjc@483
    47
}
beckerjc@483
    48
beckerjc@483
    49
beckerjc@483
    50
int main() {
beckerjc@483
    51
  UFE::MapType base;
beckerjc@483
    52
  UFE U(base);
beckerjc@483
    53
alpar@1569
    54
  U.insert(1);
alpar@1569
    55
  U.insert(2);
beckerjc@483
    56
alpar@774
    57
  check(U.join(1,2),"Test failed.");
beckerjc@483
    58
beckerjc@483
    59
  U.insert(3);
beckerjc@483
    60
  U.insert(4);
beckerjc@483
    61
  U.insert(5);
beckerjc@483
    62
  U.insert(6);
beckerjc@483
    63
  U.insert(7);
beckerjc@483
    64
alpar@774
    65
  check(U.join(1,4),"Test failed.");
alpar@774
    66
  check(!U.join(2,4),"Test failed.");
alpar@774
    67
  check(U.join(3,5),"Test failed.");
beckerjc@483
    68
beckerjc@483
    69
  U.insert(8,5);
beckerjc@483
    70
alpar@774
    71
  check(U.size(4) == 3,"Test failed.");
alpar@774
    72
  check(U.size(5) == 3,"Test failed.");
alpar@774
    73
  check(U.size(6) == 1,"Test failed.");
alpar@774
    74
  check(U.size(2) == 3,"Test failed.");
beckerjc@483
    75
beckerjc@483
    76
  U.insert(9);
beckerjc@483
    77
  U.insert(10,9);
alpar@1569
    78
  check(U.join(8,10),"Test failed.");
beckerjc@483
    79
alpar@1569
    80
  check(U.move(9,4),"Test failed.");
alpar@1569
    81
  check(!U.move(9,2),"Test failed.");
beckerjc@483
    82
alpar@1569
    83
  check(U.size(4) == 4,"Test failed.");
alpar@1569
    84
  check(U.size(9) == 4,"Test failed.");
beckerjc@483
    85
alpar@1569
    86
  check(U.move(5,6),"Test failed.");
beckerjc@483
    87
alpar@774
    88
  check(U.size(5) == 2,"Test failed.");
alpar@774
    89
  check(U.size(8) == 3,"Test failed.");
beckerjc@483
    90
alpar@774
    91
  check(U.move(7,10),"Test failed.");
alpar@774
    92
  check(U.size(7) == 4,"Test failed.");
beckerjc@483
    93
beckerjc@483
    94
  U.erase(9);
alpar@1569
    95
  U.erase(1);
beckerjc@483
    96
alpar@774
    97
  check(U.size(4) == 2,"Test failed.");
alpar@774
    98
  check(U.size(2) == 2,"Test failed.");
beckerjc@483
    99
alpar@1569
   100
  U.erase(1);
alpar@1569
   101
  U.erase(6);
alpar@1569
   102
  U.split(8);
beckerjc@483
   103
alpar@774
   104
  check(U.size(4) == 2,"Test failed.");
alpar@774
   105
  check(U.size(3) == 1,"Test failed.");
alpar@774
   106
  check(U.size(2) == 2,"Test failed.");
beckerjc@483
   107
alpar@774
   108
  check(U.join(3,4),"Test failed.");
alpar@774
   109
  check(!U.join(2,4),"Test failed.");
beckerjc@483
   110
alpar@774
   111
  check(U.size(4) == 3,"Test failed.");
alpar@774
   112
  check(U.size(3) == 3,"Test failed.");
alpar@774
   113
  check(U.size(2) == 3,"Test failed.");
beckerjc@483
   114
beckerjc@483
   115
  U.makeRep(4);
beckerjc@483
   116
  U.makeRep(3);
beckerjc@483
   117
  U.makeRep(2);
beckerjc@483
   118
alpar@774
   119
  check(U.size(4) == 3,"Test failed.");
alpar@774
   120
  check(U.size(3) == 3,"Test failed.");
alpar@774
   121
  check(U.size(2) == 3,"Test failed.");
beckerjc@483
   122
beckerjc@483
   123
beckerjc@483
   124
  U.eraseClass(4);
alpar@1569
   125
  U.eraseClass(7);
beckerjc@483
   126
beckerjc@483
   127
}