{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ > 710
{-# OPTIONS_GHC -Wno-missing-export-lists #-}
#endif
module Data.Grid.Storable.Internal
where
import Control.DeepSeq ( NFData(rnf) )
import Data.Data (Data (..))
import Text.Read (Read(..), readListPrecDefault )
import Data.Tuple (swap)
import Data.Typeable (Typeable)
import qualified Data.Vector as V
import qualified Data.Vector.Generic as G
import Foreign
import Prelude hiding ( length, null,
replicate, (++), concat,
head, last,
init, tail, take, drop, splitAt, reverse,
map, concatMap,
zipWith, zipWith3, zip, zip3, unzip, unzip3,
filter, takeWhile, dropWhile, span, break,
elem, notElem,
foldl, foldl1, foldr, foldr1,
#if __GLASGOW_HASKELL__ >= 706
foldMap,
#endif
all, any, and, or, sum, product, minimum, maximum,
scanl, scanl1, scanr, scanr1,
enumFromTo, enumFromThenTo,
mapM, mapM_, sequence, sequence_ )
import Data.Grid.Storable.Mutable
{-# ANN module ("HLint: ignore Use camelCase"::String) #-}
{-# ANN module ("HLint: ignore Eta reduce"::String) #-}
offset_to_coord :: Integral a => (a, b) -> a -> (a, a)
offset_to_coord (w,_h) n = swap $ divMod n w
{-# INLINEABLE offset_to_coord #-}
coord_to_offset :: Num a => (a, b) -> (a, a) -> a
coord_to_offset (w,_h) (x,y) = y * w + x
{-# INLINEABLE coord_to_offset #-}
data Grid el a = Grid {-# UNPACK #-} !(V.Vector a)
{-# UNPACK #-} !(ForeignPtr el)
deriving (Typeable, Data)
liftRnfV :: G.Vector a b => (b -> ()) -> a b -> ()
liftRnfV elemRnf = G.foldl' (const elemRnf) ()
instance NFData (v el) => NFData (Grid el (v el)) where
rnf = liftRnfV rnf
{-# INLINEABLE rnf #-}
instance Show (v el) => Show (Grid el (v el)) where
showsPrec = G.showsPrec
instance Read (v el) => Read (Grid el (v el)) where
readPrec = G.readPrec
readListPrec = readListPrecDefault
type instance G.Mutable (Grid el) = MGrid el
instance G.Vector (Grid el) (v el :: *) where
{-# INLINE basicUnsafeFreeze #-}
basicUnsafeFreeze _ = error "you can't possible have got hold of a mutable Grid"
{-# INLINE basicUnsafeThaw #-}
basicUnsafeThaw _ = error "you can't get thaw Grids"
{-# INLINE basicLength #-}
basicLength (Grid vec _) = G.basicLength vec
{-# INLINE basicUnsafeSlice #-}
basicUnsafeSlice j n (Grid vec ptr) = Grid (G.basicUnsafeSlice j n vec) ptr
{-# INLINE basicUnsafeIndexM #-}
basicUnsafeIndexM (Grid vec _ptr) j = G.basicUnsafeIndexM vec j
instance Eq (v el) => Eq ((Grid el) (v el)) where
{-# INLINE (==) #-}
(Grid v1 _) == (Grid v2 _) = v1 == v2
{-# INLINE (/=) #-}
(Grid v1 _) /= (Grid v2 _) = v1 /= v2
instance Ord (v el) => Ord ((Grid el) (v el)) where
{-# INLINE compare #-}
compare (Grid v1 _) (Grid v2 _) = compare v1 v2
{-# INLINE (<) #-}
(Grid v1 _) < (Grid v2 _) = v1 < v2
{-# INLINE (<=) #-}
(Grid v1 _) <= (Grid v2 _) = v1 <= v2
{-# INLINE (>) #-}
(Grid v1 _) > (Grid v2 _) = v1 > v2
{-# INLINE (>=) #-}
(Grid v1 _) >= (Grid v2 _) = v1 >= v2
length :: (Grid el) (v el) -> Int
{-# INLINE length #-}
length = G.length
null :: (Grid el) (v el) -> Bool
{-# INLINE null #-}
null = G.null
(!) :: (Grid el) (v el) -> Int -> v el
{-# INLINE (!) #-}
(!) = (G.!)
(!?) :: (Grid el) (v el) -> Int -> Maybe (v el)
{-# INLINE (!?) #-}
(!?) = (G.!?)
head :: (Grid el) (v el) -> v el
{-# INLINE head #-}
head = G.head
last :: (Grid el) (v el) -> v el
{-# INLINE last #-}
last = G.last
unsafeIndex :: (Grid el) (v el) -> Int -> v el
{-# INLINE unsafeIndex #-}
unsafeIndex = G.unsafeIndex
unsafeHead :: (Grid el) (v el) -> v el
{-# INLINE unsafeHead #-}
unsafeHead = G.unsafeHead
unsafeLast :: (Grid el) (v el) -> v el
{-# INLINE unsafeLast #-}
unsafeLast = G.unsafeLast
slice :: Int
-> Int
-> (Grid el) (v el)
-> (Grid el) (v el)
{-# INLINE slice #-}
slice = G.slice
init :: (Grid el) (v el) -> (Grid el) (v el)
{-# INLINE init #-}
init = G.init
tail :: (Grid el) (v el) -> (Grid el) (v el)
{-# INLINE tail #-}
tail = G.tail
take :: Int -> (Grid el) (v el) -> (Grid el) (v el)
{-# INLINE take #-}
take = G.take
drop :: Int -> (Grid el) (v el) -> (Grid el) (v el)
{-# INLINE drop #-}
drop = G.drop
splitAt :: Int -> (Grid el) (v el) -> ((Grid el) (v el), (Grid el) (v el))
{-# INLINE splitAt #-}
splitAt = G.splitAt
unsafeSlice :: Int
-> Int
-> (Grid el) (v el)
-> (Grid el) (v el)
{-# INLINE unsafeSlice #-}
unsafeSlice = G.unsafeSlice
unsafeInit :: (Grid el) (v el) -> (Grid el) (v el)
{-# INLINE unsafeInit #-}
unsafeInit = G.unsafeInit
unsafeTail :: (Grid el) (v el) -> (Grid el) (v el)
{-# INLINE unsafeTail #-}
unsafeTail = G.unsafeTail
unsafeTake :: Int -> (Grid el) (v el) -> (Grid el) (v el)
{-# INLINE unsafeTake #-}
unsafeTake = G.unsafeTake
unsafeDrop :: Int -> (Grid el) (v el) -> (Grid el) (v el)
{-# INLINE unsafeDrop #-}
unsafeDrop = G.unsafeDrop
reverse :: (Grid el) (v el) -> (Grid el) (v el)
{-# INLINE reverse #-}
reverse = G.reverse
toList :: (Grid el) (v el) -> [v el]
{-# INLINE toList #-}
toList = G.toList