Copyright | (c) 2015 Dan Doel |
---|---|
License | BSD3 |
Maintainer | libraries@haskell.org |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
Small arrays are boxed (im)mutable arrays.
The underlying structure of the Array
type contains a card table, allowing
segments of the array to be marked as having been mutated. This allows the
garbage collector to only re-traverse segments of the array that have been
marked during certain phases, rather than having to traverse the entire
array.
SmallArray
lacks this table. This means that it takes up less memory and
has slightly faster writes. It is also more efficient during garbage
collection so long as the card table would have a single entry covering the
entire array. These advantages make them suitable for use as arrays that are
known to be small.
The card size is 128, so for uses much larger than that, Array
would likely
be superior.
The underlying type, SmallArray#
, was introduced in GHC 7.10, so prior to
that version, this module simply implements small arrays as Array
.
- data SmallArray a = SmallArray (SmallArray# a)
- data SmallMutableArray s a = SmallMutableArray (SmallMutableArray# s a)
- newSmallArray :: PrimMonad m => Int -> a -> m (SmallMutableArray (PrimState m) a)
- readSmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> m a
- writeSmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> a -> m ()
- copySmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> SmallArray a -> Int -> Int -> m ()
- copySmallMutableArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> SmallMutableArray (PrimState m) a -> Int -> Int -> m ()
- indexSmallArray :: SmallArray a -> Int -> a
- indexSmallArrayM :: Monad m => SmallArray a -> Int -> m a
- indexSmallArray## :: SmallArray a -> Int -> (#a#)
- cloneSmallArray :: SmallArray a -> Int -> Int -> SmallArray a
- cloneSmallMutableArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> Int -> m (SmallMutableArray (PrimState m) a)
- freezeSmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> Int -> Int -> m (SmallArray a)
- unsafeFreezeSmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> m (SmallArray a)
- thawSmallArray :: PrimMonad m => SmallArray a -> Int -> Int -> m (SmallMutableArray (PrimState m) a)
- runSmallArray :: (forall s. ST s (SmallMutableArray s a)) -> SmallArray a
- unsafeThawSmallArray :: PrimMonad m => SmallArray a -> m (SmallMutableArray (PrimState m) a)
- sizeofSmallArray :: SmallArray a -> Int
- sizeofSmallMutableArray :: SmallMutableArray s a -> Int
- smallArrayFromList :: [a] -> SmallArray a
- smallArrayFromListN :: Int -> [a] -> SmallArray a
- mapSmallArray' :: (a -> b) -> SmallArray a -> SmallArray b
- traverseSmallArrayP :: PrimMonad m => (a -> m b) -> SmallArray a -> m (SmallArray b)
Documentation
data SmallArray a Source #
Monad SmallArray Source # | |
Functor SmallArray Source # | |
MonadFix SmallArray Source # | |
Applicative SmallArray Source # | |
Foldable SmallArray Source # | |
Traversable SmallArray Source # | |
Eq1 SmallArray Source # | Since: 0.6.4.0 |
Ord1 SmallArray Source # | Since: 0.6.4.0 |
Read1 SmallArray Source # | Since: 0.6.4.0 |
Show1 SmallArray Source # | Since: 0.6.4.0 |
MonadZip SmallArray Source # | |
Alternative SmallArray Source # | |
MonadPlus SmallArray Source # | |
IsList (SmallArray a) Source # | |
Eq a => Eq (SmallArray a) Source # | |
Data a => Data (SmallArray a) Source # | |
Ord a => Ord (SmallArray a) Source # | Lexicographic ordering. Subject to change between major versions. |
Read a => Read (SmallArray a) Source # | |
Show a => Show (SmallArray a) Source # | |
Semigroup (SmallArray a) Source # | Since: 0.6.3.0 |
Monoid (SmallArray a) Source # | |
PrimUnlifted (SmallArray a) Source # | |
type Item (SmallArray a) Source # | |
data SmallMutableArray s a Source #
Eq (SmallMutableArray s a) Source # | |
(Typeable * s, Typeable * a) => Data (SmallMutableArray s a) Source # | |
PrimUnlifted (SmallMutableArray s a) Source # | |
:: PrimMonad m | |
=> Int | size |
-> a | initial contents |
-> m (SmallMutableArray (PrimState m) a) |
Create a new small mutable array.
:: PrimMonad m | |
=> SmallMutableArray (PrimState m) a | array |
-> Int | index |
-> m a |
Read the element at a given index in a mutable array.
:: PrimMonad m | |
=> SmallMutableArray (PrimState m) a | array |
-> Int | index |
-> a | new element |
-> m () |
Write an element at the given idex in a mutable array.
:: PrimMonad m | |
=> SmallMutableArray (PrimState m) a | destination |
-> Int | destination offset |
-> SmallArray a | source |
-> Int | source offset |
-> Int | length |
-> m () |
Copy a slice of an immutable array into a mutable array.
copySmallMutableArray Source #
:: PrimMonad m | |
=> SmallMutableArray (PrimState m) a | destination |
-> Int | destination offset |
-> SmallMutableArray (PrimState m) a | source |
-> Int | source offset |
-> Int | length |
-> m () |
Copy a slice of one mutable array into another.
:: SmallArray a | array |
-> Int | index |
-> a |
Look up an element in an immutable array.
:: Monad m | |
=> SmallArray a | array |
-> Int | index |
-> m a |
Look up an element in an immutable array.
The purpose of returning a result using a monad is to allow the caller to avoid retaining references to the array. Evaluating the return value will cause the array lookup to be performed, even though it may not require the element of the array to be evaluated (which could throw an exception). For instance:
data Box a = Box a ... f sa = case indexSmallArrayM sa 0 of Box x -> ...
x
is not a closure that references sa
as it would be if we instead
wrote:
let x = indexSmallArray sa 0
And does not prevent sa
from being garbage collected.
Note that Identity
is not adequate for this use, as it is a newtype, and
cannot be evaluated without evaluating the element.
indexSmallArray## :: SmallArray a -> Int -> (#a#) Source #
Read a value from the immutable array at the given index, returning the result in an unboxed unary tuple. This is currently used to implement folds.
:: SmallArray a | source |
-> Int | offset |
-> Int | length |
-> SmallArray a |
Create a copy of a slice of an immutable array.
cloneSmallMutableArray Source #
:: PrimMonad m | |
=> SmallMutableArray (PrimState m) a | source |
-> Int | offset |
-> Int | length |
-> m (SmallMutableArray (PrimState m) a) |
Create a copy of a slice of a mutable array.
:: PrimMonad m | |
=> SmallMutableArray (PrimState m) a | source |
-> Int | offset |
-> Int | length |
-> m (SmallArray a) |
Create an immutable array corresponding to a slice of a mutable array.
This operation copies the portion of the array to be frozen.
unsafeFreezeSmallArray :: PrimMonad m => SmallMutableArray (PrimState m) a -> m (SmallArray a) Source #
Render a mutable array immutable.
This operation performs no copying, so care must be taken not to modify the input array after freezing.
:: PrimMonad m | |
=> SmallArray a | source |
-> Int | offset |
-> Int | length |
-> m (SmallMutableArray (PrimState m) a) |
Create a mutable array corresponding to a slice of an immutable array.
This operation copies the portion of the array to be thawed.
runSmallArray :: (forall s. ST s (SmallMutableArray s a)) -> SmallArray a Source #
unsafeThawSmallArray :: PrimMonad m => SmallArray a -> m (SmallMutableArray (PrimState m) a) Source #
Render an immutable array mutable.
This operation performs no copying, so care must be taken with its use.
sizeofSmallArray :: SmallArray a -> Int Source #
sizeofSmallMutableArray :: SmallMutableArray s a -> Int Source #
smallArrayFromList :: [a] -> SmallArray a Source #
Create a SmallArray
from a list.
smallArrayFromListN :: Int -> [a] -> SmallArray a Source #
Create a SmallArray
from a list of a known length. If the length
of the list does not match the given length, this throws an exception.
mapSmallArray' :: (a -> b) -> SmallArray a -> SmallArray b Source #
Strict map over the elements of the array.
traverseSmallArrayP :: PrimMonad m => (a -> m b) -> SmallArray a -> m (SmallArray b) Source #