github.com/kubeshop/testkube@v1.17.23/pkg/data/set/set.go (about)

     1  package set
     2  
     3  // Set implements a Set, using the go map as the underlying storage.
     4  type Set[K comparable] struct {
     5  	storage map[K]struct{}
     6  }
     7  
     8  // New returns an empty Set.
     9  func New[K comparable]() Set[K] {
    10  	return Set[K]{
    11  		storage: make(map[K]struct{}),
    12  	}
    13  }
    14  
    15  // Of returns a new Set initialized with the given 'vals'
    16  func Of[K comparable](vals ...K) Set[K] {
    17  	s := New[K]()
    18  	for _, val := range vals {
    19  		s.Put(val)
    20  	}
    21  	return s
    22  }
    23  
    24  // Put adds 'val' to the set.
    25  func (s Set[K]) Put(val K) {
    26  	s.storage[val] = struct{}{}
    27  }
    28  
    29  // Has returns true only if 'val' is in the set.
    30  func (s Set[K]) Has(val K) bool {
    31  	_, ok := s.storage[val]
    32  	return ok
    33  }
    34  
    35  // Remove removes 'val' from the set.
    36  func (s Set[K]) Remove(val K) {
    37  	delete(s.storage, val)
    38  }
    39  
    40  // ToArray returns go slice
    41  func (s Set[K]) ToArray() (arr []K) {
    42  	for v := range s.storage {
    43  		arr = append(arr, v)
    44  	}
    45  	return arr
    46  }