| 1 | package setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/ |
| 2 | |
| 3 | import ( |
| 4 | "sortscip-go gomod github.com/golang/go/src go1.18 sort/" |
| 5 | ) |
| 6 | |
| 7 | // Add inserts the given value into the receiving Set. |
| 8 | // |
| 9 | // This mutates the set in-place. This operation is not thread-safe. |
| 10 | func (s Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T]) Addscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Add().(val T) { |
| 11 | hv := s.rulesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#rules..Hashscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Rules#Hash.(val) |
| 12 | if _, ok := s.valsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#vals.[hv]; !ok { |
| 13 | s.valsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#vals.[hv] = make([]T, 0, 1) |
| 14 | } |
| 15 | bucket := s.valsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#vals.[hv] |
| 16 | |
| 17 | // See if an equivalent value is already present |
| 18 | for _, ev := range bucket { |
| 19 | if s.rulesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#rules..Equivalentscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Rules#Equivalent.(val, ev) { |
| 20 | return |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | s.valsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#vals.[hv] = append(bucket, val) |
| 25 | } |
| 26 | |
| 27 | // Remove deletes the given value from the receiving set, if indeed it was |
| 28 | // there in the first place. If the value is not present, this is a no-op. |
| 29 | func (s Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T]) Removescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Remove().(val T) { |
| 30 | hv := s.rulesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#rules..Hashscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Rules#Hash.(val) |
| 31 | bucket, ok := s.valsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#vals.[hv] |
| 32 | if !ok { |
| 33 | return |
| 34 | } |
| 35 | |
| 36 | for i, ev := range bucket { |
| 37 | if s.rulesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#rules..Equivalentscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Rules#Equivalent.(val, ev) { |
| 38 | newBucket := make([]T, 0, len(bucket)-1) |
| 39 | newBucket = append(newBucket, bucket[:i]...) |
| 40 | newBucket = append(newBucket, bucket[i+1:]...) |
| 41 | if len(newBucket) > 0 { |
| 42 | s.valsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#vals.[hv] = newBucket |
| 43 | } else { |
| 44 | delete(s.valsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#vals., hv) |
| 45 | } |
| 46 | return |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // Has returns true if the given value is in the receiving set, or false if |
| 52 | // it is not. |
| 53 | func (s Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T]) Hasscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Has().(val T) bool { |
| 54 | hv := s.rulesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#rules..Hashscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Rules#Hash.(val) |
| 55 | bucket, ok := s.valsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#vals.[hv] |
| 56 | if !ok { |
| 57 | return false |
| 58 | } |
| 59 | |
| 60 | for _, ev := range bucket { |
| 61 | if s.rulesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#rules..Equivalentscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Rules#Equivalent.(val, ev) { |
| 62 | return true |
| 63 | } |
| 64 | } |
| 65 | return false |
| 66 | } |
| 67 | |
| 68 | // Copy performs a shallow copy of the receiving set, returning a new set |
| 69 | // with the same rules and elements. |
| 70 | func (s Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T]) Copyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Copy().() Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T] { |
| 71 | ret := NewSetscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/NewSet().(s.rulesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#rules.) |
| 72 | for k, v := range s.valsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#vals. { |
| 73 | ret.valsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#vals.[k] = v |
| 74 | } |
| 75 | return ret |
| 76 | } |
| 77 | |
| 78 | // Iterator returns an iterator over values in the set. If the set's rules |
| 79 | // implement OrderedRules then the result is ordered per those rules. If |
| 80 | // no order is provided, or if it is not a total order, then the iteration |
| 81 | // order is undefined but consistent for a particular version of cty. Do not |
| 82 | // rely on specific ordering between cty releases unless the rules order is a |
| 83 | // total order. |
| 84 | // |
| 85 | // The pattern for using the returned iterator is: |
| 86 | // |
| 87 | // it := set.Iterator() |
| 88 | // for it.Next() { |
| 89 | // val := it.Value() |
| 90 | // // ... |
| 91 | // } |
| 92 | // |
| 93 | // Once an iterator has been created for a set, the set *must not* be mutated |
| 94 | // until the iterator is no longer in use. |
| 95 | func (s Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T]) Iteratorscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Iterator().() *Iteratorscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Iterator#[T] { |
| 96 | vals := s.Valuesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Values().() |
| 97 | |
| 98 | return &Iteratorscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Iterator#[T]{ |
| 99 | valsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Iterator#vals.: vals, |
| 100 | idxscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Iterator#idx.: -1, |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // EachValue calls the given callback once for each value in the set, in an |
| 105 | // undefined order that callers should not depend on. |
| 106 | func (s Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T]) EachValuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#EachValue().(cb func(T)) { |
| 107 | it := s.Iteratorscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Iterator().() |
| 108 | for it.Nextscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Iterator#Next().() { |
| 109 | cb(it.Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Iterator#Value().()) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Values returns a slice of all the values in the set. If the set rules have |
| 114 | // an order then the result is in that order. If no order is provided or if |
| 115 | // it is not a total order then the result order is undefined, but consistent |
| 116 | // for a particular set value within a specific release of cty. |
| 117 | func (s Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T]) Valuesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Values().() []T { |
| 118 | var ret []T |
| 119 | // Sort the bucketIds to ensure that we always traverse in a |
| 120 | // consistent order. |
| 121 | bucketIDs := make([]int, 0, len(s.valsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#vals.)) |
| 122 | for id := range s.valsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#vals. { |
| 123 | bucketIDs = append(bucketIDs, id) |
| 124 | } |
| 125 | sortscip-go gomod github.com/golang/go/src go1.18 sort/.Intsscip-go gomod github.com/golang/go/src go1.18 sort/Ints().(bucketIDs) |
| 126 | |
| 127 | for _, bucketID := range bucketIDs { |
| 128 | ret = append(ret, s.valsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#vals.[bucketID]...) |
| 129 | } |
| 130 | |
| 131 | if orderRules, ok := s.rulesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#rules..(OrderedRulesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/OrderedRules#[T]); ok { |
| 132 | sortscip-go gomod github.com/golang/go/src go1.18 sort/.SliceStablescip-go gomod github.com/golang/go/src go1.18 sort/SliceStable().(ret, func(i, j int) bool { |
| 133 | return orderRules.Lessscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/OrderedRules#Less.(ret[i], ret[j]) |
| 134 | }) |
| 135 | } |
| 136 | |
| 137 | return ret |
| 138 | } |
| 139 | |
| 140 | // Length returns the number of values in the set. |
| 141 | func (s Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T]) Lengthscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Length().() int { |
| 142 | var count int |
| 143 | for _, bucket := range s.valsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#vals. { |
| 144 | count = count + len(bucket) |
| 145 | } |
| 146 | return count |
| 147 | } |
| 148 | |
| 149 | // Union returns a new set that contains all of the members of both the |
| 150 | // receiving set and the given set. Both sets must have the same rules, or |
| 151 | // else this function will panic. |
| 152 | func (s1 Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T]) Unionscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Union().(s2 Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T]) Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T] { |
| 153 | mustHaveSameRulesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/mustHaveSameRules().(s1, s2) |
| 154 | rs := NewSetscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/NewSet().(s1.rulesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#rules.) |
| 155 | s1.EachValuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#EachValue().(func(v T) { |
| 156 | rs.Addscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Add().(v) |
| 157 | }) |
| 158 | s2.EachValuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#EachValue().(func(v T) { |
| 159 | rs.Addscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Add().(v) |
| 160 | }) |
| 161 | return rs |
| 162 | } |
| 163 | |
| 164 | // Intersection returns a new set that contains the values that both the |
| 165 | // receiver and given sets have in common. Both sets must have the same rules, |
| 166 | // or else this function will panic. |
| 167 | func (s1 Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T]) Intersectionscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Intersection().(s2 Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T]) Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T] { |
| 168 | mustHaveSameRulesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/mustHaveSameRules().(s1, s2) |
| 169 | rs := NewSetscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/NewSet().(s1.rulesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#rules.) |
| 170 | s1.EachValuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#EachValue().(func(v T) { |
| 171 | if s2.Hasscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Has().(v) { |
| 172 | rs.Addscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Add().(v) |
| 173 | } |
| 174 | }) |
| 175 | return rs |
| 176 | } |
| 177 | |
| 178 | // Subtract returns a new set that contains all of the values from the receiver |
| 179 | // that are not also in the given set. Both sets must have the same rules, |
| 180 | // or else this function will panic. |
| 181 | func (s1 Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T]) Subtractscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Subtract().(s2 Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T]) Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T] { |
| 182 | mustHaveSameRulesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/mustHaveSameRules().(s1, s2) |
| 183 | rs := NewSetscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/NewSet().(s1.rulesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#rules.) |
| 184 | s1.EachValuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#EachValue().(func(v T) { |
| 185 | if !s2.Hasscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Has().(v) { |
| 186 | rs.Addscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Add().(v) |
| 187 | } |
| 188 | }) |
| 189 | return rs |
| 190 | } |
| 191 | |
| 192 | // SymmetricDifference returns a new set that contains all of the values from |
| 193 | // both the receiver and given sets, except those that both sets have in |
| 194 | // common. Both sets must have the same rules, or else this function will |
| 195 | // panic. |
| 196 | func (s1 Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T]) SymmetricDifferencescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#SymmetricDifference().(s2 Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T]) Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#[T] { |
| 197 | mustHaveSameRulesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/mustHaveSameRules().(s1, s2) |
| 198 | rs := NewSetscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/NewSet().(s1.rulesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#rules.) |
| 199 | s1.EachValuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#EachValue().(func(v T) { |
| 200 | if !s2.Hasscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Has().(v) { |
| 201 | rs.Addscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Add().(v) |
| 202 | } |
| 203 | }) |
| 204 | s2.EachValuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#EachValue().(func(v T) { |
| 205 | if !s1.Hasscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Has().(v) { |
| 206 | rs.Addscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/set`/Set#Add().(v) |
| 207 | } |
| 208 | }) |
| 209 | return rs |
| 210 | } |