package bloom

import (
	
	

	
)

// Filter is an interface representing read-only bloom filters where programs
// can probe for the possible presence of a hash key.
type Filter interface {
	Check(uint64) bool
}

// SplitBlockFilter is an in-memory implementation of the parquet bloom filters.
//
// This type is useful to construct bloom filters that are later serialized
// to a storage medium.
type SplitBlockFilter []Block

// MakeSplitBlockFilter constructs a SplitBlockFilter value from the data byte
// slice.
func ( []byte) SplitBlockFilter {
	return unsafecast.Slice[Block]()
}

// NumSplitBlocksOf returns the number of blocks in a filter intended to hold
// the given number of values and bits of filter per value.
//
// This function is useful to determine the number of blocks when creating bloom
// filters in memory, for example:
//
//	f := make(bloom.SplitBlockFilter, bloom.NumSplitBlocksOf(n, 10))
func ( int64,  uint) int {
	 := ((uint() * ) + 7) / 8
	 := ( + (BlockSize - 1)) / BlockSize
	return int()
}

// Reset clears the content of the filter f.
func ( SplitBlockFilter) () {
	for  := range  {
		[] = Block{}
	}
}

// Block returns a pointer to the block that the given value hashes to in the
// bloom filter.
func ( SplitBlockFilter) ( uint64) *Block { return &[fasthash1x64(, int32(len()))] }

// InsertBulk adds all values from x into f.
func ( SplitBlockFilter) ( []uint64) { filterInsertBulk(, ) }

// Insert adds x to f.
func ( SplitBlockFilter) ( uint64) { filterInsert(, ) }

// Check tests whether x is in f.
func ( SplitBlockFilter) ( uint64) bool { return filterCheck(, ) }

// Bytes converts f to a byte slice.
//
// The returned slice shares the memory of f. The method is intended to be used
// to serialize the bloom filter to a storage medium.
func ( SplitBlockFilter) () []byte {
	return unsafecast.Slice[byte]()
}

// CheckSplitBlock is similar to bloom.SplitBlockFilter.Check but reads the
// bloom filter of n bytes from r.
//
// The size n of the bloom filter is assumed to be a multiple of the block size.
func ( io.ReaderAt,  int64,  uint64) (bool, error) {
	 := acquireBlock()
	defer releaseBlock()
	 := BlockSize * fasthash1x64(, int32(/BlockSize))
	,  := .ReadAt(.Bytes(), int64())
	return .Check(uint32()), 
}

var (
	blockPool sync.Pool
)

func acquireBlock() *Block {
	,  := blockPool.Get().(*Block)
	if  == nil {
		 = new(Block)
	}
	return 
}

func releaseBlock( *Block) {
	if  != nil {
		blockPool.Put()
	}
}