TPIE

v1.1rc1-6-g0c97303
tpie::disjoint_sets< value_t > Class Template Reference

Internal memory union find implementation. More...

#include <tpie/disjoint_sets.h>

Inherits tpie::linear_memory_base< disjoint_sets< value_t > >.

Public Member Functions

 disjoint_sets (size_type n, value_t u=default_unused< value_t >::v())
 Construct a empty collection of disjoint sets. More...
 
void make_set (value_t element)
 Make a singleton set. More...
 
bool is_set (value_t element)
 Check if a given element is a member of any set. More...
 
value_t link (value_t a, value_t b)
 Union two sets given by their representatives. More...
 
value_t find_set (value_t t)
 Find the representative of the set contaning a given element. More...
 
value_t union_set (value_t a, value_t b)
 Union the set containing a with the set containing b. More...
 
size_type count_sets ()
 Return the number of sets. More...
 

Static Public Member Functions

static double memory_coefficient ()
 Return the memory coefficient of the structure. More...
 
static double memory_overhead ()
 Return the memory overhead of the structure. More...
 
static stream_size_type memory_usage (stream_size_type size)
 Return the number of bytes required to create a data structure supporting a given number of elements. More...
 
static memory_size_type memory_fits (memory_size_type memory)
 Return the maximum number of elements that can be contained in in the structure when it is allowed to fill a given number of bytes. More...
 

Detailed Description

template<typename value_t = size_type>
class tpie::disjoint_sets< value_t >

Internal memory union find implementation.

The key space is the first n integers (from 0 to n-1). n is given in the constructor.

Template Parameters
value_tThe type of values stored (must be an integer type).

Definition at line 43 of file disjoint_sets.h.

Constructor & Destructor Documentation

template<typename value_t = size_type>
tpie::disjoint_sets< value_t >::disjoint_sets ( size_type  n,
value_t  u = default_unused<value_t>::v() 
)
inline

Construct a empty collection of disjoint sets.

Parameters
nThe maximal number of sets to support
uA value you guarentee not to use.

Definition at line 71 of file disjoint_sets.h.

Referenced by tpie::disjoint_sets< value_t >::memory_overhead().

71 : m_elements(n, u), m_unused(u), m_size(0) {}

Member Function Documentation

template<typename value_t = size_type>
size_type tpie::disjoint_sets< value_t >::count_sets ( )
inline

Return the number of sets.

Definition at line 152 of file disjoint_sets.h.

152  {
153  return m_size;
154  }
template<typename value_t = size_type>
value_t tpie::disjoint_sets< value_t >::find_set ( value_t  t)
inline

Find the representative of the set contaning a given element.

Parameters
tThe element of which to find the set representative
Returns
The representative.

Definition at line 110 of file disjoint_sets.h.

Referenced by tpie::disjoint_sets< value_t >::union_set().

110  {
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  }
template<typename value_t = size_type>
bool tpie::disjoint_sets< value_t >::is_set ( value_t  element)
inline

Check if a given element is a member of any set.

This is the same as saying if make_set has ever been called with the given key

Parameters
elementThe key to check

Definition at line 87 of file disjoint_sets.h.

87 {return m_elements[element] != m_unused;}
template<typename value_t = size_type>
value_t tpie::disjoint_sets< value_t >::link ( value_t  a,
value_t  b 
)
inline

Union two sets given by their representatives.

Parameters
aThe representative of one set
bThe representative of the other set
Returns
The representative of the combined set

Definition at line 96 of file disjoint_sets.h.

Referenced by tpie::disjoint_sets< value_t >::union_set().

96  {
97  if (a == b) return a;
98  --m_size;
99  m_elements[b] = a;
100  return a;
101  }
template<typename value_t = size_type>
void tpie::disjoint_sets< value_t >::make_set ( value_t  element)
inline

Make a singleton set.

Parameters
elementThe key of the singleton set to create

Definition at line 78 of file disjoint_sets.h.

78 {m_elements[element] = element; ++m_size;}
template<typename value_t = size_type>
static double tpie::disjoint_sets< value_t >::memory_coefficient ( )
inlinestatic

Return the memory coefficient of the structure.

Allocating a structure with n elements will use at most $ \lfloor \mathrm{memory\_coefficient} \cdot n + \mathrm{memory\_overhead} \rfloor $ bytes. This does not include memory overhead incurred if the structure is allocated using new.

Returns
The memory coefficient of the structure.

Definition at line 53 of file disjoint_sets.h.

References tpie::array< T, Allocator >::memory_coefficient().

53  {
55  }
static double memory_coefficient()
Return the memory coefficient of the structure.
Definition: array.h:360
static memory_size_type tpie::linear_memory_base< disjoint_sets< value_t > >::memory_fits ( memory_size_type  memory)
inlinestaticinherited

Return the maximum number of elements that can be contained in in the structure when it is allowed to fill a given number of bytes.

Parameters
memoryThe number of bytes the structure is allowed to occupy
Returns
The number of elements that will fit in the structure

Definition at line 93 of file util.h.

93  {
94  return static_cast<memory_size_type>(
95  floor((memory - child_t::memory_overhead()) / child_t::memory_coefficient()));
96  }
template<typename value_t = size_type>
static double tpie::disjoint_sets< value_t >::memory_overhead ( )
inlinestatic

Return the memory overhead of the structure.

See also
memory_coefficient()
Returns
The memory overhead.

Definition at line 61 of file disjoint_sets.h.

References tpie::disjoint_sets< value_t >::disjoint_sets(), and tpie::array< T, Allocator >::memory_overhead().

61  {
62  return array<value_t>::memory_overhead() + sizeof(disjoint_sets) - sizeof(array<value_t>);
63  }
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
static double memory_overhead()
Return the memory overhead of the structure.
Definition: array.h:367
static stream_size_type tpie::linear_memory_base< disjoint_sets< value_t > >::memory_usage ( stream_size_type  size)
inlinestaticinherited

Return the number of bytes required to create a data structure supporting a given number of elements.

Parameters
sizeThe number of elements to support
Returns
The amount of memory required in bytes

Definition at line 81 of file util.h.

81  {
82  return static_cast<stream_size_type>(
83  floor(static_cast<double>(size) * child_t::memory_coefficient() + child_t::memory_overhead()));
84  }
template<typename value_t = size_type>
value_t tpie::disjoint_sets< value_t >::union_set ( value_t  a,
value_t  b 
)
inline

Union the set containing a with the set containing b.

Parameters
aAn element in one set
bAn element in another set (possible)
Returns
The representative of the unioned set

Definition at line 145 of file disjoint_sets.h.

References tpie::disjoint_sets< value_t >::find_set(), and tpie::disjoint_sets< value_t >::link().

145  {
146  return link(find_set(a), find_set(b));
147  }
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.

The documentation for this class was generated from the following file: