-
Notifications
You must be signed in to change notification settings - Fork 20
/
bitset.go
71 lines (61 loc) · 1.51 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
package goga
// Bitset - a simple bitset implementation
type Bitset struct {
size int
bits []int
}
// Create - creates a bitset of length 'size'
func (b *Bitset) Create(size int) {
b.size = size
b.bits = make([]int, size)
}
// GetSize returns the size of the bitset
func (b *Bitset) GetSize() int {
return b.size
}
// Get returns the value in the bitset at index 'index'
// or -1 if the index is out of range
func (b *Bitset) Get(index int) int {
if index < b.size {
return b.bits[index]
}
return -1
}
// GetAll returns an int array of all the bits in the bitset
func (b *Bitset) GetAll() []int {
return b.bits
}
func (b *Bitset) setImpl(index, value int) {
b.bits[index] = value
}
// Set assigns value 'value' to the bit at index 'index'
func (b *Bitset) Set(index, value int) bool {
if index < b.size {
b.setImpl(index, value)
return true
}
return false
}
// SetAll assigns the value 'value' to all the bits in the set
func (b *Bitset) SetAll(value int) {
for i := 0; i < b.size; i++ {
b.setImpl(i, value)
}
}
// CreateCopy returns a bit for bit copy of the bitset
func (b *Bitset) CreateCopy() Bitset {
newBitset := Bitset{}
newBitset.Create(b.size)
for i := 0; i < b.size; i++ {
newBitset.Set(i, b.Get(i))
}
return newBitset
}
// Slice returns an out of memory slice of the current bitset
// between bits 'startingBit' and 'startingBit + size'
func (b *Bitset) Slice(startingBit, size int) Bitset {
ret := Bitset{}
ret.Create(size)
ret.bits = b.bits[startingBit : startingBit+size]
return ret
}