TPIE

2362a60
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=0,
72  value_t u = default_unused<value_t>::v(),
74  : m_elements(n, u, b), m_unused(u), m_size(0) {}
75 
76  disjoint_sets(tpie::memory_bucket_ref b): m_elements(b), m_unused(default_unused<value_t>::v()), m_size(0) {}
77 
83  inline void make_set(value_t element) {m_elements[element] = element; ++m_size;}
84 
92  inline bool is_set(value_t element) {return m_elements[element] != m_unused;}
93 
101  inline value_t link(value_t a, value_t b) {
102  if (a == b) return a;
103  --m_size;
104  m_elements[b] = a;
105  return a;
106  }
107 
115  inline value_t find_set(value_t t) {
116  // If t sits in depth d, we halve the depth of d/2 nodes in the tree,
117  // including t.
118  while (true) {
119  // Set t to point to its grandparent.
120  value_t x = m_elements[m_elements[t]];
121  if (x == t) return t;
122  m_elements[t] = x;
123  t = x;
124  }
125 
126  // The textbook implementation below is faster for some adversarial
127  // inputs, but is cache inefficient on ordinary input.
128 
129  //value_t r = m_elements[t];
130  //if (r == t)
131  // return r;
132  //while (r != m_elements[r])
133  // r = m_elements[r];
134  //while (t != r) {
135  // value_t next = m_elements[t];
136  // m_elements[t] = r;
137  // t = next;
138  //}
139  //return r;
140  }
141 
150  inline value_t union_set(value_t a, value_t b) {
151  return link(find_set(a), find_set(b));
152  }
153 
157  inline size_type count_sets() {
158  return m_size;
159  }
160 
164  void clear() {
165  std::fill(m_elements.begin(), m_elements.end(), m_unused);
166  m_size = 0;
167  }
168 
173  void resize(size_t size) {
174  m_elements.resize(size, m_unused);
175  m_size = 0;
176  }
177 };
178 
179 }
180 #endif //__TPIE_DISJOINT_SETS__
value_t link(value_t a, value_t b)
Union two sets given by their representatives.
disjoint_sets(size_type n=0, value_t u=default_unused< value_t >::v(), tpie::memory_bucket_ref b=tpie::memory_bucket_ref())
Construct a empty collection of disjoint sets.
Definition: disjoint_sets.h:71
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
void resize(size_t size)
Changes the size of the datastructure.
bool is_set(value_t element)
Check if a given element is a member of any set.
Definition: disjoint_sets.h:92
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.
Class storring a reference to a memory bucket.
Definition: memory.h:366
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:393
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:485
Internal memory union find implementation.
Definition: disjoint_sets.h:43
static double memory_overhead()
Return the memory overhead of the structure.
Definition: array.h:400
void clear()
Clears all sets contained in the datastructure.
iterator end()
Return an iterator to the end of the array.
Definition: array.h:321
iterator begin()
Return an iterator to the beginning of the array.
Definition: array.h:307
void make_set(value_t element)
Make a singleton set.
Definition: disjoint_sets.h:83
Default special unused values for standard types.