Skip to content

Commit

Permalink
Fix wrong insertShift computation in <| (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
konsumlamm committed Dec 30, 2023
1 parent 1c7e2dc commit 2862e05
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Data/RRBVector/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ computeSizes !sh arr
where
subtree = A.index arr i

-- Integer log base 2.
-- | Integer log base 2.
log2 :: Int -> Int
log2 x = bitSizeMinus1 - countLeadingZeros x
where
Expand Down Expand Up @@ -765,7 +765,10 @@ x <| Root size sh tree
-- compute the shift at which the new branch needs to be inserted (0 means there is space in the leaf)
-- the size is computed for efficient calculation of the shift in a balanced subtree
computeShift !sz !sh !min (Balanced _) =
let newShift = (log2 sz `div` blockShift) * blockShift
-- @sz - 1@ is the index of the last element
let hiShift = max ((log2 (sz - 1) `div` blockShift) * blockShift) 0 -- the shift of the root when normalizing
hi = (sz - 1) `unsafeShiftR` hiShift -- the length of the root node when normalizing minus 1
newShift = if hi < blockMask then hiShift else hiShift + blockShift
in if newShift > sh then min else newShift
computeShift _ sh min (Unbalanced arr sizes) =
let sz' = indexPrimArray sizes 0 -- the size of the first subtree
Expand Down

0 comments on commit 2862e05

Please sign in to comment.