-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin.go
56 lines (44 loc) · 1 KB
/
in.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
package xim
import (
"fmt"
)
// Bit - describes In-Filter mask bit
type Bit uint16
// InBuilder - creates Bit for In-Filter
type InBuilder struct {
nextBit Bit
}
// NewInBuilder - creates InBuilder
func NewInBuilder() *InBuilder {
return &InBuilder{nextBit: 1}
}
// NewBit - returns a new bit shifted.
func (f *InBuilder) NewBit() Bit {
if f.nextBit == 0 {
panic("overflow")
}
bit := f.nextBit
f.nextBit <<= 1
return bit
}
// Indexes - creates indexes for In-Filter with multi-bits
func (f *InBuilder) Indexes(bits ...Bit) []string {
allBits := f.combineBits(bits...)
indexes := make([]string, 0)
for i := Bit(1); i <= f.nextBit-1; i++ {
if i&allBits != 0 {
indexes = append(indexes, fmt.Sprintf("%x", i))
}
}
return indexes
}
// Indexes - creates indexes for In-Filter
func (f *InBuilder) Filter(bits ...Bit) string {
return fmt.Sprintf("%x", f.combineBits(bits...))
}
func (f *InBuilder) combineBits(bits ...Bit) (allBits Bit) {
for _, bit := range bits {
allBits |= bit
}
return
}