string.go 4.7 KB

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