Mercurial > hg > orthanc-stone
comparison OrthancStone/Resources/Graveyard/RTStructTentativeReimplementation-BGO/DisjointDataSet.h @ 1908:affde38b84de
moved tentative bgo reimplementation of rt-struct into graveyard
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 01 Feb 2022 08:38:32 +0100 |
parents | OrthancStone/Sources/Toolbox/DisjointDataSet.h@7053b8a0aaec |
children | 07964689cb0b |
comparison
equal
deleted
inserted
replaced
1907:0208f99b8bde | 1908:affde38b84de |
---|---|
1 /** | |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium | |
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium | |
7 * | |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU Lesser General Public License | |
10 * as published by the Free Software Foundation, either version 3 of | |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Lesser General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Lesser General Public | |
19 * License along with this program. If not, see | |
20 * <http://www.gnu.org/licenses/>. | |
21 **/ | |
22 | |
23 #pragma once | |
24 | |
25 #include <vector> | |
26 | |
27 #include "../StoneException.h" | |
28 | |
29 namespace OrthancStone | |
30 { | |
31 class DisjointDataSet | |
32 { | |
33 public: | |
34 DisjointDataSet(size_t itemCount) : | |
35 parents_(itemCount), | |
36 ranks_(itemCount) | |
37 { | |
38 for (size_t index = 0; index < parents_.size(); index++) | |
39 { | |
40 SetParent(index,index); | |
41 ranks_[index] = 1; | |
42 } | |
43 } | |
44 | |
45 size_t Find(size_t item) | |
46 { | |
47 /* | |
48 If parents_[i] == i, it means i is representative of a set. | |
49 Otherwise, we go up the tree... | |
50 */ | |
51 if (GetParent(item) != item) | |
52 { | |
53 // if item is not a top item (representative of its set), | |
54 // we use path compression to improve future lookups | |
55 // see: https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Path_compression | |
56 SetParent(item, Find(parents_[item])); | |
57 } | |
58 | |
59 // now that paths have been compressed, we are positively certain | |
60 // that item's parent is a set ("X is a set" means that X is the | |
61 // representative of a set) | |
62 return GetParent(item); | |
63 } | |
64 | |
65 /* | |
66 This merge the two sets that contains itemA and itemB | |
67 */ | |
68 void Union(size_t itemA, size_t itemB) | |
69 { | |
70 // Find current sets of x and y | |
71 size_t setA = Find(itemA); | |
72 size_t setB = Find(itemB); | |
73 | |
74 // if setA == setB, it means they are already in the same set and | |
75 // do not need to be merged! | |
76 if (setA != setB) | |
77 { | |
78 // we need to merge the sets, which means that the trees representing | |
79 // the sets needs to be merged (there must be a single top parent to | |
80 // all the items originally belonging to setA and setB must be the same) | |
81 | |
82 // since the algorithm speed is inversely proportional to the tree | |
83 // height (the rank), we need to combine trees in a way that | |
84 // minimizes this rank. See "Union by rank" at | |
85 // https://en.wikipedia.org/wiki/Disjoint-set_data_structure#by_rank | |
86 if (GetRank(setA) < GetRank(setB)) | |
87 { | |
88 SetParent(setA, setB); | |
89 } | |
90 else if (GetRank(setA) > GetRank(setB)) | |
91 { | |
92 SetParent(setB, setA); | |
93 } | |
94 else | |
95 { | |
96 SetParent(setB, setA); | |
97 BumpRank(setA); | |
98 // the trees had the same height but we attached the whole of setB | |
99 // under setA (under its parent), so the resulting tree is now | |
100 // 1 higher. setB is NOT representative of a set anymore. | |
101 } | |
102 } | |
103 } | |
104 | |
105 private: | |
106 size_t GetRank(size_t i) const | |
107 { | |
108 ORTHANC_ASSERT(i < ranks_.size()); | |
109 ORTHANC_ASSERT(ranks_.size() == parents_.size()); | |
110 return ranks_[i]; | |
111 } | |
112 | |
113 size_t GetParent(size_t i) const | |
114 { | |
115 ORTHANC_ASSERT(i < parents_.size()); | |
116 ORTHANC_ASSERT(ranks_.size() == parents_.size()); | |
117 return parents_[i]; | |
118 } | |
119 | |
120 void SetParent(size_t i, size_t parent) | |
121 { | |
122 ORTHANC_ASSERT(i < parents_.size()); | |
123 ORTHANC_ASSERT(ranks_.size() == parents_.size()); | |
124 parents_[i] = parent; | |
125 } | |
126 | |
127 void BumpRank(size_t i) | |
128 { | |
129 ORTHANC_ASSERT(i < ranks_.size()); | |
130 ORTHANC_ASSERT(ranks_.size() == parents_.size()); | |
131 ranks_[i] = ranks_[i] + 1u; | |
132 } | |
133 | |
134 /* | |
135 This vector contains the direct parent of each item | |
136 */ | |
137 std::vector<size_t> parents_; | |
138 | |
139 /* | |
140 This vector contains the tree height of each set. The values in the | |
141 vector for non-representative items is UNDEFINED! | |
142 */ | |
143 std::vector<size_t> ranks_; | |
144 }; | |
145 | |
146 } |