-- | Maps containing non-overlapping intervals.

module Agda.Utils.RangeMap
  ( IsBasicRangeMap(..)
  , several
  , PairInt(..)
  , RangeMap(..)
  , rangeMapInvariant
  , fromNonOverlappingNonEmptyAscendingList
  , insert
  , splitAt
  , insideAndOutside
  , restrictTo
  )
  where

import Prelude hiding (null, splitAt)

import Control.DeepSeq

import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap

import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map

import Data.Semigroup

import Data.Strict.Tuple (Pair(..))

import Agda.Interaction.Highlighting.Range
import Agda.Utils.List
import Agda.Utils.Null

------------------------------------------------------------------------
-- An abstraction

-- | A class that is intended to make it easy to swap between
-- different range map implementations.
--
-- Note that some 'RangeMap' operations are not included in this
-- class.

class IsBasicRangeMap a m | m -> a where

  -- | The map @'singleton' rs x@ contains the ranges from @rs@, and
  -- every position in those ranges is associated with @x@.

  singleton :: Ranges -> a -> m

  -- | Converts range maps to 'IntMap's from positions to values.

  toMap :: m -> IntMap a

  -- | Converts the map to a list. The ranges are non-overlapping and
  -- non-empty, and earlier ranges precede later ones in the list.

  toList :: m -> [(Range, a)]

  -- | Returns the smallest range covering everything in the map (or
  -- 'Nothing', if the range would be empty).
  --
  -- Note that the default implementation of this operation might be
  -- inefficient.

  coveringRange :: m -> Maybe Range
  coveringRange m
f = do
    Int
min <- forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. IntMap a -> Maybe (Int, a)
IntMap.lookupMin IntMap a
m
    Int
max <- forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. IntMap a -> Maybe (Int, a)
IntMap.lookupMax IntMap a
m
    forall (m :: * -> *) a. Monad m => a -> m a
return (Range { from :: Int
from = Int
min, to :: Int
to = Int
max forall a. Num a => a -> a -> a
+ Int
1 })
    where
    m :: IntMap a
m = forall a m. IsBasicRangeMap a m => m -> IntMap a
toMap m
f

-- | Like 'singleton', but with several 'Ranges' instead of only one.

several ::
  (IsBasicRangeMap a hl, Monoid hl) =>
  [Ranges] -> a -> hl
several :: forall a hl.
(IsBasicRangeMap a hl, Monoid hl) =>
[Ranges] -> a -> hl
several [Ranges]
rss a
m = forall a. Monoid a => [a] -> a
mconcat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a m. IsBasicRangeMap a m => Ranges -> a -> m
singleton a
m) [Ranges]
rss

------------------------------------------------------------------------
-- A strict pair type

-- | A strict pair type where the first argument must be an 'Int'.
--
-- This type is included because there is no 'NFData' instance for
-- 'Pair' in the package @strict@ before version 4.

newtype PairInt a = PairInt (Pair Int a)
  deriving Int -> PairInt a -> ShowS
forall a. Show a => Int -> PairInt a -> ShowS
forall a. Show a => [PairInt a] -> ShowS
forall a. Show a => PairInt a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PairInt a] -> ShowS
$cshowList :: forall a. Show a => [PairInt a] -> ShowS
show :: PairInt a -> String
$cshow :: forall a. Show a => PairInt a -> String
showsPrec :: Int -> PairInt a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> PairInt a -> ShowS
Show

instance NFData a => NFData (PairInt a) where
  rnf :: PairInt a -> ()
rnf (PairInt (Int
_ :!: a
y)) = forall a. NFData a => a -> ()
rnf a
y

-- | Constructs a pair.

pair :: Int -> a -> PairInt a
pair :: forall a. Int -> a -> PairInt a
pair Int
x a
y = forall a. Pair Int a -> PairInt a
PairInt (Int
x forall a b. a -> b -> Pair a b
:!: a
y)

------------------------------------------------------------------------
-- The type

