{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
module Data.IntervalMap.Generic.Interval (
Interval(..),
genericEquals, genericCompare
) where
import qualified Data.IntervalMap.Interval as I
class Ord e => Interval i e | i -> e where
lowerBound :: i -> e
upperBound :: i -> e
leftClosed :: i -> Bool
leftClosed i
_ = Bool
True
rightClosed :: i -> Bool
rightClosed i
_ = Bool
True
before :: i -> i -> Bool
i
a `before` i
b = i -> e
forall i e. Interval i e => i -> e
upperBound i
a e -> e -> Bool
forall a. Ord a => a -> a -> Bool
< i -> e
forall i e. Interval i e => i -> e
lowerBound i
b
Bool -> Bool -> Bool
|| (i -> e
forall i e. Interval i e => i -> e
upperBound i
a e -> e -> Bool
forall a. Eq a => a -> a -> Bool
== i -> e
forall i e. Interval i e => i -> e
lowerBound i
b Bool -> Bool -> Bool
&& Bool -> Bool
not (i -> Bool
forall i e. Interval i e => i -> Bool
rightClosed i
a Bool -> Bool -> Bool
&& i -> Bool
forall i e. Interval i e => i -> Bool
leftClosed i
b))
after :: i -> i -> Bool
i
a `after` i
b = i
b i -> i -> Bool
forall i e. Interval i e => i -> i -> Bool
`before` i
a
subsumes :: i -> i -> Bool
i
a `subsumes` i
b = (i -> e
forall i e. Interval i e => i -> e
lowerBound i
a e -> e -> Bool
forall a. Ord a => a -> a -> Bool
< i -> e
forall i e. Interval i e => i -> e
lowerBound i
b Bool -> Bool -> Bool
|| (i -> e
forall i e. Interval i e => i -> e
lowerBound i
a e -> e -> Bool
forall a. Eq a => a -> a -> Bool
== i -> e
forall i e. Interval i e => i -> e
lowerBound i
b Bool -> Bool -> Bool
&& (i -> Bool
forall i e. Interval i e => i -> Bool
leftClosed i
a Bool -> Bool -> Bool
|| Bool -> Bool
not (i -> Bool
forall i e. Interval i e => i -> Bool
leftClosed i
b))))
Bool -> Bool -> Bool
&&
(i -> e
forall i e. Interval i e => i -> e
upperBound i
a e -> e -> Bool
forall a. Ord a => a -> a -> Bool
> i -> e
forall i e. Interval i e => i -> e
upperBound i
b Bool -> Bool -> Bool
|| (i -> e
forall i e. Interval i e => i -> e
upperBound i
a e -> e -> Bool
forall a. Eq a => a -> a -> Bool
== i -> e
forall i e. Interval i e => i -> e
upperBound i
b Bool -> Bool -> Bool
&& (i -> Bool
forall i e. Interval i e => i -> Bool
rightClosed i
a Bool -> Bool -> Bool
|| Bool -> Bool
not (i -> Bool
forall i e. Interval i e => i -> Bool
rightClosed i
b))))
overlaps :: i -> i -> Bool
i
a `overlaps` i
b = (i -> e
forall i e. Interval i e => i -> e
lowerBound i
a e -> e -> Bool
forall a. Ord a => a -> a -> Bool
< i -> e
forall i e. Interval i e => i -> e
upperBound i
b Bool -> Bool -> Bool
|| (i -> e
forall i e. Interval i e => i -> e
lowerBound i
a e -> e -> Bool
forall a. Eq a => a -> a -> Bool
== i -> e
forall i e. Interval i e => i -> e
upperBound i
b Bool -> Bool -> Bool
&& i -> Bool
forall i e. Interval i e => i -> Bool
leftClosed i
a Bool -> Bool -> Bool
&& i -> Bool
forall i e. Interval i e => i -> Bool
rightClosed i
b))
Bool -> Bool -> Bool
&&
(i -> e
forall i e. Interval i e => i -> e
upperBound i
a e -> e -> Bool
forall a. Ord a => a -> a -> Bool
> i -> e
forall i e. Interval i e => i -> e
lowerBound i
b Bool -> Bool -> Bool
|| (i -> e
forall i e. Interval i e => i -> e
upperBound i
a e -> e -> Bool
forall a. Eq a => a -> a -> Bool
== i -> e
forall i e. Interval i e => i -> e
lowerBound i
b Bool -> Bool -> Bool
&& i -> Bool
forall i e. Interval i e => i -> Bool
rightClosed i
a Bool -> Bool -> Bool
&& i -> Bool
forall i e. Interval i e => i -> Bool
leftClosed i
b))
below :: e -> i -> Bool
e
p `below` i
i = case e -> e -> Ordering
forall a. Ord a => a -> a -> Ordering
compare e
p (i -> e
forall i e. Interval i e => i -> e
lowerBound i
i) of
Ordering
LT -> Bool
True
Ordering
EQ -> Bool -> Bool
not (i -> Bool
forall i e. Interval i e => i -> Bool
leftClosed i
i)
Ordering
GT -> Bool
False
above :: e -> i -> Bool
e
p `above` i
i = case e -> e -> Ordering
forall a. Ord a => a -> a -> Ordering
compare e
p (i -> e
forall i e. Interval i e => i -> e
upperBound i
i) of
Ordering
LT -> Bool
False
Ordering
EQ -> Bool -> Bool
not (i -> Bool
forall i e. Interval i e => i -> Bool
rightClosed i
i)
Ordering
GT -> Bool
True
inside :: e -> i -> Bool
e
p `inside` i
i = Bool -> Bool
not ((e
p e -> i -> Bool
forall i e. Interval i e => e -> i -> Bool
`above` i
i) Bool -> Bool -> Bool
|| (e
p e -> i -> Bool
forall i e. Interval i e => e -> i -> Bool
`below` i
i))
isEmpty :: i -> Bool
isEmpty i
i | i -> Bool
forall i e. Interval i e => i -> Bool
leftClosed i
i Bool -> Bool -> Bool
&& i -> Bool
forall i e. Interval i e => i -> Bool
rightClosed i
i = i -> e
forall i e. Interval i e => i -> e
lowerBound i
i e -> e -> Bool
forall a. Ord a => a -> a -> Bool
> i -> e
forall i e. Interval i e => i -> e
upperBound i
i
| Bool
otherwise = i -> e
forall i e. Interval i e => i -> e
lowerBound i
i e -> e -> Bool
forall a. Ord a => a -> a -> Bool
>= i -> e
forall i e. Interval i e => i -> e
upperBound i
i
compareUpperBounds :: i -> i -> Ordering
compareUpperBounds i
a i
b = case e -> e -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (i -> e
forall i e. Interval i e => i -> e
upperBound i
a) (i -> e
forall i e. Interval i e => i -> e
upperBound i
b) of
Ordering
LT -> Ordering
LT
Ordering
GT -> Ordering
GT
Ordering
EQ -> case (i -> Bool
forall i e. Interval i e => i -> Bool
rightClosed i
a, i -> Bool
forall i e. Interval i e => i -> Bool
rightClosed i
b) of
(Bool
False, Bool
True) -> Ordering
LT
(Bool
True, Bool
False) -> Ordering
GT
(Bool, Bool)
_ -> Ordering
EQ
genericEquals :: (Interval i e) => i -> i -> Bool
genericEquals :: forall i e. Interval i e => i -> i -> Bool
genericEquals i
a i
b = i -> e
forall i e. Interval i e => i -> e
lowerBound i
a e -> e -> Bool
forall a. Eq a => a -> a -> Bool
== i -> e
forall i e. Interval i e => i -> e
lowerBound i
b Bool -> Bool -> Bool
&& i -> e
forall i e. Interval i e => i -> e
upperBound i
a e -> e -> Bool
forall a. Eq a => a -> a -> Bool
== i -> e
forall i e. Interval i e => i -> e
upperBound i
b
Bool -> Bool -> Bool
&& i -> Bool
forall i e. Interval i e => i -> Bool
leftClosed i
a Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
== i -> Bool
forall i e. Interval i e => i -> Bool
leftClosed i
b
Bool -> Bool -> Bool
&& i -> Bool
forall i e. Interval i e => i -> Bool
rightClosed i
a Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
== i -> Bool
forall i e. Interval i e => i -> Bool
rightClosed i
b
genericCompare :: (Interval i e) => i -> i -> Ordering
genericCompare :: forall i e. Interval i e => i -> i -> Ordering
genericCompare i
a i
b = case i -> i -> Ordering
forall i e. Interval i e => i -> i -> Ordering
compareL i
a i
b of
Ordering
LT -> Ordering
LT
Ordering
GT -> Ordering
GT
Ordering
EQ -> i -> i -> Ordering
forall i e. Interval i e => i -> i -> Ordering
compareU i
a i
b
compareL :: (Interval i e) => i -> i -> Ordering
compareL :: forall i e. Interval i e => i -> i -> Ordering
compareL i
a i
b = case e -> e -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (i -> e
forall i e. Interval i e => i -> e
lowerBound i
a) (i -> e
forall i e. Interval i e => i -> e
lowerBound i
b) of
Ordering
LT -> Ordering
LT
Ordering
GT -> Ordering
GT
Ordering
EQ -> case (i -> Bool
forall i e. Interval i e => i -> Bool
leftClosed i
a, i -> Bool
forall i e. Interval i e => i -> Bool
leftClosed i
b) of
(Bool
True, Bool
False) -> Ordering
LT
(Bool
False, Bool
True) -> Ordering
GT
(Bool, Bool)
_ -> Ordering
EQ
compareU :: (Interval i e) => i -> i -> Ordering
compareU :: forall i e. Interval i e => i -> i -> Ordering
compareU i
a i
b = case e -> e -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (i -> e
forall i e. Interval i e => i -> e
upperBound i
a) (i -> e
forall i e. Interval i e => i -> e
upperBound i
b) of
Ordering
LT -> Ordering
LT
Ordering
GT -> Ordering
GT
Ordering
EQ -> case (i -> Bool
forall i e. Interval i e => i -> Bool
rightClosed i
a, i -> Bool
forall i e. Interval i e => i -> Bool
rightClosed i
b) of
(Bool
True, Bool
False) -> Ordering
GT
(Bool
False, Bool
True) -> Ordering
LT
(Bool, Bool)
_ -> Ordering
EQ
instance Ord a => Interval (I.Interval a) a where
lowerBound :: Interval a -> a
lowerBound = Interval a -> a
forall a. Interval a -> a
I.lowerBound
upperBound :: Interval a -> a
upperBound = Interval a -> a
forall a. Interval a -> a
I.upperBound
leftClosed :: Interval a -> Bool
leftClosed = Interval a -> Bool
forall a. Interval a -> Bool
I.leftClosed
rightClosed :: Interval a -> Bool
rightClosed = Interval a -> Bool
forall a. Interval a -> Bool
I.rightClosed
overlaps :: Interval a -> Interval a -> Bool
overlaps = Interval a -> Interval a -> Bool
forall a. Ord a => Interval a -> Interval a -> Bool
I.overlaps
subsumes :: Interval a -> Interval a -> Bool
subsumes = Interval a -> Interval a -> Bool
forall a. Ord a => Interval a -> Interval a -> Bool
I.subsumes
before :: Interval a -> Interval a -> Bool
before = Interval a -> Interval a -> Bool
forall a. Ord a => Interval a -> Interval a -> Bool
I.before
after :: Interval a -> Interval a -> Bool
after = Interval a -> Interval a -> Bool
forall a. Ord a => Interval a -> Interval a -> Bool
I.after
above :: a -> Interval a -> Bool
above = a -> Interval a -> Bool
forall a. Ord a => a -> Interval a -> Bool
I.above
below :: a -> Interval a -> Bool
below = a -> Interval a -> Bool
forall a. Ord a => a -> Interval a -> Bool
I.below
inside :: a -> Interval a -> Bool
inside = a -> Interval a -> Bool
forall a. Ord a => a -> Interval a -> Bool
I.inside
isEmpty :: Interval a -> Bool
isEmpty = Interval a -> Bool
forall a. Ord a => Interval a -> Bool
I.isEmpty
compareUpperBounds :: Interval a -> Interval a -> Ordering
compareUpperBounds = Interval a -> Interval a -> Ordering
forall a. Ord a => Interval a -> Interval a -> Ordering
I.compareByUpper