File: union_find_debug.hpp

package info (click to toggle)
terraphast 0.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 844 kB
  • sloc: cpp: 5,923; sh: 92; ansic: 55; makefile: 27
file content (82 lines) | stat: -rw-r--r-- 2,042 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef UNION_FIND_DEBUG_HPP
#define UNION_FIND_DEBUG_HPP

#include "union_find.hpp"

namespace terraces {
namespace debug {

class union_find_sets_iterator {
private:
	const union_find& m_sets;
	index_t m_i;

	index_t next_rep(index_t i) const {
		index_t j = i;
		for (; j < m_sets.size(); ++j) {
			if (m_sets.is_representative(j)) {
				break;
			}
		}
		return j;
	}

public:
	union_find_sets_iterator(const union_find& sets, index_t i = 0)
	        : m_sets{sets}, m_i{next_rep(i)} {}

	index_t operator*() const { return m_i; }
	union_find_sets_iterator& operator++() {
		m_i = next_rep(m_i + 1);
		return *this;
	}
	bool operator==(const union_find_sets_iterator& other) const { return m_i == other.m_i; }
	bool operator!=(const union_find_sets_iterator& other) const { return !(*this == other); }
};

struct union_find_iterable_sets {
	const union_find& sets;
	union_find_sets_iterator begin() const { return {sets}; }
	union_find_sets_iterator end() const { return {sets, sets.size()}; }
};

class union_find_set_iterator {
private:
	const union_find& m_sets;
	const index_t m_rep;
	index_t m_i;

	index_t next(index_t i) const {
		index_t j = i;
		for (; j < m_sets.size(); ++j) {
			if (m_sets.simple_find(j) == m_rep) {
				break;
			}
		}
		return j;
	}

public:
	union_find_set_iterator(const union_find& sets, index_t rep, index_t i = 0)
	        : m_sets{sets}, m_rep{rep}, m_i{next(i)} {}

	index_t operator*() const { return m_i; }
	union_find_set_iterator& operator++() {
		m_i = next(m_i + 1);
		return *this;
	}
	bool operator==(const union_find_set_iterator& other) const { return m_i == other.m_i; }
	bool operator!=(const union_find_set_iterator& other) const { return !(*this == other); }
};

struct union_find_iterable_set {
	const union_find& sets;
	index_t representative;
	union_find_set_iterator begin() const { return {sets, representative}; }
	union_find_set_iterator end() const { return {sets, representative, sets.size()}; }
};

} // namespace debug
} // namespace terraces

#endif // UNION_FIND_DEBUG_HPP