Chris@16
|
1 // (C) Copyright Jeremy Siek 2004
|
Chris@16
|
2 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
3 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
4 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6 #ifndef BOOST_DETAIL_DISJOINT_SETS_HPP
|
Chris@16
|
7 #define BOOST_DETAIL_DISJOINT_SETS_HPP
|
Chris@16
|
8
|
Chris@16
|
9 namespace boost {
|
Chris@16
|
10
|
Chris@16
|
11 namespace detail {
|
Chris@16
|
12
|
Chris@16
|
13 template <class ParentPA, class Vertex>
|
Chris@16
|
14 Vertex
|
Chris@16
|
15 find_representative_with_path_halving(ParentPA p, Vertex v)
|
Chris@16
|
16 {
|
Chris@16
|
17 Vertex parent = get(p, v);
|
Chris@16
|
18 Vertex grandparent = get(p, parent);
|
Chris@16
|
19 while (parent != grandparent) {
|
Chris@16
|
20 put(p, v, grandparent);
|
Chris@16
|
21 v = grandparent;
|
Chris@16
|
22 parent = get(p, v);
|
Chris@16
|
23 grandparent = get(p, parent);
|
Chris@16
|
24 }
|
Chris@16
|
25 return parent;
|
Chris@16
|
26 }
|
Chris@16
|
27
|
Chris@16
|
28 template <class ParentPA, class Vertex>
|
Chris@16
|
29 Vertex
|
Chris@16
|
30 find_representative_with_full_compression(ParentPA parent, Vertex v)
|
Chris@16
|
31 {
|
Chris@16
|
32 Vertex old = v;
|
Chris@16
|
33 Vertex ancestor = get(parent, v);
|
Chris@16
|
34 while (ancestor != v) {
|
Chris@16
|
35 v = ancestor;
|
Chris@16
|
36 ancestor = get(parent, v);
|
Chris@16
|
37 }
|
Chris@16
|
38 v = get(parent, old);
|
Chris@16
|
39 while (ancestor != v) {
|
Chris@16
|
40 put(parent, old, ancestor);
|
Chris@16
|
41 old = v;
|
Chris@16
|
42 v = get(parent, old);
|
Chris@16
|
43 }
|
Chris@16
|
44 return ancestor;
|
Chris@16
|
45 }
|
Chris@16
|
46
|
Chris@16
|
47 /* the postcondition of link sets is:
|
Chris@16
|
48 component_representative(i) == component_representative(j)
|
Chris@16
|
49 */
|
Chris@16
|
50 template <class ParentPA, class RankPA, class Vertex,
|
Chris@16
|
51 class ComponentRepresentative>
|
Chris@16
|
52 inline void
|
Chris@16
|
53 link_sets(ParentPA p, RankPA rank, Vertex i, Vertex j,
|
Chris@16
|
54 ComponentRepresentative comp_rep)
|
Chris@16
|
55 {
|
Chris@16
|
56 i = comp_rep(p, i);
|
Chris@16
|
57 j = comp_rep(p, j);
|
Chris@16
|
58 if (i == j) return;
|
Chris@16
|
59 if (get(rank, i) > get(rank, j))
|
Chris@16
|
60 put(p, j, i);
|
Chris@16
|
61 else {
|
Chris@16
|
62 put(p, i, j);
|
Chris@16
|
63 if (get(rank, i) == get(rank, j))
|
Chris@16
|
64 put(rank, j, get(rank, j) + 1);
|
Chris@16
|
65 }
|
Chris@16
|
66 }
|
Chris@16
|
67
|
Chris@16
|
68 // normalize components has the following postcondidition:
|
Chris@16
|
69 // i >= p[i]
|
Chris@16
|
70 // that is, the representative is the node with the smallest index in its class
|
Chris@16
|
71 // as its precondition it it assumes that the node container is compressed
|
Chris@16
|
72
|
Chris@16
|
73 template <class ParentPA, class Vertex>
|
Chris@16
|
74 inline void
|
Chris@16
|
75 normalize_node(ParentPA p, Vertex i)
|
Chris@16
|
76 {
|
Chris@16
|
77 if (i > get(p,i) || get(p, get(p,i)) != get(p,i))
|
Chris@16
|
78 put(p,i, get(p, get(p,i)));
|
Chris@16
|
79 else {
|
Chris@16
|
80 put(p, get(p,i), i);
|
Chris@16
|
81 put(p, i, i);
|
Chris@16
|
82 }
|
Chris@16
|
83 }
|
Chris@16
|
84
|
Chris@16
|
85 } // namespace detail
|
Chris@16
|
86 } // namespace boost
|
Chris@16
|
87
|
Chris@16
|
88 #endif // BOOST_DETAIL_DISJOINT_SETS_HPP
|