2020-11-06 13:41:42 -05:00
|
|
|
// +build 386,!appengine amd64,!appengine arm,!appengine arm64,!appengine ppc64le,!appengine mipsle,!appengine mips64le,!appengine mips64p32le,!appengine wasm,!appengine
|
2018-05-19 08:49:46 -04:00
|
|
|
|
|
|
|
package roaring
|
|
|
|
|
|
|
|
import (
|
2019-11-27 04:23:33 -05:00
|
|
|
"errors"
|
2018-05-19 08:49:46 -04:00
|
|
|
"io"
|
|
|
|
"reflect"
|
2019-11-27 04:23:33 -05:00
|
|
|
"runtime"
|
2018-05-19 08:49:46 -04:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (ac *arrayContainer) writeTo(stream io.Writer) (int, error) {
|
|
|
|
buf := uint16SliceAsByteSlice(ac.content)
|
|
|
|
return stream.Write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *bitmapContainer) writeTo(stream io.Writer) (int, error) {
|
2019-11-27 04:23:33 -05:00
|
|
|
if bc.cardinality <= arrayDefaultMaxSize {
|
|
|
|
return 0, errors.New("refusing to write bitmap container with cardinality of array container")
|
|
|
|
}
|
2018-05-19 08:49:46 -04:00
|
|
|
buf := uint64SliceAsByteSlice(bc.bitmap)
|
|
|
|
return stream.Write(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
|
|
|
|
|
2019-11-27 04:23:33 -05:00
|
|
|
// instantiate result and use KeepAlive so data isn't unmapped.
|
|
|
|
result := *(*[]byte)(unsafe.Pointer(&header))
|
|
|
|
runtime.KeepAlive(&slice)
|
|
|
|
|
2018-05-19 08:49:46 -04:00
|
|
|
// return it
|
2019-11-27 04:23:33 -05:00
|
|
|
return result
|
2018-05-19 08:49:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-11-27 04:23:33 -05:00
|
|
|
// instantiate result and use KeepAlive so data isn't unmapped.
|
|
|
|
result := *(*[]byte)(unsafe.Pointer(&header))
|
|
|
|
runtime.KeepAlive(&slice)
|
|
|
|
|
2018-05-19 08:49:46 -04:00
|
|
|
// return it
|
2019-11-27 04:23:33 -05:00
|
|
|
return result
|
2018-05-19 08:49:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *bitmapContainer) asLittleEndianByteSlice() []byte {
|
|
|
|
return uint64SliceAsByteSlice(bc.bitmap)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialization code follows
|
|
|
|
|
2019-11-27 04:23:33 -05:00
|
|
|
////
|
|
|
|
// These methods (byteSliceAsUint16Slice,...) do not make copies,
|
|
|
|
// they are pointer-based (unsafe). The caller is responsible to
|
|
|
|
// ensure that the input slice does not get garbage collected, deleted
|
|
|
|
// or modified while you hold the returned slince.
|
|
|
|
////
|
|
|
|
func byteSliceAsUint16Slice(slice []byte) (result []uint16) { // here we create a new slice holder
|
2018-05-19 08:49:46 -04:00
|
|
|
if len(slice)%2 != 0 {
|
|
|
|
panic("Slice size should be divisible by 2")
|
|
|
|
}
|
2019-11-27 04:23:33 -05:00
|
|
|
// reference: https://go101.org/article/unsafe.html
|
2018-05-19 08:49:46 -04:00
|
|
|
|
|
|
|
// make a new slice header
|
2019-11-27 04:23:33 -05:00
|
|
|
bHeader := (*reflect.SliceHeader)(unsafe.Pointer(&slice))
|
|
|
|
rHeader := (*reflect.SliceHeader)(unsafe.Pointer(&result))
|
2018-05-19 08:49:46 -04:00
|
|
|
|
2019-11-27 04:23:33 -05:00
|
|
|
// transfer the data from the given slice to a new variable (our result)
|
|
|
|
rHeader.Data = bHeader.Data
|
|
|
|
rHeader.Len = bHeader.Len / 2
|
|
|
|
rHeader.Cap = bHeader.Cap / 2
|
2018-05-19 08:49:46 -04:00
|
|
|
|
2019-11-27 04:23:33 -05:00
|
|
|
// instantiate result and use KeepAlive so data isn't unmapped.
|
|
|
|
runtime.KeepAlive(&slice) // it is still crucial, GC can free it)
|
|
|
|
|
|
|
|
// return result
|
|
|
|
return
|
2018-05-19 08:49:46 -04:00
|
|
|
}
|
|
|
|
|
2019-11-27 04:23:33 -05:00
|
|
|
func byteSliceAsUint64Slice(slice []byte) (result []uint64) {
|
2018-05-19 08:49:46 -04:00
|
|
|
if len(slice)%8 != 0 {
|
|
|
|
panic("Slice size should be divisible by 8")
|
|
|
|
}
|
2019-11-27 04:23:33 -05:00
|
|
|
// reference: https://go101.org/article/unsafe.html
|
2018-05-19 08:49:46 -04:00
|
|
|
|
|
|
|
// make a new slice header
|
2019-11-27 04:23:33 -05:00
|
|
|
bHeader := (*reflect.SliceHeader)(unsafe.Pointer(&slice))
|
|
|
|
rHeader := (*reflect.SliceHeader)(unsafe.Pointer(&result))
|
2018-05-19 08:49:46 -04:00
|
|
|
|
2019-11-27 04:23:33 -05:00
|
|
|
// transfer the data from the given slice to a new variable (our result)
|
|
|
|
rHeader.Data = bHeader.Data
|
|
|
|
rHeader.Len = bHeader.Len / 8
|
|
|
|
rHeader.Cap = bHeader.Cap / 8
|
2018-05-19 08:49:46 -04:00
|
|
|
|
2019-11-27 04:23:33 -05:00
|
|
|
// instantiate result and use KeepAlive so data isn't unmapped.
|
|
|
|
runtime.KeepAlive(&slice) // it is still crucial, GC can free it)
|
|
|
|
|
|
|
|
// return result
|
|
|
|
return
|
2018-05-19 08:49:46 -04:00
|
|
|
}
|
|
|
|
|
2019-11-27 04:23:33 -05:00
|
|
|
func byteSliceAsInterval16Slice(slice []byte) (result []interval16) {
|
2018-05-19 08:49:46 -04:00
|
|
|
if len(slice)%4 != 0 {
|
|
|
|
panic("Slice size should be divisible by 4")
|
|
|
|
}
|
2019-11-27 04:23:33 -05:00
|
|
|
// reference: https://go101.org/article/unsafe.html
|
2018-05-19 08:49:46 -04:00
|
|
|
|
|
|
|
// make a new slice header
|
2019-11-27 04:23:33 -05:00
|
|
|
bHeader := (*reflect.SliceHeader)(unsafe.Pointer(&slice))
|
|
|
|
rHeader := (*reflect.SliceHeader)(unsafe.Pointer(&result))
|
2018-05-19 08:49:46 -04:00
|
|
|
|
2019-11-27 04:23:33 -05:00
|
|
|
// transfer the data from the given slice to a new variable (our result)
|
|
|
|
rHeader.Data = bHeader.Data
|
|
|
|
rHeader.Len = bHeader.Len / 4
|
|
|
|
rHeader.Cap = bHeader.Cap / 4
|
2018-05-19 08:49:46 -04:00
|
|
|
|
2019-11-27 04:23:33 -05:00
|
|
|
// instantiate result and use KeepAlive so data isn't unmapped.
|
|
|
|
runtime.KeepAlive(&slice) // it is still crucial, GC can free it)
|
|
|
|
|
|
|
|
// return result
|
|
|
|
return
|
2018-05-19 08:49:46 -04:00
|
|
|
}
|