set.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package utils
  3. import (
  4. "fmt"
  5. )
  6. var _ = fmt.Print
  7. func Keys[M ~map[K]V, K comparable, V any](m M) []K {
  8. r := make([]K, 0, len(m))
  9. for k := range m {
  10. r = append(r, k)
  11. }
  12. return r
  13. }
  14. func Values[M ~map[K]V, K comparable, V any](m M) []V {
  15. r := make([]V, 0, len(m))
  16. for _, k := range m {
  17. r = append(r, k)
  18. }
  19. return r
  20. }
  21. type Set[T comparable] struct {
  22. items map[T]struct{}
  23. }
  24. func (self *Set[T]) Add(val T) {
  25. self.items[val] = struct{}{}
  26. }
  27. func (self *Set[T]) AddItems(val ...T) {
  28. for _, x := range val {
  29. self.items[x] = struct{}{}
  30. }
  31. }
  32. func (self *Set[T]) String() string {
  33. return fmt.Sprintf("%#v", Keys(self.items))
  34. }
  35. func (self *Set[T]) Remove(val T) {
  36. delete(self.items, val)
  37. }
  38. func (self *Set[T]) Discard(val T) {
  39. delete(self.items, val)
  40. }
  41. func (self *Set[T]) Has(val T) bool {
  42. _, ok := self.items[val]
  43. return ok
  44. }
  45. func (self *Set[T]) Clear() {
  46. clear(self.items)
  47. }
  48. func (self *Set[T]) Len() int {
  49. return len(self.items)
  50. }
  51. func (self *Set[T]) ForEach(f func(T)) {
  52. for x := range self.items {
  53. f(x)
  54. }
  55. }
  56. func (self *Set[T]) Iterable() map[T]struct{} {
  57. return self.items
  58. }
  59. func (self *Set[T]) AsSlice() []T {
  60. return Keys(self.items)
  61. }
  62. func (self *Set[T]) Intersect(other *Set[T]) (ans *Set[T]) {
  63. if other == nil {
  64. return NewSet[T]()
  65. }
  66. if self.Len() < other.Len() {
  67. ans = NewSet[T](self.Len())
  68. for x := range self.items {
  69. if _, ok := other.items[x]; ok {
  70. ans.items[x] = struct{}{}
  71. }
  72. }
  73. } else {
  74. ans = NewSet[T](other.Len())
  75. for x := range other.items {
  76. if _, ok := self.items[x]; ok {
  77. ans.items[x] = struct{}{}
  78. }
  79. }
  80. }
  81. return
  82. }
  83. func (self *Set[T]) Subtract(other *Set[T]) (ans *Set[T]) {
  84. ans = NewSet[T](self.Len())
  85. for x := range self.items {
  86. if other == nil || !other.Has(x) {
  87. ans.items[x] = struct{}{}
  88. }
  89. }
  90. return ans
  91. }
  92. func (self *Set[T]) IsSubsetOf(other *Set[T]) bool {
  93. if other == nil {
  94. return self.Len() == 0
  95. }
  96. for x := range self.items {
  97. if !other.Has(x) {
  98. return false
  99. }
  100. }
  101. return true
  102. }
  103. func (self *Set[T]) Equal(other *Set[T]) bool {
  104. l := self.Len()
  105. if other == nil {
  106. return l == 0
  107. }
  108. if l != other.Len() {
  109. return false
  110. }
  111. for x := range self.items {
  112. if !other.Has(x) {
  113. return false
  114. }
  115. }
  116. return true
  117. }
  118. func NewSet[T comparable](capacity ...int) (ans *Set[T]) {
  119. if len(capacity) == 0 {
  120. ans = &Set[T]{items: make(map[T]struct{}, 8)}
  121. } else {
  122. ans = &Set[T]{items: make(map[T]struct{}, capacity[0])}
  123. }
  124. return
  125. }
  126. func NewSetWithItems[T comparable](items ...T) (ans *Set[T]) {
  127. ans = NewSet[T](len(items))
  128. ans.AddItems(items...)
  129. return ans
  130. }