-- | Maps containing non-overlapping intervals.
--
-- The implementation does not use IntMap, because IntMap does not
-- come with a constant-time size function.
--
-- Note the invariant which 'RangeMap's should satisfy
-- ('rangeMapInvariant').

newtype RangeMap a = RangeMap
  { forall a. RangeMap a -> Map Int (PairInt a)
rangeMap :: Map Int (PairInt a)
    -- ^ The keys are starting points of ranges, and the pairs contain
    -- endpoints and values.
  }
  deriving (Int -> RangeMap a -> ShowS
forall a. Show a => Int -> RangeMap a -> ShowS
forall a. Show a => [RangeMap a] -> ShowS
forall a. Show a => RangeMap a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RangeMap a] -> ShowS
$cshowList :: forall a. Show a => [RangeMap a] -> ShowS
show :: RangeMap a -> String
$cshow :: forall a. Show a => RangeMap a -> String
showsPrec :: Int -> RangeMap a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> RangeMap a -> ShowS
Show, RangeMap a -> ()
forall a. NFData a => RangeMap a -> ()
forall a. (a -> ()) -> NFData a
rnf :: RangeMap a -> ()
$crnf :: forall a. NFData a => RangeMap a -> ()
NFData)

-- | Invariant for 'RangeMap'.
--
-- The ranges must not be empty, and they must not overlap.

rangeMapInvariant :: RangeMap a -> Bool
rangeMapInvariant :: forall a. RangeMap a -> Bool
rangeMapInvariant RangeMap a
f = forall (t :: * -> *). Foldable t => t Bool -> Bool
and
  [ forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Range -> Bool
rangeInvariant [Range]
rs
  , forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Null a => a -> Bool
null) [Range]
rs
  , forall a b. [a] -> b -> (a -> [a] -> b) -> b
caseList [Range]
rs Bool
True forall a b. (a -> b) -> a -> b
$ \ Range
r [Range]
rs' ->
      forall (t :: * -> *). Foldable t => t Bool -> Bool
and forall a b. (a -> b) -> a -> b
$ forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith forall a. Ord a => a -> a -> Bool
(<=) (forall a b. (a -> b) -> [a] -> [b]
map Range -> Int
to forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> [a]
init1 Range
r [Range]
rs') (forall a b. (a -> b) -> [a] -> [b]
map Range -> Int
from [Range]
rs')
  ]
  where
  rs :: [Range]
rs = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ forall a m. IsBasicRangeMap a m => m -> [(Range, a)]
toList RangeMap a
f

------------------------------------------------------------------------
-- Construction, conversion and inspection

instance Null (RangeMap a) where
  empty :: RangeMap a
empty = RangeMap { rangeMap :: Map Int (PairInt a)
rangeMap = forall k a. Map k a
Map.empty }
  null :: RangeMap a -> Bool
null  = forall k a. Map k a -> Bool
Map.null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. RangeMap a -> Map Int (PairInt a)
rangeMap

instance IsBasicRangeMap a (RangeMap a) where
  singleton :: Ranges -> a -> RangeMap a
singleton (Ranges [Range]
rs) a
m =
    RangeMap { rangeMap :: Map Int (PairInt a)
rangeMap = forall k a. [(k, a)] -> Map k a
Map.fromDistinctAscList [(Int, PairInt a)]
rms }
    where
    rms :: [(Int, PairInt a)]
rms =
      [ (Range -> Int
from Range
r, forall a. Int -> a -> PairInt a
pair (Range -> Int
to Range
r) a
m)
      | Range
r <- [Range]
rs
      , Bool -> Bool
not (forall a. Null a => a -> Bool
null Range
r)
      ]

  toMap :: RangeMap a -> IntMap a
toMap RangeMap a
f =
    forall a. [(Int, a)] -> IntMap a
IntMap.fromList
      [ (Int
p, a
m)
      | (Range
r, a
m) <- forall a m. IsBasicRangeMap a m => m -> [(Range, a)]
toList RangeMap a
f
      , Int
p <- Range -> [Int]
rangeToPositions Range
r
      ]

  toList :: RangeMap a -> [(Range, a)]
toList =
    forall a b. (a -> b) -> [a] -> [b]
map (\(Int
f, PairInt (Int
t :!: a
a)) -> (Range { from :: Int
from = Int
f, to :: Int
to = Int
t } , a
a)) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    forall k a. Map k a -> [(k, a)]
Map.toAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    forall a. RangeMap a -> Map Int (PairInt a)
rangeMap

  coveringRange :: RangeMap a -> Maybe Range
coveringRange RangeMap a
f = do
    Int
min <- forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall k a. Map k a -> Maybe (k, a)
Map.lookupMin (forall a. RangeMap a -> Map Int (PairInt a)
rangeMap RangeMap a
f)
    Int
max <- (\(Int
_, PairInt (Int
p :!: a
_)) -> Int
p) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall k a. Map k a -> Maybe (k, a)
Map.lookupMax (forall a. RangeMap a -> Map Int (PairInt a)
rangeMap RangeMap a
f)
    forall (m :: * -> *) a. Monad m => a -> m a
return (Range { from :: Int
from = Int
min, to :: Int
to = Int
max })

-- | Converts a list of pairs of ranges and values to a 'RangeMap'.
-- The ranges have to be non-overlapping and non-empty, and earlier
-- ranges have to precede later ones.

fromNonOverlappingNonEmptyAscendingList :: [(Range, a)] -> RangeMap a
fromNonOverlappingNonEmptyAscendingList :: forall a. [(Range, a)] -> RangeMap a
fromNonOverlappingNonEmptyAscendingList =
  forall a. Map Int (PairInt a) -> RangeMap a
RangeMap forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  forall k a. [(k, a)] -> Map k a
Map.fromDistinctAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  forall a b. (a -> b) -> [a] -> [b]
map (\(Range
r, a
m) -> (Range -> Int
from Range
r, forall a. Int -> a -> PairInt a
pair (Range -> Int
to Range
r) a
m))

-- | The number of ranges in the map.
--
-- This function should not be exported.

size :: RangeMap a -> Int
size :: forall a. RangeMap a -> Int
size = forall k a. Map k a -> Int
Map.size forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. RangeMap a -> Map Int (PairInt a)
rangeMap

------------------------------------------------------------------------
-- Merging

-- | Inserts a value, along with a corresponding 'Range', into a
-- 'RangeMap'. No attempt is made to merge adjacent ranges with equal
-- values.
--
-- The function argument is used to combine values. The inserted value
-- is given as the first argument to the function.

insert :: (a -> a -> a) -> Range -> a -> RangeMap a -> RangeMap a
insert :: forall a. (a -> a -> a) -> Range -> a -> RangeMap a -> RangeMap a
insert a -> a -> a
combine Range
r a
m (RangeMap Map Int (PairInt a)
f)
  | forall a. Null a => a -> Bool
null Range
r    = forall a. Map Int (PairInt a) -> RangeMap a
RangeMap Map Int (PairInt a)
f
  | Bool
otherwise =
    case Maybe (PairInt a)
