stringset.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2015-2017 MinIO, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package set
  18. import (
  19. "fmt"
  20. "sort"
  21. jsoniter "github.com/json-iterator/go"
  22. )
  23. // StringSet - uses map as set of strings.
  24. type StringSet map[string]struct{}
  25. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  26. // ToSlice - returns StringSet as string slice.
  27. func (set StringSet) ToSlice() []string {
  28. keys := make([]string, 0, len(set))
  29. for k := range set {
  30. keys = append(keys, k)
  31. }
  32. sort.Strings(keys)
  33. return keys
  34. }
  35. // IsEmpty - returns whether the set is empty or not.
  36. func (set StringSet) IsEmpty() bool {
  37. return len(set) == 0
  38. }
  39. // Add - adds string to the set.
  40. func (set StringSet) Add(s string) {
  41. set[s] = struct{}{}
  42. }
  43. // Remove - removes string in the set. It does nothing if string does not exist in the set.
  44. func (set StringSet) Remove(s string) {
  45. delete(set, s)
  46. }
  47. // Contains - checks if string is in the set.
  48. func (set StringSet) Contains(s string) bool {
  49. _, ok := set[s]
  50. return ok
  51. }
  52. // FuncMatch - returns new set containing each value who passes match function.
  53. // A 'matchFn' should accept element in a set as first argument and
  54. // 'matchString' as second argument. The function can do any logic to
  55. // compare both the arguments and should return true to accept element in
  56. // a set to include in output set else the element is ignored.
  57. func (set StringSet) FuncMatch(matchFn func(string, string) bool, matchString string) StringSet {
  58. nset := NewStringSet()
  59. for k := range set {
  60. if matchFn(k, matchString) {
  61. nset.Add(k)
  62. }
  63. }
  64. return nset
  65. }
  66. // ApplyFunc - returns new set containing each value processed by 'applyFn'.
  67. // A 'applyFn' should accept element in a set as a argument and return
  68. // a processed string. The function can do any logic to return a processed
  69. // string.
  70. func (set StringSet) ApplyFunc(applyFn func(string) string) StringSet {
  71. nset := NewStringSet()
  72. for k := range set {
  73. nset.Add(applyFn(k))
  74. }
  75. return nset
  76. }
  77. // Equals - checks whether given set is equal to current set or not.
  78. func (set StringSet) Equals(sset StringSet) bool {
  79. // If length of set is not equal to length of given set, the
  80. // set is not equal to given set.
  81. if len(set) != len(sset) {
  82. return false
  83. }
  84. // As both sets are equal in length, check each elements are equal.
  85. for k := range set {
  86. if _, ok := sset[k]; !ok {
  87. return false
  88. }
  89. }
  90. return true
  91. }
  92. // Intersection - returns the intersection with given set as new set.
  93. func (set StringSet) Intersection(sset StringSet) StringSet {
  94. nset := NewStringSet()
  95. for k := range set {
  96. if _, ok := sset[k]; ok {
  97. nset.Add(k)
  98. }
  99. }
  100. return nset
  101. }
  102. // Difference - returns the difference with given set as new set.
  103. func (set StringSet) Difference(sset StringSet) StringSet {
  104. nset := NewStringSet()
  105. for k := range set {
  106. if _, ok := sset[k]; !ok {
  107. nset.Add(k)
  108. }
  109. }
  110. return nset
  111. }
  112. // Union - returns the union with given set as new set.
  113. func (set StringSet) Union(sset StringSet) StringSet {
  114. nset := NewStringSet()
  115. for k := range set {
  116. nset.Add(k)
  117. }
  118. for k := range sset {
  119. nset.Add(k)
  120. }
  121. return nset
  122. }
  123. // MarshalJSON - converts to JSON data.
  124. func (set StringSet) MarshalJSON() ([]byte, error) {
  125. return json.Marshal(set.ToSlice())
  126. }
  127. // UnmarshalJSON - parses JSON data and creates new set with it.
  128. // If 'data' contains JSON string array, the set contains each string.
  129. // If 'data' contains JSON string, the set contains the string as one element.
  130. // If 'data' contains Other JSON types, JSON parse error is returned.
  131. func (set *StringSet) UnmarshalJSON(data []byte) error {
  132. sl := []string{}
  133. var err error
  134. if err = json.Unmarshal(data, &sl); err == nil {
  135. *set = make(StringSet)
  136. for _, s := range sl {
  137. set.Add(s)
  138. }
  139. } else {
  140. var s string
  141. if err = json.Unmarshal(data, &s); err == nil {
  142. *set = make(StringSet)
  143. set.Add(s)
  144. }
  145. }
  146. return err
  147. }
  148. // String - returns printable string of the set.
  149. func (set StringSet) String() string {
  150. return fmt.Sprintf("%s", set.ToSlice())
  151. }
  152. // NewStringSet - creates new string set.
  153. func NewStringSet() StringSet {
  154. return make(StringSet)
  155. }
  156. // CreateStringSet - creates new string set with given string values.
  157. func CreateStringSet(sl ...string) StringSet {
  158. set := make(StringSet)
  159. for _, k := range sl {
  160. set.Add(k)
  161. }
  162. return set
  163. }
  164. // CopyStringSet - returns copy of given set.
  165. func CopyStringSet(set StringSet) StringSet {
  166. nset := NewStringSet()
  167. for k, v := range set {
  168. nset[k] = v
  169. }
  170. return nset
  171. }