package brotli

/* Copyright 2010 Google Inc. All Rights Reserved.

   Distributed under MIT license.
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/

/* Write bits into a byte array. */

type bitWriter struct {
	dst []byte

	// Data waiting to be written is the low nbits of bits.
	bits  uint64
	nbits uint
}

func ( *bitWriter) ( uint,  uint64) {
	.bits |=  << .nbits
	.nbits += 
	if .nbits >= 32 {
		 := .bits
		.bits >>= 32
		.nbits -= 32
		.dst = append(.dst,
			byte(),
			byte(>>8),
			byte(>>16),
			byte(>>24),
		)
	}
}

func ( *bitWriter) ( bool) {
	if  {
		.writeBits(1, 1)
	} else {
		.writeBits(1, 0)
	}
}

func ( *bitWriter) () {
	 := .dst
	for .nbits != 0 {
		 = append(, byte(.bits))
		.bits >>= 8
		if .nbits > 8 { // Avoid underflow
			.nbits -= 8
		} else {
			.nbits = 0
		}
	}
	.bits = 0
	.dst = 
}