...

Source file src/github.com/cli/go-gh/v2/internal/set/string_set.go

Documentation: github.com/cli/go-gh/v2/internal/set

     1  package set
     2  
     3  var exists = struct{}{}
     4  
     5  type stringSet struct {
     6  	v []string
     7  	m map[string]struct{}
     8  }
     9  
    10  func NewStringSet() *stringSet {
    11  	s := &stringSet{}
    12  	s.m = make(map[string]struct{})
    13  	s.v = []string{}
    14  	return s
    15  }
    16  
    17  func (s *stringSet) Add(value string) {
    18  	if s.Contains(value) {
    19  		return
    20  	}
    21  	s.m[value] = exists
    22  	s.v = append(s.v, value)
    23  }
    24  
    25  func (s *stringSet) AddValues(values []string) {
    26  	for _, v := range values {
    27  		s.Add(v)
    28  	}
    29  }
    30  
    31  func (s *stringSet) Remove(value string) {
    32  	if !s.Contains(value) {
    33  		return
    34  	}
    35  	delete(s.m, value)
    36  	s.v = sliceWithout(s.v, value)
    37  }
    38  
    39  func sliceWithout(s []string, v string) []string {
    40  	idx := -1
    41  	for i, item := range s {
    42  		if item == v {
    43  			idx = i
    44  			break
    45  		}
    46  	}
    47  	if idx < 0 {
    48  		return s
    49  	}
    50  	return append(s[:idx], s[idx+1:]...)
    51  }
    52  
    53  func (s *stringSet) RemoveValues(values []string) {
    54  	for _, v := range values {
    55  		s.Remove(v)
    56  	}
    57  }
    58  
    59  func (s *stringSet) Contains(value string) bool {
    60  	_, c := s.m[value]
    61  	return c
    62  }
    63  
    64  func (s *stringSet) Len() int {
    65  	return len(s.m)
    66  }
    67  
    68  func (s *stringSet) ToSlice() []string {
    69  	return s.v
    70  }
    71  

View as plain text