string.go 5.9 KB

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