0.2.0-dev Download ZIP
140 lines ·4.3 KB Raw
| 1 | // from https://github.com/wjakob/dset, changed to add connected component |
| 2 | // computation |
| 3 | // |
| 4 | // Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch> |
| 5 | // |
| 6 | // This software is provided 'as-is', without any express or implied |
| 7 | // warranty. In no event will the authors be held liable for any damages |
| 8 | // arising from the use of this software. |
| 9 | // |
| 10 | // Permission is granted to anyone to use this software for any purpose, |
| 11 | // including commercial applications, and to alter it and redistribute it |
| 12 | // freely, subject to the following restrictions: |
| 13 | // |
| 14 | // 1. The origin of this software must not be misrepresented; you must not |
| 15 | // claim that you wrote the original software. If you use this software |
| 16 | // in a product, an acknowledgment in the product documentation would be |
| 17 | // appreciated but is not required. |
| 18 | // 2. Altered source versions must be plainly marked as such, and must not be |
| 19 | // misrepresented as being the original software. |
| 20 | // 3. This notice may not be removed or altered from any source distribution. |
| 21 | // |
| 22 | #pragma once |
| 23 | #include <atomic> |
| 24 | #include <cassert> |
| 25 | #include <cstddef> |
| 26 | #include <cstdint> |
| 27 | #include <limits> |
| 28 | #include <unordered_map> |
| 29 | #include <vector> |
| 30 | |
| 31 | #include "parallel.h" |
| 32 | |
| 33 | class DisjointSets { |
| 34 | public: |
| 35 | DisjointSets(size_t size) : mData(size) { |
| 36 | assert(size <= std::numeric_limits<uint32_t>::max()); |
| 37 | using namespace manifold; |
| 38 | for_each(autoPolicy(size), countAt(0_uz), countAt(size), |
| 39 | [this](size_t i) { mData[i] = static_cast<uint32_t>(i); }); |
| 40 | } |
| 41 | |
| 42 | size_t find(size_t id) const { return findImpl(ToIndex(id)); } |
| 43 | |
| 44 | bool same(size_t id1In, size_t id2In) const { |
| 45 | uint32_t id1 = ToIndex(id1In); |
| 46 | uint32_t id2 = ToIndex(id2In); |
| 47 | for (;;) { |
| 48 | id1 = findImpl(id1); |
| 49 | id2 = findImpl(id2); |
| 50 | if (id1 == id2) return true; |
| 51 | if (parent(id1) == id1) return false; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | size_t unite(size_t id1In, size_t id2In) { |
| 56 | uint32_t id1 = ToIndex(id1In); |
| 57 | uint32_t id2 = ToIndex(id2In); |
| 58 | for (;;) { |
| 59 | id1 = findImpl(id1); |
| 60 | id2 = findImpl(id2); |
| 61 | |
| 62 | if (id1 == id2) return id1; |
| 63 | |
| 64 | uint32_t r1 = rank(id1), r2 = rank(id2); |
| 65 | |
| 66 | if (r1 > r2 || (r1 == r2 && id1 < id2)) { |
| 67 | std::swap(r1, r2); |
| 68 | std::swap(id1, id2); |
| 69 | } |
| 70 | |
| 71 | uint64_t oldEntry = ((uint64_t)r1 << 32) | id1; |
| 72 | uint64_t newEntry = ((uint64_t)r1 << 32) | id2; |
| 73 | |
| 74 | if (!mData[id1].compare_exchange_strong(oldEntry, newEntry)) continue; |
| 75 | |
| 76 | if (r1 == r2) { |
| 77 | oldEntry = ((uint64_t)r2 << 32) | id2; |
| 78 | newEntry = ((uint64_t)(r2 + 1) << 32) | id2; |
| 79 | /* Try to update the rank (may fail, retry if rank = 0) */ |
| 80 | if (!mData[id2].compare_exchange_strong(oldEntry, newEntry) && r2 == 0) |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | break; |
| 85 | } |
| 86 | return id2; |
| 87 | } |
| 88 | |
| 89 | size_t size() const { return mData.size(); } |
| 90 | |
| 91 | uint32_t rank(uint32_t id) const { |
| 92 | return ((uint32_t)(mData[id] >> 32)) & 0x7FFFFFFFu; |
| 93 | } |
| 94 | |
| 95 | uint32_t parent(uint32_t id) const { return (uint32_t)mData[id]; } |
| 96 | |
| 97 | int connectedComponents(std::vector<int>& components) { |
| 98 | components.resize(mData.size()); |
| 99 | int lonelyNodes = 0; |
| 100 | std::unordered_map<uint32_t, int> toLabel; |
| 101 | for (size_t i = 0; i < mData.size(); ++i) { |
| 102 | // we optimize for connected component of size 1 |
| 103 | // no need to put them into the hashmap |
| 104 | uint32_t iParent = findImpl(ToIndex(i)); |
| 105 | if (rank(iParent) == 0) { |
| 106 | components[i] = static_cast<int>(toLabel.size()) + lonelyNodes++; |
| 107 | continue; |
| 108 | } |
| 109 | auto iter = toLabel.find(iParent); |
| 110 | if (iter == toLabel.end()) { |
| 111 | auto s = static_cast<uint32_t>(toLabel.size()) + lonelyNodes; |
| 112 | toLabel.insert(std::make_pair(iParent, s)); |
| 113 | components[i] = s; |
| 114 | } else { |
| 115 | components[i] = iter->second; |
| 116 | } |
| 117 | } |
| 118 | return toLabel.size() + lonelyNodes; |
| 119 | } |
| 120 | |
| 121 | mutable std::vector<std::atomic<uint64_t>> mData; |
| 122 | |
| 123 | private: |
| 124 | static uint32_t ToIndex(size_t id) { |
| 125 | assert(id <= std::numeric_limits<uint32_t>::max()); |
| 126 | return static_cast<uint32_t>(id); |
| 127 | } |
| 128 | |
| 129 | uint32_t findImpl(uint32_t id) const { |
| 130 | while (id != parent(id)) { |
| 131 | uint64_t value = mData[id]; |
| 132 | uint32_t new_parent = parent((uint32_t)value); |
| 133 | uint64_t new_value = (value & 0xFFFFFFFF00000000ULL) | new_parent; |
| 134 | /* Try to update parent (may fail, that's ok) */ |
| 135 | if (value != new_value) mData[id].compare_exchange_weak(value, new_value); |
| 136 | id = new_parent; |
| 137 | } |
| 138 | return id; |
| 139 | } |
| 140 | }; |