Mantid
Loading...
Searching...
No Matches
DisjointElement.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9namespace Mantid::Crystal {
10
14DisjointElement::DisjointElement() : m_parent(this), m_rank(0), m_id(-1) {}
15
20DisjointElement::DisjointElement(const int id) : m_parent(this), m_rank(0), m_id(id) {}
21
27 : m_parent(other.m_parent), m_rank(other.m_rank), m_id(other.m_id) {
28
29 // Don't point to copy object as parent if copy object is it's own parent.
30 if (other.m_parent == &other) {
31 m_parent = this;
32 }
33}
34
41 if (this != &other) {
42 m_parent = other.m_parent;
43 m_rank = other.m_rank;
44 m_id = other.m_id;
45 }
46 return *this;
47}
48
53int DisjointElement::getId() const { return m_id; }
54
60
69 while (temp->hasParent()) {
70 temp = temp->getParent();
71 }
72 m_parent = temp;
73 return m_parent->getRoot();
74}
75
80bool DisjointElement::hasParent() const { return m_parent != this; }
81
87 if (m_parent == this) {
88 return m_id;
89 } else {
90 return m_parent->getRoot();
91 }
92}
93
99
105
110int DisjointElement::getRank() const { return m_rank; }
111
117bool DisjointElement::isEmpty() const { return m_id == -1; }
118
128
129 if (other->getRoot() != this->getRoot()) // Check sets do not already have the
130 // same root before continuing
131 {
132 this->compress();
133 other->compress();
134
135 DisjointElement *x = this->getParent();
136 DisjointElement *y = other->getParent();
137
138 if (x->getRank() > y->getRank()) {
139 y->setParent(x);
140 } else {
141 x->setParent(y);
142 if (x->getRank() == y->getRank()) {
143 y->incrementRank();
144 }
145 }
146 }
147}
148
155
160void DisjointElement::setId(int id) { m_id = id; }
161
162} // namespace Mantid::Crystal
DisjointElement : Cluster item used in a disjoint-set data structure.
void unionWith(DisjointElement *other)
Union with other.
DisjointElement * getParent() const
Get parent element.
int getRank() const
Get the current rank.
int getRoot() const
Get root id.
int incrementRank()
Increment the rank.
DisjointElement()
Default constructor.
int compress()
Compress the tree such that element's parent becomes it's root parent.
bool hasParent() const
Determine if this instance really has a parent or whether it is a singleton.
DisjointElement * m_parent
Parent element.
void setParent(DisjointElement *other)
Setter for the parent element.
DisjointElement & operator=(const DisjointElement &other)
Assignment operator.
void unionElements(DisjointElement *a, DisjointElement *b)
Convenience non-member function.