Skip to content

Commit

Permalink
feat: add put slice (#1084)
Browse files Browse the repository at this point in the history
Co-authored-by: David Ragot <[email protected]>
  • Loading branch information
Dav-14 and David Ragot authored Dec 27, 2023
1 parent 70cd991 commit 804743c
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions libs/go-libs/collectionutils/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,25 @@ func Contains[T any](slice []T, t T) bool {

type Set[T comparable] map[T]struct{}

func (s Set[T]) Put(t T) {
s[t] = struct{}{}
func (s Set[T]) Put(t ...T) {
for _, t2 := range t {
s[t2] = struct{}{}
}
}

func (s Set[T]) Contains(t T) bool {
_, ok := s[t]
return ok
}

func (s Set[T]) ToSlice() []T {
ret := make([]T, 0)
for k := range s {
ret = append(ret, k)
}
return ret
}

func NewSet[T comparable]() Set[T] {
return make(Set[T], 0)
}

0 comments on commit 804743c

Please sign in to comment.