set.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Package set provides both threadsafe and non-threadsafe implementations of
  2. // a generic set data structure. In the threadsafe set, safety encompasses all
  3. // operations on one set. Operations on multiple sets are consistent in that
  4. // the elements of each set used was valid at exactly one point in time
  5. // between the start and the end of the operation.
  6. package set
  7. // SetType denotes which type of set is created. ThreadSafe or NonThreadSafe
  8. type SetType int
  9. const (
  10. ThreadSafe = iota
  11. NonThreadSafe
  12. )
  13. func (s SetType) String() string {
  14. switch s {
  15. case ThreadSafe:
  16. return "ThreadSafe"
  17. case NonThreadSafe:
  18. return "NonThreadSafe"
  19. }
  20. return ""
  21. }
  22. // Interface is describing a Set. Sets are an unordered, unique list of values.
  23. type Interface interface {
  24. Add(items ...interface{})
  25. Remove(items ...interface{})
  26. Pop() interface{}
  27. Has(items ...interface{}) bool
  28. Size() int
  29. Clear()
  30. IsEmpty() bool
  31. IsEqual(s Interface) bool
  32. IsSubset(s Interface) bool
  33. IsSuperset(s Interface) bool
  34. Each(func(interface{}) bool)
  35. String() string
  36. List() []interface{}
  37. Copy() Interface
  38. CopyWithOut(set2 Interface, sets ...Interface) Interface
  39. Merge(s Interface)
  40. Separate(s Interface)
  41. }
  42. // helpful to not write everywhere struct{}{}
  43. var keyExists = struct{}{}
  44. // New creates and initalizes a new Set interface. Its single parameter
  45. // denotes the type of set to create. Either ThreadSafe or
  46. // NonThreadSafe. The default is ThreadSafe.
  47. func New(settype SetType) Interface {
  48. if settype == NonThreadSafe {
  49. return newNonTS()
  50. }
  51. return newTS()
  52. }
  53. // Union is the merger of multiple sets. It returns a new set with all the
  54. // elements present in all the sets that are passed.
  55. //
  56. // The dynamic type of the returned set is determined by the first passed set's
  57. // implementation of the New() method.
  58. func Union(set1, set2 Interface, sets ...Interface) Interface {
  59. u := set1.Copy()
  60. set2.Each(func(item interface{}) bool {
  61. u.Add(item)
  62. return true
  63. })
  64. for _, set := range sets {
  65. set.Each(func(item interface{}) bool {
  66. u.Add(item)
  67. return true
  68. })
  69. }
  70. return u
  71. }
  72. // Difference returns a new set which contains items which are in in the first
  73. // set but not in the others. Unlike the Difference() method you can use this
  74. // function separately with multiple sets.
  75. func Difference(set1, set2 Interface, sets ...Interface) Interface {
  76. //return set1.CopyWithOut(set2, sets...)
  77. s := set1.Copy()
  78. s.Separate(set2)
  79. for _, set := range sets {
  80. s.Separate(set) // seperate is thread safe
  81. }
  82. return s
  83. }
  84. // Intersection returns a new set which contains items that only exist in all given sets.
  85. func Intersection(set1, set2 Interface, sets ...Interface) Interface {
  86. all := Union(set1, set2, sets...)
  87. result := Union(set1, set2, sets...)
  88. all.Each(func(item interface{}) bool {
  89. if !set1.Has(item) || !set2.Has(item) {
  90. result.Remove(item)
  91. }
  92. for _, set := range sets {
  93. if !set.Has(item) {
  94. result.Remove(item)
  95. }
  96. }
  97. return true
  98. })
  99. return result
  100. }
  101. // SymmetricDifference returns a new set which s is the difference of items which are in
  102. // one of either, but not in both.
  103. func SymmetricDifference(s Interface, t Interface) Interface {
  104. u := Difference(s, t)
  105. v := Difference(t, s)
  106. return Union(u, v)
  107. }
  108. // StringSlice is a helper function that returns a slice of strings of s. If
  109. // the set contains mixed types of items only items of type string are returned.
  110. func StringSlice(s Interface) []string {
  111. slice := make([]string, 0)
  112. for _, item := range s.List() {
  113. v, ok := item.(string)
  114. if !ok {
  115. continue
  116. }
  117. slice = append(slice, v)
  118. }
  119. return slice
  120. }
  121. // IntSlice is a helper function that returns a slice of ints of s. If
  122. // the set contains mixed types of items only items of type int are returned.
  123. func IntSlice(s Interface) []int {
  124. slice := make([]int, 0)
  125. for _, item := range s.List() {
  126. v, ok := item.(int)
  127. if !ok {
  128. continue
  129. }
  130. slice = append(slice, v)
  131. }
  132. return slice
  133. }