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:
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user