1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/core/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- $Id: Array.hs,v 1.1 2001/06/28 14:15:02 simonmar Exp $
14 -- Basic non-strict arrays.
16 -----------------------------------------------------------------------------
21 module Data.Ix -- export all of Ix
22 , Array -- Array type is abstract
24 , array -- :: (Ix a) => (a,a) -> [(a,b)] -> Array a b
25 , listArray -- :: (Ix a) => (a,a) -> [b] -> Array a b
26 , (!) -- :: (Ix a) => Array a b -> a -> b
27 , bounds -- :: (Ix a) => Array a b -> (a,a)
28 , indices -- :: (Ix a) => Array a b -> [a]
29 , elems -- :: (Ix a) => Array a b -> [b]
30 , assocs -- :: (Ix a) => Array a b -> [(a,b)]
31 , accumArray -- :: (Ix a) => (b -> c -> b) -> b -> (a,a) -> [(a,c)] -> Array a b
32 , (//) -- :: (Ix a) => Array a b -> [(a,b)] -> Array a b
33 , accum -- :: (Ix a) => (b -> c -> b) -> Array a b -> [(a,c)] -> Array a b
34 , ixmap -- :: (Ix a, Ix b) => (a,a) -> (a -> b) -> Array b c -> Array a b
38 -- Ix a => Functor (Array a)
39 -- (Ix a, Eq b) => Eq (Array a b)
40 -- (Ix a, Ord b) => Ord (Array a b)
41 -- (Ix a, Show a, Show b) => Show (Array a b)
42 -- (Ix a, Read a, Read b) => Read (Array a b)
45 -- Implementation checked wrt. Haskell 98 lib report, 1/99.
51 #ifdef __GLASGOW_HASKELL__
53 import GHC.Arr -- Most of the hard work is done here
54 import GHC.Err ( undefined )
58 INSTANCE_TYPEABLE2(Array,arrayTc,"Array")
61 ------------ HUGS (rest of file) --------------------
62 import PrelPrim ( PrimArray
67 , primUnsafeFreezeArray
75 -- -----------------------------------------------------------------------------
78 data Array ix elt = Array (ix,ix) (PrimArray elt)
80 array :: Ix a => (a,a) -> [(a,b)] -> Array a b
81 array ixs@(ix_start, ix_end) ivs = runST (do
82 { mut_arr <- primNewArray (rangeSize ixs) arrEleBottom
83 ; mapM_ (\ (i,v) -> primWriteArray mut_arr (index ixs i) v) ivs
84 ; arr <- primUnsafeFreezeArray mut_arr
85 ; return (Array ixs arr)
89 arrEleBottom = error "(Array.!): undefined array element"
91 listArray :: Ix a => (a,a) -> [b] -> Array a b
92 listArray b vs = array b (zipWith (\ a b -> (a,b)) (range b) vs)
94 (!) :: Ix a => Array a b -> a -> b
95 (Array bounds arr) ! i = primIndexArray arr (index bounds i)
97 bounds :: Ix a => Array a b -> (a,a)
98 bounds (Array b _) = b
100 indices :: Ix a => Array a b -> [a]
101 indices = range . bounds
103 elems :: Ix a => Array a b -> [b]
104 elems a = [a!i | i <- indices a]
106 assocs :: Ix a => Array a b -> [(a,b)]
107 assocs a = [(i, a!i) | i <- indices a]
109 (//) :: Ix a => Array a b -> [(a,b)] -> Array a b
110 (//) a us = array (bounds a)
111 ([(i,a!i) | i <- indices a \\ [i | (i,_) <- us]]
114 accum :: Ix a => (b -> c -> b) -> Array a b -> [(a,c)] -> Array a b
115 accum f = foldl (\a (i,v) -> a // [(i,f (a!i) v)])
117 accumArray :: Ix a => (b -> c -> b) -> b -> (a,a) -> [(a,c)] -> Array a b
118 accumArray f z b = accum f (array b [(i,z) | i <- range b])
120 ixmap :: (Ix a, Ix b) => (a,a) -> (a -> b) -> Array b c -> Array a c
121 ixmap b f a = array b [(i, a ! f i) | i <- range b]
124 instance (Ix a) => Functor (Array a) where
125 fmap f a = array (bounds a) [(i, f(a!i)) | i <- indices a]
127 instance (Ix a, Eq b) => Eq (Array a b) where
128 a == a' = assocs a == assocs a'
130 instance (Ix a, Ord b) => Ord (Array a b) where
131 a <= a' = assocs a <= assocs a'
134 instance (Ix a, Show a, Show b) => Show (Array a b) where
135 showsPrec p a = showParen (p > 9) (
136 showString "array " .
137 shows (bounds a) . showChar ' ' .
140 instance (Ix a, Read a, Read b) => Read (Array a b) where
141 readsPrec p = readParen (p > 9)
142 (\r -> [(array b as, u) | ("array",s) <- lex r,
145 #endif /* __HUGS__ */