Safe Haskell | None |
---|---|
Language | Haskell98 |
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.
A
-- Type directed automatic layout.F
-- Foreign memory buffers.N
-- Nested arrays.B
-- Boxed vectors, via Data.Vector.BoxedU
-- Adaptive unboxed vectors, via Data.Vector.Unboxed
- type Material l a = (Bulk l a, Windowable l a, Target l a)
- data A = Auto {
- autoLength :: Int
- data F = Foreign {
- foreignLength :: Int
- fromForeignPtr :: Storable a => Int -> ForeignPtr a -> Array F a
- toForeignPtr :: Storable a => Array F a -> (Int, Int, ForeignPtr a)
- fromByteString :: ByteString -> Array F Word8
- toByteString :: Array F Word8 -> ByteString
- fromStorableVector :: Vector a -> Array F a
- toStorableVector :: Array F a -> Vector a
- data N = Nested {
- nestedLength :: !Int
- fromLists :: TargetI l a => Name l -> [[a]] -> Array N (Array l a)
- fromListss :: TargetI l a => Name l -> [[[a]]] -> Array N (Array N (Array l a))
- data B = Boxed {
- boxedLength :: !Int
- fromBoxed :: Vector a -> Array B a
- toBoxed :: Array B a -> Vector a
- data U = Unboxed {
- unboxedLength :: !Int
- class (Vector Vector a, MVector MVector a) => Unbox a
- fromUnboxed :: Vector a -> Array U a
- toUnboxed :: Array U a -> Vector a
- mapElems :: (Array l1 a -> Array l2 b) -> Array N (Array l1 a) -> Array N (Array l2 b)
- decimate :: (a -> a -> Bool) -> Array B a -> Array B a
- slices :: Array F Int -> Array F Int -> Array l a -> Array N (Array l a)
- partition :: (BulkI lSrc (Int, a), Target lDst a, Index lDst ~ Int, Elt a) => Name lDst -> Int -> Array lSrc (Int, a) -> Array N (Array lDst a)
- partitionBy :: (BulkI lSrc a, Target lDst a, Index lDst ~ Int, Elt a) => Name lDst -> Int -> (a -> Int) -> Array lSrc a -> Array N (Array lDst a)
- partitionByIx :: (BulkI lSrc a, Target lDst a, Index lDst ~ Int, Elt a) => Name lDst -> Int -> (Int -> a -> Int) -> Array lSrc a -> Array N (Array lDst a)
- concats :: Array N (Array N (Array l a)) -> Array N (Array l a)
- segment :: (BulkI l a, Unbox a) => (a -> Bool) -> (a -> Bool) -> Array l a -> Array N (Array l a)
- segmentOn :: (BulkI l a, Unbox a) => (a -> Bool) -> Array l a -> Array N (Array l a)
- dice :: (BulkI l a, Windowable l a, Unbox a) => (a -> Bool) -> (a -> Bool) -> (a -> Bool) -> (a -> Bool) -> Array l a -> Array N (Array N (Array l a))
- diceSep :: (BulkI l a, Eq a) => a -> a -> Array l a -> Array N (Array N (Array l a))
- trims :: BulkI l a => (a -> Bool) -> Array N (Array l a) -> Array N (Array l a)
- trimEnds :: BulkI l a => (a -> Bool) -> Array N (Array l a) -> Array N (Array l a)
- trimStarts :: BulkI l a => (a -> Bool) -> Array N (Array l a) -> Array N (Array l a)
- ragspose3 :: Array N (Array N (Array l a)) -> Array N (Array N (Array l a))
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
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.
Auto | |
|
Foreign arrays
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.
Eq F Source # | |
Show F Source # | |
Layout F Source # | Foreign arrays. |
Storable a => Bulk F a Source # | Foreign arrays. |
Storable a => Windowable F a Source # | Windowing Foreign arrays. |
Storable a => Target F a Source # | Foreign buffers |
Eq (Name F) Source # | |
Show (Name F) Source # | |
(Eq a, Storable a) => Eq (Array F a) Source # | |
(Storable a, Show a) => Show (Array F a) Source # | |
data Name F Source # | |
type Index F Source # | |
data Array F Source # | |
data Buffer F Source # | |
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
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.
Nested | |
|
Eq N Source # | |
Show N Source # | |
Layout N Source # | Nested arrays. |
(BulkI l a, Windowable l a) => Bulk N (Array l a) Source # | Nested arrays. |
(BulkI l a, Windowable l a) => Windowable N (Array l a) Source # | Windowing Nested arrays. |
(Bulk l a, Target l a, (~) * (Index l) Int) => Target N (Array l a) Source # | |
Eq (Name N) Source # | |
Show (Name N) Source # | |
Show (Array l a) => Show (Array N (Array l a)) Source # | |
data Name N Source # | |
type Index N Source # | |
data Array N (Array l a) Source # | |
data Buffer N (Array l a) Source # | |
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
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.
Boxed | |
|
Eq B Source # | |
Show B Source # | |
Layout B Source # | Boxed arrays. |
Bulk B a Source # | Boxed arrays. |
Windowable B a Source # | Boxed windows. |
Target B a Source # | Boxed buffers. |
Eq (Name B) Source # | |
Show (Name B) Source # | |
Eq a => Eq (Array B a) Source # | |
Show a => Show (Array B a) Source # | |
data Name B Source # | |
type Index B Source # | |
data Array B Source # | |
data Buffer B Source # | |
Unboxed arrays
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.
Unboxed | |
|
Eq U Source # | |
Show U Source # | |
Layout U Source # | Unboxed arrays. |
Unbox a => Bulk U a Source # | Unboxed arrays. |
Unbox a => Windowable U a Source # | Windowing Unboxed arrays. |
Unbox a => Target U a Source # | Unboxed buffers. |
Eq (Name U) Source # | |
Show (Name U) Source # | |
(Unbox a, Eq a) => Eq (Array U a) Source # | |
(Show a, Unbox a) => Show (Array U a) Source # | |
data Name U Source # | |
type Index U Source # | |
data Array U Source # | |
data Buffer U Source # | |
class (Vector Vector a, MVector MVector a) => Unbox a #
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) | |
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
:: 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
:: (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.
:: (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.
:: (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
:: (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.
:: (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 "," "]
:: (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.
:: (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.