github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/sets/zset/zset_safe.go (about) 1 package zset 2 3 import ( 4 "github.com/songzhibin97/go-baseutils/base/bcomparator" 5 "github.com/songzhibin97/go-baseutils/structure/sets" 6 "sync" 7 ) 8 9 var _ sets.Set[int] = (*SetSafe[int])(nil) 10 11 func NewSafe[K comparable](comparator bcomparator.Comparator[K]) *SetSafe[K] { 12 return &SetSafe[K]{ 13 unsafe: New[K](comparator), 14 } 15 } 16 17 type SetSafe[K comparable] struct { 18 unsafe *Set[K] 19 lock sync.Mutex 20 } 21 22 func (s *SetSafe[K]) Add(elements ...K) { 23 s.lock.Lock() 24 defer s.lock.Unlock() 25 s.unsafe.Add(elements...) 26 27 } 28 29 func (s *SetSafe[K]) Remove(elements ...K) { 30 s.lock.Lock() 31 defer s.lock.Unlock() 32 s.unsafe.Remove(elements...) 33 34 } 35 36 func (s *SetSafe[K]) Contains(elements ...K) bool { 37 s.lock.Lock() 38 defer s.lock.Unlock() 39 return s.unsafe.Contains(elements...) 40 } 41 42 func (s *SetSafe[K]) Empty() bool { 43 s.lock.Lock() 44 defer s.lock.Unlock() 45 return s.unsafe.Empty() 46 } 47 48 func (s *SetSafe[K]) Size() int { 49 s.lock.Lock() 50 defer s.lock.Unlock() 51 return s.unsafe.Size() 52 } 53 54 func (s *SetSafe[K]) Clear() { 55 s.lock.Lock() 56 defer s.lock.Unlock() 57 s.unsafe.Clear() 58 59 } 60 61 func (s *SetSafe[K]) Values() []K { 62 s.lock.Lock() 63 defer s.lock.Unlock() 64 return s.unsafe.Values() 65 } 66 67 func (s *SetSafe[K]) String() string { 68 s.lock.Lock() 69 defer s.lock.Unlock() 70 return s.unsafe.String() 71 } 72 73 func (s *SetSafe[K]) Len() int { 74 s.lock.Lock() 75 defer s.lock.Unlock() 76 return s.unsafe.Len() 77 } 78 79 func (s *SetSafe[K]) AddB(score float64, value K) bool { 80 s.lock.Lock() 81 defer s.lock.Unlock() 82 return s.unsafe.AddB(score, value) 83 } 84 85 func (s *SetSafe[K]) RemoveB(value K) (float64, bool) { 86 s.lock.Lock() 87 defer s.lock.Unlock() 88 return s.unsafe.RemoveB(value) 89 } 90 91 func (s *SetSafe[K]) IncrBy(incr float64, value K) (float64, bool) { 92 s.lock.Lock() 93 defer s.lock.Unlock() 94 return s.unsafe.IncrBy(incr, value) 95 } 96 97 func (s *SetSafe[K]) ContainsB(value K) bool { 98 s.lock.Lock() 99 defer s.lock.Unlock() 100 return s.unsafe.ContainsB(value) 101 } 102 103 func (s *SetSafe[K]) Score(value K) (float64, bool) { 104 s.lock.Lock() 105 defer s.lock.Unlock() 106 return s.unsafe.Score(value) 107 } 108 109 func (s *SetSafe[K]) Rank(value K) int { 110 s.lock.Lock() 111 defer s.lock.Unlock() 112 return s.unsafe.Rank(value) 113 } 114 115 func (s *SetSafe[K]) RevRank(value K) int { 116 s.lock.Lock() 117 defer s.lock.Unlock() 118 return s.unsafe.RevRank(value) 119 } 120 121 func (s *SetSafe[K]) Count(min, max float64) int { 122 s.lock.Lock() 123 defer s.lock.Unlock() 124 return s.unsafe.Count(min, max) 125 } 126 127 func (s *SetSafe[K]) CountWithOpt(min, max float64, opt RangeOpt) int { 128 s.lock.Lock() 129 defer s.lock.Unlock() 130 return s.unsafe.CountWithOpt(min, max, opt) 131 } 132 133 func (s *SetSafe[K]) Range(start, stop int) []Node[K] { 134 s.lock.Lock() 135 defer s.lock.Unlock() 136 return s.unsafe.Range(start, stop) 137 } 138 139 func (s *SetSafe[K]) RangeByScore(min, max float64) []Node[K] { 140 s.lock.Lock() 141 defer s.lock.Unlock() 142 return s.unsafe.RangeByScore(min, max) 143 } 144 145 func (s *SetSafe[K]) RangeByScoreWithOpt(min, max float64, opt RangeOpt) []Node[K] { 146 s.lock.Lock() 147 defer s.lock.Unlock() 148 return s.unsafe.RangeByScoreWithOpt(min, max, opt) 149 } 150 151 func (s *SetSafe[K]) RevRange(start, stop int) []Node[K] { 152 s.lock.Lock() 153 defer s.lock.Unlock() 154 return s.unsafe.RevRange(start, stop) 155 } 156 157 func (s *SetSafe[K]) RevRangeByScore(max, min float64) []Node[K] { 158 s.lock.Lock() 159 defer s.lock.Unlock() 160 return s.unsafe.RevRangeByScore(max, min) 161 } 162 163 func (s *SetSafe[K]) RevRangeByScoreWithOpt(max, min float64, opt RangeOpt) []Node[K] { 164 s.lock.Lock() 165 defer s.lock.Unlock() 166 return s.unsafe.RevRangeByScoreWithOpt(max, min, opt) 167 } 168 169 func (s *SetSafe[K]) RemoveRangeByRank(start, stop int) []Node[K] { 170 s.lock.Lock() 171 defer s.lock.Unlock() 172 return s.unsafe.RemoveRangeByRank(start, stop) 173 } 174 175 func (s *SetSafe[K]) RemoveRangeByScore(min, max float64) []Node[K] { 176 s.lock.Lock() 177 defer s.lock.Unlock() 178 return s.unsafe.RemoveRangeByScore(min, max) 179 } 180 181 func (s *SetSafe[K]) RemoveRangeByScoreWithOpt(min, max float64, opt RangeOpt) []Node[K] { 182 s.lock.Lock() 183 defer s.lock.Unlock() 184 return s.unsafe.RemoveRangeByScoreWithOpt(min, max, opt) 185 }