1{-# LANGUAGE BangPatterns, CPP, ExistentialQuantification, MagicHash,
2 Rank2Types, ScopedTypeVariables, UnboxedTuples #-}
3{-# OPTIONS_GHC -fno-warn-unused-matches #-}
5-- Module : Data.Text.Array
6-- Copyright : (c) Bryan O'Sullivan 2009
9-- Maintainer : bos@serpentine.com, rtharper@aftereternity.co.uk,
11-- Stability : experimental
12-- Portability : portable
14-- Packed, unboxed, heap-resident arrays. Suitable for performance
15-- critical use, both in terms of large data quantities and high
18-- This module is intended to be imported @qualified@, to avoid name
19-- clashes with "Prelude" functions, e.g.
21-- > import qualified Data.Text.Array as A
23-- The names in this module resemble those in the 'Data.Array' family
24-- of modules, but are shorter due to the assumption of qualifid
47#define BOUNDS_CHECKING
48-- This fugly hack is brought by GHC's apparent reluctance to deal
49-- with MagicHash and UnboxedTuples when inferring types. Eek!
50#define CHECK_BOUNDS(_func_,_len_,_k_) \
51if (_k_) < 0 || (_k_) >= (_len_) then error ("Data.Text.Array." ++ (_func_) ++ ": bounds error, offset " ++ show (_k_) ++ ", length " ++ show (_len_)) else
53#define CHECK_BOUNDS(_func_,_len_,_k_)
56#if defined(__GLASGOW_HASKELL__)
59import GHC.Base (ByteArray#, MutableByteArray#, Int(..), indexIntArray#,
60 indexWord16Array#, newByteArray#, readIntArray#,
61 readWord16Array#, unsafeCoerce#, writeIntArray#,
62 writeWord16Array#, (+#), (*#))
64import GHC.ST (ST(..), runST)
65import GHC.Word (Word16(..))
67#elif defined(__HUGS__)
69import Hugs.ByteArray (ByteArray, MutableByteArray, readByteArray,
70 newMutableByteArray, readMutableByteArray,
71 unsafeFreezeMutableByteArray, writeMutableByteArray)
72import Foreign.Storable (Storable, sizeOf)
73import Hugs.ST (ST(..), runST)
76# error not implemented for this compiler
79import Control.Exception (assert)
80import Data.Typeable (Typeable1(..), Typeable2(..), TyCon, mkTyCon, mkTyConApp)
81import Data.Word (Word16)
82import Prelude hiding (length, read)
86-- | Immutable array type.
88 {-# UNPACK #-} !Int -- length (in units of e, not bytes)
89#if defined(__GLASGOW_HASKELL__)
91#elif defined(__HUGS__)
95INSTANCE_TYPEABLE1(Array,arrayTc,"Array")
97-- | Mutable array type, for use in the ST monad.
98data MArray s e = MArray
99 {-# UNPACK #-} !Int -- length (in units of e, not bytes)
100#if defined(__GLASGOW_HASKELL__)
101 (MutableByteArray# s)
102#elif defined(__HUGS__)
103 !(MutableByteArray s)
106INSTANCE_TYPEABLE2(MArray,mArrayTc,"MArray")
108-- | Operations supported by all arrays.
110 -- | Return the length of an array.
113instance IArray (Array e) where
114 length (Array len _ba) = len
115 {-# INLINE length #-}
117instance (Elt e, Show e) => Show (Array e) where
120instance IArray (MArray s e) where
121 length (MArray len _ba) = len
122 {-# INLINE length #-}
124check :: IArray a => String -> a -> Int -> (a -> Int -> b) -> b
126 | i >= 0 && i < length ary = f ary i
127 | otherwise = error ("Data.Array.Flat." ++ func ++ ": index out of bounds")
130-- | Operations supported by all elements that can be stored in
133 -- | Indicate how many bytes would be used for an array of the
135 bytesInArray :: Int -> e -> Int
136 -- | Unchecked read of an immutable array. May return garbage or
137 -- crash on an out-of-bounds access.
138 unsafeIndex :: Array e -> Int -> e
139 -- | Unchecked read of a mutable array. May return garbage or
140 -- crash on an out-of-bounds access.
141 unsafeRead :: MArray s e -> Int -> ST s e
142 -- | Unchecked write of a mutable array. May return garbage or
143 -- crash on an out-of-bounds access.
144 unsafeWrite :: MArray s e -> Int -> e -> ST s ()
146 -- | Read an immutable array. An invalid index results in a
148 index :: Array e -> Int -> e
149 index ary i = check "index" ary i unsafeIndex
152 -- | Read a mutable array. An invalid index results in a runtime
154 read :: Array e -> Int -> ST s e
155 read ary i = check "read" ary i read
158 -- | Write a mutable array. An invalid index results in a runtime
160 write :: Array e -> Int -> ST s e
161 write ary i = check "write" ary i write
164-- | Freeze a mutable array. Do not mutate the 'MArray' afterwards!
165unsafeFreeze :: MArray s e -> ST s (Array e)
167#if defined(__GLASGOW_HASKELL__)
169wORD16_SCALE :: Int# -> Int#
170wORD16_SCALE n# = scale# *# n# where I# scale# = SIZEOF_WORD16
172-- | Create an uninitialized mutable array.
173unsafeNew :: forall s e. Elt e => Int -> ST s (MArray s e)
174unsafeNew n = assert (n >= 0) . ST $ \s1# ->
175 case bytesInArray n (undefined :: e) of
177#if defined(BOUNDS_CHECKING)
178 if len < 0 then error (show ("unsafeNew",len)) else
180 case newByteArray# len# s1# of
181 (# s2#, marr# #) -> (# s2#, MArray n marr# #)
182{-# INLINE unsafeNew #-}
184unsafeFreeze (MArray len mba#) = ST $ \s# ->
185 (# s#, Array len (unsafeCoerce# mba#) #)
186{-# INLINE unsafeFreeze #-}
188-- | Create a mutable array, with its elements initialized with the
190new :: forall s e. Elt e => Int -> e -> ST s (MArray s e)
192#elif defined(__HUGS__)
194unsafeIndexArray :: Storable e => Array e -> Int -> e
195unsafeIndexArray (Array off len arr) i =
196 assert (i >= 0 && i < len) $ readByteArray arr (off + i)
198unsafeReadMArray :: Storable e => MArray s e -> Int -> ST s e
199unsafeReadMArray (MArray _len marr) i =
200 assert (i >= 0 && i < len) $ readMutableByteArray marr
202unsafeWriteMArray :: Storable e => MArray s e -> Int -> e -> ST s ()
203unsafeWriteMArray (MArray len marr) i =
204 assert (i >= 0 && i < len) $ writeMutableByteArray marr
206-- | Create an uninitialized mutable array.
207unsafeNew :: (Storable e) => Int -> ST s (MArray s e)
208unsafeNew n = new undefined
209 where new :: (Storable e) => e -> ST s (MArray s e)
211 marr <- newMutableByteArray (n * sizeOf unused)
212 return (MArray n marr)
214unsafeFreeze (MArray len mba) = do
215 ba <- unsafeFreezeMutableByteArray mba
216 return (Array 0 len ba)
218-- | Create a mutable array, with its elements initialized with the
220new :: (Storable e) => Int -> e -> ST s (MArray s e)
224 marr <- unsafeNew len
225 sequence_ [unsafeWrite marr i initVal | i <- [0..len-1]]
228instance Elt Word16 where
229#if defined(__GLASGOW_HASKELL__)
231 bytesInArray (I# i#) _ = I# (wORD16_SCALE i#)
232 {-# INLINE bytesInArray #-}
234 unsafeIndex (Array len ba#) i@(I# i#) =
235 CHECK_BOUNDS("unsafeIndex",len,i)
236 case indexWord16Array# ba# i# of r# -> (W16# r#)
237 {-# INLINE unsafeIndex #-}
239 unsafeRead (MArray len mba#) i@(I# i#) = ST $ \s# ->
240 CHECK_BOUNDS("unsafeRead",len,i)
241 case readWord16Array# mba# i# s# of
242 (# s2#, r# #) -> (# s2#, W16# r# #)
243 {-# INLINE unsafeRead #-}
245 unsafeWrite (MArray len marr#) i@(I# i#) (W16# e#) = ST $ \s1# ->
246 CHECK_BOUNDS("unsafeWrite",len,i)
247 case writeWord16Array# marr# i# e# s1# of
249 {-# INLINE unsafeWrite #-}
251#elif defined(__HUGS__)
253 bytesInArray n w = sizeOf w * n
254 unsafeIndex = unsafeIndexArray
255 unsafeRead = unsafeReadMArray
256 unsafeWrite = unsafeWriteMArray
260-- | Convert an immutable array to a list.
261toList :: Elt e => Array e -> [e]
263 where loop i | i < len = unsafeIndex a i : loop (i+1)
267-- | An empty immutable array.
268empty :: Elt e => Array e
269empty = runST (unsafeNew 0 >>= unsafeFreeze)
271-- | Run an action in the ST monad and return an immutable array of
273run :: Elt e => (forall s. ST s (MArray s e)) -> Array e
274run k = runST (k >>= unsafeFreeze)
276-- | Run an action in the ST monad and return an immutable array of
277-- its result paired with whatever else the action returns.
278run2 :: Elt e => (forall s. ST s (MArray s e, a)) -> (Array e, a)
281 arr <- unsafeFreeze marr
284-- | Copy an array in its entirety. The destination array must be at
285-- least as big as the source.
286copy :: Elt e => MArray s e -- ^ source array
287 -> MArray s e -- ^ destination array
290 | length dest >= length src = copy_loop 0
291 | otherwise = fail "Data.Text.Array.copy: array too small"
295 | i >= len = return ()
296 | otherwise = do unsafeRead src i >>= unsafeWrite dest i
300-- | Unsafely copy the elements of an array.
301unsafeCopy :: Elt e =>
302 MArray s e -> Int -> MArray s e -> Int -> Int -> ST s ()
303unsafeCopy src sidx dest didx count =
304 assert (sidx + count <= length src) .
305 assert (didx + count <= length dest) $
306 copy_loop sidx didx 0
309 | c >= count = return ()
310 | otherwise = do unsafeRead src i >>= unsafeWrite dest j
311 copy_loop (i+1) (j+1) (c+1)
312{-# INLINE unsafeCopy #-}
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list