Skip to content

Commit

Permalink
Add Ord instances for Interval, IntervalSet and IntervalMap
Browse files Browse the repository at this point in the history
The primary intended use case for these is to allow storage in maps and
sets.
  • Loading branch information
googleson78 committed Aug 15, 2023
1 parent 0239408 commit 901b42e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/Data/Interval/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ data Interval r
| LeftOpen !r !r
| RightOpen !r !r
| BothOpen !r !r
deriving (Eq, Typeable)
deriving
( Eq,
Ord,
-- ^ Note that this Ord is derived and not semantically meaningful.
-- The primary intended use case is to allow using 'Interval'
-- in maps and sets that require ordering.
Typeable
)

peekInterval :: (Applicative m, Monad m, Ord r) => m Int8 -> m r -> m r -> m (Interval r)
peekInterval tagM x y = do
Expand Down
9 changes: 8 additions & 1 deletion src/Data/IntervalMap/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,14 @@ import qualified GHC.Exts as GHCExts
-- Unlike 'IntervalSet', 'IntervalMap' never merge adjacent mappings,
-- even if adjacent intervals are connected and mapped to the same value.
newtype IntervalMap r a = IntervalMap (Map (LB r) (Interval r, a))
deriving (Eq, Typeable)
deriving
( Eq,
Ord,
-- ^ Note that this Ord is derived and not semantically meaningful.
-- The primary intended use case is to allow using 'IntervalSet'
-- in maps and sets that require ordering.
Typeable
)

#if __GLASGOW_HASKELL__ >= 708
type role IntervalMap nominal representational
Expand Down
9 changes: 8 additions & 1 deletion src/Data/IntervalSet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,14 @@ import qualified GHC.Exts as GHCExts
--
-- Any connected intervals are merged together, and empty intervals are ignored.
newtype IntervalSet r = IntervalSet (Map (Extended r) (Interval r))
deriving (Eq, Typeable)
deriving
( Eq,
Ord,
-- ^ Note that this Ord is derived and not semantically meaningful.
-- The primary intended use case is to allow using 'IntervalSet'
-- in maps and sets that require ordering.
Typeable
)

#if __GLASGOW_HASKELL__ >= 708
type role IntervalSet nominal
Expand Down

0 comments on commit 901b42e

Please sign in to comment.