func (h HLL) IsSparse() bool {
	return h[0]&64 == 0
}
IsSparse returns true iff the underlying HLL is sparse (and thus the cardinality estimate is exact).
package hll
import (
	"encoding/binary"
	"errors"
)
type HLL []byte
func SizeByError(errorRate float64) (int, error) {
	s, err := DenseSizeByError(errorRate)
	if err != nil {
		return 0, err
	}
	return s + 8, nil
}
func SizeByP(p int) (int, error) {
	s, err := DenseSizeByP(p)
	if err != nil {
		return 0, err
	}
	return s + 8, nil
}
func (h HLL) Add(hash uint64) {
	if h[0]&(1<<6) != 0 {
		if Dense(h[8:]).Add(hash) {
			h[0] |= 1 << 7  
		}
		return
	}
	s := sparse(h)
	if s.Add(hash) == ok {
		return
	}
	toDense(s)
	Dense(h[8:]).Add(hash)
}
func (h HLL) Merge(g HLL) error {
	if len(h) != len(g) {
		return errors.New("size mismatch")
	}
	hDense := h[0]&(1<<6) != 0
	gDense := g[0]&(1<<6) != 0
	if hDense && gDense {
		Dense(h[8:]).Merge(Dense(g[8:]))
		h[0] |= 1 << 7  
		return nil
	}
	if !hDense && !gDense {
		if mergeIntoSparse(sparse(h), sparse(g)) == ok {
			return nil
		}
		toDense(sparse(h))
		mergeIntoDense(Dense(h[8:]), sparse(g))
		h[0] = 128 + 64
		return nil
	}
	if hDense {  
		mergeIntoDense(Dense(h[8:]), sparse(g))
		h[0] |= 128
		return nil
	}
	toDense(sparse(h))
	Dense(h[8:]).Merge(Dense(g[8:]))
	return nil
}
func (h HLL) IsValid() error {
	if len(h) < 8 {
		return errors.New("size too small")
	}
	if (len(h)-8)%3 != 0 {
		return errors.New("hll byte size sould be 8 + a multiple of 3")
	}
	m := Dense(h[8:]).m()
	if m&(m-1) != 0 {
		return errors.New("hll byte size sould be 8 + 3 times a power of two")
	}
	var p byte
	for z := m; z != 0; z >>= 1 {
		p++
	}
	p--
	if p < 4 || p > 25 {
		return errors.New("p must be between 4 and 25, inclusive")
	}
	if h[0]&(1<<6) == 0 {
		sz := sparse(h).size()
		if len(h) < 8+8*int(sz) {
			return errors.New("sparse HLL is corrupted")
		}
	}
	return nil
}