| 1 | // Mgmt |
| 2 | // Copyright (C) 2013-2024+ James Shubin and the project contributors |
| 3 | // Written by James Shubin <[email protected]> and the project contributors |
| 4 | // |
| 5 | // This program is free software: you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU General Public License as published by |
| 7 | // the Free Software Foundation, either version 3 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // This program is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License |
| 16 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | // |
| 18 | // Additional permission under GNU GPL version 3 section 7 |
| 19 | // |
| 20 | // If you modify this program, or any covered work, by linking or combining it |
| 21 | // with embedded mcl code and modules (and that the embedded mcl code and |
| 22 | // modules which link with this program, contain a copy of their source code in |
| 23 | // the authoritative form) containing parts covered by the terms of any other |
| 24 | // license, the licensors of this program grant you additional permission to |
| 25 | // convey the resulting work. Furthermore, the licensors of this program grant |
| 26 | // the original author, James Shubin, additional permission to update this |
| 27 | // additional permission if he deems it necessary to achieve the goals of this |
| 28 | // additional permission. |
| 29 | |
| 30 | // Package disjoint implements a "disjoint-set data structure", otherwise known |
| 31 | // as the "union–find data structure" which is commonly used for type |
| 32 | // unification, among other things. |
| 33 | // |
| 34 | // You create new elements with the NewElem function, which returns an Elem that |
| 35 | // has both Union and Find methods available. Each Elem can store some typed |
| 36 | // data associated with it, and as a result, this uses golang generics. |
| 37 | // |
| 38 | // The Union method merges two elements into the same set. The Find method picks |
| 39 | // a representative element for that set. Unless you change the contents of a |
| 40 | // set, and in the steady-state of a set, any elements Find method will always |
| 41 | // return the same representative element for that set, and so this can be used |
| 42 | // to build the IsConnected function quite easily. |
| 43 | // |
| 44 | // The Merge and UnsafeMerge functions can be used to run the Union method for |
| 45 | // two elements, with a way to update the data for the representative element in |
| 46 | // the set. This is useful if you want to keep track of a representative piece |
| 47 | // of data for the set, such as the data type of that set if you were using this |
| 48 | // for type unification. |
| 49 | // |
| 50 | // This package does not attempt to be thread-safe, and as a result, make sure |
| 51 | // to wrap this with the synchronization primitives of your choosing. |
| 52 | // |
| 53 | // This package was built by examining wikipedia and other sources, and contains |
| 54 | // some short excerpts from the wikipedia page. |
| 55 | // |
| 56 | // https://en.wikipedia.org/wiki/Disjoint-set_data_structure |
| 57 | package disjointscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/ |
| 58 | |
| 59 | // TODO: This package needs more tests! |
| 60 | |
| 61 | // NewElem creates a new set with one element and returns the sole element (the |
| 62 | // representative element) of that set. |
| 63 | func NewElemscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/NewElem().[T any]() *Elemscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#[T] { |
| 64 | obj := &Elemscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#[T]{} |
| 65 | obj.parentscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#parent. = obj // initially point to self |
| 66 | return obj |
| 67 | } |
| 68 | |
| 69 | // Elem is the "node" or "element" type for objects contained in the set. It has |
| 70 | // a single Data field which can be used to store some user data. |
| 71 | type Elemscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#[T any] struct { |
| 72 | // Data is some data that the user might want to store with this element. |
| 73 | Datascip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Data. T |
| 74 | |
| 75 | // parent is the parent element that we link to. This points to ourself |
| 76 | // if we are the root (representative element) of the set. |
| 77 | parentscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#parent. *Elemscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#[T] |
| 78 | |
| 79 | // rank is used for union by rank, a node stores its rank, which is an |
| 80 | // upper bound for its height. When a node is initialized, its rank is |
| 81 | // set to zero. To merge trees with roots x and y, first compare their |
| 82 | // ranks. If the ranks are different, then the larger rank tree becomes |
| 83 | // the parent, and the ranks of x and y do not change. If the ranks are |
| 84 | // the same, then either one can become the parent, but the new parent's |
| 85 | // rank is incremented by one. While the rank of a node is clearly |
| 86 | // related to its height, storing ranks is more efficient than storing |
| 87 | // heights. The height of a node can change during a Find operation, so |
| 88 | // storing ranks avoids the extra effort of keeping the height correct. |
| 89 | rankscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#rank. int |
| 90 | } |
| 91 | |
| 92 | // Union combines two elements into the same set. If the elements are already |
| 93 | // part of the same set, then nothing changes. |
| 94 | func (obj *Elemscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#[T]) Unionscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Union().(elem *Elemscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#[T]) { |
| 95 | root1 := obj.Findscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Find().() |
| 96 | root2 := elem.Findscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Find().() |
| 97 | if root1 == root2 { |
| 98 | return // already part of the same union, do nothing |
| 99 | } |
| 100 | |
| 101 | // Create the union based on the relative rank's of the two elements. |
| 102 | // The larger ranked element becomes the new parent. If two elements |
| 103 | // have the same rank, then we arbitrarily make one of them the new |
| 104 | // parent. Keep in mind that this third case *does* change the |
| 105 | // representative value of the set, this expectation of constancy is |
| 106 | // only true in the steady state. |
| 107 | switch { |
| 108 | case root1.rankscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#rank. < root2.rankscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#rank.: |
| 109 | root1.parentscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#parent. = root2 |
| 110 | case root1.rankscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#rank. > root2.rankscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#rank.: |
| 111 | root2.parentscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#parent. = root1 |
| 112 | default: |
| 113 | root1.rankscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#rank.++ // starts at the zero value of 0 if uninitialized |
| 114 | root2.parentscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#parent. = root1 |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Find returns the representative element of the set. The same element will |
| 119 | // always be returned when this is called on any element in that same set. You |
| 120 | // can use the IsConnected function to determine if two elements are in the same |
| 121 | // set. |
| 122 | func (obj *Elemscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#[T]) Findscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Find().() *Elemscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#[T] { |
| 123 | for obj != obj.parentscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#parent. { // search until we reach the sentinel root value |
| 124 | // path compression! |
| 125 | obj.parentscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#parent. = obj.parentscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#parent..parentscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#parent. |
| 126 | obj = obj.parentscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#parent. |
| 127 | } |
| 128 | return obj |
| 129 | } |
| 130 | |
| 131 | // IsConnected returns true if the two elements are part of the same set. Since |
| 132 | // any set must return the same representative element for it, by comparing this |
| 133 | // value for each element, we can determine if they're connected. |
| 134 | func IsConnectedscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/IsConnected().[T any](elem1, elem2 *Elemscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#[T]) bool { |
| 135 | return elem1.Findscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Find().() == elem2.Findscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Find().() |
| 136 | } |
| 137 | |
| 138 | // Merge is exactly like UnsafeMerge, except it calls Find on each element first |
| 139 | // so that we merge the Data associated with the representative elements only. |
| 140 | // This is almost always what you want. |
| 141 | func Mergescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Merge().[T any](elem1, elem2 *Elemscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#[T], merge func(T, T) (T, error)) error { |
| 142 | return UnsafeMergescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/UnsafeMerge().(elem1.Findscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Find().(), elem2.Findscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Find().(), merge) |
| 143 | } |
| 144 | |
| 145 | // UnsafeMerge runs the Union operation on two elements. Before this happens, it |
| 146 | // runs a merge function which takes the data from each of those elements and |
| 147 | // gives it the opportunity to determine what the new resultant data should be. |
| 148 | // Note that either of the elements passed in might not be the representative |
| 149 | // element for their sets, so if you want to be sure to use the "already merged" |
| 150 | // data, then run Find on each element before passing it to this function. To do |
| 151 | // this automatically, use the regular Merge function instead. |
| 152 | func UnsafeMergescip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/UnsafeMerge().[T any](elem1, elem2 *Elemscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#[T], merge func(T, T) (T, error)) error { |
| 153 | data, err := merge(elem1.Datascip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Data., elem2.Datascip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Data.) // compute the merged data |
| 154 | if err != nil { |
| 155 | return err |
| 156 | } |
| 157 | |
| 158 | elem1.Unionscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Union().(elem2) // union always succeeds |
| 159 | elem := elem1.Findscip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Find().() // get the new representative element |
| 160 | elem.Datascip-go gomod github.com/purpleidea/mgmt . `github.com/purpleidea/mgmt/util/disjoint`/Elem#Data. = data // store the previously merged data in that element |
| 161 | |
| 162 | return nil |
| 163 | } |