-
Notifications
You must be signed in to change notification settings - Fork 0
/
intset.go
136 lines (119 loc) · 2.71 KB
/
intset.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
/**
* IntSet で使われている個々のワードの型は uint64 ですが、64ビット演算は 32ビットプラットフォーム上では非効率かもしれません。
* プラットフォームに対して最も効率的な符号なし変数である uint 型を使うようにプログラムを修正しなさい。
* 64で割る代わりに、unit の実質的サイズのビット数である 32 あるいは 64 を保持する定数を定義しなさい。
* そのためには、おそらくかなり賢い式 32 << (^uint(0) >> 63) を使えます。
*/
package intset
import (
"../popcount"
"bytes"
"fmt"
)
const bitSize = 32 << (^uint(0) >> 63)
type IntSet struct {
words []uint
}
func (s *IntSet) Len() int {
len := 0
for _, word := range s.words {
len += popcount.PopCount(uint64(word))
}
return len
}
func (s *IntSet) Elems() []int {
var ret []int
for i, word := range s.words {
if word == 0 {
continue
}
for bit := 0; bit < bitSize; bit++ {
if word&(1<<uint(bit)) != 0 {
x := i*bitSize + bit
ret = append(ret, x)
}
}
}
return ret
}
func (s *IntSet) Has(x int) bool {
word, bit := x/bitSize, uint(x%bitSize)
return word < len(s.words) && s.words[word]&(1<<bit) != 0
}
func (s *IntSet) Add(x int) {
s.add(x)
}
func (s *IntSet) AddAll(xs ...int) {
for _, x := range xs {
s.add(x)
}
}
func (s *IntSet) Remove(x int) {
word, bit := x/bitSize, uint(x%bitSize)
if word >= len(s.words) {
return // Nothing to do
}
s.words[word] &= ^(1 << bit)
}
func (s *IntSet) Clear() {
s.words = []uint{}
}
func (s *IntSet) Copy() *IntSet {
var copied IntSet
copied.words = append(copied.words, s.words...)
return &copied
}
func (s *IntSet) UnionWith(t *IntSet) {
for i, tword := range t.words {
if i < len(s.words) {
s.words[i] |= tword
} else {
s.words = append(s.words, tword)
}
}
}
func (s *IntSet) IntersectWith(t *IntSet) {
for i, tword := range t.words {
if i < len(s.words) {
s.words[i] &= tword
} else {
return
}
}
}
func (s *IntSet) DifferenceWith(t *IntSet) {
for i, tword := range t.words {
s.words[i] &^= tword
}
}
func (s *IntSet) SymmetricDifference(t *IntSet) {
for i, tword := range t.words {
s.words[i] ^= tword
}
}
func (s *IntSet) String() string {
var buf bytes.Buffer
buf.WriteByte('{')
for i, word := range s.words {
if word == 0 {
continue
}
for j := 0; j < bitSize; j++ {
if word&(1<<uint(j)) != 0 {
if buf.Len() > len("{") {
buf.WriteByte(' ')
}
fmt.Fprintf(&buf, "%d", bitSize*i+j)
}
}
}
buf.WriteByte('}')
return buf.String()
}
func (s *IntSet) add(x int) {
word, bit := x/bitSize, uint(x%bitSize)
for word >= len(s.words) {
s.words = append(s.words, 0)
}
s.words[word] |= 1 << bit
}