string.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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) String {
  39. for _, item := range items {
  40. s[item] = Empty{}
  41. }
  42. return s
  43. }
  44. // Delete removes all items from the set.
  45. func (s String) Delete(items ...string) String {
  46. for _, item := range items {
  47. delete(s, item)
  48. }
  49. return s
  50. }
  51. // Has returns true if and only if item is contained in the set.
  52. func (s String) Has(item string) bool {
  53. _, contained := s[item]
  54. return contained
  55. }
  56. // HasAll returns true if and only if all items are contained in the set.
  57. func (s String) HasAll(items ...string) bool {
  58. for _, item := range items {
  59. if !s.Has(item) {
  60. return false
  61. }
  62. }
  63. return true
  64. }
  65. // HasAny returns true if any items are contained in the set.
  66. func (s String) HasAny(items ...string) bool {
  67. for _, item := range items {
  68. if s.Has(item) {
  69. return true
  70. }
  71. }
  72. return false
  73. }
  74. // Difference returns a set of objects that are not in s2
  75. // For example:
  76. // s1 = {a1, a2, a3}
  77. // s2 = {a1, a2, a4, a5}
  78. // s1.Difference(s2) = {a3}
  79. // s2.Difference(s1) = {a4, a5}
  80. func (s String) Difference(s2 String) String {
  81. result := NewString()
  82. for key := range s {
  83. if !s2.Has(key) {
  84. result.Insert(key)
  85. }
  86. }
  87. return result
  88. }
  89. // Union returns a new set which includes items in either s1 or s2.
  90. // For example:
  91. // s1 = {a1, a2}
  92. // s2 = {a3, a4}
  93. // s1.Union(s2) = {a1, a2, a3, a4}
  94. // s2.Union(s1) = {a1, a2, a3, a4}
  95. func (s1 String) Union(s2 String) String {
  96. result := NewString()
  97. for key := range s1 {
  98. result.Insert(key)
  99. }
  100. for key := range s2 {
  101. result.Insert(key)
  102. }
  103. return result
  104. }
  105. // Intersection returns a new set which includes the item in BOTH s1 and s2
  106. // For example:
  107. // s1 = {a1, a2}
  108. // s2 = {a2, a3}
  109. // s1.Intersection(s2) = {a2}
  110. func (s1 String) Intersection(s2 String) String {
  111. var walk, other String
  112. result := NewString()
  113. if s1.Len() < s2.Len() {
  114. walk = s1
  115. other = s2
  116. } else {
  117. walk = s2
  118. other = s1
  119. }
  120. for key := range walk {
  121. if other.Has(key) {
  122. result.Insert(key)
  123. }
  124. }
  125. return result
  126. }
  127. // IsSuperset returns true if and only if s1 is a superset of s2.
  128. func (s1 String) IsSuperset(s2 String) bool {
  129. for item := range s2 {
  130. if !s1.Has(item) {
  131. return false
  132. }
  133. }
  134. return true
  135. }
  136. // Equal returns true if and only if s1 is equal (as a set) to s2.
  137. // Two sets are equal if their membership is identical.
  138. // (In practice, this means same elements, order doesn't matter)
  139. func (s1 String) Equal(s2 String) bool {
  140. return len(s1) == len(s2) && s1.IsSuperset(s2)
  141. }
  142. type sortableSliceOfString []string
  143. func (s sortableSliceOfString) Len() int { return len(s) }
  144. func (s sortableSliceOfString) Less(i, j int) bool { return lessString(s[i], s[j]) }
  145. func (s sortableSliceOfString) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  146. // List returns the contents as a sorted string slice.
  147. func (s String) List() []string {
  148. res := make(sortableSliceOfString, 0, len(s))
  149. for key := range s {
  150. res = append(res, key)
  151. }
  152. sort.Sort(res)
  153. return []string(res)
  154. }
  155. // UnsortedList returns the slice with contents in random order.
  156. func (s String) UnsortedList() []string {
  157. res := make([]string, 0, len(s))
  158. for key := range s {
  159. res = append(res, key)
  160. }
  161. return res
  162. }
  163. // Returns a single element from the set.
  164. func (s String) PopAny() (string, bool) {
  165. for key := range s {
  166. s.Delete(key)
  167. return key, true
  168. }
  169. var zeroValue string
  170. return zeroValue, false
  171. }
  172. // Len returns the size of the set.
  173. func (s String) Len() int {
  174. return len(s)
  175. }
  176. func lessString(lhs, rhs string) bool {
  177. return lhs < rhs
  178. }