{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
module Brick.BorderMap
( BorderMap
, Edges(..)
, eTopL, eBottomL, eRightL, eLeftL
, empty, clear, emptyCoordinates, singleton
, insertH, insertV, insert
, unsafeUnion
, coordinates, bounds
, values
, lookupRow, lookupCol, lookupH, lookupV, lookup
, setCoordinates, crop, expand
, translate
) where
import Brick.Types.Common (Edges(..), Location(..), eTopL, eBottomL, eRightL, eLeftL, origin)
import Control.Applicative (liftA2)
import Data.IMap (IMap, Run(Run))
import GHC.Generics
import Control.DeepSeq
import Prelude hiding (lookup)
import qualified Data.IMap as IM
neighbors :: Edges a -> Edges (a, a)
neighbors :: forall a. Edges a -> Edges (a, a)
neighbors (Edges a
vt a
vb a
vl a
vr) = (a, a) -> (a, a) -> (a, a) -> (a, a) -> Edges (a, a)
forall a. a -> a -> a -> a -> Edges a
Edges (a, a)
horiz (a, a)
horiz (a, a)
vert (a, a)
vert where
horiz :: (a, a)
horiz = (a
vl, a
vr)
vert :: (a, a)
vert = (a
vt, a
vb)
data BorderMap a = BorderMap
{ forall a. BorderMap a -> Edges Int
_coordinates :: Edges Int
, forall a. BorderMap a -> Edges (IMap a)
_values :: Edges (IMap a)
} deriving (BorderMap a -> BorderMap a -> Bool
(BorderMap a -> BorderMap a -> Bool)
-> (BorderMap a -> BorderMap a -> Bool) -> Eq (BorderMap a)
forall a. Eq a => BorderMap a -> BorderMap a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => BorderMap a -> BorderMap a -> Bool
== :: BorderMap a -> BorderMap a -> Bool
$c/= :: forall a. Eq a => BorderMap a -> BorderMap a -> Bool
/= :: BorderMap a -> BorderMap a -> Bool
Eq, Eq (BorderMap a)
Eq (BorderMap a) =>
(BorderMap a -> BorderMap a -> Ordering)
-> (BorderMap a -> BorderMap a -> Bool)
-> (BorderMap a -> BorderMap a -> Bool)
-> (BorderMap a -> BorderMap a -> Bool)
-> (BorderMap a -> BorderMap a -> Bool)
-> (BorderMap a -> BorderMap a -> BorderMap a)
-> (BorderMap a -> BorderMap a -> BorderMap a)
-> Ord (BorderMap a)
BorderMap a -> BorderMap a -> Bool
BorderMap a -> BorderMap a -> Ordering
BorderMap a -> BorderMap a -> BorderMap a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (BorderMap a)
forall a. Ord a => BorderMap a -> BorderMap a -> Bool
forall a. Ord a => BorderMap a -> BorderMap a -> Ordering
forall a. Ord a => BorderMap a -> BorderMap a -> BorderMap a
$ccompare :: forall a. Ord a => BorderMap a -> BorderMap a -> Ordering
compare :: BorderMap a -> BorderMap a -> Ordering
$c< :: forall a. Ord a => BorderMap a -> BorderMap a -> Bool
< :: BorderMap a -> BorderMap a -> Bool
$c<= :: forall a. Ord a => BorderMap a -> BorderMap a -> Bool
<= :: BorderMap a -> BorderMap a -> Bool
$c> :: forall a. Ord a => BorderMap a -> BorderMap a -> Bool
> :: BorderMap a -> BorderMap a -> Bool
$c>= :: forall a. Ord a => BorderMap a -> BorderMap a -> Bool
>= :: BorderMap a -> BorderMap a -> Bool
$cmax :: forall a. Ord a => BorderMap a -> BorderMap a -> BorderMap a
max :: BorderMap a -> BorderMap a -> BorderMap a
$cmin :: forall a. Ord a => BorderMap a -> BorderMap a -> BorderMap a
min :: BorderMap a -> BorderMap a -> BorderMap a
Ord, Int -> BorderMap a -> ShowS
[BorderMap a] -> ShowS
BorderMap a -> String
(Int -> BorderMap a -> ShowS)
-> (BorderMap a -> String)
-> ([BorderMap a] -> ShowS)
-> Show (BorderMap a)
forall a. Show a => Int -> BorderMap a -> ShowS
forall a. Show a => [BorderMap a] -> ShowS
forall a. Show a => BorderMap a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> BorderMap a -> ShowS
showsPrec :: Int -> BorderMap a -> ShowS
$cshow :: forall a. Show a => BorderMap a -> String
show :: BorderMap a -> String
$cshowList :: forall a. Show a => [BorderMap a] -> ShowS
showList :: [BorderMap a] -> ShowS
Show, (forall a b. (a -> b) -> BorderMap a -> BorderMap b)
-> (forall a b. a -> BorderMap b -> BorderMap a)
-> Functor BorderMap
forall a b. a -> BorderMap b -> BorderMap a
forall a b. (a -> b) -> BorderMap a -> BorderMap b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> BorderMap a -> BorderMap b
fmap :: forall a b. (a -> b) -> BorderMap a -> BorderMap b
$c<$ :: forall a b. a -> BorderMap b -> BorderMap a
<$ :: forall a b. a -> BorderMap b -> BorderMap a
Functor, ReadPrec [BorderMap a]
ReadPrec (BorderMap a)
Int -> ReadS (BorderMap a)
ReadS [BorderMap a]
(Int -> ReadS (BorderMap a))
-> ReadS [BorderMap a]
-> ReadPrec (BorderMap a)
-> ReadPrec [BorderMap a]
-> Read (BorderMap a)
forall a. Read a => ReadPrec [BorderMap a]
forall a. Read a => ReadPrec (BorderMap a)
forall a. Read a => Int -> ReadS (BorderMap a)
forall a. Read a => ReadS [BorderMap a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Read a => Int -> ReadS (BorderMap a)
readsPrec :: Int -> ReadS (BorderMap a)
$creadList :: forall a. Read a => ReadS [BorderMap a]
readList :: ReadS [BorderMap a]
$creadPrec :: forall a. Read a => ReadPrec (BorderMap a)
readPrec :: ReadPrec (BorderMap a)
$creadListPrec :: forall a. Read a => ReadPrec [BorderMap a]
readListPrec :: ReadPrec [BorderMap a]
Read, (forall x. BorderMap a -> Rep (BorderMap a) x)
-> (forall x. Rep (BorderMap a) x -> BorderMap a)
-> Generic (BorderMap a)
forall x. Rep (BorderMap a) x -> BorderMap a
forall x. BorderMap a -> Rep (BorderMap a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (BorderMap a) x -> BorderMap a
forall a x. BorderMap a -> Rep (BorderMap a) x
$cfrom :: forall a x. BorderMap a -> Rep (BorderMap a) x
from :: forall x. BorderMap a -> Rep (BorderMap a) x
$cto :: forall a x. Rep (BorderMap a) x -> BorderMap a
to :: forall x. Rep (BorderMap a) x -> BorderMap a
Generic, BorderMap a -> ()
(BorderMap a -> ()) -> NFData (BorderMap a)
forall a. NFData a => BorderMap a -> ()
forall a. (a -> ()) -> NFData a
$crnf :: forall a. NFData a => BorderMap a -> ()
rnf :: BorderMap a -> ()
NFData)
emptyCoordinates :: Edges Int -> BorderMap a
emptyCoordinates :: forall a. Edges Int -> BorderMap a
emptyCoordinates Edges Int
cs = BorderMap { _coordinates :: Edges Int
_coordinates = Edges Int
cs, _values :: Edges (IMap a)
_values = IMap a -> Edges (IMap a)
forall a. a -> Edges a
forall (f :: * -> *) a. Applicative f => a -> f a
pure IMap a
forall a. IMap a
IM.empty }
clear :: BorderMap a -> BorderMap b
clear :: forall a b. BorderMap a -> BorderMap b
clear = Edges Int -> BorderMap b
forall a. Edges Int -> BorderMap a
emptyCoordinates (Edges Int -> BorderMap b)
-> (BorderMap a -> Edges Int) -> BorderMap a -> BorderMap b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BorderMap a -> Edges Int
forall a. BorderMap a -> Edges Int
coordinates
empty :: BorderMap a
empty :: forall a. BorderMap a
empty = Edges Int -> BorderMap a
forall a. Edges Int -> BorderMap a
emptyCoordinates Edges
{ eTop :: Int
eTop = Int
0
, eBottom :: Int
eBottom = -Int
1
, eLeft :: Int
eLeft = Int
0
, eRight :: Int
eRight = -Int
1
}
singleton :: Location -> a -> BorderMap a
singleton :: forall a. Location -> a -> BorderMap a
singleton Location
l a
v = Location -> BorderMap a -> BorderMap a
forall a. Location -> BorderMap a -> BorderMap a
translate Location
l (BorderMap a -> BorderMap a)
-> (Edges Int -> BorderMap a) -> Edges Int -> BorderMap a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Location -> a -> BorderMap a -> BorderMap a
forall a. Location -> a -> BorderMap a -> BorderMap a
insert Location
origin a
v (BorderMap a -> BorderMap a)
-> (Edges Int -> BorderMap a) -> Edges Int -> BorderMap a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Edges Int -> BorderMap a
forall a. Edges Int -> BorderMap a
emptyCoordinates (Edges Int -> BorderMap a) -> Edges Int -> BorderMap a
forall a b. (a -> b) -> a -> b
$ Int -> Edges Int
forall a. a -> Edges a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
0
{-# INLINE coordinates #-}
coordinates :: BorderMap a -> Edges Int
coordinates :: forall a. BorderMap a -> Edges Int
coordinates = BorderMap a -> Edges Int
forall a. BorderMap a -> Edges Int
_coordinates
bounds :: BorderMap a -> Edges (Int, Int)
bounds :: forall a. BorderMap a -> Edges (Int, Int)
bounds = Edges Int -> Edges (Int, Int)
forall a. Edges a -> Edges (a, a)
neighbors (Edges Int -> Edges (Int, Int))
-> (BorderMap a -> Edges Int) -> BorderMap a -> Edges (Int, Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BorderMap a -> Edges Int
forall a. BorderMap a -> Edges Int
coordinates
{-# INLINE values #-}
values :: BorderMap a -> Edges (IMap a)
values :: forall a. BorderMap a -> Edges (IMap a)
values = BorderMap a -> Edges (IMap a)
forall a. BorderMap a -> Edges (IMap a)
_values
insertH :: Location -> Run a -> BorderMap a -> BorderMap a
insertH :: forall a. Location -> Run a -> BorderMap a -> BorderMap a
insertH = Edges (Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a)
-> Location -> Run a -> BorderMap a -> BorderMap a
forall a.
Edges (Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a)
-> Location -> Run a -> BorderMap a -> BorderMap a
insertDirAgnostic ((Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a)
-> (Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a)
-> (Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a)
-> (Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a)
-> Edges
(Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a)
forall a. a -> a -> a -> a -> Edges a
Edges Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
forall a.
Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
insertPar Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
forall a.
Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
insertPar Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
forall a.
Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
insertPerp Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
forall a.
Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
insertPerp) (Location -> Run a -> BorderMap a -> BorderMap a)
-> (Location -> Location)
-> Location
-> Run a
-> BorderMap a
-> BorderMap a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Location -> Location
swapLoc
where
swapLoc :: Location -> Location
swapLoc (Location (Int
col, Int
row)) = (Int, Int) -> Location
Location (Int
row, Int
col)
insertV :: Location -> Run a -> BorderMap a -> BorderMap a
insertV :: forall a. Location -> Run a -> BorderMap a -> BorderMap a
insertV = Edges (Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a)
-> Location -> Run a -> BorderMap a -> BorderMap a
forall a.
Edges (Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a)
-> Location -> Run a -> BorderMap a -> BorderMap a
insertDirAgnostic ((Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a)
-> (Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a)
-> (Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a)
-> (Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a)
-> Edges
(Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a)
forall a. a -> a -> a -> a -> Edges a
Edges Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
forall a.
Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
insertPerp Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
forall a.
Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
insertPerp Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
forall a.
Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
insertPar Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
forall a.
Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
insertPar)
insertDirAgnostic
:: Edges (Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a)
-> Location -> Run a -> BorderMap a -> BorderMap a
insertDirAgnostic :: forall a.
Edges (Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a)
-> Location -> Run a -> BorderMap a -> BorderMap a
insertDirAgnostic Edges (Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a)
insertions Location
l Run a
r BorderMap a
m =
BorderMap a
m { _values = insertions <*> pure l <*> pure r <*> coordinates m <*> bounds m <*> _values m }
insertPar, insertPerp :: Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
insertPar :: forall a.
Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
insertPar (Location (Int
kPar, Int
kPerp)) Run a
r Int
herePar (Int
loPerp, Int
hiPerp)
| Int
kPar Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
herePar Bool -> Bool -> Bool
&& Int
loPerp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
kPerp Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Run a -> Int
forall a. Run a -> Int
IM.len Run a
r Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 Bool -> Bool -> Bool
&& Int
kPerp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
hiPerp
= Int -> Run a -> IMap a -> IMap a
forall a. Int -> Run a -> IMap a -> IMap a
IM.insert Int
beg Run a
r { IM.len = end - beg + 1 }
| Bool
otherwise = IMap a -> IMap a
forall a. a -> a
id
where
beg :: Int
beg = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
kPerp Int
loPerp
end :: Int
end = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (Int
kPerp Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Run a -> Int
forall a. Run a -> Int
IM.len Run a
r Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int
hiPerp
insertPerp :: forall a.
Location -> Run a -> Int -> (Int, Int) -> IMap a -> IMap a
insertPerp (Location (Int
kPar, Int
kPerp)) Run a
r Int
herePerp (Int
loPar, Int
hiPar)
| Int
loPar Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
kPar Bool -> Bool -> Bool
&& Int
kPar Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
hiPar Bool -> Bool -> Bool
&& Int
kPerp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
herePerp Bool -> Bool -> Bool
&& Int
herePerp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
kPerp Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Run a -> Int
forall a. Run a -> Int
IM.len Run a
r Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
= Int -> Run a -> IMap a -> IMap a
forall a. Int -> Run a -> IMap a -> IMap a
IM.insert Int
kPar Run a
r { IM.len = 1 }
| Bool
otherwise = IMap a -> IMap a
forall a. a -> a
id
insert :: Location -> a -> BorderMap a -> BorderMap a
insert :: forall a. Location -> a -> BorderMap a -> BorderMap a
insert Location
l = Location -> Run a -> BorderMap a -> BorderMap a
forall a. Location -> Run a -> BorderMap a -> BorderMap a
insertV Location
l (Run a -> BorderMap a -> BorderMap a)
-> (a -> Run a) -> a -> BorderMap a -> BorderMap a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a -> Run a
forall a. Int -> a -> Run a
Run Int
1
lookupRow :: Int -> BorderMap a -> IMap a
lookupRow :: forall a. Int -> BorderMap a -> IMap a
lookupRow Int
row BorderMap a
m
| Int
row Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Edges Int -> Int
forall a. Edges a -> a
eTop (BorderMap a -> Edges Int
forall a. BorderMap a -> Edges Int
coordinates BorderMap a
m) = Edges (IMap a) -> IMap a
forall a. Edges a -> a
eTop (BorderMap a -> Edges (IMap a)
forall a. BorderMap a -> Edges (IMap a)
_values BorderMap a
m)
| Int
row Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Edges Int -> Int
forall a. Edges a -> a
eBottom (BorderMap a -> Edges Int
forall a. BorderMap a -> Edges Int
coordinates BorderMap a
m) = Edges (IMap a) -> IMap a
forall a. Edges a -> a
eBottom (BorderMap a -> Edges (IMap a)
forall a. BorderMap a -> Edges (IMap a)
_values BorderMap a
m)
| Bool
otherwise = [(Int, Run a)] -> IMap a
forall a. [(Int, Run a)] -> IMap a
IM.fromList
([(Int, Run a)] -> IMap a) -> [(Int, Run a)] -> IMap a
forall a b. (a -> b) -> a -> b
$ [(Edges Int -> Int
forall a. Edges a -> a
eLeft (BorderMap a -> Edges Int
forall a. BorderMap a -> Edges Int
coordinates BorderMap a
m), Int -> a -> Run a
forall a. Int -> a -> Run a
Run Int
1 a
a) | Just a
a <- [Int -> IMap a -> Maybe a
forall a. Int -> IMap a -> Maybe a
IM.lookup Int
row (Edges (IMap a) -> IMap a
forall a. Edges a -> a
eLeft (BorderMap a -> Edges (IMap a)
forall a. BorderMap a -> Edges (IMap a)
_values BorderMap a
m))]]
[(Int, Run a)] -> [(Int, Run a)] -> [(Int, Run a)]
forall a. [a] -> [a] -> [a]
++ [(Edges Int -> Int
forall a. Edges a -> a
eRight (BorderMap a -> Edges Int
forall a. BorderMap a -> Edges Int
coordinates BorderMap a
m), Int -> a -> Run a
forall a. Int -> a -> Run a
Run Int
1 a
a) | Just a
a <- [Int -> IMap a -> Maybe a
forall a. Int -> IMap a -> Maybe a
IM.lookup Int
row (Edges (IMap a) -> IMap a
forall a. Edges a -> a
eRight (BorderMap a -> Edges (IMap a)
forall a. BorderMap a -> Edges (IMap a)
_values BorderMap a
m))]]
lookupCol :: Int -> BorderMap a -> IMap a
lookupCol :: forall a. Int -> BorderMap a -> IMap a
lookupCol Int
col BorderMap a
m
| Int
col Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Edges Int -> Int
forall a. Edges a -> a
eLeft (BorderMap a -> Edges Int
forall a. BorderMap a -> Edges Int
coordinates BorderMap a
m) = Edges (IMap a) -> IMap a
forall a. Edges a -> a
eLeft (BorderMap a -> Edges (IMap a)
forall a. BorderMap a -> Edges (IMap a)
_values BorderMap a
m)
| Int
col Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Edges Int -> Int
forall a. Edges a -> a
eRight (BorderMap a -> Edges Int
forall a. BorderMap a -> Edges Int
coordinates BorderMap a
m) = Edges (IMap a) -> IMap a
forall a. Edges a -> a
eRight (BorderMap a -> Edges (IMap a)
forall a. BorderMap a -> Edges (IMap a)
_values BorderMap a
m)
| Bool
otherwise = [(Int, Run a)] -> IMap a
forall a. [(Int, Run a)] -> IMap a
IM.fromList
([(Int, Run a)] -> IMap a) -> [(Int, Run a)] -> IMap a
forall a b. (a -> b) -> a -> b
$ [(Edges Int -> Int
forall a. Edges a -> a
eTop (BorderMap a -> Edges Int
forall a. BorderMap a -> Edges Int
coordinates BorderMap a
m), Int -> a -> Run a
forall a. Int -> a -> Run a
Run Int
1 a
a) | Just a
a <- [Int -> IMap a -> Maybe a
forall a. Int -> IMap a -> Maybe a
IM.lookup Int
col (Edges (IMap a) -> IMap a
forall a. Edges a -> a
eTop (BorderMap a -> Edges (IMap a)
forall a. BorderMap a -> Edges (IMap a)
_values BorderMap a
m))]]
[(Int, Run a)] -> [(Int, Run a)] -> [(Int, Run a)]
forall a. [a] -> [a] -> [a]
++ [(Edges Int -> Int
forall a. Edges a -> a
eBottom (BorderMap a -> Edges Int
forall a. BorderMap a -> Edges Int
coordinates BorderMap a
m), Int -> a -> Run a
forall a. Int -> a -> Run a
Run Int
1 a
a) | Just a
a <- [Int -> IMap a -> Maybe a
forall a. Int -> IMap a -> Maybe a
IM.lookup Int
col (Edges (IMap a) -> IMap a
forall a. Edges a -> a
eBottom (BorderMap a -> Edges (IMap a)
forall a. BorderMap a -> Edges (IMap a)
_values BorderMap a
m))]]
lookupH :: Location -> Run ignored -> BorderMap a -> IMap a
lookupH :: forall ignored a. Location -> Run ignored -> BorderMap a -> IMap a
lookupH (Location (Int
col, Int
row)) Run ignored
r = Int -> Run ignored -> IMap a -> IMap a
forall ignored a. Int -> Run ignored -> IMap a -> IMap a
IM.restrict Int
col Run ignored
r (IMap a -> IMap a)
-> (BorderMap a -> IMap a) -> BorderMap a -> IMap a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> BorderMap a -> IMap a
forall a. Int -> BorderMap a -> IMap a
lookupRow Int
row
lookupV :: Location -> Run ignored -> BorderMap a -> IMap a
lookupV :: forall ignored a. Location -> Run ignored -> BorderMap a -> IMap a
lookupV (Location (Int
col, Int
row)) Run ignored
r = Int -> Run ignored -> IMap a -> IMap a
forall ignored a. Int -> Run ignored -> IMap a -> IMap a
IM.restrict Int
row Run ignored
r (IMap a -> IMap a)
-> (BorderMap a -> IMap a) -> BorderMap a -> IMap a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> BorderMap a -> IMap a
forall a. Int -> BorderMap a -> IMap a
lookupCol Int
col
lookup :: Location -> BorderMap a -> Maybe a
lookup :: forall a. Location -> BorderMap a -> Maybe a
lookup (Location (Int
col, Int
row)) = Int -> IMap a -> Maybe a
forall a. Int -> IMap a -> Maybe a
IM.lookup Int
row (IMap a -> Maybe a)
-> (BorderMap a -> IMap a) -> BorderMap a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> BorderMap a -> IMap a
forall a. Int -> BorderMap a -> IMap a
lookupCol Int
col
setCoordinates :: Edges Int -> BorderMap a -> BorderMap a
setCoordinates :: forall a. Edges Int -> BorderMap a -> BorderMap a
setCoordinates Edges Int
coordinates' BorderMap a
m = BorderMap
{ _values :: Edges (IMap a)
_values = Edges (IMap a)
values'
, _coordinates :: Edges Int
_coordinates = Edges Int
coordinates'
}
where
bounds' :: Edges (Int, Int)
bounds' = Edges Int -> Edges (Int, Int)
forall a. Edges a -> Edges (a, a)
neighbors Edges Int
coordinates'
values' :: Edges (IMap a)
values' = Int
-> Int
-> (Int, Int)
-> IMap a
-> (Int -> BorderMap a -> IMap a)
-> IMap a
forall {t} {a}.
Eq t =>
t
-> t
-> (Int, Int)
-> IMap a
-> (t -> BorderMap a -> IMap a)
-> IMap a
gc
(Int
-> Int
-> (Int, Int)
-> IMap a
-> (Int -> BorderMap a -> IMap a)
-> IMap a)
-> Edges Int
-> Edges
(Int
-> (Int, Int)
-> IMap a
-> (Int -> BorderMap a -> IMap a)
-> IMap a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BorderMap a -> Edges Int
forall a. BorderMap a -> Edges Int
_coordinates BorderMap a
m
Edges
(Int
-> (Int, Int)
-> IMap a
-> (Int -> BorderMap a -> IMap a)
-> IMap a)
-> Edges Int
-> Edges
((Int, Int) -> IMap a -> (Int -> BorderMap a -> IMap a) -> IMap a)
forall a b. Edges (a -> b) -> Edges a -> Edges b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Edges Int
coordinates'
Edges
((Int, Int) -> IMap a -> (Int -> BorderMap a -> IMap a) -> IMap a)
-> Edges (Int, Int)
-> Edges (IMap a -> (Int -> BorderMap a -> IMap a) -> IMap a)
forall a b. Edges (a -> b) -> Edges a -> Edges b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Edges (Int, Int)
bounds'
Edges (IMap a -> (Int -> BorderMap a -> IMap a) -> IMap a)
-> Edges (IMap a)
-> Edges ((Int -> BorderMap a -> IMap a) -> IMap a)
forall a b. Edges (a -> b) -> Edges a -> Edges b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> BorderMap a -> Edges (IMap a)
forall a. BorderMap a -> Edges (IMap a)
_values BorderMap a
m
Edges ((Int -> BorderMap a -> IMap a) -> IMap a)
-> Edges (Int -> BorderMap a -> IMap a) -> Edges (IMap a)
forall a b. Edges (a -> b) -> Edges a -> Edges b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Edges { eTop :: Int -> BorderMap a -> IMap a
eTop = Int -> BorderMap a -> IMap a
forall a. Int -> BorderMap a -> IMap a
lookupRow, eBottom :: Int -> BorderMap a -> IMap a
eBottom = Int -> BorderMap a -> IMap a
forall a. Int -> BorderMap a -> IMap a
lookupRow, eLeft :: Int -> BorderMap a -> IMap a
eLeft = Int -> BorderMap a -> IMap a
forall a. Int -> BorderMap a -> IMap a
lookupCol, eRight :: Int -> BorderMap a -> IMap a
eRight = Int -> BorderMap a -> IMap a
forall a. Int -> BorderMap a -> IMap a
lookupCol }
gc :: t
-> t
-> (Int, Int)
-> IMap a
-> (t -> BorderMap a -> IMap a)
-> IMap a
gc t
oldPar t
newPar (Int
loPerp, Int
hiPerp) IMap a
imPar t -> BorderMap a -> IMap a
lookupPerp
| t
oldPar t -> t -> Bool
forall a. Eq a => a -> a -> Bool
== t
newPar = Int -> Run () -> IMap a -> IMap a
forall ignored a. Int -> Run ignored -> IMap a -> IMap a
IM.restrict Int
loPerp (Int -> () -> Run ()
forall a. Int -> a -> Run a
Run (Int
hiPerpInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
loPerpInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) ()) IMap a
imPar
| Bool
otherwise = t -> BorderMap a -> IMap a
lookupPerp t
newPar BorderMap a
m
crop :: Edges Int -> BorderMap a -> BorderMap a
crop :: forall a. Edges Int -> BorderMap a -> BorderMap a
crop Edges Int
cs BorderMap a
m = Edges Int -> BorderMap a -> BorderMap a
forall a. Edges Int -> BorderMap a -> BorderMap a
setCoordinates (Edges (Int -> Int -> Int)
shrink Edges (Int -> Int -> Int) -> Edges Int -> Edges (Int -> Int)
forall a b. Edges (a -> b) -> Edges a -> Edges b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Edges Int
cs Edges (Int -> Int) -> Edges Int -> Edges Int
forall a b. Edges (a -> b) -> Edges a -> Edges b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> BorderMap a -> Edges Int
forall a. BorderMap a -> Edges Int
coordinates BorderMap a
m) BorderMap a
m where
shrink :: Edges (Int -> Int -> Int)
shrink = Edges
{ eTop :: Int -> Int -> Int
eTop = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max
, eBottom :: Int -> Int -> Int
eBottom = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min
, eLeft :: Int -> Int -> Int
eLeft = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max
, eRight :: Int -> Int -> Int
eRight = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min
}
expand :: Edges Int -> BorderMap a -> BorderMap a
expand :: forall a. Edges Int -> BorderMap a -> BorderMap a
expand Edges Int
cs BorderMap a
m = Edges Int -> BorderMap a -> BorderMap a
forall a. Edges Int -> BorderMap a -> BorderMap a
setCoordinates (Edges (Int -> Int -> Int)
grow Edges (Int -> Int -> Int) -> Edges Int -> Edges (Int -> Int)
forall a b. Edges (a -> b) -> Edges a -> Edges b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Edges Int
cs Edges (Int -> Int) -> Edges Int -> Edges Int
forall a b. Edges (a -> b) -> Edges a -> Edges b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> BorderMap a -> Edges Int
forall a. BorderMap a -> Edges Int
coordinates BorderMap a
m) BorderMap a
m where
grow :: Edges (Int -> Int -> Int)
grow = Edges
{ eTop :: Int -> Int -> Int
eTop = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min
, eBottom :: Int -> Int -> Int
eBottom = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max
, eLeft :: Int -> Int -> Int
eLeft = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min
, eRight :: Int -> Int -> Int
eRight = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max
}
translate :: Location -> BorderMap a -> BorderMap a
translate :: forall a. Location -> BorderMap a -> BorderMap a
translate (Location (Int
0, Int
0)) BorderMap a
m = BorderMap a
m
translate (Location (Int
c, Int
r)) BorderMap a
m = BorderMap
{ _coordinates :: Edges Int
_coordinates = (Int -> Int -> Int) -> Edges Int -> Edges Int -> Edges Int
forall a b c. (a -> b -> c) -> Edges a -> Edges b -> Edges c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 Int -> Int -> Int
forall a. Num a => a -> a -> a
(+) Edges Int
cOffsets (BorderMap a -> Edges Int
forall a. BorderMap a -> Edges Int
_coordinates BorderMap a
m)
, _values :: Edges (IMap a)
_values = (Int -> IMap a -> IMap a)
-> Edges Int -> Edges (IMap a) -> Edges (IMap a)
forall a b c. (a -> b -> c) -> Edges a -> Edges b -> Edges c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 Int -> IMap a -> IMap a
forall a. Int -> IMap a -> IMap a
IM.addToKeys Edges Int
vOffsets (BorderMap a -> Edges (IMap a)
forall a. BorderMap a -> Edges (IMap a)
_values BorderMap a
m)
}
where
cOffsets :: Edges Int
cOffsets = Edges { eTop :: Int
eTop = Int
r, eBottom :: Int
eBottom = Int
r, eLeft :: Int
eLeft = Int
c, eRight :: Int
eRight = Int
c }
vOffsets :: Edges Int
vOffsets = Edges { eTop :: Int
eTop = Int
c, eBottom :: Int
eBottom = Int
c, eLeft :: Int
eLeft = Int
r, eRight :: Int
eRight = Int
r }
unsafeUnion :: BorderMap a -> BorderMap a -> BorderMap a
unsafeUnion :: forall a. BorderMap a -> BorderMap a -> BorderMap a
unsafeUnion BorderMap a
m BorderMap a
m' = BorderMap a
m { _values = liftA2 IM.unsafeUnion (_values m) (_values m') }