module Recalc.Engine.DependencyMap (DependencyMap (..), Slow) where
import Control.Arrow (second)
import Data.Functor.Classes (Show1 (..))
import Data.Map qualified as Map
import Data.Map.Strict (Map)
import Data.Maybe (fromMaybe)
import Recalc.Engine.Core (CellAddr, CellRange, SheetId)
contains :: CellRange -> CellAddr -> Bool
contains :: CellRange -> CellAddr -> Bool
contains ((Int
x0, Int
y0), (Int
x1, Int
y1)) (Int
x, Int
y) =
[Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and
[ Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
x0 Int
x1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
x
, Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
x0 Int
x1
, Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
y0 Int
y1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
y
, Int
y Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
y0 Int
y1
]
class Show1 t => DependencyMap t where
query :: t a -> (SheetId, CellAddr) -> [(SheetId, a)]
delete :: Eq a => SheetId -> CellRange -> a -> t a -> t a
deleteSheetId :: SheetId -> t a -> t a
insert :: SheetId -> CellRange -> a -> t a -> t a
empty :: t a
newtype Slow a = Slow (Map SheetId [(CellRange, (SheetId, a))])
deriving (Int -> Slow a -> ShowS
[Slow a] -> ShowS
Slow a -> String
(Int -> Slow a -> ShowS)
-> (Slow a -> String) -> ([Slow a] -> ShowS) -> Show (Slow a)
forall a. Show a => Int -> Slow a -> ShowS
forall a. Show a => [Slow a] -> ShowS
forall a. Show a => Slow a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Slow a -> ShowS
showsPrec :: Int -> Slow a -> ShowS
$cshow :: forall a. Show a => Slow a -> String
show :: Slow a -> String
$cshowList :: forall a. Show a => [Slow a] -> ShowS
showList :: [Slow a] -> ShowS
Show)
instance Show1 Slow where
liftShowsPrec :: forall a.
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> Slow a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
_ Int
x (Slow Map SheetId [(CellRange, (SheetId, a))]
m) =
String -> ShowS
showString (String -> ShowS)
-> ([(SheetId, [(CellRange, (SheetId, String))])] -> String)
-> [(SheetId, [(CellRange, (SheetId, String))])]
-> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(SheetId, [(CellRange, (SheetId, String))])] -> String
forall a. Show a => a -> String
show
([(SheetId, [(CellRange, (SheetId, String))])] -> ShowS)
-> [(SheetId, [(CellRange, (SheetId, String))])] -> ShowS
forall a b. (a -> b) -> a -> b
$ ([(CellRange, (SheetId, a))] -> [(CellRange, (SheetId, String))])
-> (SheetId, [(CellRange, (SheetId, a))])
-> (SheetId, [(CellRange, (SheetId, String))])
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second (((CellRange, (SheetId, a)) -> (CellRange, (SheetId, String)))
-> [(CellRange, (SheetId, a))] -> [(CellRange, (SheetId, String))]
forall a b. (a -> b) -> [a] -> [b]
map (((SheetId, a) -> (SheetId, String))
-> (CellRange, (SheetId, a)) -> (CellRange, (SheetId, String))
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second ((a -> String) -> (SheetId, a) -> (SheetId, String)
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second ((ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ String
"") (ShowS -> String) -> (a -> ShowS) -> a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a -> ShowS
sp Int
x)))) ((SheetId, [(CellRange, (SheetId, a))])
-> (SheetId, [(CellRange, (SheetId, String))]))
-> [(SheetId, [(CellRange, (SheetId, a))])]
-> [(SheetId, [(CellRange, (SheetId, String))])]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map SheetId [(CellRange, (SheetId, a))]
-> [(SheetId, [(CellRange, (SheetId, a))])]
forall k a. Map k a -> [(k, a)]
Map.assocs Map SheetId [(CellRange, (SheetId, a))]
m
instance DependencyMap Slow where
query :: forall a. Slow a -> (SheetId, CellAddr) -> [(SheetId, a)]
query (Slow Map SheetId [(CellRange, (SheetId, a))]
m) (SheetId
sid, CellAddr
ca) =
[(SheetId, a)
a | (CellRange
x, (SheetId, a)
a) <- [(CellRange, (SheetId, a))]
-> Maybe [(CellRange, (SheetId, a))] -> [(CellRange, (SheetId, a))]
forall a. a -> Maybe a -> a
fromMaybe [] (SheetId
-> Map SheetId [(CellRange, (SheetId, a))]
-> Maybe [(CellRange, (SheetId, a))]
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup SheetId
sid Map SheetId [(CellRange, (SheetId, a))]
m), CellRange
x CellRange -> CellAddr -> Bool
`contains` CellAddr
ca]
delete :: forall a. Eq a => SheetId -> CellRange -> a -> Slow a -> Slow a
delete SheetId
sid CellRange
x a
a (Slow Map SheetId [(CellRange, (SheetId, a))]
m) =
Map SheetId [(CellRange, (SheetId, a))] -> Slow a
forall a. Map SheetId [(CellRange, (SheetId, a))] -> Slow a
Slow
(Map SheetId [(CellRange, (SheetId, a))] -> Slow a)
-> Map SheetId [(CellRange, (SheetId, a))] -> Slow a
forall a b. (a -> b) -> a -> b
$ ([(CellRange, (SheetId, a))] -> Maybe [(CellRange, (SheetId, a))])
-> SheetId
-> Map SheetId [(CellRange, (SheetId, a))]
-> Map SheetId [(CellRange, (SheetId, a))]
forall k a. Ord k => (a -> Maybe a) -> k -> Map k a -> Map k a
Map.update
(\[(CellRange, (SheetId, a))]
xs -> case ((CellRange, (SheetId, a)) -> Bool)
-> [(CellRange, (SheetId, a))] -> [(CellRange, (SheetId, a))]
forall a. (a -> Bool) -> [a] -> [a]
filter ((CellRange, (SheetId, a)) -> (CellRange, (SheetId, a)) -> Bool
forall a. Eq a => a -> a -> Bool
/= (CellRange
x, (SheetId
sid, a
a))) [(CellRange, (SheetId, a))]
xs of [] -> Maybe [(CellRange, (SheetId, a))]
forall a. Maybe a
Nothing; [(CellRange, (SheetId, a))]
xs' -> [(CellRange, (SheetId, a))] -> Maybe [(CellRange, (SheetId, a))]
forall a. a -> Maybe a
Just [(CellRange, (SheetId, a))]
xs')
SheetId
sid
Map SheetId [(CellRange, (SheetId, a))]
m
deleteSheetId :: forall a. SheetId -> Slow a -> Slow a
deleteSheetId SheetId
sid (Slow Map SheetId [(CellRange, (SheetId, a))]
m) = Map SheetId [(CellRange, (SheetId, a))] -> Slow a
forall a. Map SheetId [(CellRange, (SheetId, a))] -> Slow a
Slow (SheetId
-> Map SheetId [(CellRange, (SheetId, a))]
-> Map SheetId [(CellRange, (SheetId, a))]
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete SheetId
sid Map SheetId [(CellRange, (SheetId, a))]
m)
insert :: forall a. SheetId -> CellRange -> a -> Slow a -> Slow a
insert SheetId
sid CellRange
x a
a (Slow Map SheetId [(CellRange, (SheetId, a))]
m) = Map SheetId [(CellRange, (SheetId, a))] -> Slow a
forall a. Map SheetId [(CellRange, (SheetId, a))] -> Slow a
Slow (Map SheetId [(CellRange, (SheetId, a))] -> Slow a)
-> Map SheetId [(CellRange, (SheetId, a))] -> Slow a
forall a b. (a -> b) -> a -> b
$ (Maybe [(CellRange, (SheetId, a))]
-> Maybe [(CellRange, (SheetId, a))])
-> SheetId
-> Map SheetId [(CellRange, (SheetId, a))]
-> Map SheetId [(CellRange, (SheetId, a))]
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
Map.alter ([(CellRange, (SheetId, a))] -> Maybe [(CellRange, (SheetId, a))]
forall a. a -> Maybe a
Just ([(CellRange, (SheetId, a))] -> Maybe [(CellRange, (SheetId, a))])
-> (Maybe [(CellRange, (SheetId, a))]
-> [(CellRange, (SheetId, a))])
-> Maybe [(CellRange, (SheetId, a))]
-> Maybe [(CellRange, (SheetId, a))]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((CellRange
x, (SheetId
sid, a
a)) :) ([(CellRange, (SheetId, a))] -> [(CellRange, (SheetId, a))])
-> (Maybe [(CellRange, (SheetId, a))]
-> [(CellRange, (SheetId, a))])
-> Maybe [(CellRange, (SheetId, a))]
-> [(CellRange, (SheetId, a))]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(CellRange, (SheetId, a))]
-> Maybe [(CellRange, (SheetId, a))] -> [(CellRange, (SheetId, a))]
forall a. a -> Maybe a -> a
fromMaybe []) SheetId
sid Map SheetId [(CellRange, (SheetId, a))]
m
empty :: forall a. Slow a
empty = Map SheetId [(CellRange, (SheetId, a))] -> Slow a
forall a. Map SheetId [(CellRange, (SheetId, a))] -> Slow a
Slow Map SheetId [(CellRange, (SheetId, a))]
forall a. Monoid a => a
mempty