repa-array-4.2.3.1: Bulk array representations and operators.

Safe HaskellNone
LanguageHaskell98

Data.Repa.Array.Material

Contents

Description

Material arrays are represented as concrete data in memory.

For performance reasons, random access indexing into these layouts is not bounds checked. However, all bulk operators like map and concat are guaranteed to be safe.

Synopsis

Documentation

type Material l a = (Bulk l a, Windowable l a, Target l a) Source #

Classes supported by all material representations.

We can index them in a random-access manner, window them in constant time, and use them as targets for a computation.

In particular, delayed arrays are not material as we cannot use them as targets for a computation.

Auto arrays

data A Source #

Arrays where the elements that are automatically layed out into some efficient runtime representation.

The implementation uses type families to chose unboxed representations for all elements that can be unboxed. In particular: arrays of unboxed tuples are represented as tuples of unboxed arrays, and nested arrays are represented using a segment descriptor and a single single flat vector containing all the elements.

Constructors

Auto 

Fields

Instances

Eq A Source # 

Methods

(==) :: A -> A -> Bool #

(/=) :: A -> A -> Bool #

Show A Source # 

Methods

showsPrec :: Int -> A -> ShowS #

show :: A -> String #

showList :: [A] -> ShowS #

Layout A Source # 

Associated Types

data Name A :: * Source #

type Index A :: * Source #

Eq (Name A) Source # 

Methods

(==) :: Name A -> Name A -> Bool #

(/=) :: Name A -> Name A -> Bool #

Show (Name A) Source # 

Methods

showsPrec :: Int -> Name A -> ShowS #

show :: Name A -> String #

showList :: [Name A] -> ShowS #

data Name A Source # 
data Name A = A
type Index A Source # 
type Index A = Int
data Array A Char Source # 
data Array A Double Source # 
data Array A Float Source # 
data Array A Int Source # 
data Array A Int8 Source # 
data Array A Int16 Source # 
data Array A Int32 Source # 
data Array A Int64 Source # 
data Array A Word8 Source # 
data Array A () Source # 
data Array A () = AArray_Unit !Int
data Array A Date32 Source # 
data Array A Text Source # 
data Buffer A Char Source # 
data Buffer A Double Source # 
data Buffer A Float Source # 
data Buffer A Int Source # 
data Buffer A Int8 Source # 
data Buffer A Int16 Source # 
data Buffer A Int32 Source # 
data Buffer A Int64 Source # 
data Buffer A Word8 Source # 
data Buffer A () Source # 
data Buffer A Date32 Source # 
data Buffer A Text Source # 
data Array A [a] Source # 
data Array A [a] = AArray_List !(Array B [a])
data Array A (Maybe a) Source # 
data Array A (Maybe a) = AArray_Maybe !(Array B (Maybe a))
data Array A (Box a) Source # 
data Array A (Box a) = AArray_Box !(Array B a)
data Buffer A [a] Source # 
data Buffer A [a] = ABuffer_List !(Buffer B [a])
data Buffer A (Maybe a) Source # 
data Buffer A (Box a) Source # 
data Buffer A (Box a) = ABuffer_Box !(Buffer B a)
type Select' Z (Array A ((:*:) t1 ts)) # 
type Select' Z (Array A ((:*:) t1 ts)) = Array A t1
type Discard' Z (Array A ((:*:) t1 ts)) # 
type Discard' Z (Array A ((:*:) t1 ts)) = Array A ts
type Mask' () (Array A ()) # 
type Mask' () (Array A ()) = Array A ()
data Array A (a, b) Source # 
data Array A (a, b) = AArray_T2 !(Array (T2 A A) (a, b))
data Array A ((:*:) a b) Source # 
data Array A ((:*:) a b) = AArray_Prod !(Array A a) !(Array A b)
data Array A (Array r a) Source # 
data Array A (Array r a) = AArray_Array !(Array N (Array r a))
data Buffer A (a, b) Source # 
data Buffer A (a, b) = ABuffer_T2 !(Buffer (T2 A A) (a, b))
data Buffer A ((:*:) a b) Source # 
data Buffer A ((:*:) a b) = ABuffer_Prod !(Buffer A a) !(Buffer A b)
data Buffer A (Array l a) Source # 
data Buffer A (Array l a) = ABuffer_Array !(Buffer N (Array l a))
type Select' (S n) (Array A ((:*:) t1 ts)) # 
type Select' (S n) (Array A ((:*:) t1 ts)) = Select' n (Array A ts)
type Discard' (S n) (Array A ((:*:) t1 ts)) # 
type Discard' (S n) (Array A ((:*:) t1 ts)) = Array A ((:*:) t1 (Discard' n ts))
type Mask' ((:*:) Drop ms) (Array A ((:*:) t1 ts)) # 
type Mask' ((:*:) Drop ms) (Array A ((:*:) t1 ts)) = Mask' ms (Array A ts)
type Mask' ((:*:) Keep ms) (Array A ((:*:) t1 ts)) # 
type Mask' ((:*:) Keep ms) (Array A ((:*:) t1 ts)) = Array A ((:*:) t1 (Mask' ms ts))

