lilypond-1.1.55
[lilypond.git] / flower / unionfind.cc
blob1bbf1f409ce279ce8e2b960136949848c4e6c687
1 #include "unionfind.hh"
2 /*
3 see a book on data structures
4 */
6 Union_find::Union_find (int n)
8 classes.set_size (n);
10 for (int i=0; i < n; i++)
12 classes[i] = i;
16 int
17 Union_find::find (int i)
19 int rep = i;
20 while (classes[rep] != rep)
21 rep = classes[rep];
22 while (classes[i] != rep)
24 int next =classes[i];
25 classes[i] = rep;
26 i = next;
28 return rep;
31 void
32 Union_find::connect (int i, int j)
34 i = find (i);
35 j = find (j);
36 classes[i] = j;