Merge branch 'set' into 'main'
add StrSet type See merge request CRThaze/sugar!2
This commit is contained in:
commit
faba797beb
29
sugar.go
29
sugar.go
@ -137,3 +137,32 @@ func CheckExit(err error) {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Sets
|
||||
*/
|
||||
|
||||
// StrSet a set type for strings.
|
||||
type StrSet map[string]struct{}
|
||||
|
||||
// Add adds a new key to the set if it does not already exist.
|
||||
func (s *StrSet) Add(str string) {
|
||||
(*s)[str] = struct{}{}
|
||||
}
|
||||
|
||||
// Has checks if the given string is present in the set.
|
||||
func (s *StrSet) Has(str string) bool {
|
||||
if _, ok := (*s)[str]; ok {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// AddHas behaves like Add, but returns true if the key already existed.
|
||||
func (s *StrSet) AddHas(str string) bool {
|
||||
if ok := s.Has(str); ok {
|
||||
return ok
|
||||
}
|
||||
s.Add(str)
|
||||
return false
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user