Foreign arrays

data F Source #

Layout for dense Foreign arrays.

UNSAFE: Indexing into raw material arrays is not bounds checked. You may want to wrap this with a Checked layout as well.

Constructors

Foreign 

Fields

Instances

Eq F Source # 

Methods

(==) :: F -> F -> Bool #

(/=) :: F -> F -> Bool #

Show F Source # 

Methods

showsPrec :: Int -> F -> ShowS #

show :: F -> String #

showList :: [F] -> ShowS #

Layout F Source #

Foreign arrays.

Associated Types

data Name F :: * Source #

type Index F :: * Source #

Storable a => Bulk F a Source #

Foreign arrays.

Associated Types

data Array F a :: * Source #

Methods

layout :: Array F a -> F Source #

index :: Array F a -> Index F -> a Source #

Storable a => Windowable F a Source #

Windowing Foreign arrays.

Methods

window :: Index F -> Index F -> Array F a -> Array F a Source #

Storable a => Target F a Source #

Foreign buffers

Eq (Name F) Source # 

Methods

(==) :: Name F -> Name F -> Bool #

(/=) :: Name F -> Name F -> Bool #

Show (Name F) Source # 

Methods

showsPrec :: Int -> Name F -> ShowS #

show :: Name F -> String #

showList :: [Name F] -> ShowS #

(Eq a, Storable a) => Eq (Array F a) Source # 

Methods

(==) :: Array F a -> Array F a -> Bool #

(/=) :: Array F a -> Array F a -> Bool #

(Storable a, Show a) => Show (Array F a) Source # 

Methods

showsPrec :: Int -> Array F a -> ShowS #

show :: Array F a -> String #

showList :: [Array F a] -> ShowS #

data Name F Source # 
data Name F = F
type Index F Source # 
type Index F = Int
data Array F Source # 
data Array F = FArray !(Vector a)
data Buffer F Source # 
data Buffer F = FBuffer !(IOVector a)

fromForeignPtr :: Storable a => Int -> ForeignPtr a -> Array F a Source #

O(1). Wrap a ForeignPtr as an array.

toForeignPtr :: Storable a => Array F a -> (Int, Int, ForeignPtr a) Source #

O(1). Unwrap a ForeignPtr from an array.

fromByteString :: ByteString -> Array F Word8 Source #

O(1). Convert a ByteString to an foreign Array.

toByteString :: Array F Word8 -> ByteString Source #

O(1). Convert a foreign Vector to a ByteString.

fromStorableVector :: Vector a -> Array F a Source #

O(1). Convert a storable Vector to a foreign Array

toStorableVector :: Array F a -> Vector a Source #

O(1). Convert a foreign array to a storable Vector.

Nested arrays

data N Source #

Nested array represented as a flat array of elements, and a segment descriptor that describes how the elements are partitioned into the sub-arrays. Using this representation for multidimentional arrays is significantly more efficient than using a boxed array of arrays, as there is no need to allocate the sub-arrays individually in the heap.

With a nested type like: Array N (Array N (Array U Int)), the concrete representation consists of five flat unboxed vectors: two for each of the segment descriptors associated with each level of nesting, and one unboxed vector to hold all the integer elements.

UNSAFE: Indexing into raw material arrays is not bounds checked. You may want to wrap this with a Checked layout as well.

Constructors

Nested 

Fields

Instances

Eq N Source # 

Methods

(==) :: N -> N -> Bool #

(/=) :: N -> N -> Bool #

Show N Source # 

Methods

showsPrec :: Int -> N -> ShowS #

show :: N -> String #

