// Copyright 2018 Klaus Post. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.// Based on work Copyright (c) 2013, Yann Collet, released under BSD License.package zstd// bitWriter will write bits.// First bit will be LSB of the first byte of output.type bitWriter struct { bitContainer uint64 nBits uint8 out []byte}// bitMask16 is bitmasks. Has extra to avoid bounds check.var bitMask16 = [32]uint16{0, 1, 3, 7, 0xF, 0x1F,0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF,0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0xFFFF,0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,0xFFFF, 0xFFFF} /* up to 16 bits */var bitMask32 = [32]uint32{0, 1, 3, 7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF,0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF,0x1ffff, 0x3ffff, 0x7FFFF, 0xfFFFF, 0x1fFFFF, 0x3fFFFF, 0x7fFFFF, 0xffFFFF,0x1ffFFFF, 0x3ffFFFF, 0x7ffFFFF, 0xfffFFFF, 0x1fffFFFF, 0x3fffFFFF, 0x7fffFFFF,} // up to 32 bits// addBits16NC will add up to 16 bits.// It will not check if there is space for them,// so the caller must ensure that it has flushed recently.func ( *bitWriter) ( uint16, uint8) { .bitContainer |= uint64(&bitMask16[&31]) << (.nBits & 63) .nBits += }// addBits32NC will add up to 31 bits.// It will not check if there is space for them,// so the caller must ensure that it has flushed recently.func ( *bitWriter) ( uint32, uint8) { .bitContainer |= uint64(&bitMask32[&31]) << (.nBits & 63) .nBits += }// addBits64NC will add up to 64 bits.// There must be space for 32 bits.func ( *bitWriter) ( uint64, uint8) {if <= 31 { .addBits32Clean(uint32(), )return } .addBits32Clean(uint32(), 32) .flush32() .addBits32Clean(uint32(>>32), -32)}// addBits32Clean will add up to 32 bits.// It will not check if there is space for them.// The input must not contain more bits than specified.func ( *bitWriter) ( uint32, uint8) { .bitContainer |= uint64() << (.nBits & 63) .nBits += }// addBits16Clean will add up to 16 bits. value may not contain more set bits than indicated.// It will not check if there is space for them, so the caller must ensure that it has flushed recently.func ( *bitWriter) ( uint16, uint8) { .bitContainer |= uint64() << (.nBits & 63) .nBits += }// flush32 will flush out, so there are at least 32 bits available for writing.func ( *bitWriter) () {if .nBits < 32 {return } .out = append(.out,byte(.bitContainer),byte(.bitContainer>>8),byte(.bitContainer>>16),byte(.bitContainer>>24)) .nBits -= 32 .bitContainer >>= 32}// flushAlign will flush remaining full bytes and align to next byte boundary.func ( *bitWriter) () { := (.nBits + 7) >> 3for := uint8(0); < ; ++ { .out = append(.out, byte(.bitContainer>>(*8))) } .nBits = 0 .bitContainer = 0}// close will write the alignment bit and write the final byte(s)// to the output.func ( *bitWriter) () {// End mark .addBits16Clean(1, 1)// flush until next byte. .flushAlign()}// reset and continue writing by appending to out.func ( *bitWriter) ( []byte) { .bitContainer = 0 .nBits = 0 .out = }
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.