string.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package sets
  2. import (
  3. "reflect"
  4. "sort"
  5. )
  6. type Empty struct{}
  7. // sets.String is a set of strings, implemented via map[string]struct{} for minimal memory consumption.
  8. type String map[string]Empty
  9. // NewString creates a String from a list of values.
  10. func NewString(items ...string) String {
  11. ss := String{}
  12. ss.Insert(items...)
  13. return ss
  14. }
  15. // StringKeySet creates a String from a keys of a map[string](? extends interface{}).
  16. // If the value passed in is not actually a map, this will panic.
  17. func StringKeySet(theMap interface{}) String {
  18. v := reflect.ValueOf(theMap)
  19. ret := String{}
  20. for _, keyValue := range v.MapKeys() {
  21. ret.Insert(keyValue.Interface().(string))
  22. }
  23. return ret
  24. }
  25. // Insert adds items to the set.
  26. func (s String) Insert(items ...string) {
  27. for _, item := range items {
  28. s[item] = Empty{}
  29. }
  30. }
  31. // Delete removes all items from the set.
  32. func (s String) Delete(items ...string) {
  33. for _, item := range items {
  34. delete(s, item)
  35. }
  36. }
  37. // Has returns true if and only if item is contained in the set.
  38. func (s String) Has(item string) bool {
  39. _, contained := s[item]
  40. return contained
  41. }
  42. // HasAll returns true if and only if all items are contained in the set.
  43. func (s String) HasAll(items ...string) bool {
  44. for _, item := range items {
  45. if !s.Has(item) {
  46. return false
  47. }
  48. }
  49. return true
  50. }
  51. // HasAny returns true if any items are contained in the set.
  52. func (s String) HasAny(items ...string) bool {
  53. for _, item := range items {
  54. if s.Has(item) {
  55. return true
  56. }
  57. }
  58. return false
  59. }
  60. // Difference returns a set of objects that are not in s2
  61. // For example:
  62. // s1 = {a1, a2, a3}
  63. // s2 = {a1, a2, a4, a5}
  64. // s1.Difference(s2) = {a3}
  65. // s2.Difference(s1) = {a4, a5}
  66. func (s String) Difference(s2 String) String {
  67. result := NewString()
  68. for key := range s {
  69. if !s2.Has(key) {
  70. result.Insert(key)
  71. }
  72. }
  73. return result
  74. }
  75. // Union returns a new set which includes items in either s1 or s2.
  76. // For example:
  77. // s1 = {a1, a2}
  78. // s2 = {a3, a4}
  79. // s1.Union(s2) = {a1, a2, a3, a4}
  80. // s2.Union(s1) = {a1, a2, a3, a4}
  81. func (s1 String) Union(s2 String) String {
  82. result := NewString()
  83. for key := range s1 {
  84. result.Insert(key)
  85. }
  86. for key := range s2 {
  87. result.Insert(key)
  88. }
  89. return result
  90. }
  91. // Intersection returns a new set which includes the item in BOTH s1 and s2
  92. // For example:
  93. // s1 = {a1, a2}
  94. // s2 = {a2, a3}
  95. // s1.Intersection(s2) = {a2}
  96. func (s1 String) Intersection(s2 String) String {
  97. var walk, other String
  98. result := NewString()
  99. if s1.Len() < s2.Len() {
  100. walk = s1
  101. other = s2
  102. } else {
  103. walk = s2
  104. other = s1
  105. }
  106. for key := range walk {
  107. if other.Has(key) {
  108. result.Insert(key)
  109. }
  110. }
  111. return result
  112. }
  113. // IsSuperset returns true if and only if s1 is a superset of s2.
  114. func (s1 String) IsSuperset(s2 String) bool {
  115. for item := range s2 {
  116. if !s1.Has(item) {
  117. return false
  118. }
  119. }
  120. return true
  121. }
  122. // Equal returns true if and only if s1 is equal (as a set) to s2.
  123. // Two sets are equal if their membership is identical.
  124. // (In practice, this means same elements, order doesn't matter)
  125. func (s1 String) Equal(s2 String) bool {
  126. return len(s1) == len(s2) && s1.IsSuperset(s2)
  127. }
  128. type sortableSliceOfString []string
  129. func (s sortableSliceOfString) Len() int { return len(s) }
  130. func (s sortableSliceOfString) Less(i, j int) bool { return lessString(s[i], s[j]) }
  131. func (s sortableSliceOfString) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  132. // List returns the contents as a sorted string slice.
  133. func (s String) List() []string {
  134. res := make(sortableSliceOfString, 0, len(s))
  135. for key := range s {
  136. res = append(res, key)
  137. }
  138. sort.Sort(res)
  139. return []string(res)
  140. }
  141. // UnsortedList returns the slice with contents in random order.
  142. func (s String) UnsortedList() []string {
  143. res := make([]string, 0, len(s))
  144. for key := range s {
  145. res = append(res, key)
  146. }
  147. return res
  148. }
  149. // Returns a single element from the set.
  150. func (s String) PopAny() (string, bool) {
  151. for key := range s {
  152. s.Delete(key)
  153. return key, true
  154. }
  155. var zeroValue string
  156. return zeroValue, false
  157. }
  158. // Len returns the size of the set.
  159. func (s String) Len() int {
  160. return len(s)
  161. }
  162. func lessString(lhs, rhs string) bool {
  163. return lhs < rhs
  164. }