test/unionfind_test.cc
author hegyi
Thu, 05 Jan 2006 12:30:09 +0000
changeset 1878 409a31271efd
parent 1569 2a455f46f85a
child 1956 a055123339d5
permissions -rw-r--r--
Several changes. \n If new map is added to mapstorage it emits signal with the name of the new map. This was important, because from now on not only tha mapwin should be updated. \n Furthermore algobox gets a pointer to mapstorage instead of only the mapnames from it. This is important because without it it would be complicated to pass all of the required maps to algobox.
alpar@906
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * test/unionfind_test.cc - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@1875
     4
 * Copyright (C) 2006 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@1569
    52
  U.insert(1);
alpar@1569
    53
  U.insert(2);
beckerjc@483
    54
alpar@774
    55
  check(U.join(1,2),"Test failed.");
beckerjc@483
    56
beckerjc@483
    57
  U.insert(3);
beckerjc@483
    58
  U.insert(4);
beckerjc@483
    59
  U.insert(5);
beckerjc@483
    60
  U.insert(6);
beckerjc@483
    61
  U.insert(7);
beckerjc@483
    62
alpar@774
    63
  check(U.join(1,4),"Test failed.");
alpar@774
    64
  check(!U.join(2,4),"Test failed.");
alpar@774
    65
  check(U.join(3,5),"Test failed.");
beckerjc@483
    66
beckerjc@483
    67
  U.insert(8,5);
beckerjc@483
    68
alpar@774
    69
  check(U.size(4) == 3,"Test failed.");
alpar@774
    70
  check(U.size(5) == 3,"Test failed.");
alpar@774
    71
  check(U.size(6) == 1,"Test failed.");
alpar@774
    72
  check(U.size(2) == 3,"Test failed.");
beckerjc@483
    73
beckerjc@483
    74
  U.insert(9);
beckerjc@483
    75
  U.insert(10,9);
alpar@1569
    76
  check(U.join(8,10),"Test failed.");
beckerjc@483
    77
alpar@1569
    78
  check(U.move(9,4),"Test failed.");
alpar@1569
    79
  check(!U.move(9,2),"Test failed.");
beckerjc@483
    80
alpar@1569
    81
  check(U.size(4) == 4,"Test failed.");
alpar@1569
    82
  check(U.size(9) == 4,"Test failed.");
beckerjc@483
    83
alpar@1569
    84
  check(U.move(5,6),"Test failed.");
beckerjc@483
    85
alpar@774
    86
  check(U.size(5) == 2,"Test failed.");
alpar@774
    87
  check(U.size(8) == 3,"Test failed.");
beckerjc@483
    88
alpar@774
    89
  check(U.move(7,10),"Test failed.");
alpar@774
    90
  check(U.size(7) == 4,"Test failed.");
beckerjc@483
    91
beckerjc@483
    92
  U.erase(9);
alpar@1569
    93
  U.erase(1);
beckerjc@483
    94
alpar@774
    95
  check(U.size(4) == 2,"Test failed.");
alpar@774
    96
  check(U.size(2) == 2,"Test failed.");
beckerjc@483
    97
alpar@1569
    98
  U.erase(1);
alpar@1569
    99
  U.erase(6);
alpar@1569
   100
  U.split(8);
beckerjc@483
   101
alpar@774
   102
  check(U.size(4) == 2,"Test failed.");
alpar@774
   103
  check(U.size(3) == 1,"Test failed.");
alpar@774
   104
  check(U.size(2) == 2,"Test failed.");
beckerjc@483
   105
alpar@774
   106
  check(U.join(3,4),"Test failed.");
alpar@774
   107
  check(!U.join(2,4),"Test failed.");
beckerjc@483
   108
alpar@774
   109
  check(U.size(4) == 3,"Test failed.");
alpar@774
   110
  check(U.size(3) == 3,"Test failed.");
alpar@774
   111
  check(U.size(2) == 3,"Test failed.");
beckerjc@483
   112
beckerjc@483
   113
  U.makeRep(4);
beckerjc@483
   114
  U.makeRep(3);
beckerjc@483
   115
  U.makeRep(2);
beckerjc@483
   116
alpar@774
   117
  check(U.size(4) == 3,"Test failed.");
alpar@774
   118
  check(U.size(3) == 3,"Test failed.");
alpar@774
   119
  check(U.size(2) == 3,"Test failed.");
beckerjc@483
   120
beckerjc@483
   121
beckerjc@483
   122
  U.eraseClass(4);
alpar@1569
   123
  U.eraseClass(7);
beckerjc@483
   124
beckerjc@483
   125
}