TPIE

v1.1rc1-6-g0c97303
disjoint_sets.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2010, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 #ifndef __TPIE_DISJOINT_SETS__
20 #define __TPIE_DISJOINT_SETS__
21 
27 #include <tpie/array.h>
28 #include <tpie/unused.h>
29 #include <tpie/util.h>
30 
31 namespace tpie {
32 
42 template <typename value_t=size_type>
43 class disjoint_sets: public linear_memory_base< disjoint_sets<value_t> > {
44 private:
45  array<value_t> m_elements;
46  value_t m_unused;
47  size_type m_size;
48 public:
53  static double memory_coefficient() {
55  }
56 
61  static double memory_overhead() {
63  }
64 
71  disjoint_sets(size_type n, value_t u = default_unused<value_t>::v()): m_elements(n, u), m_unused(u), m_size(0) {}
72 
78  inline void make_set(value_t element) {m_elements[element] = element; ++m_size;}
79 
87  inline bool is_set(value_t element) {return m_elements[element] != m_unused;}
88 
96  inline value_t link(value_t a, value_t b) {
97  if (a == b) return a;
98  --m_size;
99  m_elements[b] = a;
100  return a;
101  }
102 
110  inline value_t find_set(value_t t) {
111  // If t sits in depth d, we halve the depth of d/2 nodes in the tree,
112  // including t.
113  while (true) {
114  // Set t to point to its grandparent.
115  value_t x = m_elements[m_elements[t]];
116  if (x == t) return t;
117  m_elements[t] = x;
118  t = x;
119  }
120 
121  // The textbook implementation below is faster for some adversarial
122  // inputs, but is cache inefficient on ordinary input.
123 
124  //value_t r = m_elements[t];
125  //if (r == t)
126  // return r;
127  //while (r != m_elements[r])
128  // r = m_elements[r];
129  //while (t != r) {
130  // value_t next = m_elements[t];
131  // m_elements[t] = r;
132  // t = next;
133  //}
134  //return r;
135  }
136 
145  inline value_t union_set(value_t a, value_t b) {
146  return link(find_set(a), find_set(b));
147  }
148 
152  inline size_type count_sets() {
153  return m_size;
154  }
155 };
156 
157 }
158 #endif //__TPIE_DISJOINT_SETS__
value_t link(value_t a, value_t b)
Union two sets given by their representatives.
Definition: disjoint_sets.h:96
value_t find_set(value_t t)
Find the representative of the set contaning a given element.
value_t union_set(value_t a, value_t b)
Union the set containing a with the set containing b.
Base class of data structures with linear memory usage.
Definition: util.h:73
bool is_set(value_t element)
Check if a given element is a member of any set.
Definition: disjoint_sets.h:87
static double memory_coefficient()
Return the memory coefficient of the structure.
Definition: disjoint_sets.h:53
Generic internal array with known memory requirements.
size_type count_sets()
Return the number of sets.
disjoint_sets(size_type n, value_t u=default_unused< value_t >::v())
Construct a empty collection of disjoint sets.
Definition: disjoint_sets.h:71
Miscellaneous utility functions.
static double memory_overhead()
Return the memory overhead of the structure.
Definition: disjoint_sets.h:61
static double memory_coefficient()
Return the memory coefficient of the structure.
Definition: array.h:360
Internal memory union find implementation.
Definition: disjoint_sets.h:43
static double memory_overhead()
Return the memory overhead of the structure.
Definition: array.h:367
void make_set(value_t element)
Make a singleton set.
Definition: disjoint_sets.h:78
Default special unused values for standard types.