2024-02-08 03:48:59 -06:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 10:33:06 -05:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2021-07-10 17:18:19 -05:00
|
|
|
package addrs
|
|
|
|
|
|
|
|
// Set represents a set of addresses of types that implement UniqueKeyer.
|
2022-06-13 12:35:02 -05:00
|
|
|
//
|
|
|
|
// Modify the set only by the methods on this type. This type exposes its
|
|
|
|
// internals for convenience during reading, such as iterating over set elements
|
|
|
|
// by ranging over the map values, but making direct modifications could
|
|
|
|
// potentially make the set data invalid or inconsistent, leading to undefined
|
|
|
|
// behavior elsewhere.
|
2022-06-13 13:56:53 -05:00
|
|
|
type Set[T UniqueKeyer] map[UniqueKey]T
|
2021-07-10 17:18:19 -05:00
|
|
|
|
2022-06-14 19:58:53 -05:00
|
|
|
func MakeSet[T UniqueKeyer](elems ...T) Set[T] {
|
|
|
|
ret := Set[T](make(map[UniqueKey]T, len(elems)))
|
|
|
|
for _, elem := range elems {
|
|
|
|
ret.Add(elem)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2022-06-13 12:35:02 -05:00
|
|
|
// Has returns true if and only if the set includes the given address.
|
2022-06-13 13:56:53 -05:00
|
|
|
func (s Set[T]) Has(addr T) bool {
|
2021-07-10 17:18:19 -05:00
|
|
|
_, exists := s[addr.UniqueKey()]
|
|
|
|
return exists
|
|
|
|
}
|
|
|
|
|
2022-06-13 12:35:02 -05:00
|
|
|
// Add inserts the given address into the set, if not already present. If
|
|
|
|
// an equivalent address is already in the set, this replaces that address
|
|
|
|
// with the new value.
|
2022-06-13 13:56:53 -05:00
|
|
|
func (s Set[T]) Add(addr T) {
|
2021-07-10 17:18:19 -05:00
|
|
|
s[addr.UniqueKey()] = addr
|
|
|
|
}
|
|
|
|
|
2022-06-13 12:35:02 -05:00
|
|
|
// Remove deletes the given address from the set, if present. If not present,
|
|
|
|
// this is a no-op.
|
2022-06-13 13:56:53 -05:00
|
|
|
func (s Set[T]) Remove(addr T) {
|
2021-07-10 17:18:19 -05:00
|
|
|
delete(s, addr.UniqueKey())
|
|
|
|
}
|
|
|
|
|
2022-06-13 12:35:02 -05:00
|
|
|
// Union returns a new set which contains the union of all of the elements
|
2024-08-29 12:20:33 -05:00
|
|
|
// of both the receiver and the given other set.
|
2022-06-13 12:35:02 -05:00
|
|
|
func (s Set[T]) Union(other Set[T]) Set[T] {
|
|
|
|
ret := make(Set[T])
|
2021-07-10 17:18:19 -05:00
|
|
|
for k, addr := range s {
|
|
|
|
ret[k] = addr
|
|
|
|
}
|
|
|
|
for k, addr := range other {
|
|
|
|
ret[k] = addr
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2022-06-13 12:35:02 -05:00
|
|
|
// Intersection returns a new set which contains the intersection of all of the
|
2024-08-29 12:20:33 -05:00
|
|
|
// elements of both the receiver and the given other set.
|
2022-06-13 12:35:02 -05:00
|
|
|
func (s Set[T]) Intersection(other Set[T]) Set[T] {
|
|
|
|
ret := make(Set[T])
|
2021-07-10 17:18:19 -05:00
|
|
|
for k, addr := range s {
|
|
|
|
if _, exists := other[k]; exists {
|
|
|
|
ret[k] = addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, addr := range other {
|
|
|
|
if _, exists := s[k]; exists {
|
|
|
|
ret[k] = addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|