primitive-indexed-0.1.0.0

Safe HaskellNone
LanguageHaskell2010

Data.Primitive.Indexed.Array

Contents

Description

Arrays of boxed elements. This mimics the API of Data.Primitive.Array. However, all functions that deal with indices are total and cannot cause runtime crashes.

Synopsis

Types

data Vector n a Source #

Instances

Functor (Vector k n) Source # 

Methods

fmap :: (a -> b) -> Vector k n a -> Vector k n b #

(<$) :: a -> Vector k n b -> Vector k n a #

Foldable (Vector k n) Source # 

Methods

fold :: Monoid m => Vector k n m -> m #

foldMap :: Monoid m => (a -> m) -> Vector k n a -> m #

foldr :: (a -> b -> b) -> b -> Vector k n a -> b #

foldr' :: (a -> b -> b) -> b -> Vector k n a -> b #

foldl :: (b -> a -> b) -> b -> Vector k n a -> b #

foldl' :: (b -> a -> b) -> b -> Vector k n a -> b #

foldr1 :: (a -> a -> a) -> Vector k n a -> a #

foldl1 :: (a -> a -> a) -> Vector k n a -> a #

toList :: Vector k n a -> [a] #

null :: Vector k n a -> Bool #

length :: Vector k n a -> Int #

elem :: Eq a => a -> Vector k n a -> Bool #

maximum :: Ord a => Vector k n a -> a #

minimum :: Ord a => Vector k n a -> a #

sum :: Num a => Vector k n a -> a #

product :: Num a => Vector k n a -> a #

Traversable (Vector k n) Source # 

Methods

traverse :: Applicative f => (a -> f b) -> Vector k n a -> f (Vector k n b) #

sequenceA :: Applicative f => Vector k n (f a) -> f (Vector k n a) #

mapM :: Monad m => (a -> m b) -> Vector k n a -> m (Vector k n b) #

sequence :: Monad m => Vector k n (m a) -> m (Vector k n a) #

Eq a => Eq (Vector k n a) Source # 

Methods

(==) :: Vector k n a -> Vector k n a -> Bool #

(/=) :: Vector k n a -> Vector k n a -> Bool #

Ord a => Ord (Vector k n a) Source # 

Methods

compare :: Vector k n a -> Vector k n a -> Ordering #

(<) :: Vector k n a -> Vector k n a -> Bool #

(<=) :: Vector k n a -> Vector k n a -> Bool #

(>) :: Vector k n a -> Vector k n a -> Bool #

(>=) :: Vector k n a -> Vector k n a -> Bool #

max :: Vector k n a -> Vector k n a -> Vector k n a #

min :: Vector k n a -> Vector k n a -> Vector k n a #

Primops

replicate :: PrimMonad m => Length n -> a -> m (MutableVector n (PrimState m) a) Source #

index :: Vector n a -> Index n -> a Source #

Index into a vector. Use this with functions from Data.Primitive.Index.Types to get a hold of indices that can be used.

>>> let cities = ["Cairo","Atlanta","Tokyo"] :: Array String
>>> with cities $ \v -> ascendM (\ix -> putStrLn ("I ❤️ " ++ index v ix)) (length v)
I ❤️ Cairo
I ❤️ Atlanta
I ❤️ Tokyo

read :: PrimMonad m => MutableVector n (PrimState m) a -> Index n -> m a Source #

write :: PrimMonad m => MutableVector n (PrimState m) a -> Index n -> a -> m () Source #

length :: Vector n a -> Length n Source #

Get the length of a vector.

size :: MutableVector n s a -> Length n Source #

Get the length of a mutable vector.

thaw :: PrimMonad m => Vector n a -> m (MutableVector n (PrimState m) a) Source #

Create a mutable array from a slice of an immutable array.

This operation makes a copy of the immutable array, so it is safe to use the immutable array afterward.

unsafeFreeze :: PrimMonad m => MutableVector n (PrimState m) a -> m (Vector n a) Source #

Freeze the mutable vector. The argument must not be reused after this function is called on it.

Array Interop

forget :: Vector n a -> Array a Source #

Discard the phantom length associated with an indexed vector.

with :: Array a -> (forall n. Vector n a -> b) -> b Source #

Pass an unindexed array to a function that operates on a vector of any length.

Functions

All of these can be safely written using the other primitives provided by this module. They are provided here to demonstrate how to write index-safe variants of functions from the vector library without sacrificing performance.

zipWith :: (a -> b -> c) -> Vector n a -> Vector n b -> Vector n c Source #

Zip two vectors of equal length together by applying the given function.

reverse :: Vector n a -> Vector n a Source #

Reverse a vector

update Source #

Arguments

:: Vector n a

initial vector (of length n)

-> PrimVector m (Index n)

vector of indices

-> Vector m a

vector of values

-> Vector n a 

O(m+n) For each pair (i,a) from the index/value pair vectors, replace the vector element at position i by a.

accumulate Source #

Arguments

:: (a -> b -> a)

accumulating function f

-> Vector n a

initial vector (of length n)

-> PrimVector m (Index n)

vector of indices

-> Vector m b

vector of values

-> Vector n a 

O(m+n) For each pair (i,a) from the index/value pair vectors, replace the vector element at position i by f a b.

backpermute :: Vector n a -> PrimVector m (Index n) -> Vector m a Source #

O(n) Yield the vector obtained by replacing each element i of the index vector by index xs i.