// bufferBits buffers slices of bits to be imported as a batch.
967914
func (cmd *ImportCommand) bufferBits(ctx context.Context, useColumnKeys, useRowKeys bool, path string) error {
	a := make([]pilosa.Bit, 0, cmd.BufferSize)

	var r *csv.Reader

	if path != "-" {
		// Open file for reading.
		f, err := os.Open(path)
		if err != nil {
			return errors.Wrap(err, "opening file")
		}
		defer f.Close()

		// Read rows as bits.
		r = csv.NewReader(f)
	} else {
		r = csv.NewReader(cmd.Stdin)
	}

	r.FieldsPerRecord = -1
	rnum := 0
	for {
		rnum++

		// Read CSV row.
		record, err := r.Read()
		if err == io.EOF {
			break
		} else if err != nil {
			return errors.Wrap(err, "reading")
		}

		// Ignore blank rows.
		if record[0] == "" {
			continue
		} else if len(record) < 2 {
			return fmt.Errorf("bad column count on row %d: col=%d", rnum, len(record))
		}

		var bit pilosa.Bit

		// Parse row id.
		if useRowKeys {
			bit.RowKey = record[0]
		} else {
			if bit.RowID, err = strconv.ParseUint(record[0], 10, 64); err != nil {
				return fmt.Errorf("invalid row id on row %d: %q", rnum, record[0])
			}
		}

		// Parse column id.
		if useColumnKeys {
			bit.ColumnKey = record[1]
		} else {
			if bit.ColumnID, err = strconv.ParseUint(record[1], 10, 64); err != nil {
				return fmt.Errorf("invalid column id on row %d: %q", rnum, record[1])
			}
		}

		// Parse time, if exists.
		if len(record) > 2 && record[2] != "" {
			t, err := time.Parse(pilosa.TimeFormat, record[2])
			if err != nil {
				return fmt.Errorf("invalid timestamp on row %d: %q", rnum, record[2])
			}
			bit.Timestamp = t.UnixNano()
		}

		a = append(a, bit)

		// If we've reached the buffer size then import bits.
		if len(a) == cmd.BufferSize {
			if err := cmd.importBits(ctx, useColumnKeys, useRowKeys, a); err != nil {
				return err
			}
			a = a[:0]
		}
	}

	// If there are still bits in the buffer then flush them.
	return cmd.importBits(ctx, useColumnKeys, useRowKeys, a)
}
634839
func (pw *Writer) PushBits() (uint, error) {
	if pw.cntBuf >= len(pw.buf)-8 {
		cnt, err := pw.wr.Write(pw.buf[:pw.cntBuf])
		pw.cntBuf -= cnt
		pw.Offset += int64(cnt)
		if err != nil {
			return 0, err
		}
	}

	u := pw.bufBits
	if pw.bigEndian {
		// Swap all the bits within each byte.
		u = (u&0xaaaaaaaaaaaaaaaa)>>1 | (u&0x5555555555555555)<<1
		u = (u&0xcccccccccccccccc)>>2 | (u&0x3333333333333333)<<2
		u = (u&0xf0f0f0f0f0f0f0f0)>>4 | (u&0x0f0f0f0f0f0f0f0f)<<4
	}
	// Starting with Go 1.7, the compiler should use a wide integer
	// store here if the architecture supports it.
	binary.LittleEndian.PutUint64(pw.buf[pw.cntBuf:], u)

	nb := pw.numBits / 8 // Number of bytes to copy from bit buffer
	pw.cntBuf += int(nb)
	pw.bufBits >>= 8 * nb
	pw.numBits -= 8 * nb
	return 8 * nb, nil
}
180480
func (adp *BufferAdapter) Bytes() [][]byte {
	bufs := make([][]byte, len(adp.bufs))

	for i, buf := range adp.bufs {
		bufs[i] = buf.Bytes()
	}
	return bufs
}
146663
func CopyBitPB(bitmap C.MMBitmapRef) bool {
	abool := C.bitmap_copy_to_pboard(bitmap)
	gbool := bool(abool)

	return gbool
}
319844
func (b *ByteBuf) PeekByte(offset int) (byte, error) {
	if b.Readable() < offset || offset < 0 {
		return 0, io.ErrShortBuffer
	}

	return b.buf[b.readerIndex+offset], nil
}
11212
func formatBits(dst io.Writer, u uint64, neg bool) (int, error) {

	var a = pool_integerBuffer.Get().(*byteSliceBuffer)

	i := integerBufferSize

	if neg {
		u = -u
	}

	// common case: use constants for / because
	// the compiler can optimize it into a multiply+shift

	if ^uintptr(0)>>32 == 0 {
		for u > uint64(^uintptr(0)) {
			q := u / 1e9
			us := uintptr(u - q*1e9) // us % 1e9 fits into a uintptr
			for j := 9; j > 0; j-- {
				i--
				qs := us / 10
				a.bytes[i] = byte(us - qs*10 + '0')
				us = qs
			}
			u = q
		}
	}

	// u guaranteed to fit into a uintptr
	us := uintptr(u)
	for us >= 10 {
		i--
		q := us / 10
		a.bytes[i] = byte(us - q*10 + '0')
		us = q
	}
	// u < 10
	i--
	a.bytes[i] = byte(us + '0')

	// add sign, if any
	if neg {
		i--
		a.bytes[i] = '-'
	}
	counter, err := dst.Write(a.bytes[i:])
	pool_integerBuffer.Put(a)
	return counter, err
}
1316613
func ReadBloomBits(db ethdb.Reader, bit uint, section uint64, head common.Hash) ([]byte, error) {
	return db.Get(bloomBitsKey(bit, section, head))
}
1065826
func (b *buffers) Hits() int {
	b.lck.Lock()
	defer b.lck.Unlock()
	return b.hit
}
392103
func RandomBits(bits int) []byte {
	size := bits / 8
	if bits%8 != 0 {
		size++
	}
	b := make([]byte, size)
	if _, err := rand.Read(b); err != nil {
		panic(err) // rand should never fail
	}
	return b
}
605093
func (b *buffers) Miss() int {
	b.lck.Lock()
	defer b.lck.Unlock()
	return b.miss
}
111667
func (b *Bitset) Bits() []bool {
	result := make([]bool, b.numBits)

	var i int
	for i = 0; i < b.numBits; i++ {
		result[i] = (b.bits[i/8] & (0x80 >> byte(i%8))) != 0
	}

	return result
}
125774
func (b *bitReader) getBits(n uint8) uint16 {
	if n == 0 || b.bitsRead >= 64 {
		return 0
	}
	return b.getBitsFast(n)
}
1053192
func (alac *Alac) readbits(bits int) uint32 {
	var result int32 = 0

	if bits > 16 {
		bits -= 16
		result = int32(alac.readbits_16(16) << uint(bits))
	}

	result |= int32(alac.readbits_16(bits))

	return uint32(result)
}
1373550
func (b *buffer) Slice(off, n int) ([]byte, error) {
	end := off + n
	if err := b.fill(end); err != nil {
		return nil, err
	}
	return b.buf[off:end], nil
}
1026180
func (br *bitReader) ReadBits(nb uint) uint {
	br.FeedBits(nb)
	val := uint(br.bufBits & uint64(1<<nb-1))
	br.bufBits >>= nb
	br.numBits -= nb
	return val
}
308692
func (l *StringLexer) BufferString() string {
	return l.input[l.start:l.pos]
}
1182951
func (pw *Writer) TryWriteBits(v, nb uint) bool {
	if 64-pw.numBits < nb {
		return false
	}
	pw.bufBits |= uint64(v) << pw.numBits
	pw.numBits += nb
	return true
}