set.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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]) Any() T {
  60. for x := range self.items {
  61. return x
  62. }
  63. panic("set is empty")
  64. }
  65. func (self *Set[T]) AsSlice() []T {
  66. return Keys(self.items)
  67. }
  68. func (self *Set[T]) Intersect(other *Set[T]) (ans *Set[T]) {
  69. if other == nil {
  70. return NewSet[T]()
  71. }
  72. if self.Len() < other.Len() {
  73. ans = NewSet[T](self.Len())
  74. for x := range self.items {
  75. if _, ok := other.items[x]; ok {
  76. ans.items[x] = struct{}{}
  77. }
  78. }
  79. } else {
  80. ans = NewSet[T](other.Len())
  81. for x := range other.items {
  82. if _, ok := self.items[x]; ok {
  83. ans.items[x] = struct{}{}
  84. }
  85. }
  86. }
  87. return
  88. }
  89. func (self *Set[T]) Subtract(other *Set[T]) (ans *Set[T]) {
  90. ans = NewSet[T](self.Len())
  91. for x := range self.items {
  92. if other == nil || !other.Has(x) {
  93. ans.items[x] = struct{}{}
  94. }
  95. }
  96. return ans
  97. }
  98. func (self *Set[T]) IsSubsetOf(other *Set[T]) bool {
  99. if other == nil {
  100. return self.Len() == 0
  101. }
  102. for x := range self.items {
  103. if !other.Has(x) {
  104. return false
  105. }
  106. }
  107. return true
  108. }
  109. func (self *Set[T]) Equal(other *Set[T]) bool {
  110. l := self.Len()
  111. if other == nil {
  112. return l == 0
  113. }
  114. if l != other.Len() {
  115. return false
  116. }
  117. for x := range self.items {
  118. if !other.Has(x) {
  119. return false
  120. }
  121. }
  122. return true
  123. }
  124. func NewSet[T comparable](capacity ...int) (ans *Set[T]) {
  125. if len(capacity) == 0 {
  126. ans = &Set[T]{items: make(map[T]struct{}, 8)}
  127. } else {
  128. ans = &Set[T]{items: make(map[T]struct{}, capacity[0])}
  129. }
  130. return
  131. }
  132. func NewSetWithItems[T comparable](items ...T) (ans *Set[T]) {
  133. ans = NewSet[T](len(items))
  134. ans.AddItems(items...)
  135. return ans
  136. }