Skip to content

Commit

Permalink
Fix size tracking in 'filterMapAuxInternal'
Browse files Browse the repository at this point in the history
Because values are bound to 'sz' often in the many local functions
used in 'filterMapAuxInternal', some shadowing occured that
caused the size calculated to be incorrect. This commit renames
those variables to avoid collisions.
  • Loading branch information
rockbmb committed Oct 15, 2017
1 parent a4f9bc6 commit 717da46
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions Data/HashMap/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1295,30 +1295,30 @@ filterMapAuxInternal onLeaf onColl = go 0
go !sz (Full ary) = filterA sz ary fullNodeMask
go !sz (Collision h ary) = filterC sz ary h

filterA sz ary0 b0 =
filterA sze ary0 b0 =
let !n = A.length ary0
in runST $ do
mary <- A.new_ n
step ary0 mary b0 0 0 1 n sz
step ary0 mary b0 0 0 1 n sze
where
step :: A.Array (Tree k v1) -> A.MArray s (Tree k v2)
-> Bitmap -> Int -> Int -> Bitmap -> Int -> Int
-> ST s (A.Sized (Tree k v2))
step !ary !mary !b i !j !bi n !siz
| i >= n = case j of
0 -> return (A.Sized siz Empty)
0 -> return $! A.Sized siz Empty
1 -> do
ch <- A.read mary 0
case ch of
t | isLeafOrCollision t -> return (A.Sized siz t)
t | isLeafOrCollision t -> return $! A.Sized siz t
_ -> A.Sized siz . BitmapIndexed b <$> trim mary 1
_ -> do
ary2 <- trim mary j
return . A.Sized siz $! if j == maxChildren
then Full ary2
else BitmapIndexed b ary2
| bi .&. b == 0 = step ary mary b i j (bi `unsafeShiftL` 1) n sz
| otherwise = case go sz (A.index ary i) of
| bi .&. b == 0 = step ary mary b i j (bi `unsafeShiftL` 1) n siz
| otherwise = case go siz (A.index ary i) of
A.Sized dsz Empty -> step ary mary (b .&. complement bi) (i+1) j
(bi `unsafeShiftL` 1) n dsz
A.Sized dsz t -> do A.write mary j t
Expand All @@ -1334,18 +1334,18 @@ filterMapAuxInternal onLeaf onColl = go 0
step :: A.Array (Leaf k v1) -> A.MArray s (Leaf k v2)
-> Int -> Int -> Int -> Int
-> ST s (A.Sized (Tree k v2))
step !ary !mary i !j n !sz
step !ary !mary i !j n !sze
| i >= n = case j of
0 -> return (A.Sized sz Empty)
0 -> return (A.Sized sze Empty)
1 -> do l <- A.read mary 0
return $! (A.Sized sz (Leaf h l))
return . A.Sized sze $! Leaf h l
_ | i == j -> do ary2 <- A.unsafeFreeze mary
return $! (A.Sized sz (Collision h ary2))
return . A.Sized sze $! Collision h ary2
| otherwise -> do ary2 <- trim mary j
return $! (A.Sized sz (Collision h ary2))
return . A.Sized sze $! Collision h ary2
| Just el <- onColl (A.index ary i)
= A.write mary j el >> step ary mary (i+1) (j+1) n (sz + 1)
| otherwise = step ary mary (i+1) j n sz
= A.write mary j el >> step ary mary (i+1) (j+1) n (sze + 1)
| otherwise = step ary mary (i+1) j n sze
{-# INLINE filterMapAuxInternal #-}

-- | /O(n)/ Filter this map by retaining only elements which values
Expand Down

0 comments on commit 717da46

Please sign in to comment.