-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbit.go
440 lines (348 loc) · 8.6 KB
/
bit.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package asdf
type Flag8 uint8
type Flag16 uint16
type Flag32 uint32
type Flag64 uint64
type FlagU uint
/******************************************************************************/
func (me Flag8) Flag(bit int) Flag8 {
return 1 << Flag8(bit)
}
func (me Flag16) Flag(bit int) Flag16 {
return 1 << Flag16(bit)
}
func (me Flag32) Flag(bit int) Flag32 {
return 1 << Flag32(bit)
}
func (me Flag64) Flag(bit int) Flag64 {
return 1 << Flag64(bit)
}
func (me FlagU) Flag(bit int) FlagU {
return 1 << FlagU(bit)
}
/******************************************************************************/
func (me Flag8) Set(flag Flag8) Flag8 {
return me | flag
}
func (me Flag16) Set(flag Flag16) Flag16 {
return me | flag
}
func (me Flag32) Set(flag Flag32) Flag32 {
return me | flag
}
func (me Flag64) Set(flag Flag64) Flag64 {
return me | flag
}
func (me FlagU) Set(flag FlagU) FlagU {
return me | flag
}
/******************************************************************************/
func (me Flag8) SetBit(bit int) Flag8 {
return me.Set(me.Flag(bit))
}
func (me Flag16) SetBit(bit int) Flag16 {
return me.Set(me.Flag(bit))
}
func (me Flag32) SetBit(bit int) Flag32 {
return me.Set(me.Flag(bit))
}
func (me Flag64) SetBit(bit int) Flag64 {
return me.Set(me.Flag(bit))
}
func (me FlagU) SetBit(bit int) FlagU {
return me.Set(me.Flag(bit))
}
/******************************************************************************/
func (me Flag8) Clear(flag Flag8) Flag8 {
return me & ^flag
}
func (me Flag16) Clear(flag Flag16) Flag16 {
return me & ^flag
}
func (me Flag32) Clear(flag Flag32) Flag32 {
return me & ^flag
}
func (me Flag64) Clear(flag Flag64) Flag64 {
return me & ^flag
}
func (me FlagU) Clear(flag FlagU) FlagU {
return me & ^flag
}
/******************************************************************************/
func (me Flag8) ClearBit(bit int) Flag8 {
return me.Clear(me.Flag(bit))
}
func (me Flag16) ClearBit(bit int) Flag16 {
return me.Clear(me.Flag(bit))
}
func (me Flag32) ClearBit(bit int) Flag32 {
return me.Clear(me.Flag(bit))
}
func (me Flag64) ClearBit(bit int) Flag64 {
return me.Clear(me.Flag(bit))
}
func (me FlagU) ClearBit(bit int) FlagU {
return me.Clear(me.Flag(bit))
}
/******************************************************************************/
func (me Flag8) Has(flag Flag8) bool {
return flag == (flag & me)
}
func (me Flag16) Has(flag Flag16) bool {
return flag == (flag & me)
}
func (me Flag32) Has(flag Flag32) bool {
return flag == (flag & me)
}
func (me Flag64) Has(flag Flag64) bool {
return flag == (flag & me)
}
func (me FlagU) Has(flag FlagU) bool {
return flag == (flag & me)
}
/******************************************************************************/
func (me Flag8) HasBit(bit int) bool {
return me.Has(me.Flag(bit))
}
func (me Flag16) HasBit(bit int) bool {
return me.Has(me.Flag(bit))
}
func (me Flag32) HasBit(bit int) bool {
return me.Has(me.Flag(bit))
}
func (me Flag64) HasBit(bit int) bool {
return me.Has(me.Flag(bit))
}
func (me FlagU) HasBit(bit int) bool {
return me.Has(me.Flag(bit))
}
//==============================================================================
func SetFlag8(x, flag uint8) uint8 {
return x | flag
}
func SetFlag16(x, flag uint16) uint16 {
return x | flag
}
func SetFlag32(x, flag uint32) uint32 {
return x | flag
}
func SetFlag64(x, flag uint64) uint64 {
return x | flag
}
func SetFlag(x, flag uint) uint {
return x | flag
}
/******************************************************************************/
func ClrFlag8(x, flag uint8) uint8 {
return x & ^flag
}
func ClrFlag16(x, flag uint16) uint16 {
return x & ^flag
}
func ClrFlag32(x, flag uint32) uint32 {
return x & ^flag
}
func ClrFlag64(x, flag uint64) uint64 {
return x & ^flag
}
func ClrFlag(x, flag uint) uint {
return x & ^flag
}
/******************************************************************************/
func HasFlag8(x, flag uint8) bool {
return flag == (x & flag)
}
func HasFlag16(x, flag uint16) bool {
return flag == (x & flag)
}
func HasFlag32(x, flag uint32) bool {
return flag == (x & flag)
}
func HasFlag64(x, flag uint64) bool {
return flag == (x & flag)
}
func HasFlag(x, flag uint) bool {
return flag == (x & flag)
}
/******************************************************************************/
func SetBit8(x, bit uint8) uint8 {
return SetFlag8(x, 1<<bit)
}
func SetBit16(x, bit uint16) uint16 {
return SetFlag16(x, 1<<bit)
}
func SetBit32(x, bit uint32) uint32 {
return SetFlag32(x, 1<<bit)
}
func SetBit64(x, bit uint64) uint64 {
return SetFlag64(x, 1<<bit)
}
func SetBit(x, bit uint) uint {
return SetFlag(x, 1<<bit)
}
/******************************************************************************/
func ClrBit8(x, bit uint8) uint8 {
return ClrFlag8(x, 1<<bit)
}
func ClrBit16(x, bit uint16) uint16 {
return ClrFlag16(x, 1<<bit)
}
func ClrBit32(x, bit uint32) uint32 {
return ClrFlag32(x, 1<<bit)
}
func ClrBit64(x, bit uint64) uint64 {
return ClrFlag64(x, 1<<bit)
}
func ClrBit(x, bit uint) uint {
return ClrFlag(x, 1<<bit)
}
/******************************************************************************/
func HasBit8(x, bit uint8) bool {
return HasFlag8(x, 1<<bit)
}
func HasBit16(x, bit uint16) bool {
return HasFlag16(x, 1<<bit)
}
func HasBit32(x, bit uint32) bool {
return HasFlag32(x, 1<<bit)
}
func HasBit64(x, bit uint64) bool {
return HasFlag64(x, 1<<bit)
}
func HasBit(x, bit uint) bool {
return HasFlag(x, 1<<bit)
}
/******************************************************************************/
type BitMap32 []uint32
const BitMapSlot32 = 32
func (me BitMap32) isGoodIdx(idx uint32) bool {
return int(idx) < len(me)
}
func (me BitMap32) SetBit(bit uint32) {
idx := bit / BitMapSlot32
if me.isGoodIdx(idx) {
SetBit32(me[idx], bit%BitMapSlot32)
}
}
func (me BitMap32) ClrBit(bit uint32) {
idx := bit / BitMapSlot32
if me.isGoodIdx(idx) {
ClrBit32(me[idx], bit%BitMapSlot32)
}
}
func (me BitMap32) HasBit(bit uint32) bool {
idx := bit / BitMapSlot32
if !me.isGoodIdx(idx) {
return false
}
return HasBit32(me[idx], bit%BitMapSlot32)
}
/******************************************************************************/
type BitMap64 []uint64
const BitMapSlot64 = 64
func (me BitMap64) isGoodIdx(idx uint64) bool {
return int(idx) < len(me)
}
func (me BitMap64) SetBit(bit uint64) {
idx := bit / BitMapSlot64
if me.isGoodIdx(idx) {
SetBit64(me[idx], bit%BitMapSlot64)
}
}
func (me BitMap64) ClrBit(bit uint64) {
idx := bit / BitMapSlot64
if me.isGoodIdx(idx) {
ClrBit64(me[idx], bit%BitMapSlot64)
}
}
func (me BitMap64) HasBit(bit uint64) bool {
idx := bit / BitMapSlot64
if !me.isGoodIdx(idx) {
return false
}
return HasBit64(me[idx], bit%BitMapSlot64)
}
/******************************************************************************/
type bitset_entry_t = uint64
const (
__BITSET_HDRSIZE = 2 * SizeofInt32
__BITSET_N = 8 * SizeofInt64
__BITSET_MAX = SizeofG
)
func __BITSET_ELM(idx uint32) uint32 {
return idx / __BITSET_N
}
func __BITSET_MASK(idx uint32) bitset_entry_t {
return 1 << (bitset_entry_t(idx) % __BITSET_N)
}
func __BITSET_END(Cap uint32) uint32 {
return Align32(Cap, __BITSET_N)
}
func __BITSET_ENTRY_SIZE(Cap uint32) uint32 {
return SizeofInt64 * __BITSET_END(Cap)
}
func __BITSET_SIZE(Cap uint32) uint32 {
return __BITSET_ENTRY_SIZE(Cap) + __BITSET_HDRSIZE
}
type BitSet struct {
Cap uint32
Count uint32
entry [__BITSET_MAX]bitset_entry_t
}
func (me *BitSet) Init(Cap uint32) {
me.Cap = Cap
me.Zero()
}
func (me *BitSet) Zero() {
me.Count = 0
count := __BITSET_END(me.Cap)
for i := uint32(0); i < count; i++ {
me.entry[i] = 0
}
}
func (me *BitSet) EntrySize() uint32 {
return __BITSET_ENTRY_SIZE(me.Cap)
}
func (me *BitSet) Size() uint32 {
return __BITSET_SIZE(me.Cap)
}
func (me *BitSet) IsSet(idx uint32) bool {
return 0 != me.entry[__BITSET_ELM(idx)]&__BITSET_MASK(idx)
}
func (me *BitSet) Clear(idx uint32) {
if me.IsSet(idx) {
me.Count--
me.entry[__BITSET_ELM(idx)] &= ^__BITSET_MASK(idx)
}
}
func (me *BitSet) Set(idx uint32) {
if false == me.IsSet(idx) {
me.Count++
me.entry[__BITSET_ELM(idx)] |= __BITSET_MASK(idx)
}
}
/******************************************************************************/
type BitsMapper struct {
Type string
Bits uint64
Names map[int]string
}
func (me *BitsMapper) Name(flags uint64) string {
name := make([]byte, 0, 1024)
for i := uint64(0); i < me.Bits; i++ {
flag := uint64(1) << i
if flag == (flag & flags) {
v, ok := me.Names[int(flag)]
if ok {
name = append(name, []byte(v)...)
name = append(name, '|')
}
}
}
Len := len(name)
if Len > 0 {
return string(name[:Len-1])
} else {
return Unknow
}
}