Skip to content

Commit

Permalink
Create NonEmptyMap type
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericson2314 committed Apr 19, 2019
1 parent 30ccbaa commit 4f29d02
Show file tree
Hide file tree
Showing 4 changed files with 396 additions and 342 deletions.
26 changes: 13 additions & 13 deletions containers-tests/benchmarks/LookupGE/LookupGE_Map.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ lookupGE2 :: Ord k => k -> Map k a -> Maybe (k,a)
lookupGE2 = go
where
go !_ Tip = Nothing
go !k (Bin _ kx x l r) =
go !k (NE (Bin _ kx x l r)) =
case compare k kx of
LT -> case go k l of
Nothing -> Just (kx,x)
Expand All @@ -27,7 +27,7 @@ lookupGE3 :: Ord k => k -> Map k a -> Maybe (k,a)
lookupGE3 = go Nothing
where
go def !_ Tip = def
go def !k (Bin _ kx x l r) =
go def !k (NE (Bin _ kx x l r)) =
case compare k kx of
LT -> go (Just (kx,x)) k l
GT -> go def k r
Expand All @@ -38,26 +38,26 @@ lookupGE4 :: Ord k => k -> Map k a -> Maybe (k,a)
lookupGE4 k = k `seq` goNothing
where
goNothing Tip = Nothing
goNothing (Bin _ kx x l r) = case compare k kx of
LT -> goJust kx x l
EQ -> Just (kx, x)
GT -> goNothing r
goNothing (NE (Bin _ kx x l r)) = case compare k kx of
LT -> goJust kx x l
EQ -> Just (kx, x)
GT -> goNothing r

goJust ky y Tip = Just (ky, y)
goJust ky y (Bin _ kx x l r) = case compare k kx of
LT -> goJust kx x l
EQ -> Just (kx, x)
GT -> goJust ky y r
goJust ky y (NE (Bin _ kx x l r)) = case compare k kx of
LT -> goJust kx x l
EQ -> Just (kx, x)
GT -> goJust ky y r
{-# INLINABLE lookupGE4 #-}

-------------------------------------------------------------------------------
-- Utilities
-------------------------------------------------------------------------------

findMinMaybe :: Map k a -> Maybe (k,a)
findMinMaybe (Bin _ kx x Tip _) = Just (kx,x)
findMinMaybe (Bin _ _ _ l _) = findMinMaybe l
findMinMaybe Tip = Nothing
findMinMaybe (NE (Bin _ kx x Tip _)) = Just (kx,x)
findMinMaybe (NE (Bin _ _ _ l _)) = findMinMaybe l
findMinMaybe Tip = Nothing

#ifdef TESTING
-------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 4f29d02

Please sign in to comment.