Help us shape the future of Go! Take the 2026 Go Developer Survey

compints

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2019 License: Apache-2.0 Imports: 3 Imported by: 1

README

compints

GoDoc

compints is a package that provides compression/decompression algorithm for integer list.

Functions (Simple)

  • Compress(input []uint32, diff bool) (output, cbytes []byte, elementCount int)
  • Decompress(input, cbytes []byte, elementCount int, diff bool) ([]uint32, error)

Compress returns three result. All result are important when calling Decompress. It is not useful than CompressToBytes, but you can store these values in block devices when update the result.

diff flag makes result more efficient if input integer slice are sorted in ascend order.

var input = []uint32{1, 2, 3, 4, 5, 6, 6, 8, 10}

compressed, cbytes, length := compints.Compress(input, true)
output, err := compints.Decompress(compressed, cbytes, length, true)
// output is as same as input

Functions (Easy)

  • CompressToBytes(input []uint32, diff bool) []byte
  • DecompressFromBytes(input, []byte, diff bool) ([]uint32, error)

CompressToBytes returns one result. Just pass this result to DecompressFromBytes, you can get decompressed data.

diff flag makes result more efficient if input integer slice are sorted in ascend order.

var input = []uint32{1, 2, 3, 4, 5, 6, 6, 8, 10}

compressed := compints.CompressToBytes(input, true)
output, err := compints.DecompressFromBytes(compressed, true)
// output is as same as input

License

Apache 2

Documentation

Overview

package compints provides useful wrapper for nelz9999's StreamVByte algorithm.

Input is just slice of uint32. Slice size is flexible.

There are two pairs of functions grouped by output data:

  • Compress/Decompress: returns compressed data, control bytes, element count
  • CompressToBytes/DecompressFromBytes: returns just []byte

If just serialize the result, CompressToBytes/DecompressFromBytes are useful.

If you stores the value into block devices and keeps flexibility to modify, Compress/Decompress allow you to implement more efficient code.

Both functions has mode "diff". If diff==true, my code assumes input data is sorted ascendant order.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compress

func Compress(input []uint32, diff bool) (output, cbytes []byte, elementCount int)

Compress receives []uint32 slice and compress into compress data, control bytes, element count.

These three output are required by Decompress.

If you needs more simple method, use CompressToBytes.

Example
package main

import (
	"fmt"
	"github.com/shibukawa/compints"
	"reflect"
)

var input = []uint32{1, 2, 3, 4, 5, 6, 6, 8, 10}

func main() {
	compressed, cbytes, length := compints.Compress(input, true)
	output, err := compints.Decompress(compressed, cbytes, length, true)
	fmt.Printf("equal: %v\n", reflect.DeepEqual(input, output))
	fmt.Printf("error: %v\n", err)
}
Output:
equal: true
error: <nil>

func CompressToBytes

func CompressToBytes(input []uint32, diff bool) []byte

CompressToBytes receives []uint32 slice and compress into []byte.

Example
package main

import (
	"fmt"
	"github.com/shibukawa/compints"
	"reflect"
)

var input = []uint32{1, 2, 3, 4, 5, 6, 6, 8, 10}

func main() {
	compressed := compints.CompressToBytes(input, false)
	output, err := compints.DecompressFromBytes(compressed, false)
	fmt.Printf("equal: %v\n", reflect.DeepEqual(input, output))
	fmt.Printf("error: %v\n", err)
}
Output:
equal: true
error: <nil>

func Decompress

func Decompress(input, cbytes []byte, elementCount int, diff bool) (output []uint32, err error)

Decompress consumes the result of Compress and returns original []uint32 slice.

func DecompressFromBytes

func DecompressFromBytes(input []byte, diff bool) ([]uint32, error)

DecompressFromBytes consumes the result of CompressToBytes and returns original []uint32 slice.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL