{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
#ifndef MIN_VERSION_lens
#define MIN_VERSION_lens(x,y,z) 1
#endif
module Text.Trifecta.Util.IntervalMap
(
Interval(..)
, IntervalMap(..), singleton, insert
, search, intersections, dominators
, offset
, IntInterval(..)
, fromList
) where
import qualified Data.Foldable.WithIndex as WithIndex
import Control.Lens as Lens hiding ((:<), (<|), (|>))
import Data.FingerTree
(FingerTree, Measured (..), ViewL (..), (<|), (><))
import qualified Data.FingerTree as FT
import qualified Data.Functor.WithIndex as WithIndex
#if !(MIN_VERSION_base(4,11,0))
import Data.Semigroup
#endif
import Data.Semigroup.Reducer
import Data.Semigroup.Union
import qualified Data.Traversable.WithIndex as WithIndex
data Interval v = Interval { forall v. Interval v -> v
low :: v, forall v. Interval v -> v
high :: v }
deriving Int -> Interval v -> ShowS
forall v. Show v => Int -> Interval v -> ShowS
forall v. Show v => [Interval v] -> ShowS
forall v. Show v => Interval v -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Interval v] -> ShowS
$cshowList :: forall v. Show v => [Interval v] -> ShowS
show :: Interval v -> String
$cshow :: forall v. Show v => Interval v -> String
showsPrec :: Int -> Interval v -> ShowS
$cshowsPrec :: forall v. Show v => Int -> Interval v -> ShowS
Show
instance Ord v => Semigroup (Interval v) where
Interval v
a v
b <> :: Interval v -> Interval v -> Interval v
<> Interval v
c v
d = forall v. v -> v -> Interval v
Interval (forall a. Ord a => a -> a -> a
min v
a v
c) (forall a. Ord a => a -> a -> a
max v
b v
d)
instance (Ord v, Monoid v) => Reducer v (Interval v) where
unit :: v -> Interval v
unit v
v = forall v. v -> v -> Interval v
Interval v
v v
v
cons :: v -> Interval v -> Interval v
cons v
v (Interval v
a v
b) = forall v. v -> v -> Interval v
Interval (v
v forall a. Monoid a => a -> a -> a
`mappend` v
a) (v
v forall a. Monoid a => a -> a -> a
`mappend` v
b)
snoc :: Interval v -> v -> Interval v
snoc (Interval v
a v
b) v
v = forall v. v -> v -> Interval v
Interval (v
a forall a. Monoid a => a -> a -> a
`mappend` v
v) (v
b forall a. Monoid a => a -> a -> a
`mappend` v
v)
instance Eq v => Eq (Interval v) where
Interval v
a v
b == :: Interval v -> Interval v -> Bool
== Interval v
c v
d = v
a forall a. Eq a => a -> a -> Bool
== v
c Bool -> Bool -> Bool
&& v
d forall a. Eq a => a -> a -> Bool
== v
b
instance Ord v => Ord (Interval v) where
compare :: Interval v -> Interval v -> Ordering
compare (Interval v
a v
b) (Interval v
c v
d) = case forall a. Ord a => a -> a -> Ordering
compare v
a v
c of
Ordering
LT -> Ordering
LT
Ordering
EQ -> forall a. Ord a => a -> a -> Ordering
compare v
d v
b
Ordering
GT -> Ordering
GT
instance Functor Interval where
fmap :: forall a b. (a -> b) -> Interval a -> Interval b
fmap a -> b
f (Interval a
a a
b) = forall v. v -> v -> Interval v
Interval (a -> b
f a
a) (a -> b
f a
b)
instance Foldable Interval where
foldMap :: forall m a. Monoid m => (a -> m) -> Interval a -> m
foldMap a -> m
f (Interval a
a a
b) = a -> m
f a
a forall a. Monoid a => a -> a -> a
`mappend` a -> m
f a
b
instance Traversable Interval where
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Interval a -> f (Interval b)
traverse a -> f b
f (Interval a
a a
b) = forall v. v -> v -> Interval v
Interval forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
b
data Node v a = Node (Interval v) a
instance Functor (Node v) where
fmap :: forall a b. (a -> b) -> Node v a -> Node v b
fmap a -> b
f (Node Interval v
i a
x) = forall v a. Interval v -> a -> Node v a
Node Interval v
i (a -> b
f a
x)
instance WithIndex.FunctorWithIndex (Interval v) (Node v) where
imap :: forall a b. (Interval v -> a -> b) -> Node v a -> Node v b
imap Interval v -> a -> b
f (Node Interval v
i a
x) = forall v a. Interval v -> a -> Node v a
Node Interval v
i (Interval v -> a -> b
f Interval v
i a
x)
instance Foldable (Node v) where
foldMap :: forall m a. Monoid m => (a -> m) -> Node v a -> m
foldMap a -> m
f (Node Interval v
_ a
x) = a -> m
f a
x
instance WithIndex.FoldableWithIndex (Interval v) (Node v) where
ifoldMap :: forall m a. Monoid m => (Interval v -> a -> m) -> Node v a -> m
ifoldMap Interval v -> a -> m
f (Node Interval v
k a
v) = Interval v -> a -> m
f Interval v
k a
v
instance Traversable (Node v) where
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Node v a -> f (Node v b)
traverse a -> f b
f (Node Interval v
i a
x) = forall v a. Interval v -> a -> Node v a
Node Interval v
i forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
x
instance WithIndex.TraversableWithIndex (Interval v) (Node v) where
itraverse :: forall (f :: * -> *) a b.
Applicative f =>
(Interval v -> a -> f b) -> Node v a -> f (Node v b)
itraverse Interval v -> a -> f b
f (Node Interval v
i a
x) = forall v a. Interval v -> a -> Node v a
Node Interval v
i forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Interval v -> a -> f b
f Interval v
i a
x
#if !MIN_VERSION_lens(5,0,0)
instance Lens.FunctorWithIndex (Interval v) (Node v) where imap = WithIndex.imap
instance Lens.FoldableWithIndex (Interval v) (Node v) where ifoldMap = WithIndex.ifoldMap
instance Lens.TraversableWithIndex (Interval v) (Node v) where itraverse = WithIndex.itraverse
#endif
data IntInterval v = NoInterval | IntInterval (Interval v) v
instance Ord v => Monoid (IntInterval v) where
mempty :: IntInterval v
mempty = forall v. IntInterval v
NoInterval
mappend :: IntInterval v -> IntInterval v -> IntInterval v
mappend = forall a. Semigroup a => a -> a -> a
(<>)
instance Ord v => Semigroup (IntInterval v) where
IntInterval v
NoInterval <> :: IntInterval v -> IntInterval v -> IntInterval v
<> IntInterval v
i = IntInterval v
i
IntInterval v
i <> IntInterval v
NoInterval = IntInterval v
i
IntInterval Interval v
_ v
hi1 <> IntInterval Interval v
int2 v
hi2 =
forall v. Interval v -> v -> IntInterval v
IntInterval Interval v
int2 (forall a. Ord a => a -> a -> a
max v
hi1 v
hi2)
instance Ord v => Measured (IntInterval v) (Node v a) where
measure :: Node v a -> IntInterval v
measure (Node Interval v
i a
_) = forall v. Interval v -> v -> IntInterval v
IntInterval Interval v
i (forall v. Interval v -> v
high Interval v
i)
newtype IntervalMap v a = IntervalMap { forall v a.
IntervalMap v a -> FingerTree (IntInterval v) (Node v a)
runIntervalMap :: FingerTree (IntInterval v) (Node v a) }
instance Functor (IntervalMap v) where
fmap :: forall a b. (a -> b) -> IntervalMap v a -> IntervalMap v b
fmap a -> b
f (IntervalMap FingerTree (IntInterval v) (Node v a)
t) = forall v a.
FingerTree (IntInterval v) (Node v a) -> IntervalMap v a
IntervalMap (forall a b v. (a -> b) -> FingerTree v a -> FingerTree v b
FT.unsafeFmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) FingerTree (IntInterval v) (Node v a)
t)
instance FunctorWithIndex (Interval v) (IntervalMap v) where
imap :: forall a b.
(Interval v -> a -> b) -> IntervalMap v a -> IntervalMap v b
imap Interval v -> a -> b
f (IntervalMap FingerTree (IntInterval v) (Node v a)
t) = forall v a.
FingerTree (IntInterval v) (Node v a) -> IntervalMap v a
IntervalMap (forall a b v. (a -> b) -> FingerTree v a -> FingerTree v b
FT.unsafeFmap (forall i (f :: * -> *) a b.
FunctorWithIndex i f =>
(i -> a -> b) -> f a -> f b
imap Interval v -> a -> b
f) FingerTree (IntInterval v) (Node v a)
t)
instance Foldable (IntervalMap v) where
foldMap :: forall m a. Monoid m => (a -> m) -> IntervalMap v a -> m
foldMap a -> m
f (IntervalMap FingerTree (IntInterval v) (Node v a)
t) = forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f) FingerTree (IntInterval v) (Node v a)
t
instance FoldableWithIndex (Interval v) (IntervalMap v) where
ifoldMap :: forall m a.
Monoid m =>
(Interval v -> a -> m) -> IntervalMap v a -> m
ifoldMap Interval v -> a -> m
f (IntervalMap FingerTree (IntInterval v) (Node v a)
t) = forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (forall i (f :: * -> *) m a.
(FoldableWithIndex i f, Monoid m) =>
(i -> a -> m) -> f a -> m
ifoldMap Interval v -> a -> m
f) FingerTree (IntInterval v) (Node v a)
t
instance Traversable (IntervalMap v) where
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> IntervalMap v a -> f (IntervalMap v b)
traverse a -> f b
f (IntervalMap FingerTree (IntInterval v) (Node v a)
t) =
forall v a.
FingerTree (IntInterval v) (Node v a) -> IntervalMap v a
IntervalMap forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a b v.
Applicative f =>
(a -> f b) -> FingerTree v a -> f (FingerTree v b)
FT.unsafeTraverse (forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f) FingerTree (IntInterval v) (Node v a)
t
instance TraversableWithIndex (Interval v) (IntervalMap v) where
itraverse :: forall (f :: * -> *) a b.
Applicative f =>
(Interval v -> a -> f b) -> IntervalMap v a -> f (IntervalMap v b)
itraverse Interval v -> a -> f b
f (IntervalMap FingerTree (IntInterval v) (Node v a)
t) =
forall v a.
FingerTree (IntInterval v) (Node v a) -> IntervalMap v a
IntervalMap forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a b v.
Applicative f =>
(a -> f b) -> FingerTree v a -> f (FingerTree v b)
FT.unsafeTraverse (forall i (t :: * -> *) (f :: * -> *) a b.
(TraversableWithIndex i t, Applicative f) =>
(i -> a -> f b) -> t a -> f (t b)
itraverse Interval v -> a -> f b
f) FingerTree (IntInterval v) (Node v a)
t
instance Ord v => Measured (IntInterval v) (IntervalMap v a) where
measure :: IntervalMap v a -> IntInterval v
measure (IntervalMap FingerTree (IntInterval v) (Node v a)
m) = forall v a. Measured v a => a -> v
measure FingerTree (IntInterval v) (Node v a)
m
largerError :: a
largerError :: forall a. a
largerError = forall a. HasCallStack => String -> a
error String
"Text.Trifecta.IntervalMap.larger: the impossible happened"
instance Ord v => HasUnion (IntervalMap v a) where
union :: IntervalMap v a -> IntervalMap v a -> IntervalMap v a
union (IntervalMap FingerTree (IntInterval v) (Node v a)
xs) (IntervalMap FingerTree (IntInterval v) (Node v a)
ys) = forall v a.
FingerTree (IntInterval v) (Node v a) -> IntervalMap v a
IntervalMap (forall {v} {a}.
Ord v =>
FingerTree (IntInterval v) (Node v a)
-> FingerTree (IntInterval v) (Node v a)
-> FingerTree (IntInterval v) (Node v a)
merge1 FingerTree (IntInterval v) (Node v a)
xs FingerTree (IntInterval v) (Node v a)
ys) where
merge1 :: FingerTree (IntInterval v) (Node v a)
-> FingerTree (IntInterval v) (Node v a)
-> FingerTree (IntInterval v) (Node v a)
merge1 FingerTree (IntInterval v) (Node v a)
as FingerTree (IntInterval v) (Node v a)
bs = case forall v a.
Measured v a =>
FingerTree v a -> ViewL (FingerTree v) a
FT.viewl FingerTree (IntInterval v) (Node v a)
as of
ViewL (FingerTree (IntInterval v)) (Node v a)
EmptyL -> FingerTree (IntInterval v) (Node v a)
bs
a :: Node v a
a@(Node Interval v
i a
_) :< FingerTree (IntInterval v) (Node v a)
as' -> FingerTree (IntInterval v) (Node v a)
l forall v a.
Measured v a =>
FingerTree v a -> FingerTree v a -> FingerTree v a
>< Node v a
a forall v a. Measured v a => a -> FingerTree v a -> FingerTree v a
<| FingerTree (IntInterval v) (Node v a)
-> FingerTree (IntInterval v) (Node v a)
-> FingerTree (IntInterval v) (Node v a)
merge2 FingerTree (IntInterval v) (Node v a)
as' FingerTree (IntInterval v) (Node v a)
r
where
(FingerTree (IntInterval v) (Node v a)
l, FingerTree (IntInterval v) (Node v a)
r) = forall v a.
Measured v a =>
(v -> Bool) -> FingerTree v a -> (FingerTree v a, FingerTree v a)
FT.split IntInterval v -> Bool
larger FingerTree (IntInterval v) (Node v a)
bs
larger :: IntInterval v -> Bool
larger (IntInterval Interval v
k v
_) = Interval v
k forall a. Ord a => a -> a -> Bool
>= Interval v
i
larger IntInterval v
_ = forall a. a
largerError
merge2 :: FingerTree (IntInterval v) (Node v a)
-> FingerTree (IntInterval v) (Node v a)
-> FingerTree (IntInterval v) (Node v a)
merge2 FingerTree (IntInterval v) (Node v a)
as FingerTree (IntInterval v) (Node v a)
bs = case forall v a.
Measured v a =>
FingerTree v a -> ViewL (FingerTree v) a
FT.viewl FingerTree (IntInterval v) (Node v a)
bs of
ViewL (FingerTree (IntInterval v)) (Node v a)
EmptyL -> FingerTree (IntInterval v) (Node v a)
as
b :: Node v a
b@(Node Interval v
i a
_) :< FingerTree (IntInterval v) (Node v a)
bs' -> FingerTree (IntInterval v) (Node v a)
l forall v a.
Measured v a =>
FingerTree v a -> FingerTree v a -> FingerTree v a
>< Node v a
b forall v a. Measured v a => a -> FingerTree v a -> FingerTree v a
<| FingerTree (IntInterval v) (Node v a)
-> FingerTree (IntInterval v) (Node v a)
-> FingerTree (IntInterval v) (Node v a)
merge1 FingerTree (IntInterval v) (Node v a)
r FingerTree (IntInterval v) (Node v a)
bs'
where
(FingerTree (IntInterval v) (Node v a)
l, FingerTree (IntInterval v) (Node v a)
r) = forall v a.
Measured v a =>
(v -> Bool) -> FingerTree v a -> (FingerTree v a, FingerTree v a)
FT.split IntInterval v -> Bool
larger FingerTree (IntInterval v) (Node v a)
as
larger :: IntInterval v -> Bool
larger (IntInterval Interval v
k v
_) = Interval v
k forall a. Ord a => a -> a -> Bool
>= Interval v
i
larger IntInterval v
_ = forall a. a
largerError
instance Ord v => HasUnion0 (IntervalMap v a) where
empty :: IntervalMap v a
empty = forall v a.
FingerTree (IntInterval v) (Node v a) -> IntervalMap v a
IntervalMap forall v a. Measured v a => FingerTree v a
FT.empty
instance Ord v => Monoid (IntervalMap v a) where
mempty :: IntervalMap v a
mempty = forall f. HasUnion0 f => f
empty
mappend :: IntervalMap v a -> IntervalMap v a -> IntervalMap v a
mappend = forall a. Semigroup a => a -> a -> a
(<>)
instance Ord v => Semigroup (IntervalMap v a) where
<> :: IntervalMap v a -> IntervalMap v a -> IntervalMap v a
(<>) = forall f. HasUnion f => f -> f -> f
union
offset :: (Ord v, Monoid v) => v -> IntervalMap v a -> IntervalMap v a
offset :: forall v a.
(Ord v, Monoid v) =>
v -> IntervalMap v a -> IntervalMap v a
offset v
v (IntervalMap FingerTree (IntInterval v) (Node v a)
m) = forall v a.
FingerTree (IntInterval v) (Node v a) -> IntervalMap v a
IntervalMap forall a b. (a -> b) -> a -> b
$ forall v1 a1 v2 a2.
(Measured v1 a1, Measured v2 a2) =>
(a1 -> a2) -> FingerTree v1 a1 -> FingerTree v2 a2
FT.fmap' (\(Node (Interval v
lo v
hi) a
a) -> forall v a. Interval v -> a -> Node v a
Node (forall v. v -> v -> Interval v
Interval (forall a. Monoid a => a -> a -> a
mappend v
v v
lo) (forall a. Monoid a => a -> a -> a
mappend v
v v
hi)) a
a) FingerTree (IntInterval v) (Node v a)
m
singleton :: Ord v => Interval v -> a -> IntervalMap v a
singleton :: forall v a. Ord v => Interval v -> a -> IntervalMap v a
singleton Interval v
i a
x = forall v a.
FingerTree (IntInterval v) (Node v a) -> IntervalMap v a
IntervalMap (forall v a. Measured v a => a -> FingerTree v a
FT.singleton (forall v a. Interval v -> a -> Node v a
Node Interval v
i a
x))
insert :: Ord v => v -> v -> a -> IntervalMap v a -> IntervalMap v a
insert :: forall v a.
Ord v =>
v -> v -> a -> IntervalMap v a -> IntervalMap v a
insert v
lo v
hi a
_ IntervalMap v a
m | v
lo forall a. Ord a => a -> a -> Bool
> v
hi = IntervalMap v a
m
insert v
lo v
hi a
x (IntervalMap FingerTree (IntInterval v) (Node v a)
t) = forall v a.
FingerTree (IntInterval v) (Node v a) -> IntervalMap v a
IntervalMap (FingerTree (IntInterval v) (Node v a)
l forall v a.
Measured v a =>
FingerTree v a -> FingerTree v a -> FingerTree v a
>< forall v a. Interval v -> a -> Node v a
Node Interval v
i a
x forall v a. Measured v a => a -> FingerTree v a -> FingerTree v a
<| FingerTree (IntInterval v) (Node v a)
r) where
i :: Interval v
i = forall v. v -> v -> Interval v
Interval v
lo v
hi
(FingerTree (IntInterval v) (Node v a)
l, FingerTree (IntInterval v) (Node v a)
r) = forall v a.
Measured v a =>
(v -> Bool) -> FingerTree v a -> (FingerTree v a, FingerTree v a)
FT.split IntInterval v -> Bool
larger FingerTree (IntInterval v) (Node v a)
t
larger :: IntInterval v -> Bool
larger (IntInterval Interval v
k v
_) = Interval v
k forall a. Ord a => a -> a -> Bool
>= Interval v
i
larger IntInterval v
_ = forall a. a
largerError
dominators :: Ord v => v -> v -> IntervalMap v a -> [(Interval v, a)]
dominators :: forall v a. Ord v => v -> v -> IntervalMap v a -> [(Interval v, a)]
dominators v
i v
j = forall v a. Ord v => v -> v -> IntervalMap v a -> [(Interval v, a)]
intersections v
j v
i
search :: Ord v => v -> IntervalMap v a -> [(Interval v, a)]
search :: forall v a. Ord v => v -> IntervalMap v a -> [(Interval v, a)]
search v
p = forall v a. Ord v => v -> v -> IntervalMap v a -> [(Interval v, a)]
intersections v
p v
p
intersections :: Ord v => v -> v -> IntervalMap v a -> [(Interval v, a)]
intersections :: forall v a. Ord v => v -> v -> IntervalMap v a -> [(Interval v, a)]
intersections v
lo v
hi (IntervalMap FingerTree (IntInterval v) (Node v a)
t) = FingerTree (IntInterval v) (Node v a) -> [(Interval v, a)]
matches (forall v a.
Measured v a =>
(v -> Bool) -> FingerTree v a -> FingerTree v a
FT.takeUntil (forall v. Ord v => v -> IntInterval v -> Bool
greater v
hi) FingerTree (IntInterval v) (Node v a)
t) where
matches :: FingerTree (IntInterval v) (Node v a) -> [(Interval v, a)]
matches FingerTree (IntInterval v) (Node v a)
xs = case forall v a.
Measured v a =>
FingerTree v a -> ViewL (FingerTree v) a
FT.viewl (forall v a.
Measured v a =>
(v -> Bool) -> FingerTree v a -> FingerTree v a
FT.dropUntil (forall v. Ord v => v -> IntInterval v -> Bool
atleast v
lo) FingerTree (IntInterval v) (Node v a)
xs) of
ViewL (FingerTree (IntInterval v)) (Node v a)
EmptyL -> []
Node Interval v
i a
x :< FingerTree (IntInterval v) (Node v a)
xs' -> (Interval v
i, a
x) forall a. a -> [a] -> [a]
: FingerTree (IntInterval v) (Node v a) -> [(Interval v, a)]
matches FingerTree (IntInterval v) (Node v a)
xs'
atleast :: Ord v => v -> IntInterval v -> Bool
atleast :: forall v. Ord v => v -> IntInterval v -> Bool
atleast v
k (IntInterval Interval v
_ v
hi) = v
k forall a. Ord a => a -> a -> Bool
<= v
hi
atleast v
_ IntInterval v
_ = Bool
False
greater :: Ord v => v -> IntInterval v -> Bool
greater :: forall v. Ord v => v -> IntInterval v -> Bool
greater v
k (IntInterval Interval v
i v
_) = forall v. Interval v -> v
low Interval v
i forall a. Ord a => a -> a -> Bool
> v
k
greater v
_ IntInterval v
_ = Bool
False
fromList :: Ord v => [(v, v, a)] -> IntervalMap v a
fromList :: forall v a. Ord v => [(v, v, a)] -> IntervalMap v a
fromList = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall {v} {a}.
Ord v =>
(v, v, a) -> IntervalMap v a -> IntervalMap v a
ins forall f. HasUnion0 f => f
empty where
ins :: (v, v, a) -> IntervalMap v a -> IntervalMap v a
ins (v
lo, v
hi, a
n) = forall v a.
Ord v =>
v -> v -> a -> IntervalMap v a -> IntervalMap v a
insert v
lo v
hi a
n