stringset.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. "encoding/json"
  20. "fmt"
  21. "sort"
  22. )
  23. // StringSet - uses map as set of strings.
  24. type StringSet map[string]struct{}
  25. // ToSlice - returns StringSet as string slice.
  26. func (set StringSet) ToSlice() []string {
  27. keys := make([]string, 0, len(set))
  28. for k := range set {
  29. keys = append(keys, k)
  30. }
  31. sort.Strings(keys)
  32. return keys
  33. }
  34. // IsEmpty - returns whether the set is empty or not.
  35. func (set StringSet) IsEmpty() bool {
  36. return len(set) == 0
  37. }
  38. // Add - adds string to the set.
  39. func (set StringSet) Add(s string) {
  40. set[s] = struct{}{}
  41. }
  42. // Remove - removes string in the set. It does nothing if string does not exist in the set.
  43. func (set StringSet) Remove(s string) {
  44. delete(set, s)
  45. }
  46. // Contains - checks if string is in the set.
  47. func (set StringSet) Contains(s string) bool {
  48. _, ok := set[s]
  49. return ok
  50. }
  51. // FuncMatch - returns new set containing each value who passes match function.
  52. // A 'matchFn' should accept element in a set as first argument and
  53. // 'matchString' as second argument. The function can do any logic to
  54. // compare both the arguments and should return true to accept element in
  55. // a set to include in output set else the element is ignored.
  56. func (set StringSet) FuncMatch(matchFn func(string, string) bool, matchString string) StringSet {
  57. nset := NewStringSet()
  58. for k := range set {
  59. if matchFn(k, matchString) {
  60. nset.Add(k)
  61. }
  62. }
  63. return nset
  64. }
  65. // ApplyFunc - returns new set containing each value processed by 'applyFn'.
  66. // A 'applyFn' should accept element in a set as a argument and return
  67. // a processed string. The function can do any logic to return a processed
  68. // string.
  69. func (set StringSet) ApplyFunc(applyFn func(string) string) StringSet {
  70. nset := NewStringSet()
  71. for k := range set {
  72. nset.Add(applyFn(k))
  73. }
  74. return nset
  75. }
  76. // Equals - checks whether given set is equal to current set or not.
  77. func (set StringSet) Equals(sset StringSet) bool {
  78. // If length of set is not equal to length of given set, the
  79. // set is not equal to given set.
  80. if len(set) != len(sset) {
  81. return false
  82. }
  83. // As both sets are equal in length, check each elements are equal.
  84. for k := range set {
  85. if _, ok := sset[k]; !ok {
  86. return false
  87. }
  88. }
  89. return true
  90. }
  91. // Intersection - returns the intersection with given set as new set.
  92. func (set StringSet) Intersection(sset StringSet) StringSet {
  93. nset := NewStringSet()
  94. for k := range set {
  95. if _, ok := sset[k]; ok {
  96. nset.Add(k)
  97. }
  98. }
  99. return nset
  100. }
  101. // Difference - returns the difference with given set as new set.
  102. func (set StringSet) Difference(sset StringSet) StringSet {
  103. nset := NewStringSet()
  104. for k := range set {
  105. if _, ok := sset[k]; !ok {
  106. nset.Add(k)
  107. }
  108. }
  109. return nset
  110. }
  111. // Union - returns the union with given set as new set.
  112. func (set StringSet) Union(sset StringSet) StringSet {
  113. nset := NewStringSet()
  114. for k := range set {
  115. nset.Add(k)
  116. }
  117. for k := range sset {
  118. nset.Add(k)
  119. }
  120. return nset
  121. }
  122. // MarshalJSON - converts to JSON data.
  123. func (set StringSet) MarshalJSON() ([]byte, error) {
  124. return json.Marshal(set.ToSlice())
  125. }
  126. // UnmarshalJSON - parses JSON data and creates new set with it.
  127. // If 'data' contains JSON string array, the set contains each string.
  128. // If 'data' contains JSON string, the set contains the string as one element.
  129. // If 'data' contains Other JSON types, JSON parse error is returned.
  130. func (set *StringSet) UnmarshalJSON(data []byte) error {
  131. sl := []string{}
  132. var err error
  133. if err = json.Unmarshal(data, &sl); err == nil {
  134. *set = make(StringSet)
  135. for _, s := range sl {
  136. set.Add(s)
  137. }
  138. } else {
  139. var s string
  140. if err = json.Unmarshal(data, &s); err == nil {
  141. *set = make(StringSet)
  142. set.Add(s)
  143. }
  144. }
  145. return err
  146. }
  147. // String - returns printable string of the set.
  148. func (set StringSet) String() string {
  149. return fmt.Sprintf("%s", set.ToSlice())
  150. }
  151. // NewStringSet - creates new string set.
  152. func NewStringSet() StringSet {
  153. return make(StringSet)
  154. }
  155. // CreateStringSet - creates new string set with given string values.
  156. func CreateStringSet(sl ...string) StringSet {
  157. set := make(StringSet)
  158. for _, k := range sl {
  159. set.Add(k)
  160. }
  161. return set
  162. }
  163. // CopyStringSet - returns copy of given set.
  164. func CopyStringSet(set StringSet) StringSet {
  165. nset := NewStringSet()
  166. for k, v := range set {
  167. nset[k] = v
  168. }
  169. return nset
  170. }