Skip to content

Commit

Permalink
Fix & document computeShift
Browse files Browse the repository at this point in the history
  • Loading branch information
konsumlamm committed Dec 30, 2023
1 parent 60db9a2 commit a3e39f0
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 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 @@ -768,8 +768,9 @@ 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 hiShift = (log2 (sz - 1) `div` blockShift) * blockShift
hi = (sz - 1) `unsafeShiftR` hiShift
-- @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) =
Expand Down

0 comments on commit a3e39f0

Please sign in to comment.