showList :: [N] -> ShowS #

Layout N Source #

Nested arrays.

Associated Types

data Name N :: * Source #

type Index N :: * Source #

(BulkI l a, Windowable l a) => Bulk N (Array l a) Source #

Nested arrays.

Associated Types

data Array N (Array l a) :: * Source #

Methods

layout :: Array N (Array l a) -> N Source #

index :: Array N (Array l a) -> Index N -> Array l a Source #

(BulkI l a, Windowable l a) => Windowable N (Array l a) Source #

Windowing Nested arrays.

Methods

window :: Index N -> Index N -> Array N (Array l a) -> Array N (Array l a) Source #

(Bulk l a, Target l a, (~) * (Index l) Int) => Target N (Array l a) Source # 

Associated Types

data Buffer N (Array l a) :: * Source #

Eq (Name N) Source # 

Methods

(==) :: Name N -> Name N -> Bool #

(/=) :: Name N -> Name N -> Bool #

Show (Name N) Source # 

Methods

showsPrec :: Int -> Name N -> ShowS #

show :: Name N -> String #

showList :: [Name N] -> ShowS #

Show (Array l a) => Show (Array N (Array l a)) Source # 

Methods

showsPrec :: Int -> Array N (Array l a) -> ShowS #

show :: Array N (Array l a) -> String #

showList :: [Array N (Array l a)] -> ShowS #

data Name N Source # 
data Name N = N
type Index N Source # 
type Index N = Int
data Array N (Array l a) Source # 
data Buffer N (Array l a) Source # 
data Buffer N (Array l a) = NBuffer !(IOVector (Array l a))

fromLists :: TargetI l a => Name l -> [[a]] -> Array N (Array l a) Source #

O(size src) Convert some lists to a nested array.

fromListss :: TargetI l a => Name l -> [[[a]]] -> Array N (Array N (Array l a)) Source #

O(size src) Convert a triply nested list to a triply nested array.

Boxed arrays

data B Source #

Layout an array as flat vector of boxed elements.

UNSAFE: Indexing into raw material arrays is not bounds checked. You may want to wrap this with a Checked layout as well.

Constructors

Boxed 

Fields

Instances

Eq B Source # 

Methods

(==) :: B -> B -> Bool #

(/=) :: B -> B -> Bool #

Show B Source # 

Methods

showsPrec :: Int -> B -> ShowS #

show :: B -> String #

showList :: [B] -> ShowS #

Layout B Source #

Boxed arrays.

Associated Types

data Name B :: * Source #

type Index B :: * Source #

Bulk B a Source #

Boxed arrays.

Associated Types

data Array B a :: * Source #

Methods

layout :: Array B a -> B Source #

index :: Array B a -> Index B -> a Source #

Windowable B a Source #

Boxed windows.

Methods

window :: Index B -> Index B -> Array B a -> Array B a Source #

Target B a Source #

Boxed buffers.

Eq (Name B) Source # 

Methods

(==) :: Name B -> Name B -> Bool #

(/=) :: Name B -> Name B -> Bool #

Show (Name B) Source # 

Methods

showsPrec :: Int -> Name B -> ShowS #

show :: Name B -> String #

showList :: [Name B] -> ShowS #

Eq a => Eq (Array B a) Source # 

Methods

(==) :: Array B a -> Array B a -> Bool #

(/=) :: Array B a -> Array B a -> Bool #

Show a => Show (Array B a) Source # 

Methods

showsPrec :: Int -> Array B a -> ShowS #

show :: Array B a -> String #

showList :: [Array B a] -> ShowS #

data Name B Source # 
data Name B = B
type Index B Source # 
type Index B = Int
data Array B Source # 
data Array B = BArray !(Vector a)
data Buffer B Source # 
data Buffer B = BBuffer !(IOVector a)

fromBoxed :: Vector a -> Array B a Source #

O(1). Wrap a boxed vector as an array.

toBoxed :: Array B a -> Vector a Source #

O(1). Unwrap a boxed vector from an array.

Unboxed arrays

data U Source #

Layout an array as a flat vector of unboxed elements.

This is the most efficient representation for numerical data.

The implementation uses Data.Vector.Unboxed which picks an efficient, specialised representation for every element type. In particular, unboxed vectors of pairs are represented as pairs of unboxed vectors.

