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 703B

123456789101112131415161718192021222324252627282930313233343536
  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 CopyMap[K comparable, V any](input map[K]V) (result map[K]V) {
  17. result = make(map[K]V, len(input))
  18. for key, value := range input {
  19. result[key] = value
  20. }
  21. return
  22. }
  23. // reverse the order of a slice in place
  24. func ReverseSlice[T any](results []T) {
  25. for i, j := 0, len(results)-1; i < j; i, j = i+1, j-1 {
  26. results[i], results[j] = results[j], results[i]
  27. }
  28. }