github.com/pulumi/terraform@v1.4.0/pkg/addrs/set.go (about) 1 package addrs 2 3 // Set represents a set of addresses of types that implement UniqueKeyer. 4 // 5 // Modify the set only by the methods on this type. This type exposes its 6 // internals for convenience during reading, such as iterating over set elements 7 // by ranging over the map values, but making direct modifications could 8 // potentially make the set data invalid or inconsistent, leading to undefined 9 // behavior elsewhere. 10 type Set[T UniqueKeyer] map[UniqueKey]T 11 12 func MakeSet[T UniqueKeyer](elems ...T) Set[T] { 13 ret := Set[T](make(map[UniqueKey]T, len(elems))) 14 for _, elem := range elems { 15 ret.Add(elem) 16 } 17 return ret 18 } 19 20 // Has returns true if and only if the set includes the given address. 21 func (s Set[T]) Has(addr T) bool { 22 _, exists := s[addr.UniqueKey()] 23 return exists 24 } 25 26 // Add inserts the given address into the set, if not already present. If 27 // an equivalent address is already in the set, this replaces that address 28 // with the new value. 29 func (s Set[T]) Add(addr T) { 30 s[addr.UniqueKey()] = addr 31 } 32 33 // Remove deletes the given address from the set, if present. If not present, 34 // this is a no-op. 35 func (s Set[T]) Remove(addr T) { 36 delete(s, addr.UniqueKey()) 37 } 38 39 // Union returns a new set which contains the union of all of the elements 40 // of both the reciever and the given other set. 41 func (s Set[T]) Union(other Set[T]) Set[T] { 42 ret := make(Set[T]) 43 for k, addr := range s { 44 ret[k] = addr 45 } 46 for k, addr := range other { 47 ret[k] = addr 48 } 49 return ret 50 } 51 52 // Intersection returns a new set which contains the intersection of all of the 53 // elements of both the reciever and the given other set. 54 func (s Set[T]) Intersection(other Set[T]) Set[T] { 55 ret := make(Set[T]) 56 for k, addr := range s { 57 if _, exists := other[k]; exists { 58 ret[k] = addr 59 } 60 } 61 for k, addr := range other { 62 if _, exists := s[k]; exists { 63 ret[k] = addr 64 } 65 } 66 return ret 67 }