-
Notifications
You must be signed in to change notification settings - Fork 8
/
ef.go
175 lines (155 loc) · 4.07 KB
/
ef.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
package ef
import (
"errors"
"github.com/willf/bitset"
"log"
"math"
)
const (
efInfo = `Universe: %d
Elements: %d
Lower_bits: %d
Higher_bits_length: %d
Mask: 0b%b
Lower_bits offset: %d
Bitvector length: %d
`
)
// EliasFano codec structure
type EliasFano struct {
universe uint64
n uint64
lowerBits uint64
higherBitsLength uint64
mask uint64
lowerBitsOffset uint64
bvLen uint64
b *bitset.BitSet
curValue uint64
position uint64
highBitsPos uint64
}
// New creates a new empty EliasFano object
func New(universe uint64, n uint64) *EliasFano {
var lowerBits uint64
if lowerBits = 0; universe > n {
lowerBits = msb(universe / n)
}
higherBitsLength := n + (universe >> lowerBits) + 2
mask := (uint64(1) << lowerBits) - 1
lowerBitsOffset := higherBitsLength
bvLen := lowerBitsOffset + n*uint64(lowerBits)
b := bitset.New(uint(bvLen))
return &EliasFano{universe, n, lowerBits, higherBitsLength, mask, lowerBitsOffset, bvLen, b, 0, 0, 0}
}
// Compress a monotone increasing array of positive integers. It sets the position at the beginning.
func (ef *EliasFano) Compress(elems []uint64) {
last := uint64(0)
for i, elem := range elems {
if i > 0 && elem < last {
log.Fatal("Sequence is not sorted")
}
if elem > ef.universe {
log.Fatalf("Element %d is greater than universe", elem)
}
high := (elem >> ef.lowerBits) + uint64(i) + 1
low := elem & ef.mask
ef.b.Set(uint(high))
offset := ef.lowerBitsOffset + uint64(i)*ef.lowerBits
setBits(ef.b, offset, low, ef.lowerBits)
last = elem
if i == 0 {
ef.curValue = elem
ef.highBitsPos = high
}
}
}
// Move the internal iterator to the given position and returns a value or an error.
func (ef *EliasFano) Move(position uint64) (uint64, error) {
if position >= ef.Size() {
return 0, errors.New("Out of bound")
}
if ef.position == position {
return ef.Value(), nil
}
if position < ef.position {
ef.Reset()
}
skip := position - ef.position
pos := uint(ef.highBitsPos)
for i := uint64(0); i < skip; i++ {
pos, _ = ef.b.NextSet(pos + 1)
}
ef.highBitsPos = uint64(pos - 1)
ef.position = position
ef.readCurrentValue()
return ef.Value(), nil
}
// Next moves the internal iterator to the next position and returns a value or an error.
func (ef *EliasFano) Next() (uint64, error) {
ef.position++
if ef.position >= ef.Size() {
return 0, errors.New("End reached")
}
ef.readCurrentValue()
return ef.Value(), nil
}
// Position return the current position of the internal iterator.
func (ef *EliasFano) Position() uint64 {
return ef.position
}
// Reset moves the internal iterator to the beginning.
func (ef *EliasFano) Reset() {
ef.highBitsPos = 0
ef.position = 0
ef.readCurrentValue()
}
// Info prints info regarding the EliasFano codec.
func (ef *EliasFano) Info() {
log.Printf(efInfo, ef.universe, ef.n, ef.lowerBits, ef.higherBitsLength, ef.mask, ef.lowerBitsOffset, ef.bvLen)
}
// Value returns the value of the current element.
func (ef *EliasFano) Value() uint64 {
return ef.curValue
}
// Size returns the number of elements encoded.
func (ef *EliasFano) Size() uint64 {
return ef.n
}
// Bitsize returns the size of the internal bitvector.
func (ef *EliasFano) Bitsize() uint64 {
return uint64(ef.b.BinaryStorageSize())
}
func setBits(b *bitset.BitSet, offset uint64, bits uint64, length uint64) {
for i := uint64(0); i < length; i++ {
val := bits & (1 << (length - i - 1))
b.SetTo(uint(offset+i+1), val > 0)
}
}
func (ef *EliasFano) readCurrentValue() {
pos := uint(ef.highBitsPos)
if pos > 0 {
pos++
}
pos, _ = ef.b.NextSet(pos)
ef.highBitsPos = uint64(pos)
low := uint64(0)
offset := ef.lowerBitsOffset + ef.position*ef.lowerBits
for i := uint64(0); i < ef.lowerBits; i++ {
if ef.b.Test(uint(offset + i + 1)) {
low++
}
low = low << 1
}
low = low >> 1
ef.curValue = uint64(((ef.highBitsPos - ef.position - 1) << ef.lowerBits) | low)
}
func round(a float64) int64 {
if a < 0 {
return int64(a - 0.5)
}
return int64(a + 0.5)
}
func msb(x uint64) uint64 {
return uint64(round(math.Log2(float64(x))))
}