Skip to content

Add 'lookup' function to Data.Set #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Data/Set.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ module Data.Set (
, size
, member
, notMember
, lookup
, lookupLT
, lookupGT
, lookupLE
Expand Down Expand Up @@ -146,6 +147,7 @@ module Data.Set (
) where

import Data.Set.Base as S
import Prelude ()

-- $strictness
--
Expand Down
18 changes: 17 additions & 1 deletion Data/Set/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ module Data.Set.Base (
, size
, member
, notMember
, lookup
, lookupLT
, lookupGT
, lookupLE
Expand Down Expand Up @@ -194,7 +195,7 @@ module Data.Set.Base (
, merge
) where

import Prelude hiding (filter,foldl,foldr,null,map)
import Prelude hiding (filter,foldl,foldr,null,map,lookup)
import qualified Data.List as List
import Data.Bits (shiftL, shiftR)
#if !MIN_VERSION_base(4,8,0)
Expand Down Expand Up @@ -370,6 +371,21 @@ notMember a t = not $ member a t
{-# INLINE notMember #-}
#endif

-- | /O(log n)/. Find the given element and return the copy contained in the set.
lookup :: Ord a => a -> Set a -> Maybe a
lookup = go
where
go !_ Tip = Nothing
go x (Bin _ y l r) = case compare x y of
LT -> go x l
GT -> go x r
EQ -> Just y
#if __GLASGOW_HASKELL__
{-# INLINABLE lookup #-}
#else
{-# INLINE lookup #-}
#endif

-- | /O(log n)/. Find largest element smaller than the given one.
--
-- > lookupLT 3 (fromList [3, 5]) == Nothing
Expand Down
12 changes: 11 additions & 1 deletion tests/set-properties.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import Test.HUnit hiding (Test, Testable)
import Test.QuickCheck

main :: IO ()
main = defaultMain [ testCase "lookupLT" test_lookupLT
main = defaultMain [ testCase "lookup" test_lookup
, testCase "lookupLT" test_lookupLT
, testCase "lookupGT" test_lookupGT
, testCase "lookupLE" test_lookupLE
, testCase "lookupGE" test_lookupGE
Expand All @@ -24,6 +25,7 @@ main = defaultMain [ testCase "lookupLT" test_lookupLT
, testProperty "prop_Single" prop_Single
, testProperty "prop_Member" prop_Member
, testProperty "prop_NotMember" prop_NotMember
, testProperty "prop_Lookup" prop_Lookup
, testProperty "prop_LookupLT" prop_LookupLT
, testProperty "prop_LookupGT" prop_LookupGT
, testProperty "prop_LookupLE" prop_LookupLE
Expand Down Expand Up @@ -73,6 +75,11 @@ main = defaultMain [ testCase "lookupLT" test_lookupLT
-- Unit tests
----------------------------------------------------------------

test_lookup :: Assertion
test_lookup = do
lookup 3 (fromList [3, 5]) @?= Just 3
lookup 4 (fromList [3, 5]) @?= Nothing

test_lookupLT :: Assertion
test_lookupLT = do
lookupLT 3 (fromList [3, 5]) @?= Nothing
Expand Down Expand Up @@ -192,6 +199,9 @@ test_LookupSomething lookup' cmp xs =
filter_odd [_] = []
filter_odd (_ : o : xs) = o : filter_odd xs

prop_Lookup :: [Int] -> Bool
prop_Lookup = test_LookupSomething lookup (==)

prop_LookupLT :: [Int] -> Bool
prop_LookupLT = test_LookupSomething lookupLT (<)

Expand Down