2 % (c) The AQUA Project, Glasgow University, 1994-1996
5 \section[Array]{Module @Array@}
8 {-# OPTIONS -fno-implicit-prelude #-}
11 module Ix, -- export all of Ix
12 Array, -- Array type is abstract
14 array, listArray, (!), bounds, indices, elems, assocs,
15 accumArray, (//), accum, ixmap
21 import PrelArr -- Most of the hard work is done here
29 %*********************************************************
31 \subsection{Definitions of array, !, bounds}
33 %*********************************************************
37 #ifdef USE_FOLDR_BUILD
38 {-# INLINE indices #-}
43 {-# SPECIALISE listArray :: (Int,Int) -> [b] -> Array Int b #-}
44 listArray :: (Ix a) => (a,a) -> [b] -> Array a b
45 listArray b vs = array b (zipWith (\ a b -> (a,b)) (range b) vs)
47 {-# SPECIALISE indices :: Array Int b -> [Int] #-}
48 indices :: (Ix a) => Array a b -> [a]
49 indices = range . bounds
51 {-# SPECIALISE elems :: Array Int b -> [b] #-}
52 elems :: (Ix a) => Array a b -> [b]
53 elems a = [a!i | i <- indices a]
55 {-# SPECIALISE assocs :: Array Int b -> [(Int,b)] #-}
56 assocs :: (Ix a) => Array a b -> [(a,b)]
57 assocs a = [(i, a!i) | i <- indices a]
59 {-# SPECIALISE amap :: (b -> c) -> Array Int b -> Array Int c #-}
60 amap :: (Ix a) => (b -> c) -> Array a b -> Array a c
61 amap f a = array b [(i, f (a!i)) | i <- range b]
64 ixmap :: (Ix a, Ix b) => (a,a) -> (a -> b) -> Array b c -> Array a c
65 ixmap b f a = array b [(i, a ! f i) | i <- range b]
69 %*********************************************************
71 \subsection{Instance declarations for Array type}
73 %*********************************************************
76 instance Ix a => Functor (Array a) where
79 instance (Ix a, Eq b) => Eq (Array a b) where
80 a == a' = assocs a == assocs a'
81 a /= a' = assocs a /= assocs a'
83 instance (Ix a, Ord b) => Ord (Array a b) where
84 compare a b = compare (assocs a) (assocs b)
86 instance (Ix a, Show a, Show b) => Show (Array a b) where
87 showsPrec p a = showParen (p > 9) (
89 shows (bounds a) . showChar ' ' .
91 showList = showList__ (showsPrec 0)
93 instance (Ix a, Read a, Read b) => Read (Array a b) where
94 readsPrec p = readParen (p > 9)
95 (\r -> [(array b as, u) | ("array",s) <- lex r,
98 readList = readList__ (readsPrec 0)