Go to the first, previous, next, last section, table of contents.
Primitive arrays are linear, and indexed starting at zero.
The size and indices of a `ByteArray#', `Addr#', and
`MutableByteArray#' are all in bytes. It's up to the program to
calculate the correct byte offset from the start of the array. This
allows a `ByteArray#' to contain a mixture of values of different
type, which is often needed when preparing data for and unpicking
results from C. (Umm... not true of indices... WDP 95/09)
Should we provide some `sizeOfDouble#' constants?
Out-of-range errors on indexing should be caught by the code which
uses the primitive operation; the primitive operations themselves do
not check for out-of-range indexes. The intention is that the
primitive ops compile to one machine instruction or thereabouts.
We use the terms "reading" and "writing" to refer to accessing
mutable arrays (see Section See section Mutable arrays), and
"indexing" to refer to reading a value from an immutable
array.
Immutable byte arrays are straightforward to index (all indices in bytes):
indexCharArray# :: ByteArray# -> Int# -> Char#
indexIntArray# :: ByteArray# -> Int# -> Int#
indexAddrArray# :: ByteArray# -> Int# -> Addr#
indexFloatArray# :: ByteArray# -> Int# -> Float#
indexDoubleArray# :: ByteArray# -> Int# -> Double#
indexCharOffAddr# :: Addr# -> Int# -> Char#
indexIntOffAddr# :: Addr# -> Int# -> Int#
indexFloatOffAddr# :: Addr# -> Int# -> Float#
indexDoubleOffAddr# :: Addr# -> Int# -> Double#
indexAddrOffAddr# :: Addr# -> Int# -> Addr#
-- Get an Addr# from an Addr# offset
The last of these, `indexAddrOffAddr#', extracts an `Addr#' using an offset
from another `Addr#', thereby providing the ability to follow a chain of
C pointers.
Something a bit more interesting goes on when indexing arrays of boxed
objects, because the result is simply the boxed object. So presumably
it should be entered -- we never usually return an unevaluated
object! This is a pain: primitive ops aren't supposed to do
complicated things like enter objects. The current solution is to
return a lifted value, but I don't like it!
indexArray# :: Array# elt -> Int# -> PrelBase.Lift elt -- Yuk!
Go to the first, previous, next, last section, table of contents.