equal of
      Just (PairInt (Int
p :!: a
m')) ->
        case forall a. Ord a => a -> a -> Ordering
compare (Range -> Int
to Range
r) Int
p of
          Ordering
EQ ->
            -- The range r matches exactly.
            forall a. Map Int (PairInt a) -> RangeMap a
RangeMap forall a b. (a -> b) -> a -> b
$
            forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (Range -> Int
from Range
r) (forall a. Int -> a -> PairInt a
pair Int
p (a -> a -> a
combine a
m a
m')) Map Int (PairInt a)
f
          Ordering
LT ->
            -- The range r is strictly shorter.
            forall a. Map Int (PairInt a) -> RangeMap a
RangeMap forall a b. (a -> b) -> a -> b
$
            forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (Range -> Int
to Range
r) (forall a. Int -> a -> PairInt a
pair Int
p a
m') forall a b. (a -> b) -> a -> b
$
            forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (Range -> Int
from Range
r) (forall a. Int -> a -> PairInt a
pair (Range -> Int
to Range
r) (a -> a -> a
combine a
m a
m')) Map Int (PairInt a)
f
          Ordering
GT ->
            -- The range r is strictly longer. Continue recursively.
            forall a. (a -> a -> a) -> Range -> a -> RangeMap a -> RangeMap a
insert a -> a -> a
combine (Range { from :: Int
from = Int
p, to :: Int
to = Range -> Int
to Range
r }) a
m forall a b. (a -> b) -> a -> b
$
            forall a. Map Int (PairInt a) -> RangeMap a
RangeMap forall a b. (a -> b) -> a -> b
$
            forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (Range -> Int
from Range
r) (forall a. Int -> a -> PairInt a
pair Int
p (a -> a -> a
combine a
m a
m')) Map Int (PairInt a)
f
      Maybe (PairInt a)
Nothing ->
        -- Find the part of r that does not overlap with anything in
        -- smaller or larger, if any.
        case (Maybe (Int, PairInt a)
overlapLeft, Maybe Int
overlapRight) of
          (Maybe (Int, PairInt a)
Nothing, Maybe Int
Nothing) ->
            -- No overlap.
            forall a. Map Int (PairInt a) -> RangeMap a
RangeMap forall a b. (a -> b) -> a -> b
$
            forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (Range -> Int
from Range
r) (forall a. Int -> a -> PairInt a
pair (Range -> Int
to Range
r) a
m) Map Int (PairInt a)
f
          (Maybe (Int, PairInt a)
Nothing, Just Int
p) ->
            -- Overlap on the right. Continue recursively.
            forall a. (a -> a -> a) -> Range -> a -> RangeMap a -> RangeMap a
insert a -> a -> a
combine (Range { from :: Int
from = Int
p, to :: Int
to = Range -> Int
to Range
r }) a
m forall a b. (a -> b) -> a -> b
$
            forall a. Map Int (PairInt a) -> RangeMap a
RangeMap forall a b. (a -> b) -> a -> b
$
            forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (Range -> Int
from Range
r) (forall a. Int -> a -> PairInt a
pair Int
p a
m) Map Int (PairInt a)
f
          (Just (Int
p1, PairInt (Int
p2 :!: a
m')), Just Int
p3) ->
            -- Overlap on both sides. Continue recursively.
            forall a. (a -> a -> a) -> Range -> a -> RangeMap a -> RangeMap a
insert a -> a -> a
combine (Range { from :: Int
from = Int
p3, to :: Int
to = Range -> Int
to Range
r }) a
m forall a b. (a -> b) -> a -> b
$
            forall a. Map Int (PairInt a) -> RangeMap a
RangeMap forall a b. (a -> b) -> a -> b
$
            (if Int
p2 forall a. Eq a => a -> a -> Bool
== Int
p3 then
               -- The left range ends exactly where the right range
               -- starts.
               forall a. a -> a
id
             else
               -- There is something between the left and right
               -- ranges.
               forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Int
p2 (forall a. Int -> a -> PairInt a
pair Int
p3 a
m)) forall a b. (a -> b) -> a -> b
$
            forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (Range -> Int
from Range
r) (forall a. Int -> a -> PairInt a
pair Int
p2 (a -> a -> a
combine a
m a
m')) forall a b. (a -> b) -> a -> b
$
            forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Int
p1 (forall a. Int -> a -> PairInt a
pair (Range -> Int
from Range
r) a
m') Map Int (PairInt a)
f
          (Just (Int
p1, PairInt (Int
p2 :!: a
m')), Maybe Int
Nothing) ->
            case forall a. Ord a => a -> a -> Ordering
compare Int
p2 (Range -> Int
to Range
r) of
              Ordering
LT ->
                -- Overlap on the left, the left range ends before r.
                forall a. Map Int (PairInt a) -> RangeMap a
RangeMap forall a b. (a -> b) -> a -> b
$
                forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Int
p2 (forall a. Int -> a -> PairInt a
pair (Range -> Int
to Range
r) a
m) forall a b. (a -> b) -> a -> b
$
                forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (Range -> Int
from Range
r) (forall a. Int -> a -> PairInt a
pair Int
p2 (a -> a -> a
combine a
m a
m')) forall a b. (a -> b) -> a -> b
$
                forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Int
p1 (forall a. Int -> a -> PairInt a
pair (Range -> Int
from Range
r) a
m') Map Int (PairInt a)
f
              Ordering
EQ ->
                -- Overlap on the left, the left range ends where r
                -- ends.
                forall a. Map Int (PairInt a) -> RangeMap a
RangeMap forall a b. (a -> b) -> a -> b
$
                forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (Range -> Int
from Range
r) (forall a. Int -> a -> PairInt a
pair (Range -> Int
to Range
r) (a -> a -> a
combine a
m a
m')) forall a b. (a -> b) -> a -> b
$
                forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Int
p1 (forall a. Int -> a -> PairInt a
pair (Range -> Int
from Range
r) a
m') Map Int (PairInt a)
f
              Ordering
GT ->
                -- Overlap on the left, the left range ends after r.
                forall a. Map Int (PairInt a) -> RangeMap a
RangeMap forall a b. (a -> b) -> a -> b
$
                forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (Range -> Int
to Range
r) (forall a. Int -> a -> PairInt a
pair Int
p2 a
m') forall a b. (a -> b) -> a -> b
$
                forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (Range -> Int
from Range
r) (forall a. Int -> a -> PairInt a
pair (Range -> Int
to Range
r) (a -> a -> a
combine a
m a
m')) forall a b. (a -> b) -> a -> b
$
                forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Int
p1 (forall a. Int -> a -> PairInt a
pair (Range -> Int
from Range
r) a
m') Map Int (PairInt a)
f
    where
    (Map Int (PairInt a)
smaller, Maybe (PairInt a)
equal, Map Int (PairInt a)
larger) = forall k a. Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)
Map.splitLookup (Range -> Int
from Range
r) Map Int (PairInt a)
f

    overlapRight :: Maybe Int
overlapRight = case forall k a. Map k a -> Maybe (k, a)
Map.lookupMin Map Int (PairInt a)
larger of
      Maybe (Int, PairInt a)
Nothing -> forall a. Maybe a
Nothing
      Just (Int
from, PairInt a
_)
        | Int
from forall a. Ord a => a -> a -> Bool
< Range -> Int
to Range
r -> forall a. a -> Maybe a
Just Int
from
        | Bool
otherwise   -> forall a. Maybe a
Nothing

    overlapLeft :: Maybe (Int, PairInt a)
overlapLeft = case forall k a. Map k a -> Maybe (k, a)
Map.lookupMax Map Int (PairInt a)
smaller of
      Maybe (Int, PairInt a)
Nothing -> forall a. Maybe a
Nothing
      Just s :: (Int, PairInt a)
s@(Int
_, PairInt (Int
to :!: a
_))
        | Range -> Int
from Range
r forall a. Ord a => a -> a -> Bool
< Int
to -> forall a. a -> Maybe a
Just (Int, PairInt a)
s
        | Bool
otherwise   -> forall a. Maybe a
Nothing

-- | Merges 'RangeMap's by inserting every \"piece\" of the smaller
-- one into the larger one.

instance Semigroup a => Semigroup (RangeMap a) where
  RangeMap a
f1 <> :: RangeMap a -> RangeMap a -> RangeMap a
<> RangeMap a
f2
    | forall a. RangeMap a -> Int
size RangeMap a
f1 forall a. Ord a => a -> a -> Bool
<= forall a. RangeMap a -> Int
size RangeMap a
f2 =
      forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a b. (a -> b) -> a -> b
$ forall a. (a -> a -> a) -> Range -> a -> RangeMap a -> RangeMap a
insert forall a. Semigroup a => a -> a -> a
(<>)) RangeMap a
f2 (forall a m. IsBasicRangeMap a m => m -> [(Range, a)]
toList RangeMap a
f1)
    | Bool
otherwise =
      forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a b. (a -> b) -> a -> b
$ forall a. (a -> a -> a) -> Range -> a -> RangeMap a -> RangeMap a
insert (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. Semigroup a => a -> a -> a
(<>))) RangeMap a
f1 (forall a m. IsBasicRangeMap a m => m -> [(Range, a)]
toList RangeMap a
f2)

-- | Merges 'RangeMap's by inserting every \"piece\" of the smaller
-- one into the larger one.

instance Semigroup a => Monoid (RangeMap a) where
  mempty :: RangeMap a
mempty  = forall a. Null a => a
empty
  mappend :: RangeMap a -> RangeMap a -> RangeMap a
mappend = forall a. Semigroup a => a -> a -> a
(<>)

------------------------------------------------------------------------
-- Splitting

-- | The value of @'splitAt' p f@ is a pair @(f1, f2)@ which contains
-- everything from @f@. All the positions in @f1@ are less than @p@,
-- and all the positions in @f2@ are greater than or equal to @p@.

splitAt :: Int -> RangeMap a -> (RangeMap a, RangeMap a)
splitAt :: forall a. Int -> RangeMap a -> (RangeMap a, RangeMap a)
splitAt Int
p RangeMap a
f = (RangeMap a
before, RangeMap a
after)
  where
  (RangeMap a
before, Maybe ((Int, PairInt a), (Int, PairInt a))
_, RangeMap a
after) = forall a.
Int
-> RangeMap a
-> (RangeMap a, Maybe ((Int, PairInt a), (Int, PairInt a)),
    RangeMap a)
splitAt' Int
p RangeMap a
f

-- | A variant of 'splitAt'. If a range in the middle was split into
-- two pieces, then those two pieces are returned.

splitAt' ::
  Int -> RangeMap a ->
  ( RangeMap a
  , Maybe ((Int, PairInt a), (Int, PairInt a))
  , RangeMap a
  )
splitAt' :: forall a.
Int
-> RangeMap a
-> (RangeMap a, Maybe ((Int, PairInt a), (Int, PairInt a)),
    RangeMap a)
splitAt' Int
p (RangeMap Map Int (PairInt a)
f) =
  case Maybe (PairInt a)
equal of
    Just PairInt a
r  -> ( forall a. Map Int (PairInt a) -> RangeMap a
RangeMap Map Int (PairInt a)
maybeOverlapping
               , forall a. Maybe a
Nothing
               , forall a. Map Int (PairInt a) -> RangeMap a
RangeMap (forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Int
p PairInt a
r Map Int (PairInt a)
larger)
               )
    Maybe (PairInt a)
Nothing ->
      -- Check if maybeOverlapping overlaps with p.
      case forall k a. Map k a -> Maybe ((k, a), Map k a)
Map.maxViewWithKey Map Int (PairInt a)
maybeOverlapping of
        Maybe ((Int, PairInt a), Map Int (PairInt a))
Nothing ->
          (forall a. Null a => a
empty, forall a. Maybe a
Nothing, forall a. Map Int (PairInt a) -> RangeMap a
RangeMap Map Int (PairInt a)
larger)
        Just ((Int
from, PairInt (Int
to :!: a
m)), Map Int (PairInt a)
smaller)
          | Int
to forall a. Ord a => a -> a -> Bool
<= Int
p ->
            ( forall a. Map Int (PairInt a) -> RangeMap a
RangeMap Map Int (PairInt a)
maybeOverlapping
            , forall a. Maybe a
Nothing
            , forall a. Map Int (PairInt a) -> RangeMap a
RangeMap Map Int (PairInt a)
larger
            )
          | Bool
otherwise ->
            -- Here from < p < to.
            ( forall a. Map Int (PairInt a) -> RangeMap a
RangeMap (forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Int
from (forall a. Int -> a -> PairInt a
pair Int
p a
m) Map Int (PairInt a)
smaller)
            , forall a. a -> Maybe a
Just ((Int
from, forall a. Int -> a -> PairInt a
pair Int
p a
m), (Int
p, forall a. Int -> a -> PairInt a
pair Int
to a
m))
            , forall a. Map Int (PairInt a) -> RangeMap a
RangeMap (forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Int
p (forall a. Int -> a -> PairInt a
pair Int
to a
m) Map Int (PairInt a)
larger)
            )
  where
  (Map Int (PairInt a)
maybeOverlapping, Maybe (PairInt a)
equal, Map Int (PairInt a)
larger) = forall k a. Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)
Map.splitLookup Int
p Map Int (PairInt a)
f

-- | Returns a 'RangeMap' overlapping the given range, as well as the
-- rest of the map.

insideAndOutside :: Range -> RangeMap a -> (RangeMap a, RangeMap a)
insideAndOutside :: forall a. Range -> RangeMap a -> (RangeMap a, RangeMap a)
insideAndOutside Range
r RangeMap a
f
  | Range -> Int
from Range
r forall a. Eq a => a -> a -> Bool
== Range -> Int
to Range
r = (forall a. Null a => a
empty, RangeMap a
f)
  | Bool
otherwise      =
    ( RangeMap a
middle
    , -- Because it takes so long to recompile Agda with all
      -- optimisations and run a benchmark no experimental
      -- verification has been made that the code below is better than
      -- reasonable variants. Perhaps it would make sense to benchmark
      -- RangeMap independently of Agda.
      if forall a. RangeMap a -> Int
size RangeMap a
before forall a. Ord a => a -> a -> Bool
< forall a. RangeMap a -> Int
size RangeMap a
middle Bool -> Bool -> Bool
|| forall a. RangeMap a -> Int
size RangeMap a
after forall a. Ord a => a -> a -> Bool
< forall a. RangeMap a -> Int
size RangeMap a
middle then
        forall a. Map Int (PairInt a) -> RangeMap a
RangeMap forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => Map k a -> Map k a -> Map k a
Map.union (forall a. RangeMap a -> Map Int (PairInt a)
rangeMap RangeMap a
before) (forall a. RangeMap a -> Map Int (PairInt a)
rangeMap RangeMap a
after)
      else
        -- If the number of pieces in the middle is "small", remove
        -- the pieces from f instead of merging before and after.
        forall a. Map Int (PairInt a) -> RangeMap a
RangeMap forall a b. (a -> b) -> a -> b
$
        forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) Maybe ((Int, PairInt a), (Int, PairInt a))
split1 forall a b. (a -> b) -> a -> b
$
        forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) Maybe ((Int, PairInt a), (Int, PairInt a))
split2 forall a b. (a -> b) -> a -> b
$
        forall k a b. Ord k => Map k a -> Map k b -> Map k a
Map.difference (forall a. RangeMap a -> Map Int (PairInt a)
rangeMap RangeMap a
f) (forall a. RangeMap a -> Map Int (PairInt a)
rangeMap RangeMap a
middle)
    )
  where
  (RangeMap a
beforeMiddle, Maybe ((Int, PairInt a), (Int, PairInt a))
split1, RangeMap a
after) = forall a.
Int
-> RangeMap a
-> (RangeMap a, Maybe ((Int, PairInt a), (Int, PairInt a)),
    RangeMap a)
splitAt' (Range -> Int
to Range
r) RangeMap a
f
  (RangeMap a
before, Maybe ((Int, PairInt a), (Int, PairInt a))
split2, RangeMap a
middle)      = forall a.
Int
-> RangeMap a
-> (RangeMap a, Maybe ((Int, PairInt a), (Int, PairInt a)),
    RangeMap a)
splitAt' (Range -> Int
from Range
r) RangeMap a
beforeMiddle

-- | Restricts the 'RangeMap' to the given range.

restrictTo :: Range -> RangeMap a -> RangeMap a
restrictTo :: forall a. Range -> RangeMap a -> RangeMap a
restrictTo Range
r = forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Range -> RangeMap a -> (RangeMap a, RangeMap a)
insideAndOutside Range
r