From 1db0927a391e75bcdaefef16dafb368482851c3f Mon Sep 17 00:00:00 2001 From: Andrzej Liszka Date: Thu, 5 Dec 2024 12:15:38 +0100 Subject: [PATCH] chore: size configurable bitmap constructor --- bitmap.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/bitmap.go b/bitmap.go index 8a39029..8a419f7 100644 --- a/bitmap.go +++ b/bitmap.go @@ -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)