MCPcopy Create free account
hub / github.com/SZAILAB/MaterialDFT-Demo / build_equivalent_groups

Function build_equivalent_groups

cpp_core/src/symmetry.cpp:152–190  ·  view source on GitHub ↗

Union-find for equivalent groups

Source from the content-addressed store, hash-verified

150
151// Union-find for equivalent groups
152std::vector<std::vector<int>> build_equivalent_groups(
153 const std::vector<std::vector<int>>& all_perms,
154 int n_atoms
155) {
156 std::vector<int> parent(n_atoms);
157 std::iota(parent.begin(), parent.end(), 0);
158
159 std::function<int(int)> find = [&](int x) -> int {
160 while (parent[x] != x) {
161 parent[x] = parent[parent[x]];
162 x = parent[x];
163 }
164 return x;
165 };
166
167 auto unite = [&](int a, int b) {
168 int ra = find(a), rb = find(b);
169 if (ra != rb) parent[rb] = ra;
170 };
171
172 for (const auto& perm : all_perms) {
173 for (int i = 0; i < static_cast<int>(perm.size()); ++i) {
174 unite(i, perm[i]);
175 }
176 }
177
178 std::map<int, std::vector<int>> groups;
179 for (int i = 0; i < n_atoms; ++i) {
180 groups[find(i)].push_back(i);
181 }
182
183 std::vector<std::vector<int>> result;
184 result.reserve(groups.size());
185 for (auto& [_, g] : groups) {
186 std::sort(g.begin(), g.end());
187 result.push_back(std::move(g));
188 }
189 return result;
190}
191
192// Positive modulo: ((x % n) + n) % n — always returns [0, n)
193int posmod(int x, int n) {

Callers 1

Calls 2

findFunction · 0.85
sizeMethod · 0.80

Tested by

no test coverage detected