Skip to content
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

Implemented a lookupAround for IntervalMap #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions src/Data/IntervalMap/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module Data.IntervalMap.Base
, member
, notMember
, lookup
, lookupAround
, findWithDefault
, span

Expand Down Expand Up @@ -246,6 +247,19 @@ lookup k (IntervalMap m) =
Just (_, (i, a)) | k `Interval.member` i -> Just a
_ -> Nothing

-- | Lookup the value and interval at a key in the map.
--
-- The function will return the corresponding value and a
-- surrounding interval as @('Just' (interval, value))@,
-- or 'Nothing' if the key isn't in the map. The surrounding interval
-- @interval@ is such that it contains @value@ and such that @k@ is
-- constantly equal to @value@ on this interval.
lookupAround :: Ord k => k -> IntervalMap k a -> Maybe (Interval k, a)
lookupAround k (IntervalMap m) =
case Map.lookupLE (LB (Finite k, Interval.Closed)) m of
Just (_, (i, a)) | k `Interval.member` i -> Just (i, a)
_ -> Nothing

-- | The expression @('findWithDefault' def k map)@ returns
-- the value at key @k@ or returns default value @def@
-- when the key is not in the map.
Expand Down
1 change: 1 addition & 0 deletions src/Data/IntervalMap/Lazy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module Data.IntervalMap.Lazy
, member
, notMember
, lookup
, lookupAround
, findWithDefault
, span

Expand Down
1 change: 1 addition & 0 deletions src/Data/IntervalMap/Strict.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module Data.IntervalMap.Strict
, member
, notMember
, lookup
, lookupAround
, findWithDefault
, span

Expand Down
9 changes: 9 additions & 0 deletions test/TestIntervalMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ prop_insert_lookup =
Just k -> IML.lookup k (IML.insert i a m) == Just a
Nothing -> True

prop_insert_lookupAround_just_inserted :: Property
prop_insert_lookupAround_just_inserted =
forAll arbitrary $ \(m :: IntervalMap Rational Integer) ->
forAll arbitrary $ \i ->
forAll arbitrary $ \a ->
case Interval.pickup i of
Just k -> IML.lookupAround k (IML.insert i a m) == Just (i, a)
Nothing -> True

prop_insert_bang :: Property
prop_insert_bang =
forAll arbitrary $ \(m :: IntervalMap Rational Integer) ->
Expand Down