1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-23 21:51:07 +02:00

46 lines
846 B
Go
Raw Normal View History

2022-03-19 12:26:30 +11:00
package set
2022-03-19 15:36:46 +11:00
import "github.com/jesseduffield/generics/maps"
2022-03-19 12:26:30 +11:00
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}
}
2022-03-19 16:34:46 +11:00
func (s *Set[T]) Add(values ...T) {
for _, value := range values {
s.hashMap[value] = true
2022-03-19 12:26:30 +11:00
}
}
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 {
2022-03-19 15:36:46 +11:00
return maps.Keys(s.hashMap)
2022-03-19 12:26:30 +11:00
}