You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
2.6 KiB

  1. // +build !amd64,!386 appengine
  2. package roaring
  3. import (
  4. "encoding/binary"
  5. "io"
  6. )
  7. func (b *arrayContainer) writeTo(stream io.Writer) (int, error) {
  8. buf := make([]byte, 2*len(b.content))
  9. for i, v := range b.content {
  10. base := i * 2
  11. buf[base] = byte(v)
  12. buf[base+1] = byte(v >> 8)
  13. }
  14. return stream.Write(buf)
  15. }
  16. func (b *arrayContainer) readFrom(stream io.Reader) (int, error) {
  17. err := binary.Read(stream, binary.LittleEndian, b.content)
  18. if err != nil {
  19. return 0, err
  20. }
  21. return 2 * len(b.content), nil
  22. }
  23. func (b *bitmapContainer) writeTo(stream io.Writer) (int, error) {
  24. // Write set
  25. buf := make([]byte, 8*len(b.bitmap))
  26. for i, v := range b.bitmap {
  27. base := i * 8
  28. buf[base] = byte(v)
  29. buf[base+1] = byte(v >> 8)
  30. buf[base+2] = byte(v >> 16)
  31. buf[base+3] = byte(v >> 24)
  32. buf[base+4] = byte(v >> 32)
  33. buf[base+5] = byte(v >> 40)
  34. buf[base+6] = byte(v >> 48)
  35. buf[base+7] = byte(v >> 56)
  36. }
  37. return stream.Write(buf)
  38. }
  39. func (b *bitmapContainer) readFrom(stream io.Reader) (int, error) {
  40. err := binary.Read(stream, binary.LittleEndian, b.bitmap)
  41. if err != nil {
  42. return 0, err
  43. }
  44. b.computeCardinality()
  45. return 8 * len(b.bitmap), nil
  46. }
  47. func (bc *bitmapContainer) asLittleEndianByteSlice() []byte {
  48. by := make([]byte, len(bc.bitmap)*8)
  49. for i := range bc.bitmap {
  50. binary.LittleEndian.PutUint64(by[i*8:], bc.bitmap[i])
  51. }
  52. return by
  53. }
  54. func uint64SliceAsByteSlice(slice []uint64) []byte {
  55. by := make([]byte, len(slice)*8)
  56. for i, v := range slice {
  57. binary.LittleEndian.PutUint64(by[i*8:], v)
  58. }
  59. return by
  60. }
  61. func byteSliceAsUint16Slice(slice []byte) []uint16 {
  62. if len(slice)%2 != 0 {
  63. panic("Slice size should be divisible by 2")
  64. }
  65. b := make([]uint16, len(slice)/2)
  66. for i := range b {
  67. b[i] = binary.LittleEndian.Uint16(slice[2*i:])
  68. }
  69. return b
  70. }
  71. func byteSliceAsUint64Slice(slice []byte) []uint64 {
  72. if len(slice)%8 != 0 {
  73. panic("Slice size should be divisible by 8")
  74. }
  75. b := make([]uint64, len(slice)/8)
  76. for i := range b {
  77. b[i] = binary.LittleEndian.Uint64(slice[8*i:])
  78. }
  79. return b
  80. }
  81. // Converts a byte slice to a interval16 slice.
  82. // The function assumes that the slice byte buffer is run container data
  83. // encoded according to Roaring Format Spec
  84. func byteSliceAsInterval16Slice(byteSlice []byte) []interval16 {
  85. if len(byteSlice)%4 != 0 {
  86. panic("Slice size should be divisible by 4")
  87. }
  88. intervalSlice := make([]interval16, len(byteSlice)/4)
  89. for i := range intervalSlice {
  90. intervalSlice[i] = interval16{
  91. start: binary.LittleEndian.Uint16(byteSlice[i*4:]),
  92. length: binary.LittleEndian.Uint16(byteSlice[i*4+2:]),
  93. }
  94. }
  95. return intervalSlice
  96. }