set.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. Merge(s Interface)
  39. Separate(s Interface)
  40. }
  41. // helpful to not write everywhere struct{}{}
  42. var keyExists = struct{}{}
  43. // New creates and initalizes a new Set interface. Its single parameter
  44. // denotes the type of set to create. Either ThreadSafe or
  45. // NonThreadSafe. The default is ThreadSafe.
  46. func New(settype SetType) Interface {
  47. if settype == NonThreadSafe {
  48. return newNonTS()
  49. }
  50. return newTS()
  51. }
  52. // Union is the merger of multiple sets. It returns a new set with all the
  53. // elements present in all the sets that are passed.
  54. //
  55. // The dynamic type of the returned set is determined by the first passed set's
  56. // implementation of the New() method.
  57. func Union(set1, set2 Interface, sets ...Interface) Interface {
  58. u := set1.Copy()
  59. set2.Each(func(item interface{}) bool {
  60. u.Add(item)
  61. return true
  62. })
  63. for _, set := range sets {
  64. set.Each(func(item interface{}) bool {
  65. u.Add(item)
  66. return true
  67. })
  68. }
  69. return u
  70. }
  71. // Difference returns a new set which contains items which are in in the first
  72. // set but not in the others. Unlike the Difference() method you can use this
  73. // function separately with multiple sets.
  74. func Difference(set1, set2 Interface, sets ...Interface) Interface {
  75. s := set1.Copy()
  76. s.Separate(set2)
  77. for _, set := range sets {
  78. s.Separate(set) // seperate is thread safe
  79. }
  80. return s
  81. }
  82. // Intersection returns a new set which contains items that only exist in all given sets.
  83. func Intersection(set1, set2 Interface, sets ...Interface) Interface {
  84. all := Union(set1, set2, sets...)
  85. result := Union(set1, set2, sets...)
  86. all.Each(func(item interface{}) bool {
  87. if !set1.Has(item) || !set2.Has(item) {
  88. result.Remove(item)
  89. }
  90. for _, set := range sets {
  91. if !set.Has(item) {
  92. result.Remove(item)
  93. }
  94. }
  95. return true
  96. })
  97. return result
  98. }
  99. // SymmetricDifference returns a new set which s is the difference of items which are in
  100. // one of either, but not in both.
  101. func SymmetricDifference(s Interface, t Interface) Interface {
  102. u := Difference(s, t)
  103. v := Difference(t, s)
  104. return Union(u, v)
  105. }
  106. // StringSlice is a helper function that returns a slice of strings of s. If
  107. // the set contains mixed types of items only items of type string are returned.
  108. func StringSlice(s Interface) []string {
  109. slice := make([]string, 0)
  110. for _, item := range s.List() {
  111. v, ok := item.(string)
  112. if !ok {
  113. continue
  114. }
  115. slice = append(slice, v)
  116. }
  117. return slice
  118. }
  119. // IntSlice is a helper function that returns a slice of ints of s. If
  120. // the set contains mixed types of items only items of type int are returned.
  121. func IntSlice(s Interface) []int {
  122. slice := make([]int, 0)
  123. for _, item := range s.List() {
  124. v, ok := item.(int)
  125. if !ok {
  126. continue
  127. }
  128. slice = append(slice, v)
  129. }
  130. return slice
  131. }