-
Notifications
You must be signed in to change notification settings - Fork 0
/
crush.go
39 lines (34 loc) · 770 Bytes
/
crush.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
// Copyright (c) 2009 Helmar Wodtke. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// The MIT License is an OSI approved license and can
// be found at
// http://www.opensource.org/licenses/mit-license.php
// "crush" bytes into bits - variable length.
package crush
type BitT struct {
s []byte
p, b int
}
var mask = [9]int{0, 1, 3, 7, 15, 31, 63, 127, 255}
func (x *BitT) Get(n int) (r int) {
if x.b == 0 {
x.b = 8
x.p++
}
if x.b >= n {
x.b -= n
r = int(x.s[x.p]>>uint8(x.b)) & mask[n]
} else {
n -= x.b
r = x.Get(x.b) << uint8(n)
r += x.Get(n)
}
return
}
func NewBits(s []byte) *BitT {
r := new(BitT)
r.s = s
r.b = 8
return r
}