-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitset.go
149 lines (128 loc) · 3.5 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
package intset
import (
"fmt"
"math/big"
"strings"
)
const wordSize = 1 << (^uintptr(0)>>32&1 + ^uintptr(0)>>16&1 + ^uintptr(0)>>8&1 + 3)
// BitSet is an integer set backed by a bitset implemented using big.Int.
type BitSet struct {
data *big.Int
}
// NewBitSet is the constructor for BitSet. Max is ignored in this set
// implementation.
func NewBitSet(max int) *BitSet {
return new(BitSet).init(max)
}
func (set *BitSet) init(max int) *BitSet {
set.data = big.NewInt(0)
return set
}
// Clear the set.
func (set *BitSet) Clear() *BitSet {
set.data = big.NewInt(0)
return set
}
// Size returns the number of integers in the set.
// Alogrithm stolen from github.com/kisielk/bigset
func (set *BitSet) Size() int {
var l int
zero := big.NewInt(0)
v := new(big.Int).Set(set.data)
for l = 0; v.Cmp(zero) != 0; l++ {
vMinusOne := new(big.Int).Sub(v, big.NewInt(1))
v.And(v, vMinusOne)
}
return l
}
// Add one or more integers to the set.
func (set *BitSet) Add(ints ...int) *BitSet {
for _, i := range ints {
set.data.SetBit(set.data, i, 1)
}
return set
}
// Remove one or more integers from the set.
func (set *BitSet) Remove(ints ...int) *BitSet {
for _, i := range ints {
set.data.SetBit(set.data, i, 0)
}
return set
}
// All returns a slice of all the integers in the set in ascending order.
func (set *BitSet) All() []int {
var all []int
for i, w := range set.data.Bits() {
for j, z := 0, w; z != 0; j, z = j+1, z>>1 {
if z&1 != 0 {
all = append(all, i*wordSize+j)
}
}
}
return all
}
// Contains returns true if all ints are in the set, otherwise false.
func (set *BitSet) Contains(ints ...int) bool {
for _, i := range ints {
if set.data.Bit(i) != uint(1) {
return false
}
}
return true
}
// Equal checks if two sets both contains all the same items.
func (set *BitSet) Equal(other *BitSet) bool {
return set.data.Cmp(other.data) == 0
}
// SubsetOf checks if all items in set are also present in other set.
func (set *BitSet) SubsetOf(other *BitSet) bool {
for _, i := range set.All() {
if !other.Contains(i) {
return false
}
}
return true
}
// SupersetOf checks if a set is a superset of another set.
func (set *BitSet) SupersetOf(other *BitSet) bool {
return other.SubsetOf(set)
}
// Union returns a new set which is the union of two sets.
func (set *BitSet) Union(other *BitSet) *BitSet {
result := NewBitSet(0)
result.data.Or(set.data, other.data)
return result
}
// Intersection returns a new set with integers common to both sets.
func (set *BitSet) Intersection(other *BitSet) *BitSet {
result := NewBitSet(0)
result.data.And(set.data, other.data)
return result
}
// Difference returns a new set with the integers in set which are not in other.
func (set *BitSet) Difference(other *BitSet) *BitSet {
result := NewBitSet(0)
result.data.And(set.data, new(big.Int).Not(other.data))
return result
}
// SymetricDifference returns a new set with the integers in current and other,
// but not in both.
func (set *BitSet) SymetricDifference(other *BitSet) *BitSet {
result := NewBitSet(0)
result.data.Xor(set.data, other.data)
return result
}
// Clone returns a new set which is a clone of current set.
func (set *BitSet) Clone() *BitSet {
result := NewBitSet(0)
result.data.Set(set.data)
return result
}
// String implements the Stringer interface for BitSet.
func (set *BitSet) String() string {
items := make([]string, 0, set.Size())
for _, i := range set.All() {
items = append(items, fmt.Sprintf("%v", i))
}
return fmt.Sprintf("Set{%s}", strings.Join(items, ", "))
}