Skip to content

Commit

Permalink
chore: size configurable bitmap constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
aliszka committed Dec 13, 2024
1 parent 03a0842 commit 1db0927
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,27 @@ func NewBitmap() *Bitmap {
}

func NewBitmapWith(numKeys int) *Bitmap {
return newBitmapWithSize(numKeys, minContainerSize, 0)
}

func newBitmapWithSize(numKeys, initialContainerSize, additionalContainersSize int) *Bitmap {
if numKeys < 2 {
panic("Must contain at least two keys.")
}
// Each key must also keep an offset. So, we need to double the number
// of uint64s allocated. Plus, we need to make space for the first 2
// uint64s to store the number of keys and node size.
keysLen := 4 * (2*numKeys + 2)
containersLen := initialContainerSize + additionalContainersSize
ra := &Bitmap{
// Each key must also keep an offset. So, we need to double the number
// of uint64s allocated. Plus, we need to make space for the first 2
// uint64s to store the number of keys and node size.
data: make([]uint16, 4*(2*numKeys+2)),
data: make([]uint16, keysLen, keysLen+containersLen),
}
ra.keys = toUint64Slice(ra.data)
ra.keys.setNodeSize(len(ra.data))
ra.keys = toUint64Slice(ra.data[:keysLen])
ra.keys.setNodeSize(keysLen)

// Always generate a container for key = 0x00. Otherwise, node gets confused
// about whether a zero key is a new key or not.
offset := ra.newContainer(minContainerSize)
offset := ra.newContainer(uint16(initialContainerSize))
// First two are for num keys. index=2 -> 0 key. index=3 -> offset.
ra.keys.setAt(indexNodeStart+1, offset)
ra.keys.setNumKeys(1)
Expand Down

0 comments on commit 1db0927

Please sign in to comment.