UNSAFE: Indexing into raw material arrays is not bounds checked. You may want to wrap this with a Checked layout as well.

Constructors

Unboxed 

Fields

Instances

Eq U Source # 

Methods

(==) :: U -> U -> Bool #

(/=) :: U -> U -> Bool #

Show U Source # 

Methods

showsPrec :: Int -> U -> ShowS #

show :: U -> String #

showList :: [U] -> ShowS #

Layout U Source #

Unboxed arrays.

Associated Types

data Name U :: * Source #

type Index U :: * Source #

Unbox a => Bulk U a Source #

Unboxed arrays.

Associated Types

data Array U a :: * Source #

Methods

layout :: Array U a -> U Source #

index :: Array U a -> Index U -> a Source #

Unbox a => Windowable U a Source #

Windowing Unboxed arrays.

Methods

window :: Index U -> Index U -> Array U a -> Array U a Source #

Unbox a => Target U a Source #

Unboxed buffers.

Eq (Name U) Source # 

Methods

(==) :: Name U -> Name U -> Bool #

(/=) :: Name U -> Name U -> Bool #

Show (Name U) Source # 

Methods

showsPrec :: Int -> Name U -> ShowS #

show :: Name U -> String #

showList :: [Name U] -> ShowS #

(Unbox a, Eq a) => Eq (Array U a) Source # 

Methods

(==) :: Array U a -> Array U a -> Bool #

(/=) :: Array U a -> Array U a -> Bool #

(Show a, Unbox a) => Show (Array U a) Source # 

Methods

showsPrec :: Int -> Array U a -> ShowS #

show :: Array U a -> String #

showList :: [Array U a] -> ShowS #

data Name U Source # 
data Name U = U
type Index U Source # 
type Index U = Int
data Array U Source # 
data Array U = UArray !(Vector a)
data Buffer U Source # 
data Buffer U = UBuffer !(IOVector a)

class (Vector Vector a, MVector MVector a) => Unbox a #

Instances

Unbox Bool 
Unbox Char 
Unbox Double 
Unbox Float 
Unbox Int 
Unbox Int8 
Unbox Int16 
Unbox Int32 
Unbox Int64 
Unbox Word 
Unbox Word8 
Unbox Word16 
Unbox Word32 
Unbox Word64 
Unbox () 
(RealFloat a, Unbox a) => Unbox (Complex a) 
(Unbox a, Unbox b) => Unbox (a, b) 
(Unbox a, Unbox b) => Unbox ((:*:) a b) 
(Unbox a, Unbox b, Unbox c) => Unbox (a, b, c) 
(Unbox a, Unbox b, Unbox c, Unbox d) => Unbox (a, b, c, d) 
(Unbox a, Unbox b, Unbox c, Unbox d, Unbox e) => Unbox (a, b, c, d, e) 
(Unbox a, Unbox b, Unbox c, Unbox d, Unbox e, Unbox f) => Unbox (a, b, c, d, e, f) 

fromUnboxed :: Vector a -> Array U a Source #

O(1). Wrap an unboxed vector as an array.

toUnboxed :: Array U a -> Vector a Source #

O(1). Unwrap an unboxed vector from an array.

Material operators

These operators work on particular material representations, rather than being generic like the ones in Data.Repa.Array.Generic

Mapping

mapElems :: (Array l1 a -> Array l2 b) -> Array N (Array l1 a) -> Array N (Array l2 b) Source #

Apply a function to all the elements of a doubly nested array, preserving the nesting structure.

Filtering

decimate :: (a -> a -> Bool) -> Array B a -> Array B a Source #

Scan through an array from front to back. For pairs of successive elements, drop the second one when the given predicate returns true.

This function can be used to remove duplicates from a sorted array.

TODO: generalise to other array types.

Slicing

slices Source #

Arguments

:: Array F Int

Segment starting positions.

-> Array F Int

Segment lengths.

-> Array l a

Array elements.

-> Array N (Array l a) 

O(1). Produce a nested array by taking slices from some array of elements.

This is a constant time operation, as the representation for nested vectors just wraps the starts, lengths and elements vectors.

Partitioning

partition Source #

Arguments

:: (BulkI lSrc (Int, a), Target lDst a, Index lDst ~ Int, Elt a) 
=> Name lDst

Name of destination layout.

-> Int

Total number of segments.

-> Array lSrc (Int, a)

Segment numbers and values.

