Guest User

Untitled

a guest
Feb 25th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. package set
  2.  
  3. import "sync"
  4.  
  5. //===========================================================================
  6. // Base Set
  7. //===========================================================================
  8.  
  9. // Provides a common internal structure for thread-safe and non-thread-safe
  10. // Set objects, as well as internal helper methods used by those objects.
  11. //
  12. // Items is a mapping of an interface (the set member) to an empty value,
  13. // struct{}, which doesn't take up any space.
  14. type set struct {
  15. items map[interface{}]struct{}
  16. }
  17.  
  18. // Used as the value in the items map; the key represents the set member.
  19. var exists = struct{}{}
  20.  
  21. // Initialize the internal data structure
  22. func (s *set) init() *set {
  23. s.items = make(map[interface{}]struct{})
  24. return s
  25. }
  26.  
  27. // Add one or more items to the set.
  28. func (s *set) add(items ...interface{}) {
  29. for _, item := range items {
  30. s.items[item] = exists
  31. }
  32. }
  33.  
  34. // Remove one or more items from the set.
  35. func (s *set) remove(items ...interface{}) {
  36. for _, item := range items {
  37. delete(s.items, item)
  38. }
  39. }
  40.  
  41. // Check for the existence of the item in the set.
  42. func (s *set) contains(item interface{}) bool {
  43. _, contains := s.items[item]
  44. return contains
  45. }
  46.  
  47. //===========================================================================
  48. // Non-Thread-Safe Set
  49. //===========================================================================
  50.  
  51. // SetNonTS defines a non-thread safe set data structure.
  52. type SetNonTS struct {
  53. set
  54. }
  55.  
  56. // NewNonTS creates and initializes a new non-thread-safe Set. It accepts a
  57. // variable number of arguments to populate the initial set with.
  58. func NewNonTS(items ...interface{}) *SetNonTS {
  59. s := new(SetNonTS)
  60. s.init()
  61. s.add(items...)
  62. return s
  63. }
  64.  
  65. // Add one or more items to the set. If no items, silently exit.
  66. func (s *SetNonTS) Add(items ...interface{}) {
  67. if len(items) == 0 {
  68. return
  69. }
  70.  
  71. s.add(items...)
  72. }
  73.  
  74. // Remove one or more items from the set. If no items, silently exit.
  75. func (s *SetNonTS) Remove(items ...interface{}) {
  76. if len(items) == 0 {
  77. return
  78. }
  79.  
  80. s.remove(items...)
  81. }
  82.  
  83. // Contains returns true if the item is in the set.
  84. func (s *SetNonTS) Contains(item interface{}) bool {
  85. return s.contains(item)
  86. }
  87.  
  88. //===========================================================================
  89. // Thread Safe Set
  90. //===========================================================================
  91.  
  92. // Set defines a thread-safe set data structure.
  93. type Set struct {
  94. set
  95. sync.RWMutex
  96. }
  97.  
  98. // New creates and initializes a new thread-safe Set. It accepts a
  99. // variable number of arguments to populate the initial set with.
  100. func New(items ...interface{}) *Set {
  101. s := new(Set)
  102. s.Lock()
  103. defer s.Unlock()
  104.  
  105. s.init()
  106. s.add(items...)
  107. return s
  108. }
  109.  
  110. // Add one or more items to the set. If no items, silently exit.
  111. func (s *Set) Add(items ...interface{}) {
  112. if len(items) == 0 {
  113. return
  114. }
  115.  
  116. s.Lock()
  117. defer s.Unlock()
  118. s.add(items...)
  119. }
  120.  
  121. // Remove one or more items from the set. If no items, silently exit.
  122. func (s *Set) Remove(items ...interface{}) {
  123. if len(items) == 0 {
  124. return
  125. }
  126.  
  127. s.Lock()
  128. defer s.Unlock()
  129. s.remove(items...)
  130. }
  131.  
  132. // Contains returns true if the item is in the set.
  133. func (s *Set) Contains(item interface{}) bool {
  134. s.RLock()
  135. defer s.RUnlock()
  136. return s.contains(item)
  137. }
Add Comment
Please, Sign In to add comment