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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* This file is part of the source code of the software program
* Vampire. It is protected by applicable
* copyright laws.
*
* This source code is distributed under the licence found here
* https://vprover.github.io/license.html
* and in the source directory
*/
/**
* @file IntUnionFind.hpp
* Defines class IntUnionFind implementing union-find algorithm for integers.
*/
#ifndef __IntUnionFind__
#define __IntUnionFind__
#include "Reflection.hpp"
#include "Stack.hpp"
namespace Lib {
class IntUnionFind {
public:
IntUnionFind(int cnt);
~IntUnionFind();
bool doUnion(int c1, int c2);
int root(int c) const;
void reset();
void evalComponents();
int getComponentCount();
private:
int _cnt;
bool _modified;
/**
* @b _parents[c] contains number of the parent element of @b c -th
* element. If @b _parents[c]==-1, c is the root element of its
* component.
*
* Invariant: The root element is always the one with the
* lowest number in the component.
*
* Is mutable to allow path compression in the @c root function which
* is const.
*/
mutable int* _parents;
/**
* After call to the @b finish() method contains data about how
* elements are connected into components.
*
* @b _data contains components in the form of linked lists.
* @b _data[c] contains number of the next element in the same
* component as @b c -th one, or -1 if there is no such.
*/
int* _data;
Stack<int> _components;
public:
class ComponentIterator;
class ElementIterator
{
public:
DECL_ELEMENT_TYPE(int);
bool hasNext() { return _next!=-1; }
int next()
{
ASS_NEQ(_next,-1);
int res=_next;
_next=_data[_next];
return res;
}
private:
ElementIterator(int first, const int* data)
: _next(first), _data(data) {}
int _next;
const int* _data;
friend class ComponentIterator;
};
/**
* Iterator over iterators on component
*
* Calling the @b doUnion function does not affect the iterator
* as long as the @b evalComponents function is not called
*/
class ComponentIterator
{
public:
DECL_ELEMENT_TYPE(ElementIterator);
/**
* Construct the iterator
*
* The @b evalComponents function must be called before
* this constructor is called (and if the @b doUnion is called
* later, the @b evalComponents has to be called again).
*/
ComponentIterator(const IntUnionFind& obj)
: _cit(obj._components), _data(obj._data)
{
ASS(!obj._modified); //the evalComponents function must have been called before
}
bool hasNext() { return _cit.hasNext(); }
ElementIterator next() { return ElementIterator(_cit.next(), _data); }
private:
Stack<int>::ConstIterator _cit;
const int* _data;
};
};
}
#endif /* __IntUnionFind__ */