dense-0.1.0.0: Mutable and immutable dense multidimensional arrays

Copyright(c) Christopher Chalmers
LicenseBSD3
MaintainerChristopher Chalmers
Stabilityprovisional
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Data.Dense.Index

Contents

Description

This module provides a class for types that can be converted to and from linear indexes.

The default instances are defined in row-major order.

Synopsis

Shape class

type Layout f = f Int Source #

A Layout is the full size of an array. This alias is used to help distinguish between the layout of an array and an index (usually just l Int) in a type signature.

class (Eq1 f, Additive f, Traversable f) => Shape f where Source #

Class for types that can be converted to and from linear indexes.

Methods

shapeToIndex :: Layout f -> f Int -> Int Source #

Convert a shape to its linear index using the Layout.

shapeFromIndex :: Layout f -> Int -> f Int Source #

Convert a linear index to a shape the Layout.

shapeIntersect :: Layout f -> Layout f -> Layout f Source #

Calculate the intersection of two shapes.

unsafeShapeStep :: Layout f -> f Int -> f Int Source #

Increment a shape by one. It is assumed that the provided index is inRange.

shapeStep :: Layout f -> f Int -> Maybe (f Int) Source #

Increment a shape by one. It is assumed that the provided index is inRange.

shapeStepBetween :: f Int -> Layout f -> f Int -> Maybe (f Int) Source #

Increment a shape by one between the two bounds

shapeInRange :: Layout f -> f Int -> Bool Source #

inRange ex i checks i < ex for every coordinate of f.

shapeSize :: Layout f -> Int Source #

The number of elements in a shape.

Instances

Shape V4 Source # 
Shape V3 Source # 
Shape V2 Source # 
Shape V1 Source # 
Shape V0 Source # 

indexIso :: Shape f => Layout f -> Iso' (f Int) Int Source #

toIndex l and fromIndex l form two halfs of an isomorphism.

HasLayout

class Shape f => HasLayout f a | a -> f where Source #

Class of things that have a Layout. This means we can use the same functions for the various different arrays in the library.

Methods

layout :: Lens' a (Layout f) Source #

Lens onto the Layout of something.

layout :: a ~ f Int => (Layout f -> g (Layout f)) -> a -> g a Source #

Lens onto the Layout of something.

Instances

(~) * i Int => HasLayout V4 (V4 i) Source # 

Methods

layout :: Lens' (V4 i) (Layout V4) Source #

(~) * i Int => HasLayout V3 (V3 i) Source # 

Methods

layout :: Lens' (V3 i) (Layout V3) Source #

(~) * i Int => HasLayout V2 (V2 i) Source # 

Methods

layout :: Lens' (V2 i) (Layout V2) Source #

(~) * i Int => HasLayout V1 (V1 i) Source # 

Methods

layout :: Lens' (V1 i) (Layout V1) Source #

(~) * i Int => HasLayout V0 (V0 i) Source # 

Methods

layout :: Lens' (V0 i) (Layout V0) Source #

Shape f => HasLayout f (Focused f a) Source #

The size of the layout must remain the same or an error is thrown.

Methods

layout :: Lens' (Focused f a) (Layout f) Source #

Shape f => HasLayout f (Delayed f a) Source #

The size of the layout must remain the same or an error is thrown.

Methods

layout :: Lens' (Delayed f a) (Layout f) Source #

Shape f => HasLayout f (Array v f a) Source #

The size of the layout must remain the same or an error is thrown.

Methods

layout :: Lens' (Array v f a) (Layout f) Source #

Shape f => HasLayout f (MArray v f s a) Source # 

Methods

layout :: Lens' (MArray v f s a) (Layout f) Source #

extent :: HasLayout f a => a -> f Int Source #

Get the extent of an array.

extent :: Array v f a    -> f Int
extent :: MArray v f s a -> f Int
extent :: Delayed f a    -> f Int
extent :: Focused f a    -> f Int

size :: HasLayout f a => a -> Int Source #

Get the total number of elements in an array.

size :: Array v f a    -> Int
size :: MArray v f s a -> Int
size :: Delayed f a    -> Int
size :: Focused f a    -> Int

indexes :: HasLayout f a => IndexedFold Int a (f Int) Source #

Indexed fold for all the indexes in the layout.

indexesBetween :: HasLayout f a => f Int -> f Int -> IndexedFold Int a (f Int) Source #

Indexed fold between the two indexes where the index is the linear index for the original layout.

indexesFrom :: HasLayout f a => f Int -> IndexedFold Int a (f Int) Source #

Indexed fold starting starting from some point, where the index is the linear index for the original layout.

Exceptions

boundsCheck :: Shape l => Layout l -> l Int -> a -> a Source #

boundsCheck l i performs a bounds check for index i and layout l. Throws an IndexOutOfBounds exception when out of range in the form (i, l). This can be caught with the _IndexOutOfBounds prism.

>>> boundsCheck (V2 3 5) (V2 1 4) "in range"
"in range"
>>> boundsCheck (V2 10 20) (V2 10 5) "in bounds"
"*** Exception: array index out of range: (V2 10 5, V2 10 20)
>>> catching _IndexOutOfBounds (boundsCheck (V1 2) (V1 2) (putStrLn "in range")) print
"(V1 2, V1 2)"

The output format is suitable to be read using the _Show prism:

>>> trying (_IndexOutOfBounds . _Show) (boundsCheck (V1 2) (V1 20) (putStrLn "in range")) :: IO (Either (V1 Int, V1 Int) ())
Left (V1 20,V1 2)

sizeMissmatch :: Int -> Int -> String -> a -> a Source #

Check the sizes are equal. If not, throw SizeMissmatch.

Utilities

showShape :: Shape f => f Int -> String Source #

Show a shape in the form VN i1 i2 .. iN where N is the length of the shape.