-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
79 lines (64 loc) · 1.78 KB
/
set.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Highly optimized Cuckoo hash set implementation
* LICENSE: MIT
*/
package cuckoohash
import (
"fmt"
"strings"
)
type Set struct {
m Map
}
func newSet(bytesPerKey, keysPerBucket, bucketCount uint32, hasher1, hasher2 hash64WithSeedFunc, debug, expandable bool) (*Set, error) {
m, err := newMap(bytesPerKey, keysPerBucket, bucketCount, hasher1, hasher2, debug, expandable)
if err != nil {
return nil, err
}
return &Set{m: *m}, nil
}
func NewSet(bytesPerKey, keysPerBucket, bucketCount uint32, hasher1, hasher2 hash64WithSeedFunc, expandableOpt ...bool) (*Set, error) {
expandable := true
if n := len(expandableOpt); n > 1 {
panic(fmt.Sprintf("at most one `expandableOpt` argument can be passed, got %v", n))
} else if n != 0 {
expandable = expandableOpt[0]
}
return newSet(bytesPerKey, keysPerBucket, bucketCount, hasher1, hasher2, false, expandable)
}
func (s *Set) Clear() {
s.m.Clear()
}
func (s *Set) Count() uint64 {
return s.m.Count()
}
func (s *Set) IsEmpty() bool {
return s.Count() == 0
}
func (s *Set) MemoryInBytes() uint64 {
return s.m.MemoryInBytes()
}
func (s *Set) LoadFactor() float64 {
return s.m.LoadFactor()
}
func (s *Set) Contains(key []byte) bool {
return s.m.ContainsKey(key)
}
// Return true if key deleted from Set, false if key absent previously.
func (s *Set) Del(key []byte) bool {
_, err := s.m.Del(key)
// The only possible error is ErrKeyNotFound
return err == nil
}
// Return true if key put in Set, false if the bucket if full(s.m.expandable is false)
func (s *Set) Put(key []byte) bool {
_, err := s.m.Put(key, nil, true)
return err == nil
}
var (
mapTypeString = fmt.Sprintf("%T", Map{})
setTypeString = fmt.Sprintf("%T", Set{})
)
func (s *Set) String() string {
return strings.ReplaceAll(s.m.String(), mapTypeString, setTypeString)
}