Commit 8bd82b88 authored by qrczak's avatar qrczak
Browse files

[project @ 2001-04-14 22:27:00 by qrczak]

Implementation of arrays rewritten

Bulk operations like listArray, elems, fmap/amap, (==), getElems,
getAssocs, freeze etc. no longer check whether indices which are
not provided by the programmer are in bounds (they always are), and
avoid unnecessary translation between Ix indices and Int indices.
Some operations are implemented more efficiently, most notably (==)
and compare.

This applies to all IArray and MArray instances, including Haskell
98 Arrays.

Old methods of IArray and MArray are now functions; this is the only
change in the interface. New methods are exported only by ArrayBase,
i.e. not officially exported. They work on Int indices and are unsafe:
they don't do bounds checks themselves. Public functions do checks
and index translation instead where necessary.

More is inlined, to ensure that anything worth specialization or list
fusion gets specialized and fused. Perhaps a bit too much is inlined.
If it was possible to say that a function should be instantiated in
other modules for each type but not inlined on each use, it would be
useful here.

Using UArray Int Char wrapped in a nice interface (not included
here) instead of PackedString should be reasonable. PackedStrings
are 10% faster than UArray in a sorting test (and don't support
Unicode). Standard Strings are 50% slower (and take up more memory),
even though other test versions convert input from standard Strings
and convert output to them. ByteArrays tuned by hand for the benchmark
are 15% faster. The same UArray test compiled with released ghc-5.00,
with compare defined in terms of assocs, is 7 times slower.
parent 940d9d97
% -----------------------------------------------------------------------------
% $Id: Array.lhs,v 1.15 2000/11/08 15:54:05 simonpj Exp $
% $Id: Array.lhs,v 1.16 2001/04/14 22:27:00 qrczak Exp $
% (c) The University of Glasgow, 1994-2000
......@@ -38,18 +38,21 @@ module Array
-- Implementation checked wrt. Haskell 98 lib report, 1/99.
) where
#ifndef __HUGS__
------------ GHC --------------------
import Ix
import PrelList
import PrelArr -- Most of the hard work is done here
import PrelBase
------------ End of GHC --------------------
------------ HUGS --------------------
------------ HUGS (rest of file) --------------------
import PrelPrim ( PrimArray
, runST
, primNewArray
......@@ -62,46 +65,15 @@ import Ix
import List( (\\) )
infixl 9 !, //
------------ End of HUGS --------------------
%* *
\subsection{Definitions of array, !, bounds}
\subsection{The Array type}
%* *
#ifndef __HUGS__
------------ GHC --------------------
{-# SPECIALISE listArray :: (Int,Int) -> [b] -> Array Int b #-}
listArray :: (Ix a) => (a,a) -> [b] -> Array a b
listArray b vs = array b (zip (range b) vs)
{-# INLINE elems #-}
elems :: (Ix a) => Array a b -> [b]
elems a = [a!i | i <- indices a]
ixmap :: (Ix a, Ix b) => (a,a) -> (a -> b) -> Array b c -> Array a c
ixmap b f a = array b [(i, a ! f i) | i <- range b]
------------ End of GHC --------------------
%* *
\subsection{Instance declarations for Array type}
%* *
------------ HUGS (rest of file) --------------------
data Array ix elt = Array (ix,ix) (PrimArray elt)
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment