1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-11 16:22:26 +08:00

feat: add AddIfNotExist function for set

This commit is contained in:
dudaodong
2022-11-29 23:39:29 +08:00
parent eb66d038ac
commit 29bdca1bd2
2 changed files with 24 additions and 0 deletions

View File

@@ -17,6 +17,19 @@ func (s Set[T]) Add(values ...T) {
}
}
// AddIfNotExist checks if item exists in the set,
// it adds the item to set and returns true if it does not exist in the set,
// or else it does nothing and returns false.
func (s Set[T]) AddIfNotExist(value T) bool {
if !s.Contain(value) {
if _, ok := s[value]; !ok {
s[value] = struct{}{}
return true
}
}
return false
}
// Contain checks if set contains value or not
func (s Set[T]) Contain(value T) bool {
_, ok := s[value]