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
|
// Package bits provides a bit vector backed with uints
package bits
import (
"strconv"
)
// The Vector interface defines methods on the bit vector
type Vector interface {
// Retrieves the bit at the designated position
Peek(uint) bool
// Sets the bit at the designated position
Poke(uint, bool)
// Flips the bit at the designated position
Flip(uint)
// Returns the total number of elements supported by the vector
Len() uint
}
type boolvector []bool
// Retrieves the bit at the designated position
func (v boolvector) Peek(pos uint) bool {
return v[pos]
}
|
|
>
|
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
|
// Package bits provides a bit vector interface and several implementations
package bits
import (
"strconv"
)
// The Vector interface defines methods on the bit vector
type Vector interface {
// Retrieves the bit at the designated position
Peek(uint) bool
// Sets the bit at the designated position
Poke(uint, bool)
// Flips the bit at the designated position
Flip(uint)
// Returns the total number of elements supported by the vector
Len() uint
}
// A Vector type that simply stores booleans in a slice
type boolvector []bool
// Retrieves the bit at the designated position
func (v boolvector) Peek(pos uint) bool {
return v[pos]
}
|
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
v[pos] = !v[pos]
}
// Returns the total number of elements supported by the vector
func (v boolvector) Len() uint {
return uint(len(v))
}
type vector []uint
// Retrieves the bit at the designated position
func (v vector) Peek(pos uint) bool {
var slot, offset uint = at(pos)
return (v[slot] & (1 << offset)) > 0
|
>
>
>
>
>
|
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
v[pos] = !v[pos]
}
// Returns the total number of elements supported by the vector
func (v boolvector) Len() uint {
return uint(len(v))
}
func NewBool(size uint) Vector {
var slice []bool = make([]bool, size)
return boolvector(slice)
}
type vector []uint
// Retrieves the bit at the designated position
func (v vector) Peek(pos uint) bool {
var slot, offset uint = at(pos)
return (v[slot] & (1 << offset)) > 0
|
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
}
func at(pos uint) (uint, uint) {
return pos / strconv.IntSize, pos % strconv.IntSize
}
// Create a new bit vector sized up to the desired number of elements
func New(size uint) Vector {
var length uint = size / strconv.IntSize
if size%strconv.IntSize > 0 {
// Allocate one additional slot if the desired
// size is does not divide by 32/64
length++
}
var slice []uint = make([]uint, length)
return vector(slice)
}
func NewBool(size uint) Vector {
var slice []bool = make([]bool, size)
return boolvector(slice)
}
|
|
<
<
<
<
<
|
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
}
func at(pos uint) (uint, uint) {
return pos / strconv.IntSize, pos % strconv.IntSize
}
// Create a new bit vector sized up to the desired number of elements
func NewBit(size uint) Vector {
var length uint = size / strconv.IntSize
if size%strconv.IntSize > 0 {
// Allocate one additional slot if the desired
// size is does not divide by 32/64
length++
}
var slice []uint = make([]uint, length)
return vector(slice)
}
|