module Data.Graph.Grid where

import qualified Data.ByteString.Char8 as C
import qualified Data.Vector.Unboxed as U

inGrid :: Int -> Int -> Int -> Int -> Bool
inGrid :: Int -> Int -> Int -> Int -> Bool
inGrid Int
h Int
w Int
x Int
y = Int
0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
x Bool -> Bool -> Bool
&& Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
h Bool -> Bool -> Bool
&& Int
0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
y Bool -> Bool -> Bool
&& Int
y Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
w
{-# INLINE inGrid #-}

neighbor4 :: (Applicative f) => Int -> Int -> (Int -> Int -> f ()) -> f ()
neighbor4 :: forall (f :: * -> *).
Applicative f =>
Int -> Int -> (Int -> Int -> f ()) -> f ()
neighbor4 Int
x Int
y Int -> Int -> f ()
f = Int -> Int -> f ()
f (Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int
y f () -> f () -> f ()
forall a b. f a -> f b -> f b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Int -> Int -> f ()
f Int
x (Int
y Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) f () -> f () -> f ()
forall a b. f a -> f b -> f b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Int -> Int -> f ()
f Int
x (Int
y Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) f () -> f () -> f ()
forall a b. f a -> f b -> f b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Int -> Int -> f ()
f (Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
y
{-# INLINE neighbor4 #-}

mkGrid :: C.ByteString -> U.Vector Char
mkGrid :: ByteString -> Vector Char
mkGrid ByteString
bs = Int
-> (ByteString -> Maybe (Char, ByteString))
-> ByteString
-> Vector Char
forall a b. Unbox a => Int -> (b -> Maybe (a, b)) -> b -> Vector a
U.unfoldrN (ByteString -> Int
C.length ByteString
bs) ByteString -> Maybe (Char, ByteString)
C.uncons ByteString
bs