slice.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package set
  4. import (
  5. "slices"
  6. "tailscale.com/types/views"
  7. )
  8. // Slice is a set of elements tracked in a slice of unique elements.
  9. type Slice[T comparable] struct {
  10. slice []T
  11. set map[T]bool // nil until/unless slice is large enough
  12. }
  13. // Slice returns a view of the underlying slice.
  14. // The elements are in order of insertion.
  15. // The returned value is only valid until ss is modified again.
  16. func (ss *Slice[T]) Slice() views.Slice[T] { return views.SliceOf(ss.slice) }
  17. // Len returns the number of elements in the set.
  18. func (ss *Slice[T]) Len() int { return len(ss.slice) }
  19. // Contains reports whether v is in the set.
  20. // The amortized cost is O(1).
  21. func (ss *Slice[T]) Contains(v T) bool {
  22. if ss.set != nil {
  23. return ss.set[v]
  24. }
  25. return slices.Index(ss.slice, v) != -1
  26. }
  27. // Remove removes v from the set.
  28. // The cost is O(n).
  29. func (ss *Slice[T]) Remove(v T) {
  30. if ss.set != nil {
  31. if !ss.set[v] {
  32. return
  33. }
  34. delete(ss.set, v)
  35. }
  36. if ix := slices.Index(ss.slice, v); ix != -1 {
  37. ss.slice = append(ss.slice[:ix], ss.slice[ix+1:]...)
  38. }
  39. }
  40. // Add adds each element in vs to the set.
  41. // The amortized cost is O(1) per element.
  42. func (ss *Slice[T]) Add(vs ...T) {
  43. for _, v := range vs {
  44. if ss.Contains(v) {
  45. continue
  46. }
  47. ss.slice = append(ss.slice, v)
  48. if ss.set != nil {
  49. ss.set[v] = true
  50. } else if len(ss.slice) > 8 {
  51. ss.set = make(map[T]bool, len(ss.slice))
  52. for _, v := range ss.slice {
  53. ss.set[v] = true
  54. }
  55. }
  56. }
  57. }
  58. // AddSlice adds all elements in vs to the set.
  59. func (ss *Slice[T]) AddSlice(vs views.Slice[T]) {
  60. for i := range vs.Len() {
  61. ss.Add(vs.At(i))
  62. }
  63. }