module Language.Parser.Ptera.Data.Alignable.Array where

import           Language.Parser.Ptera.Prelude

import qualified Data.Array                               as DataArray
import qualified Language.Parser.Ptera.Data.Alignable     as Alignable
import qualified Language.Parser.Ptera.Data.Alignable.Map as AlignableMap


type T = Array

newtype Array n a = Array (DataArray.Array Int a)
    deriving (Array n a -> Array n a -> Bool
(Array n a -> Array n a -> Bool)
-> (Array n a -> Array n a -> Bool) -> Eq (Array n a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k (n :: k) a. Eq a => Array n a -> Array n a -> Bool
/= :: Array n a -> Array n a -> Bool
$c/= :: forall k (n :: k) a. Eq a => Array n a -> Array n a -> Bool
== :: Array n a -> Array n a -> Bool
$c== :: forall k (n :: k) a. Eq a => Array n a -> Array n a -> Bool
Eq, Int -> Array n a -> ShowS
[Array n a] -> ShowS
Array n a -> String
(Int -> Array n a -> ShowS)
-> (Array n a -> String)
-> ([Array n a] -> ShowS)
-> Show (Array n a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall k (n :: k) a. Show a => Int -> Array n a -> ShowS
forall k (n :: k) a. Show a => [Array n a] -> ShowS
forall k (n :: k) a. Show a => Array n a -> String
showList :: [Array n a] -> ShowS
$cshowList :: forall k (n :: k) a. Show a => [Array n a] -> ShowS
show :: Array n a -> String
$cshow :: forall k (n :: k) a. Show a => Array n a -> String
showsPrec :: Int -> Array n a -> ShowS
$cshowsPrec :: forall k (n :: k) a. Show a => Int -> Array n a -> ShowS
Show, a -> Array n b -> Array n a
(a -> b) -> Array n a -> Array n b
(forall a b. (a -> b) -> Array n a -> Array n b)
-> (forall a b. a -> Array n b -> Array n a) -> Functor (Array n)
forall k (n :: k) a b. a -> Array n b -> Array n a
forall k (n :: k) a b. (a -> b) -> Array n a -> Array n b
forall a b. a -> Array n b -> Array n a
forall a b. (a -> b) -> Array n a -> Array n b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Array n b -> Array n a
$c<$ :: forall k (n :: k) a b. a -> Array n b -> Array n a
fmap :: (a -> b) -> Array n a -> Array n b
$cfmap :: forall k (n :: k) a b. (a -> b) -> Array n a -> Array n b
Functor, Array n a -> Bool
(a -> m) -> Array n a -> m
(a -> b -> b) -> b -> Array n a -> b
(forall m. Monoid m => Array n m -> m)
-> (forall m a. Monoid m => (a -> m) -> Array n a -> m)
-> (forall m a. Monoid m => (a -> m) -> Array n a -> m)
-> (forall a b. (a -> b -> b) -> b -> Array n a -> b)
-> (forall a b. (a -> b -> b) -> b -> Array n a -> b)
-> (forall b a. (b -> a -> b) -> b -> Array n a -> b)
-> (forall b a. (b -> a -> b) -> b -> Array n a -> b)
-> (forall a. (a -> a -> a) -> Array n a -> a)
-> (forall a. (a -> a -> a) -> Array n a -> a)
-> (forall a. Array n a -> [a])
-> (forall a. Array n a -> Bool)
-> (forall a. Array n a -> Int)
-> (forall a. Eq a => a -> Array n a -> Bool)
-> (forall a. Ord a => Array n a -> a)
-> (forall a. Ord a => Array n a -> a)
-> (forall a. Num a => Array n a -> a)
-> (forall a. Num a => Array n a -> a)
-> Foldable (Array n)
forall a. Eq a => a -> Array n a -> Bool
forall a. Num a => Array n a -> a
forall a. Ord a => Array n a -> a
forall m. Monoid m => Array n m -> m
forall a. Array n a -> Bool
forall a. Array n a -> Int
forall a. Array n a -> [a]
forall a. (a -> a -> a) -> Array n a -> a
forall k (n :: k) a. Eq a => a -> Array n a -> Bool
forall k (n :: k) a. Num a => Array n a -> a
forall k (n :: k) a. Ord a => Array n a -> a
forall k (n :: k) m. Monoid m => Array n m -> m
forall k (n :: k) a. Array n a -> Bool
forall k (n :: k) a. Array n a -> Int
forall k (n :: k) a. Array n a -> [a]
forall k (n :: k) a. (a -> a -> a) -> Array n a -> a
forall k (n :: k) m a. Monoid m => (a -> m) -> Array n a -> m
forall k (n :: k) b a. (b -> a -> b) -> b -> Array n a -> b
forall k (n :: k) a b. (a -> b -> b) -> b -> Array n a -> b
forall m a. Monoid m => (a -> m) -> Array n a -> m
forall b a. (b -> a -> b) -> b -> Array n a -> b
forall a b. (a -> b -> b) -> b -> Array n a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: Array n a -> a
$cproduct :: forall k (n :: k) a. Num a => Array n a -> a
sum :: Array n a -> a
$csum :: forall k (n :: k) a. Num a => Array n a -> a
minimum :: Array n a -> a
$cminimum :: forall k (n :: k) a. Ord a => Array n a -> a
maximum :: Array n a -> a
$cmaximum :: forall k (n :: k) a. Ord a => Array n a -> a
elem :: a -> Array n a -> Bool
$celem :: forall k (n :: k) a. Eq a => a -> Array n a -> Bool
length :: Array n a -> Int
$clength :: forall k (n :: k) a. Array n a -> Int
null :: Array n a -> Bool
$cnull :: forall k (n :: k) a. Array n a -> Bool
toList :: Array n a -> [a]
$ctoList :: forall k (n :: k) a. Array n a -> [a]
foldl1 :: (a -> a -> a) -> Array n a -> a
$cfoldl1 :: forall k (n :: k) a. (a -> a -> a) -> Array n a -> a
foldr1 :: (a -> a -> a) -> Array n a -> a
$cfoldr1 :: forall k (n :: k) a. (a -> a -> a) -> Array n a -> a
foldl' :: (b -> a -> b) -> b -> Array n a -> b
$cfoldl' :: forall k (n :: k) b a. (b -> a -> b) -> b -> Array n a -> b
foldl :: (b -> a -> b) -> b -> Array n a -> b
$cfoldl :: forall k (n :: k) b a. (b -> a -> b) -> b -> Array n a -> b
foldr' :: (a -> b -> b) -> b -> Array n a -> b
$cfoldr' :: forall k (n :: k) a b. (a -> b -> b) -> b -> Array n a -> b
foldr :: (a -> b -> b) -> b -> Array n a -> b
$cfoldr :: forall k (n :: k) a b. (a -> b -> b) -> b -> Array n a -> b
foldMap' :: (a -> m) -> Array n a -> m
$cfoldMap' :: forall k (n :: k) m a. Monoid m => (a -> m) -> Array n a -> m
foldMap :: (a -> m) -> Array n a -> m
$cfoldMap :: forall k (n :: k) m a. Monoid m => (a -> m) -> Array n a -> m
fold :: Array n m -> m
$cfold :: forall k (n :: k) m. Monoid m => Array n m -> m
Foldable)

fromTotalMap :: Alignable.T n => n -> AlignableMap.T n a -> Array n a
fromTotalMap :: n -> T n a -> Array n a
fromTotalMap n
b T n a
m = Array Int a -> Array n a
forall k (n :: k) a. Array Int a -> Array n a
Array
    do (Int, Int) -> [(Int, a)] -> Array Int a
forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
DataArray.array (Int
0, Int -> Int
forall a. Enum a => a -> a
pred do n -> Int
coerce n
b) do [(n, a)] -> [(Int, a)]
coerce do T n a -> [(n, a)]
forall n a. T n => Map n a -> [(n, a)]
AlignableMap.toAscList T n a
m

fromList :: forall n a. Alignable.T n => [a] -> Array n a
fromList :: [a] -> Array n a
fromList [a]
xs = Array Int a -> Array n a
forall k (n :: k) a. Array Int a -> Array n a
Array
    do (Int, Int) -> [a] -> Array Int a
forall i e. Ix i => (i, i) -> [e] -> Array i e
DataArray.listArray (Int
0, [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [a]
xs

mapWithIx :: Alignable.T n => (n -> a -> a) -> Array n a -> Array n a
mapWithIx :: (n -> a -> a) -> Array n a -> Array n a
mapWithIx n -> a -> a
f (Array Array Int a
arr) = Array Int a -> Array n a
forall k (n :: k) a. Array Int a -> Array n a
Array
    do (Int, Int) -> [a] -> Array Int a
forall i e. Ix i => (i, i) -> [e] -> Array i e
DataArray.listArray
        do Array Int a -> (Int, Int)
forall i e. Array i e -> (i, i)
DataArray.bounds Array Int a
arr
        do [ n -> a -> a
f (Int -> n
coerce Int
i) a
x | (Int
i, a
x) <- Array Int a -> [(Int, a)]
forall i e. Ix i => Array i e -> [(i, e)]
DataArray.assocs Array Int a
arr ]

index :: forall n a. Alignable.T n => Array n a -> n -> Maybe a
index :: Array n a -> n -> Maybe a
index Array n a
arr n
i = case n -> Int
forall i. Alignable i => i -> Int
Alignable.numIncrements n
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Array n a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Array n a
arr of
    Bool
False -> Maybe a
forall a. Maybe a
Nothing
    Bool
True  -> a -> Maybe a
forall a. a -> Maybe a
Just do Array n a -> n -> a
forall n a. T n => Array n a -> n -> a
forceIndex Array n a
arr n
i

forceIndex :: forall n a. Alignable.T n => Array n a -> n -> a
forceIndex :: Array n a -> n -> a
forceIndex = (Array Int a -> Int -> a) -> Array n a -> n -> a
coerce do Ix Int => Array Int a -> Int -> a
forall i e. Ix i => Array i e -> i -> e
(DataArray.!) @Int @a

assocs :: forall n a. Alignable.T n => Array n a -> [(n, a)]
assocs :: Array n a -> [(n, a)]
assocs = (Array Int a -> [(Int, a)]) -> Array n a -> [(n, a)]
coerce do Ix Int => Array Int a -> [(Int, a)]
forall i e. Ix i => Array i e -> [(i, e)]
DataArray.assocs @Int @a