-
Notifications
You must be signed in to change notification settings - Fork 228
/
serialization_littleendian.go
53 lines (41 loc) · 1.17 KB
/
serialization_littleendian.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
// +build 386 amd64,!appengine
package roaring
import (
"io"
"reflect"
"unsafe"
)
func (b *arrayContainer) writeTo(stream io.Writer) (int, error) {
buf := uint16SliceAsByteSlice(b.content)
return stream.Write(buf)
}
func (b *bitmapContainer) writeTo(stream io.Writer) (int, error) {
buf := uint64SliceAsByteSlice(b.bitmap)
return stream.Write(buf)
}
func (b *arrayContainer) readFrom(stream io.Reader) (int, error) {
buf := uint16SliceAsByteSlice(b.content)
return io.ReadFull(stream, buf)
}
func (b *bitmapContainer) readFrom(stream io.Reader) (int, error) {
buf := uint64SliceAsByteSlice(b.bitmap)
return io.ReadFull(stream, buf)
}
func uint64SliceAsByteSlice(slice []uint64) []byte {
// make a new slice header
header := *(*reflect.SliceHeader)(unsafe.Pointer(&slice))
// update its capacity and length
header.Len *= 8
header.Cap *= 8
// return it
return *(*[]byte)(unsafe.Pointer(&header))
}
func uint16SliceAsByteSlice(slice []uint16) []byte {
// make a new slice header
header := *(*reflect.SliceHeader)(unsafe.Pointer(&slice))
// update its capacity and length
header.Len *= 2
header.Cap *= 2
// return it
return *(*[]byte)(unsafe.Pointer(&header))
}