setmatrix.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package setmatrix
  2. import (
  3. "sync"
  4. mapset "github.com/deckarep/golang-set"
  5. )
  6. // SetMatrix is a map of Sets
  7. type SetMatrix interface {
  8. // Get returns the members of the set for a specific key as a slice.
  9. Get(key string) ([]interface{}, bool)
  10. // Contains is used to verify if an element is in a set for a specific key
  11. // returns true if the element is in the set
  12. // returns true if there is a set for the key
  13. Contains(key string, value interface{}) (bool, bool)
  14. // Insert inserts the value in the set of a key
  15. // returns true if the value is inserted (was not already in the set), false otherwise
  16. // returns also the length of the set for the key
  17. Insert(key string, value interface{}) (bool, int)
  18. // Remove removes the value in the set for a specific key
  19. // returns true if the value is deleted, false otherwise
  20. // returns also the length of the set for the key
  21. Remove(key string, value interface{}) (bool, int)
  22. // Cardinality returns the number of elements in the set for a key
  23. // returns false if the set is not present
  24. Cardinality(key string) (int, bool)
  25. // String returns the string version of the set, empty otherwise
  26. // returns false if the set is not present
  27. String(key string) (string, bool)
  28. // Returns all the keys in the map
  29. Keys() []string
  30. }
  31. type setMatrix struct {
  32. matrix map[string]mapset.Set
  33. mu sync.Mutex
  34. }
  35. // NewSetMatrix creates a new set matrix object
  36. func NewSetMatrix() SetMatrix {
  37. s := &setMatrix{}
  38. s.init()
  39. return s
  40. }
  41. func (s *setMatrix) init() {
  42. s.matrix = make(map[string]mapset.Set)
  43. }
  44. func (s *setMatrix) Get(key string) ([]interface{}, bool) {
  45. s.mu.Lock()
  46. defer s.mu.Unlock()
  47. set, ok := s.matrix[key]
  48. if !ok {
  49. return nil, ok
  50. }
  51. return set.ToSlice(), ok
  52. }
  53. func (s *setMatrix) Contains(key string, value interface{}) (bool, bool) {
  54. s.mu.Lock()
  55. defer s.mu.Unlock()
  56. set, ok := s.matrix[key]
  57. if !ok {
  58. return false, ok
  59. }
  60. return set.Contains(value), ok
  61. }
  62. func (s *setMatrix) Insert(key string, value interface{}) (bool, int) {
  63. s.mu.Lock()
  64. defer s.mu.Unlock()
  65. set, ok := s.matrix[key]
  66. if !ok {
  67. s.matrix[key] = mapset.NewSet()
  68. s.matrix[key].Add(value)
  69. return true, 1
  70. }
  71. return set.Add(value), set.Cardinality()
  72. }
  73. func (s *setMatrix) Remove(key string, value interface{}) (bool, int) {
  74. s.mu.Lock()
  75. defer s.mu.Unlock()
  76. set, ok := s.matrix[key]
  77. if !ok {
  78. return false, 0
  79. }
  80. var removed bool
  81. if set.Contains(value) {
  82. set.Remove(value)
  83. removed = true
  84. // If the set is empty remove it from the matrix
  85. if set.Cardinality() == 0 {
  86. delete(s.matrix, key)
  87. }
  88. }
  89. return removed, set.Cardinality()
  90. }
  91. func (s *setMatrix) Cardinality(key string) (int, bool) {
  92. s.mu.Lock()
  93. defer s.mu.Unlock()
  94. set, ok := s.matrix[key]
  95. if !ok {
  96. return 0, ok
  97. }
  98. return set.Cardinality(), ok
  99. }
  100. func (s *setMatrix) String(key string) (string, bool) {
  101. s.mu.Lock()
  102. defer s.mu.Unlock()
  103. set, ok := s.matrix[key]
  104. if !ok {
  105. return "", ok
  106. }
  107. return set.String(), ok
  108. }
  109. func (s *setMatrix) Keys() []string {
  110. s.mu.Lock()
  111. defer s.mu.Unlock()
  112. keys := make([]string, 0, len(s.matrix))
  113. for k := range s.matrix {
  114. keys = append(keys, k)
  115. }
  116. return keys
  117. }