-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
54 lines (46 loc) · 780 Bytes
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package kongjian
import (
"sync"
)
type StoreInter[T any] interface {
Add(v T) error
Exist(v T) (bool, error)
Delete(v T) error
}
var _ StoreInter[int64] = (*StoreInt64)(nil)
type StoreInt64 struct {
sync.Mutex
count map[int64]uint64
}
func NewStoreInt64() *StoreInt64 {
return &StoreInt64{
Mutex: sync.Mutex{},
count: make(map[int64]uint64),
}
}
func (s *StoreInt64) Add(v int64) error {
s.Lock()
defer s.Unlock()
_, ok := s.count[v]
if ok {
s.count[v]++
} else {
s.count[v] = 1
}
return nil
}
func (s *StoreInt64) Exist(v int64) (bool, error) {
s.Lock()
defer s.Unlock()
_, ok := s.count[v]
return ok, nil
}
func (s *StoreInt64) Delete(v int64) error {
s.Lock()
defer s.Unlock()
_, ok := s.count[v]
if ok {
s.count[v]--
}
return nil
}