-> Array N (Array lDst a)

Result array

Take a desired number of segments, and array of key value pairs where the key is the segment number. Partition the values into the stated number of segments, discarding values where the key falls outside the given range.

  • This function operates by first allocating a buffer of size (segs * len src) and filling it with a default value. Both the worst case runtime and memory use will be poor for a large number of destination segments.

TODO: we need the pre-init because otherwise unused values in the elems array are undefined. We could avoid this by copying out the used elements after the partition loop finishes. Use a segmented extract function. This would also remove the dependency on the Elt class.

partitionBy Source #

Arguments

:: (BulkI lSrc a, Target lDst a, Index lDst ~ Int, Elt a) 
=> Name lDst

Name of destination layout.

-> Int

Total number of Segments.

-> (a -> Int)

Get the segment number for this element.

-> Array lSrc a

Source values.

-> Array N (Array lDst a) 

Like partition but use the provided function to compute the segment number for each element.

partitionByIx Source #

Arguments

:: (BulkI lSrc a, Target lDst a, Index lDst ~ Int, Elt a) 
=> Name lDst

Name of destination layout.

-> Int

Total number of Segments.

-> (Int -> a -> Int)

Get the segment number for this element.

-> Array lSrc a

Source values.

-> Array N (Array lDst a) 

Like partition but use the provided function to compute the segment number for each element. The function is given the index of the each element, along with the element itself.

Concatenation

concats :: Array N (Array N (Array l a)) -> Array N (Array l a) Source #

Segmented concatenation. Concatenate triply nested vector, producing a doubly nested vector.

  • Unlike the plain concat function, this operation is performed entirely on the segment descriptors of the nested arrays, and does not require the inner array elements to be copied.
> import Data.Repa.Nice
> nice $ concats $ fromListss U [["red", "green", "blue"], ["grey", "white"], [], ["black"]]
["red","green","blue","grey","white","black"]

Splitting

segment Source #

Arguments

:: (BulkI l a, Unbox a) 
=> (a -> Bool)

Detect the start of a segment.

-> (a -> Bool)

Detect the end of a segment.

-> Array l a

Vector to segment.

-> Array N (Array l a) 

O(len src). Given predicates which detect the start and end of a segment, split an vector into the indicated segments.

segmentOn Source #

Arguments

:: (BulkI l a, Unbox a) 
=> (a -> Bool)

Detect the end of a segment.

-> Array l a

Vector to segment.

-> Array N (Array l a) 

O(len src). Given a terminating value, split an vector into segments.

The result segments do not include the terminator.

> import Data.Repa.Nice
> nice $ segmentOn (== ' ') (fromList U "fresh   fried fish  ") 
["fresh "," "," ","fried ","fish "," "]

dice Source #

Arguments

:: (BulkI l a, Windowable l a, Unbox a) 
=> (a -> Bool)

Detect the start of an inner segment.

-> (a -> Bool)

Detect the end of an inner segment.

-> (a -> Bool)

Detect the start of an outer segment.

-> (a -> Bool)

Detect the end of an outer segment.

-> Array l a

Array to dice.

-> Array N (Array N (Array l a)) 

O(len src). Like segment, but cut the source array twice.

diceSep Source #

Arguments

:: (BulkI l a, Eq a) 
=> a

Terminating element for inner segments.

-> a

Terminating element for outer segments.

-> Array l a

Vector to dice.

-> Array N (Array N (Array l a)) 

O(len src). Given field and row terminating values, split an array into rows and fields.

Trimming

trims :: BulkI l a => (a -> Bool) -> Array N (Array l a) -> Array N (Array l a) Source #

For each segment of a nested array, trim elements off the start and end of the segment that match the given predicate.

trimEnds :: BulkI l a => (a -> Bool) -> Array N (Array l a) -> Array N (Array l a) Source #

For each segment of a nested array, trim elements off the end of the segment that match the given predicate.

trimStarts :: BulkI l a => (a -> Bool) -> Array N (Array l a) -> Array N (Array l a) Source #

For each segment of a nested array, trim elements off the start of the segment that match the given predicate.

Transpose

ragspose3 :: Array N (Array N (Array l a)) -> Array N (Array N (Array l a)) Source #

Ragged transpose of a triply nested array.

  • This operation is performed entirely on the segment descriptors of the nested arrays, and does not require the inner array elements to be copied.