You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

types.go 825B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright (c) 2020 Shivaram Lingamneni
  2. // released under the MIT license
  3. package utils
  4. type empty struct{}
  5. type HashSet[T comparable] map[T]empty
  6. func (s HashSet[T]) Has(elem T) bool {
  7. _, ok := s[elem]
  8. return ok
  9. }
  10. func (s HashSet[T]) Add(elem T) {
  11. s[elem] = empty{}
  12. }
  13. func (s HashSet[T]) Remove(elem T) {
  14. delete(s, elem)
  15. }
  16. func SetLiteral[T comparable](elems ...T) HashSet[T] {
  17. result := make(HashSet[T], len(elems))
  18. for _, elem := range elems {
  19. result.Add(elem)
  20. }
  21. return result
  22. }
  23. func CopyMap[K comparable, V any](input map[K]V) (result map[K]V) {
  24. result = make(map[K]V, len(input))
  25. for key, value := range input {
  26. result[key] = value
  27. }
  28. return
  29. }
  30. func SliceContains[T comparable](slice []T, elem T) (result bool) {
  31. for _, t := range slice {
  32. if elem == t {
  33. return true
  34. }
  35. }
  36. return false
  37. }