forked from nielsAD/gowarcraft3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitset.go
184 lines (158 loc) · 3.52 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
// Author: Niels A.D.
// Project: gowarcraft3 (https://github.com/nielsAD/gowarcraft3)
// License: Mozilla Public License, v2.0
package protocol
import (
"fmt"
"strconv"
)
// BitSet8 is a set of 8 bits
type BitSet8 uint8
// BS8 is a BitSet8 contructor
func BS8(b ...bool) BitSet8 {
var res BitSet8
for i := uint(0); i < uint(len(b)); i++ {
if b[i] {
res.Set(i + 1)
}
}
return res
}
// Test if bit i is in set
func (b BitSet8) Test(i uint) bool {
if i < 1 || i > 8 {
panic("bitset: Index out of range")
}
return b&(1<<(i-1)) != 0
}
// Set bit i
func (b *BitSet8) Set(i uint) *BitSet8 {
if i < 1 || i > 8 {
panic("bitset: Index out of range")
}
*b |= (1 << (i - 1))
return b
}
// Clear bit i
func (b *BitSet8) Clear(i uint) *BitSet8 {
if i < 1 || i > 8 {
panic("bitset: Index out of range")
}
*b &= ^(1 << (i - 1))
return b
}
func (b BitSet8) String() string {
return fmt.Sprintf("0b%v", strconv.FormatUint(uint64(b), 2))
}
// MarshalText implements TextMarshaler
func (b BitSet8) MarshalText() ([]byte, error) {
return []byte(strconv.FormatUint(uint64(b), 2)), nil
}
// UnmarshalText implements TextUnmarshaler
func (b *BitSet8) UnmarshalText(txt []byte) error {
i, err := strconv.ParseUint(string(txt), 2, 8)
if err == nil {
*b = BitSet8(i)
}
return err
}
// BitSet16 is a set of 16 bits
type BitSet16 uint16
// BS16 is a BitSet16 contructor
func BS16(b ...bool) BitSet16 {
var res BitSet16
for i := uint(0); i < uint(len(b)); i++ {
if b[i] {
res.Set(i + 1)
}
}
return res
}
// Test if bit i is in set
func (b BitSet16) Test(i uint) bool {
if i < 1 || i > 16 {
panic("bitset: Index out of range")
}
return b&(1<<(i-1)) != 0
}
// Set bit i
func (b *BitSet16) Set(i uint) *BitSet16 {
if i < 1 || i > 16 {
panic("bitset: Index out of range")
}
*b |= (1 << (i - 1))
return b
}
// Clear bit i
func (b *BitSet16) Clear(i uint) *BitSet16 {
if i < 1 || i > 16 {
panic("bitset: Index out of range")
}
*b &= ^(1 << (i - 1))
return b
}
func (b BitSet16) String() string {
return fmt.Sprintf("0b%v", strconv.FormatUint(uint64(b), 2))
}
// MarshalText implements TextMarshaler
func (b BitSet16) MarshalText() ([]byte, error) {
return []byte(strconv.FormatUint(uint64(b), 2)), nil
}
// UnmarshalText implements TextUnmarshaler
func (b *BitSet16) UnmarshalText(txt []byte) error {
i, err := strconv.ParseUint(string(txt), 2, 8)
if err == nil {
*b = BitSet16(i)
}
return err
}
// BitSet32 is a set of 32 bits
type BitSet32 uint32
// BS32 is a BitSet32 contructor
func BS32(b ...bool) BitSet32 {
var res BitSet32
for i := uint(0); i < uint(len(b)); i++ {
if b[i] {
res.Set(i + 1)
}
}
return res
}
// Test if bit i is in set
func (b BitSet32) Test(i uint) bool {
if i < 1 || i > 32 {
panic("bitset: Index out of range")
}
return b&(1<<(i-1)) != 0
}
// Set bit i
func (b *BitSet32) Set(i uint) *BitSet32 {
if i < 1 || i > 32 {
panic("bitset: Index out of range")
}
*b |= (1 << (i - 1))
return b
}
// Clear bit i
func (b *BitSet32) Clear(i uint) *BitSet32 {
if i < 1 || i > 32 {
panic("bitset: Index out of range")
}
*b &= ^(1 << (i - 1))
return b
}
func (b BitSet32) String() string {
return fmt.Sprintf("0b%v", strconv.FormatUint(uint64(b), 2))
}
// MarshalText implements TextMarshaler
func (b BitSet32) MarshalText() ([]byte, error) {
return []byte(strconv.FormatUint(uint64(b), 2)), nil
}
// UnmarshalText implements TextUnmarshaler
func (b *BitSet32) UnmarshalText(txt []byte) error {
i, err := strconv.ParseUint(string(txt), 2, 8)
if err == nil {
*b = BitSet32(i)
}
return err
}