Permalink
Browse files

Don't leave BitmapIndexed with a single Leaf child (issue #39).

  • Loading branch information...
1 parent 0b7ef83 commit 74a1314960069500bf4fba12eb395c1e74f62e1e @michalt committed Oct 9, 2012
Showing with 14 additions and 1 deletion.
  1. +14 −1 Data/HashMap/Base.hs
View
15 Data/HashMap/Base.hs
@@ -479,7 +479,15 @@ delete k0 m0 = go h0 k0 0 m0
then t
else case st' of
Empty | A.length ary == 1 -> Empty
- | otherwise -> BitmapIndexed (b .&. complement m) (A.delete ary i)
+ | A.length ary == 2 ->
+ case (i, A.index ary 0, A.index ary 1) of
+ (0, _, l@(Leaf _ _)) -> l
+ (1, l@(Leaf _ _), _) -> l
+ _ -> bIndexed
+ | otherwise -> bIndexed
+ where
+ bIndexed = BitmapIndexed (b .&. complement m) (A.delete ary i)
+ l@(Leaf _ _) | A.length ary == 1 -> l
_ -> BitmapIndexed b (A.update ary i st')
where m = mask h s
i = sparseIndex b m
@@ -826,6 +834,11 @@ filterWithKey pred = go
step !ary !mary !b i !j !bi n
| i >= n = case j of
0 -> return Empty
+ 1 -> do
+ ch <- A.read mary 0
+ case ch of
+ l@(Leaf _ _) -> return l
+ _ -> BitmapIndexed b <$> trim mary 1
_ -> do
ary2 <- trim mary j
return $! if j == maxChildren

0 comments on commit 74a1314

Please sign in to comment.