string.go 4.9 KB

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