2020-08-05 03:46:16 +02:00
|
|
|
// Copyright (c) 2020 Shivaram Lingamneni
|
|
|
|
// released under the MIT license
|
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
type empty struct{}
|
|
|
|
|
2022-03-30 06:44:51 +02:00
|
|
|
type HashSet[T comparable] map[T]empty
|
2020-08-05 03:46:16 +02:00
|
|
|
|
2022-03-30 06:44:51 +02:00
|
|
|
func (s HashSet[T]) Has(elem T) bool {
|
|
|
|
_, ok := s[elem]
|
2020-08-05 03:46:16 +02:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2022-03-30 06:44:51 +02:00
|
|
|
func (s HashSet[T]) Add(elem T) {
|
|
|
|
s[elem] = empty{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s HashSet[T]) Remove(elem T) {
|
|
|
|
delete(s, elem)
|
|
|
|
}
|
|
|
|
|
|
|
|
func CopyMap[K comparable, V any](input map[K]V) (result map[K]V) {
|
|
|
|
result = make(map[K]V, len(input))
|
|
|
|
for key, value := range input {
|
|
|
|
result[key] = value
|
|
|
|
}
|
|
|
|
return
|
2020-08-05 03:46:16 +02:00
|
|
|
}
|
2022-04-29 19:39:11 +02:00
|
|
|
|
|
|
|
// reverse the order of a slice in place
|
|
|
|
func ReverseSlice[T any](results []T) {
|
|
|
|
for i, j := 0, len(results)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
results[i], results[j] = results[j], results[i]
|
|
|
|
}
|
|
|
|
}
|
2022-11-06 20:41:29 +01:00
|
|
|
|
|
|
|
func SliceContains[T comparable](slice []T, elem T) (result bool) {
|
|
|
|
for _, t := range slice {
|
|
|
|
if elem == t {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|