diff options
Diffstat (limited to 'vendor/github.com/jesseduffield/generics/set/set.go')
-rw-r--r-- | vendor/github.com/jesseduffield/generics/set/set.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/vendor/github.com/jesseduffield/generics/set/set.go b/vendor/github.com/jesseduffield/generics/set/set.go new file mode 100644 index 000000000..3e9b9d9bf --- /dev/null +++ b/vendor/github.com/jesseduffield/generics/set/set.go @@ -0,0 +1,49 @@ +package set + +import "github.com/jesseduffield/generics/hashmap" + +type Set[T comparable] struct { + hashMap map[T]bool +} + +func New[T comparable]() *Set[T] { + return &Set[T]{hashMap: make(map[T]bool)} +} + +func NewFromSlice[T comparable](slice []T) *Set[T] { + hashMap := make(map[T]bool) + for _, value := range slice { + hashMap[value] = true + } + + return &Set[T]{hashMap: hashMap} +} + +func (s *Set[T]) Add(value T) { + s.hashMap[value] = true +} + +func (s *Set[T]) AddSlice(slice []T) { + for _, value := range slice { + s.Add(value) + } +} + +func (s *Set[T]) Remove(value T) { + delete(s.hashMap, value) +} + +func (s *Set[T]) RemoveSlice(slice []T) { + for _, value := range slice { + s.Remove(value) + } +} + +func (s *Set[T]) Includes(value T) bool { + return s.hashMap[value] +} + +// output slice is not necessarily in the same order that items were added +func (s *Set[T]) ToSlice() []T { + return hashmap.Keys(s.hashMap) +} |