-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitmap.go
More file actions
157 lines (136 loc) · 2.82 KB
/
bitmap.go
File metadata and controls
157 lines (136 loc) · 2.82 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package gin
import (
"github.com/RoaringBitmap/roaring/v2"
"github.com/pkg/errors"
)
type RGSet struct {
bitmap *roaring.Bitmap
NumRGs int
}
type RGSetOption func(*RGSet) error
func NewRGSet(numRGs int, opts ...RGSetOption) (*RGSet, error) {
if numRGs <= 0 {
return nil, errors.New("numRGs must be greater than 0")
}
rs := &RGSet{
bitmap: roaring.New(),
NumRGs: numRGs,
}
for _, opt := range opts {
if err := opt(rs); err != nil {
return nil, err
}
}
return rs, nil
}
func MustNewRGSet(numRGs int, opts ...RGSetOption) *RGSet {
rs, err := NewRGSet(numRGs, opts...)
if err != nil {
panic(err)
}
return rs
}
func RGSetFromRoaring(bitmap *roaring.Bitmap, numRGs int) *RGSet {
return &RGSet{
bitmap: bitmap,
NumRGs: numRGs,
}
}
func (rs *RGSet) Set(rgID int) {
if rgID < 0 || rgID >= rs.NumRGs {
return
}
rs.bitmap.Add(uint32(rgID))
}
func (rs *RGSet) Clear(rgID int) {
if rgID < 0 || rgID >= rs.NumRGs {
return
}
rs.bitmap.Remove(uint32(rgID))
}
func (rs *RGSet) IsSet(rgID int) bool {
if rgID < 0 || rgID >= rs.NumRGs {
return false
}
return rs.bitmap.Contains(uint32(rgID))
}
func (rs *RGSet) Intersect(other *RGSet) *RGSet {
result := rs.bitmap.Clone()
result.And(other.bitmap)
return &RGSet{
bitmap: result,
NumRGs: rs.NumRGs,
}
}
func (rs *RGSet) Union(other *RGSet) *RGSet {
result := rs.bitmap.Clone()
result.Or(other.bitmap)
maxRGs := rs.NumRGs
if other.NumRGs > maxRGs {
maxRGs = other.NumRGs
}
return &RGSet{
bitmap: result,
NumRGs: maxRGs,
}
}
// UnionWith merges other into rs in place, avoiding the per-call clone of
// Union. Use this when the receiver is exclusively owned and the result
// does not need to preserve the prior state.
func (rs *RGSet) UnionWith(other *RGSet) {
rs.bitmap.Or(other.bitmap)
if other.NumRGs > rs.NumRGs {
rs.NumRGs = other.NumRGs
}
}
func (rs *RGSet) All() *RGSet {
result := roaring.New()
result.AddRange(0, uint64(rs.NumRGs))
return &RGSet{
bitmap: result,
NumRGs: rs.NumRGs,
}
}
func AllRGs(numRGs int) *RGSet {
bitmap := roaring.New()
bitmap.AddRange(0, uint64(numRGs))
return &RGSet{
bitmap: bitmap,
NumRGs: numRGs,
}
}
func NoRGs(numRGs int) *RGSet {
return MustNewRGSet(numRGs)
}
func (rs *RGSet) IsEmpty() bool {
return rs.bitmap.IsEmpty()
}
func (rs *RGSet) Count() int {
return int(rs.bitmap.GetCardinality())
}
func (rs *RGSet) ToSlice() []int {
vals := rs.bitmap.ToArray()
result := make([]int, len(vals))
for i, v := range vals {
result[i] = int(v)
}
return result
}
func (rs *RGSet) Clone() *RGSet {
return &RGSet{
bitmap: rs.bitmap.Clone(),
NumRGs: rs.NumRGs,
}
}
func (rs *RGSet) Invert() *RGSet {
all := roaring.New()
all.AddRange(0, uint64(rs.NumRGs))
all.AndNot(rs.bitmap)
return &RGSet{
bitmap: all,
NumRGs: rs.NumRGs,
}
}
func (rs *RGSet) Roaring() *roaring.Bitmap {
return rs.bitmap
}