github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/util/fixedBitSet.go (about)

     1  package util
     2  
     3  /*
     4  BitSet of fixed length (numBits), backed by accessible bits() []int64,
     5  accessed with an int index, implementing Bits and DocIdSet. Unlike
     6  OpenBitSet, this bit set does not auto-expand, cannot handle long
     7  index, and does not have fastXX/XX variants (just X).
     8  */
     9  type FixedBitSet struct {
    10  	bits     []int64
    11  	numBits  int
    12  	numWords int
    13  }
    14  
    15  /*
    16  If the given FixedBitSet is large enough to hold numBits, returns the
    17  given bits, otherwise returns a new FixedBitSet which can hold the
    18  rquired number of bits.
    19  
    20  NOTE: the returned bitset reuses the underlying []int64 of the given
    21  bits if possible. Also, calling length() on the returned bits may
    22  return a value greater than numBits.
    23  */
    24  func EnsureFixedBitSet(bits *FixedBitSet, numBits int) *FixedBitSet {
    25  	panic("not implemented yet")
    26  }
    27  
    28  /* returns the number of 64 bit words it would take to hold numBits */
    29  func fbits2words(numBits int) int {
    30  	numLong := int(uint(numBits) >> 6)
    31  	if (numBits & 63) != 0 {
    32  		numLong++
    33  	}
    34  	return numLong
    35  }
    36  
    37  func NewFixedBitSetOf(numBits int) *FixedBitSet {
    38  	wordLength := fbits2words(numBits)
    39  	return &FixedBitSet{
    40  		numBits:  numBits,
    41  		bits:     make([]int64, wordLength),
    42  		numWords: wordLength,
    43  	}
    44  }
    45  
    46  func (b *FixedBitSet) Bits() Bits {
    47  	return b
    48  }
    49  
    50  func (b *FixedBitSet) Length() int {
    51  	return b.numBits
    52  }
    53  
    54  func (b *FixedBitSet) IsCacheable() bool {
    55  	return true
    56  }
    57  
    58  func (b *FixedBitSet) RamBytesUsed() int64 {
    59  	panic("not implemented yet")
    60  }
    61  
    62  /*
    63  Returns number of set bits. NOTE: this visits every int64 in the
    64  backing bits slice, and the result is not internaly cached!
    65  */
    66  func (b *FixedBitSet) Cardinality() int {
    67  	return int(pop_array(b.bits))
    68  }
    69  
    70  func (b *FixedBitSet) At(index int) bool {
    71  	panic("not implemented yet")
    72  }
    73  
    74  func (b *FixedBitSet) Set(index int) {
    75  	assert2(index >= 0 && index < b.numBits, "index=%v, numBits=%v", index, b.numBits)
    76  	wordNum := index >> 6 // div 64
    77  	bitmask := int64(1 << uint(index))
    78  	b.bits[wordNum] |= bitmask
    79  }