9 template <
typename T,
typename Allocator = PolymorphicAllocator<T>>
26 using allocator_type = std::allocator_traits<Allocator>::template rebind_alloc<Node>;
51 noexcept(
noexcept(
container_type(std::move(rhs.data_), allocator)))
79 template <std::forward_iterator It>
93 :
DisjointSet(list.begin(), list.end(), allocator) {
100 return data_.get_allocator();
107 constexpr std::size_t
size()
const {
120 return data_.empty();
131 constexpr void add(
const T& value) {
138 constexpr void add(T&& value) {
147 return data_[i].value;
154 return data_[i].value;
161 constexpr std::size_t
find(std::size_t i) {
163 if (
data_[i].groupId == i) {
182 constexpr bool merge(std::size_t i, std::size_t j) {
183 std::size_t p1 =
find(i);
184 std::size_t p2 =
find(j);
190 data_[p1].groupId = p2;
195 data_[p2].groupId = p1;
A disjoint union set that uses path compression and union by rank.
constexpr T & operator[](std::size_t i)
Get the value of the i-th element.
constexpr std::size_t find(std::size_t i)
Find the group id of the i-th node. Perform path compression along the way.
constexpr bool isSameGroup(std::size_t i, std::size_t j)
Check if the i-th element and the j-th element are in the same group.
constexpr void add(const T &value)
Add an element to the disjoint set.
void reserve(std::size_t n)
Reserve enough memory for at least n elements.
std::allocator_traits< Allocator >::template rebind_alloc< Node > allocator_type
constexpr bool empty() const
Check if the disjoint set is empty.
constexpr DisjointSet(const allocator_type &allocator) noexcept(noexcept(container_type(allocator)))
constexpr DisjointSet() noexcept(noexcept(container_type()))
std::ptrdiff_t difference_type
constexpr DisjointSet(DisjointSet &&rhs, const allocator_type &allocator) noexcept(noexcept(container_type(std::move(rhs.data_), allocator)))
constexpr std::size_t groupSize() const
Return the number of group.
constexpr DisjointSet(const DisjointSet &)=default
constexpr void add(T &&value)
Add an element to the disjoint set.
constexpr DisjointSet(It first, It last, const allocator_type &allocator={})
Iterator range constructor.
allocator_type get_allocator() const noexcept
constexpr const T & operator[](std::size_t i) const
Get the value of the i-th element.
const T & const_reference
constexpr DisjointSet(const DisjointSet &rhs, const allocator_type &allocator)
constexpr DisjointSet(DisjointSet &&)=default
constexpr std::size_t size() const
Return the number of input elements.
constexpr DisjointSet(std::initializer_list< T > list, const allocator_type &allocator={})
Initializer list constructor.
constexpr bool merge(std::size_t i, std::size_t j)
Merge the i-th element's group with the j-th element's group. Internally, it unions them by ranking.
Vector< Node, allocator_type > container_type
void pushBack(const T &value)
Adds an element to the end of the vector.
void reserve(std::size_t capacity)
Ensures the vector can hold at least the given capacity without reallocating.