foundation-0.0.13: Alternative prelude with batteries and no dependencies

LicenseBSD-style
MaintainerHaskell Foundation
Safe HaskellNone
LanguageHaskell2010

Foundation.Primitive.Block

Contents

Description

A block of memory that contains elements of a type, very similar to an unboxed array but with the key difference:

  • It doesn't have slicing capability (no cheap take or drop)
  • It consume less memory: 1 Offset, 1 CountOf
  • It's unpackable in any constructor
  • It uses unpinned memory by default

Synopsis

Documentation

data Block ty

A block of memory containing unpacked bytes representing values of type ty

Constructors

Block ByteArray# 

Instances

PrimType ty => IsList (Block ty) 
(PrimType ty, Eq ty) => Eq (Block ty) 
Data ty => Data (Block ty) 
(PrimType ty, Ord ty) => Ord (Block ty) 
(PrimType ty, Show ty) => Show (Block ty) 
PrimType ty => Monoid (Block ty) 
NormalForm (Block ty) 
PrimType ty => Fold1able (Block ty) 
PrimType ty => Foldable (Block ty) 
PrimType ty => IndexedCollection (Block ty) 
PrimType ty => Collection (Block ty) 
PrimType ty => Sequential (Block ty) 
PrimType ty => Copy (Block ty) 
type Item (Block ty) = ty 
type Element (Block ty) = ty 

data MutableBlock ty st

A Mutable block of memory containing unpacked bytes representing values of type ty

Constructors

MutableBlock (MutableByteArray# st) 

Instances

Properties

length :: forall ty. PrimType ty => Block ty -> CountOf ty

Lowlevel functions

unsafeThaw :: (PrimType ty, PrimMonad prim) => Block ty -> prim (MutableBlock ty (PrimState prim))

Thaw an immutable block.

If the immutable block is modified, then the original immutable block will be modified too, but lead to unexpected results when querying

unsafeFreeze :: PrimMonad prim => MutableBlock ty (PrimState prim) -> prim (Block ty)

Freeze a mutable block into a block.

If the mutable block is still use after freeze, then the modification will be reflected in an unexpected way in the Block.

unsafeIndex :: forall ty. PrimType ty => Block ty -> Offset ty -> ty

Return the element at a specific index from an array without bounds checking.

Reading from invalid memory can return unpredictable and invalid values. use index if unsure.

thaw :: (PrimMonad prim, PrimType ty) => Block ty -> prim (MutableBlock ty (PrimState prim))

Thaw a Block into a MutableBlock

the Block is not modified, instead a new Mutable Block is created and its content is copied to the mutable block

freeze :: (PrimType ty, PrimMonad prim) => MutableBlock ty (PrimState prim) -> prim (Block ty)

copy :: PrimType ty => Block ty -> Block ty

Copy every cells of an existing Block to a new Block

safer api

create

Arguments

:: PrimType ty 
=> CountOf ty

the size of the block (in element of ty)

-> (Offset ty -> ty)

the function that set the value at the index

-> Block ty

the array created

Create a new array of size n by settings each cells through the function f.

isPinned :: Block ty -> PinnedStatus

isMutablePinned :: MutableBlock s ty -> PinnedStatus

singleton :: PrimType ty => ty -> Block ty

replicate :: PrimType ty => CountOf ty -> ty -> Block ty

index :: PrimType ty => Block ty -> Offset ty -> ty

Return the element at a specific index from an array.

If the index @n is out of bounds, an error is raised.

map :: (PrimType a, PrimType b) => (a -> b) -> Block a -> Block b

Map all element a from a block to a new block of b

foldl' :: PrimType ty => (a -> ty -> a) -> a -> Block ty -> a

foldr :: PrimType ty => (ty -> a -> a) -> a -> Block ty -> a

foldl1' :: PrimType ty => (ty -> ty -> ty) -> NonEmpty (Block ty) -> ty

foldr1 :: PrimType ty => (ty -> ty -> ty) -> NonEmpty (Block ty) -> ty

cons :: PrimType ty => ty -> Block ty -> Block ty

snoc :: PrimType ty => Block ty -> ty -> Block ty

uncons :: PrimType ty => Block ty -> Maybe (ty, Block ty)

unsnoc :: PrimType ty => Block ty -> Maybe (Block ty, ty)

sub :: PrimType ty => Block ty -> Offset ty -> Offset ty -> Block ty

splitAt :: PrimType ty => CountOf ty -> Block ty -> (Block ty, Block ty)

revSplitAt :: PrimType ty => CountOf ty -> Block ty -> (Block ty, Block ty)

splitOn :: PrimType ty => (ty -> Bool) -> Block ty -> [Block ty]

break :: PrimType ty => (ty -> Bool) -> Block ty -> (Block ty, Block ty)

span :: PrimType ty => (ty -> Bool) -> Block ty -> (Block ty, Block ty)

elem :: PrimType ty => ty -> Block ty -> Bool

all :: PrimType ty => (ty -> Bool) -> Block ty -> Bool

any :: PrimType ty => (ty -> Bool) -> Block ty -> Bool

find :: PrimType ty => (ty -> Bool) -> Block ty -> Maybe ty

filter :: PrimType ty => (ty -> Bool) -> Block ty -> Block ty

reverse :: forall ty. PrimType ty => Block ty -> Block ty

sortBy :: forall ty. PrimType ty => (ty -> ty -> Ordering) -> Block ty -> Block ty

intersperse :: forall ty. PrimType ty => ty -> Block ty -> Block ty

Foreign interfaces

unsafeCopyToPtr

Arguments

:: PrimMonad prim 
=> Block ty

the source block to copy

-> Ptr ty

The destination address where the copy is going to start

-> prim () 

Copy all the block content to the memory starting at the destination address