forked from Consensys/handel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitset.go
207 lines (180 loc) · 5.56 KB
/
bitset.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package handel
import (
"bytes"
"encoding/binary"
"github.com/willf/bitset"
)
// BitSet interface. Available implementation is a wrapper around wilff's bitset
// library.
type BitSet interface {
// BitLength returns the fixed size of this BitSet
BitLength() int
// Cardinality returns the number of '1''s set
Cardinality() int
// Set the bit at the given index to 1 or 0 depending on the given boolean.
// A set out of bounds is an error, implementations should panic in such a case.
Set(int, bool)
// Get returns the status of the i-th bit in this bitset.
// A get out of bounds is an error, implementations should panic in such a case.
Get(int) bool
// MarshalBinary returns the binary representation of the BitSet.
MarshalBinary() ([]byte, error)
// UnmarshalBinary fills the bitset from the given buffer.
UnmarshalBinary([]byte) error
// returns the binary representation of this bitset in string
String() string
// All returns true if all bits are set, false otherwise. Returns true for
// empty sets.
All() bool
// None returns true if no bit is set, false otherwise. Returns true for
// empty sets.
None() bool
// Any returns true if any bit is set, false otherwise
Any() bool
// Or between this bitset and another, returns a new bitset.
Or(b2 BitSet) BitSet
// And between this bitset and another, returns a new bitset.
And(b2 BitSet) BitSet
// Xor between this bitset and another, returns a new bitset.
Xor(b2 BitSet) BitSet
// IsSuperSet returns true if this is a superset of the other set
IsSuperSet(b2 BitSet) bool
// NextSet returns the next bit set from the specified index,
// including possibly the current index
// along with an error code (true = valid, false = no set bit found)
// for i,e := v.NextSet(0); e; i,e = v.NextSet(i + 1) {...}
NextSet(i int) (int, bool)
// IntersectionCardinality computes the cardinality of the differnce
IntersectionCardinality(b2 BitSet) int
// Clone this BitSet
Clone() BitSet
}
// WilffBitSet implements a BitSet using the wilff library.
type WilffBitSet struct {
b *bitset.BitSet
l int
}
// NewWilffBitset returns a BitSet implemented using the wilff's bitset library.
func NewWilffBitset(length int) BitSet {
return &WilffBitSet{
b: bitset.New(uint(length)),
l: length,
}
}
func newWilffBitset(bs *bitset.BitSet) BitSet {
return &WilffBitSet{
b: bs,
l: int(bs.Len()),
}
}
// BitLength implements the BitSet interface
func (w *WilffBitSet) BitLength() int {
return int(w.l)
}
// Cardinality implements the BitSet interface
func (w *WilffBitSet) Cardinality() int {
return int(w.b.Count())
}
// Set implements the BitSet interface
func (w *WilffBitSet) Set(idx int, status bool) {
if !w.inBound(idx) {
panic("bitset: set out of bounds")
}
w.b = w.b.SetTo(uint(idx), status)
}
// Get implements the BitSet interface
func (w *WilffBitSet) Get(idx int) bool {
if !w.inBound(idx) {
panic("bitset: get out of bounds")
}
return w.b.Test(uint(idx))
}
// Combine implements the BitSet interface
func (w *WilffBitSet) Combine(b2 BitSet) BitSet {
// XXX Panics if used wrongly at the moment. Could be possible to use other
// implementations by using the interface's method and implementing or
// ourselves.
w2 := b2.(*WilffBitSet)
totalLength := w.l + w2.l
w3 := NewWilffBitset(totalLength).(*WilffBitSet)
w3.b.InPlaceUnion(w.b)
for i := 0; i < w2.l; i++ {
w3.Set(i+w.l, w2.Get(i))
}
return w
}
// Or implements the BitSet interface
func (w *WilffBitSet) Or(b2 BitSet) BitSet {
return newWilffBitset(w.b.Union(b2.(*WilffBitSet).b))
}
// And implements the BitSet interface
func (w *WilffBitSet) And(b2 BitSet) BitSet {
return newWilffBitset(w.b.Intersection(b2.(*WilffBitSet).b))
}
// Xor implements the BitSet interface
func (w *WilffBitSet) Xor(b2 BitSet) BitSet {
return newWilffBitset(w.b.SymmetricDifference(b2.(*WilffBitSet).b))
}
// Clone implements the BitSet interface
func (w *WilffBitSet) Clone() BitSet {
return newWilffBitset(w.b.Clone())
}
func (w *WilffBitSet) inBound(idx int) bool {
return !(idx < 0 || idx >= w.l)
}
// IsSuperSet implements the BitSet interface
func (w *WilffBitSet) IsSuperSet(b2 BitSet) bool {
return w.b.IsSuperSet(b2.(*WilffBitSet).b)
}
// MarshalBinary implements the go Marshaler interface. It encodes the size
// first and then the bitset.
func (w *WilffBitSet) MarshalBinary() ([]byte, error) {
var b bytes.Buffer
err := binary.Write(&b, binary.BigEndian, uint16(w.l))
if err != nil {
return nil, err
}
buff, err := w.b.MarshalBinary()
if err != nil {
return nil, err
}
b.Write(buff)
return b.Bytes(), nil
}
// UnmarshalBinary implements the go Marshaler interface. It decodes the length
// first and then the bitset.
func (w *WilffBitSet) UnmarshalBinary(buff []byte) error {
var b = bytes.NewBuffer(buff)
var length uint16
err := binary.Read(b, binary.BigEndian, &length)
if err != nil {
return err
}
w.b = new(bitset.BitSet)
w.l = int(length)
return w.b.UnmarshalBinary(b.Bytes())
}
func (w *WilffBitSet) String() string {
return w.b.String()
}
// All implements the BitSet interface
func (w *WilffBitSet) All() bool {
return w.b.All()
}
// None implements the BitSet interface
func (w *WilffBitSet) None() bool {
return w.b.None()
}
// Any implements the BitSet interface
func (w *WilffBitSet) Any() bool {
return w.b.Any()
}
// NextSet implements the BitSet interface
func (w *WilffBitSet) NextSet(i int) (int, bool) {
ni, res := w.b.NextSet(uint(i))
return int(ni), res
}
// IntersectionCardinality implements the BitSet interface
func (w *WilffBitSet) IntersectionCardinality(b2 BitSet) int {
return int(w.b.IntersectionCardinality(b2.(*WilffBitSet).b))
}