module Tile.Tiling
(
Tiling (..),
BlockPartitioning (..),
Bisection (..),
Split (..),
Relation (..),
TileNode (..),
)
where
import Tile.Affine
import Tile.Tile
class Tiling t where
childNodes :: t -> Tile -> [TileNode]
data Split = Split
{
Split -> Int
dim :: Int,
Split -> Int
index :: Int
}
deriving (Int -> Split -> ShowS
[Split] -> ShowS
Split -> String
(Int -> Split -> ShowS)
-> (Split -> String) -> ([Split] -> ShowS) -> Show Split
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Split -> ShowS
showsPrec :: Int -> Split -> ShowS
$cshow :: Split -> String
show :: Split -> String
$cshowList :: [Split] -> ShowS
showList :: [Split] -> ShowS
Show, Split -> Split -> Bool
(Split -> Split -> Bool) -> (Split -> Split -> Bool) -> Eq Split
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Split -> Split -> Bool
== :: Split -> Split -> Bool
$c/= :: Split -> Split -> Bool
/= :: Split -> Split -> Bool
Eq, Eq Split
Eq Split =>
(Split -> Split -> Ordering)
-> (Split -> Split -> Bool)
-> (Split -> Split -> Bool)
-> (Split -> Split -> Bool)
-> (Split -> Split -> Bool)
-> (Split -> Split -> Split)
-> (Split -> Split -> Split)
-> Ord Split
Split -> Split -> Bool
Split -> Split -> Ordering
Split -> Split -> Split
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
$ccompare :: Split -> Split -> Ordering
compare :: Split -> Split -> Ordering
$c< :: Split -> Split -> Bool
< :: Split -> Split -> Bool
$c<= :: Split -> Split -> Bool
<= :: Split -> Split -> Bool
$c> :: Split -> Split -> Bool
> :: Split -> Split -> Bool
$c>= :: Split -> Split -> Bool
>= :: Split -> Split -> Bool
$cmax :: Split -> Split -> Split
max :: Split -> Split -> Split
$cmin :: Split -> Split -> Split
min :: Split -> Split -> Split
Ord)
data Relation
=
Root
|
Anchor Split
|
Sibling Split
deriving (Int -> Relation -> ShowS
[Relation] -> ShowS
Relation -> String
(Int -> Relation -> ShowS)
-> (Relation -> String) -> ([Relation] -> ShowS) -> Show Relation
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Relation -> ShowS
showsPrec :: Int -> Relation -> ShowS
$cshow :: Relation -> String
show :: Relation -> String
$cshowList :: [Relation] -> ShowS
showList :: [Relation] -> ShowS
Show, Relation -> Relation -> Bool
(Relation -> Relation -> Bool)
-> (Relation -> Relation -> Bool) -> Eq Relation
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Relation -> Relation -> Bool
== :: Relation -> Relation -> Bool
$c/= :: Relation -> Relation -> Bool
/= :: Relation -> Relation -> Bool
Eq, Eq Relation
Eq Relation =>
(Relation -> Relation -> Ordering)
-> (Relation -> Relation -> Bool)
-> (Relation -> Relation -> Bool)
-> (Relation -> Relation -> Bool)
-> (Relation -> Relation -> Bool)
-> (Relation -> Relation -> Relation)
-> (Relation -> Relation -> Relation)
-> Ord Relation
Relation -> Relation -> Bool
Relation -> Relation -> Ordering
Relation -> Relation -> Relation
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
$ccompare :: Relation -> Relation -> Ordering
compare :: Relation -> Relation -> Ordering
$c< :: Relation -> Relation -> Bool
< :: Relation -> Relation -> Bool
$c<= :: Relation -> Relation -> Bool
<= :: Relation -> Relation -> Bool
$c> :: Relation -> Relation -> Bool
> :: Relation -> Relation -> Bool
$c>= :: Relation -> Relation -> Bool
>= :: Relation -> Relation -> Bool
$cmax :: Relation -> Relation -> Relation
max :: Relation -> Relation -> Relation
$cmin :: Relation -> Relation -> Relation
min :: Relation -> Relation -> Relation
Ord)
data TileNode = TileNode
{
TileNode -> Tile
tile :: Tile,
TileNode -> Relation
relation :: Relation
}
deriving (Int -> TileNode -> ShowS
[TileNode] -> ShowS
TileNode -> String
(Int -> TileNode -> ShowS)
-> (TileNode -> String) -> ([TileNode] -> ShowS) -> Show TileNode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TileNode -> ShowS
showsPrec :: Int -> TileNode -> ShowS
$cshow :: TileNode -> String
show :: TileNode -> String
$cshowList :: [TileNode] -> ShowS
showList :: [TileNode] -> ShowS
Show, TileNode -> TileNode -> Bool
(TileNode -> TileNode -> Bool)
-> (TileNode -> TileNode -> Bool) -> Eq TileNode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TileNode -> TileNode -> Bool
== :: TileNode -> TileNode -> Bool
$c/= :: TileNode -> TileNode -> Bool
/= :: TileNode -> TileNode -> Bool
Eq)
fixTileDim :: Tile -> Int -> Int -> Maybe Tile
fixTileDim :: Tile -> Int -> Int -> Maybe Tile
fixTileDim Tile
tile Int
dim Int
i = AffineRankSpace -> Tile
Tile (AffineRankSpace -> Tile) -> Maybe AffineRankSpace -> Maybe Tile
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AffineRankSpace -> Int -> Int -> Maybe AffineRankSpace
fixDim (Tile -> AffineRankSpace
space Tile
tile) Int
dim Int
i
selectTileDim :: Tile -> Int -> Int -> Int -> Maybe Tile
selectTileDim :: Tile -> Int -> Int -> Int -> Maybe Tile
selectTileDim Tile
tile Int
dim Int
begin Int
end =
AffineRankSpace -> Tile
Tile (AffineRankSpace -> Tile) -> Maybe AffineRankSpace -> Maybe Tile
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AffineRankSpace
-> Int -> Int -> Int -> Int -> Maybe AffineRankSpace
select (Tile -> AffineRankSpace
space Tile
tile) Int
dim Int
begin Int
end Int
1
data BlockPartitioning = BlockPartitioning
deriving (Int -> BlockPartitioning -> ShowS
[BlockPartitioning] -> ShowS
BlockPartitioning -> String
(Int -> BlockPartitioning -> ShowS)
-> (BlockPartitioning -> String)
-> ([BlockPartitioning] -> ShowS)
-> Show BlockPartitioning
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BlockPartitioning -> ShowS
showsPrec :: Int -> BlockPartitioning -> ShowS
$cshow :: BlockPartitioning -> String
show :: BlockPartitioning -> String
$cshowList :: [BlockPartitioning] -> ShowS
showList :: [BlockPartitioning] -> ShowS
Show, BlockPartitioning -> BlockPartitioning -> Bool
(BlockPartitioning -> BlockPartitioning -> Bool)
-> (BlockPartitioning -> BlockPartitioning -> Bool)
-> Eq BlockPartitioning
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BlockPartitioning -> BlockPartitioning -> Bool
== :: BlockPartitioning -> BlockPartitioning -> Bool
$c/= :: BlockPartitioning -> BlockPartitioning -> Bool
/= :: BlockPartitioning -> BlockPartitioning -> Bool
Eq)
instance Tiling BlockPartitioning where
childNodes :: BlockPartitioning -> Tile -> [TileNode]
childNodes BlockPartitioning
_ Tile
tile = Tile -> Int -> [TileNode]
go Tile
tile Int
0
where
go :: Tile -> Int -> [TileNode]
go Tile
t Int
d
| Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= [Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (AffineRankSpace -> [Int]
sizes (Tile -> AffineRankSpace
space Tile
t)) = []
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
1 = Tile -> Int -> [TileNode]
go Tile
t (Int
d Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
| Bool
otherwise =
let siblings :: [TileNode]
siblings =
[ Tile -> Relation -> TileNode
TileNode Tile
child (Split -> Relation
Sibling (Int -> Int -> Split
Split Int
d Int
i))
| Int
i <- [Int
1 .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1],
Just Tile
child <- [Tile -> Int -> Int -> Maybe Tile
fixTileDim Tile
t Int
d Int
i]
]
anchors :: [TileNode]
anchors =
[ Tile -> Relation -> TileNode
TileNode Tile
child (Split -> Relation
Anchor (Int -> Int -> Split
Split Int
d Int
0))
| Just Tile
child <- [Tile -> Int -> Int -> Maybe Tile
fixTileDim Tile
t Int
d Int
0]
]
in [TileNode]
siblings [TileNode] -> [TileNode] -> [TileNode]
forall a. [a] -> [a] -> [a]
++ [TileNode]
anchors
where
n :: Int
n = AffineRankSpace -> [Int]
sizes (Tile -> AffineRankSpace
space Tile
t) [Int] -> Int -> Int
forall a. HasCallStack => [a] -> Int -> a
!! Int
d
data Bisection = Bisection
deriving (Int -> Bisection -> ShowS
[Bisection] -> ShowS
Bisection -> String
(Int -> Bisection -> ShowS)
-> (Bisection -> String)
-> ([Bisection] -> ShowS)
-> Show Bisection
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Bisection -> ShowS
showsPrec :: Int -> Bisection -> ShowS
$cshow :: Bisection -> String
show :: Bisection -> String
$cshowList :: [Bisection] -> ShowS
showList :: [Bisection] -> ShowS
Show, Bisection -> Bisection -> Bool
(Bisection -> Bisection -> Bool)
-> (Bisection -> Bisection -> Bool) -> Eq Bisection
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Bisection -> Bisection -> Bool
== :: Bisection -> Bisection -> Bool
$c/= :: Bisection -> Bisection -> Bool
/= :: Bisection -> Bisection -> Bool
Eq)
instance Tiling Bisection where
childNodes :: Bisection -> Tile -> [TileNode]
childNodes Bisection
_ Tile
tile =
case Tile -> Maybe Int
firstNonSingletonDim Tile
tile of
Maybe Int
Nothing -> []
Just Int
d ->
let n :: Int
n = AffineRankSpace -> [Int]
sizes (Tile -> AffineRankSpace
space Tile
tile) [Int] -> Int -> Int
forall a. HasCallStack => [a] -> Int -> a
!! Int
d
lower :: Int
lower = Int
n Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2
siblings :: [TileNode]
siblings =
[ Tile -> Relation -> TileNode
TileNode Tile
child (Split -> Relation
Sibling (Int -> Int -> Split
Split Int
d Int
lower))
| Just Tile
child <- [Tile -> Int -> Int -> Int -> Maybe Tile
selectTileDim Tile
tile Int
d Int
lower Int
n]
]
anchors :: [TileNode]
anchors =
[ Tile -> Relation -> TileNode
TileNode Tile
child (Split -> Relation
Anchor (Int -> Int -> Split
Split Int
d Int
0))
| Just Tile
child <- [Tile -> Int -> Int -> Int -> Maybe Tile
selectTileDim Tile
tile Int
d Int
0 Int
lower]
]
in [TileNode]
siblings [TileNode] -> [TileNode] -> [TileNode]
forall a. [a] -> [a] -> [a]
++ [TileNode]
anchors
firstNonSingletonDim :: Tile -> Maybe Int
firstNonSingletonDim :: Tile -> Maybe Int
firstNonSingletonDim Tile
tile =
Int -> [Int] -> Maybe Int
forall {a} {t}. (Ord a, Num a, Num t) => t -> [a] -> Maybe t
go Int
0 (AffineRankSpace -> [Int]
sizes (Tile -> AffineRankSpace
space Tile
tile))
where
go :: t -> [a] -> Maybe t
go t
_ [] = Maybe t
forall a. Maybe a
Nothing
go t
d (a
n : [a]
ns)
| a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
1 = t -> Maybe t
forall a. a -> Maybe a
Just t
d
| Bool
otherwise = t -> [a] -> Maybe t
go (t
d t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) [a]
ns