1{-# LANGUAGE BangPatterns, CPP, MagicHash, Rank2Types, RecordWildCards,
3{-# OPTIONS_GHC -fno-warn-unused-matches #-}
5-- Module : Data.Text.Array
6-- Copyright : (c) 2009, 2010 Bryan O'Sullivan
9-- Maintainer : bos@serpentine.com, rtomharper@googlemail.com,
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
49-- This fugly hack is brought by GHC's apparent reluctance to deal
50-- with MagicHash and UnboxedTuples when inferring types. Eek!
51# define CHECK_BOUNDS(_func_,_len_,_k_) \
52if (_k_) < 0 || (_k_) >= (_len_) then error ("Data.Text.Array." ++ (_func_) ++ ": bounds error, offset " ++ show (_k_) ++ ", length " ++ show (_len_)) else
54# define CHECK_BOUNDS(_func_,_len_,_k_)
60import Control.Exception (assert)
62import Data.Bits ((.&.))
63import Data.Text.UnsafeShift (shiftL, shiftR)
64import GHC.Base (ByteArray#, MutableByteArray#, Int(..),
65 indexWord16Array#, indexWordArray#, newByteArray#,
66 readWord16Array#, readWordArray#, unsafeCoerce#,
67 writeWord16Array#, writeWordArray#)
68import GHC.ST (ST(..), runST)
69import GHC.Word (Word16(..), Word(..))
70import Prelude hiding (length, read)
72-- | Immutable array type.
76 , aLen :: {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
80-- | Mutable array type, for use in the ST monad.
81data MArray s = MArray {
82 maBA :: MutableByteArray# s
84 , maLen :: {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
89-- | Operations supported by all arrays.
91 -- | Return the length of an array.
94instance IArray Array where
98instance IArray (MArray s) where
100 {-# INLINE length #-}
103-- | Create an uninitialized mutable array.
104unsafeNew :: forall s. Int -> ST s (MArray s)
110 case bytesInArray n of
113 if len < 0 then error (show ("unsafeNew",len)) else
115 case newByteArray# len# s1# of
116 (# s2#, marr# #) -> (# s2#, MArray marr#
121{-# INLINE unsafeNew #-}
123-- | Freeze a mutable array. Do not mutate the 'MArray' afterwards!
124unsafeFreeze :: MArray s -> ST s Array
125unsafeFreeze MArray{..} = ST $ \s# ->
126 (# s#, Array (unsafeCoerce# maBA)
131{-# INLINE unsafeFreeze #-}
133-- | Indicate how many bytes would be used for an array of the given
135bytesInArray :: Int -> Int
136bytesInArray n = n `shiftL` 1
137{-# INLINE bytesInArray #-}
139-- | Unchecked read of an immutable array. May return garbage or
140-- crash on an out-of-bounds access.
141unsafeIndex :: Array -> Int -> Word16
142unsafeIndex Array{..} i@(I# i#) =
143 CHECK_BOUNDS("unsafeIndex",aLen,i)
144 case indexWord16Array# aBA i# of r# -> (W16# r#)
145{-# INLINE unsafeIndex #-}
147-- | Unchecked read of an immutable array. May return garbage or
148-- crash on an out-of-bounds access.
149unsafeIndexWord :: Array -> Int -> Word
150unsafeIndexWord Array{..} i@(I# i#) =
151 CHECK_BOUNDS("unsafeIndexWord",aLen`div`wordFactor,i)
152 case indexWordArray# aBA i# of r# -> (W# r#)
153{-# INLINE unsafeIndexWord #-}
155-- | Unchecked read of a mutable array. May return garbage or
156-- crash on an out-of-bounds access.
157unsafeRead :: MArray s -> Int -> ST s Word16
158unsafeRead MArray{..} i@(I# i#) = ST $ \s# ->
159 CHECK_BOUNDS("unsafeRead",maLen,i)
160 case readWord16Array# maBA i# s# of
161 (# s2#, r# #) -> (# s2#, W16# r# #)
162{-# INLINE unsafeRead #-}
164-- | Unchecked write of a mutable array. May return garbage or crash
165-- on an out-of-bounds access.
166unsafeWrite :: MArray s -> Int -> Word16 -> ST s ()
167unsafeWrite MArray{..} i@(I# i#) (W16# e#) = ST $ \s1# ->
168 CHECK_BOUNDS("unsafeWrite",maLen,i)
169 case writeWord16Array# maBA i# e# s1# of
171{-# INLINE unsafeWrite #-}
173-- | Unchecked read of a mutable array. May return garbage or
174-- crash on an out-of-bounds access.
175unsafeReadWord :: MArray s -> Int -> ST s Word
176unsafeReadWord MArray{..} i@(I# i#) = ST $ \s# ->
177 CHECK_BOUNDS("unsafeRead64",maLen`div`wordFactor,i)
178 case readWordArray# maBA i# s# of
179 (# s2#, r# #) -> (# s2#, W# r# #)
180{-# INLINE unsafeReadWord #-}
182-- | Unchecked write of a mutable array. May return garbage or crash
183-- on an out-of-bounds access.
184unsafeWriteWord :: MArray s -> Int -> Word -> ST s ()
185unsafeWriteWord MArray{..} i@(I# i#) (W# e#) = ST $ \s1# ->
186 CHECK_BOUNDS("unsafeWriteWord",maLen`div`wordFactor,i)
187 case writeWordArray# maBA i# e# s1# of
189{-# INLINE unsafeWriteWord #-}
191-- | Convert an immutable array to a list.
192toList :: Array -> Int -> Int -> [Word16]
193toList ary off len = loop 0
194 where loop i | i < len = unsafeIndex ary (off+i) : loop (i+1)
197-- | An empty immutable array.
199empty = runST (unsafeNew 0 >>= unsafeFreeze)
201-- | Run an action in the ST monad and return an immutable array of
203run :: (forall s. ST s (MArray s)) -> Array
204run k = runST (k >>= unsafeFreeze)
206-- | Run an action in the ST monad and return an immutable array of
207-- its result paired with whatever else the action returns.
208run2 :: (forall s. ST s (MArray s, a)) -> (Array, a)
211 arr <- unsafeFreeze marr
214-- | The amount to divide or multiply by to switch between units of
215-- 'Word16' and units of 'Word'.
217wordFactor = SIZEOF_HSWORD `shiftR` 1
219-- | Indicate whether an offset is word-aligned.
220wordAligned :: Int -> Bool
221wordAligned i = i .&. (wordFactor - 1) == 0
223-- | Copy some elements of a mutable array.
224copyM :: MArray s -- ^ Destination
225 -> Int -- ^ Destination offset
226 -> MArray s -- ^ Source
227 -> Int -- ^ Source offset
230copyM dest didx src sidx count =
232 assert (sidx + count <= length src) .
233 assert (didx + count <= length dest) $
235 if srem == 0 && drem == 0
239 (swidx,srem) = sidx `divMod` wordFactor
240 (dwidx,drem) = didx `divMod` wordFactor
241 nwds = count `div` wordFactor
243 | i >= nwds = slow_loop (i * wordFactor)
244 | otherwise = do w <- unsafeReadWord src (swidx+i)
245 unsafeWriteWord dest (dwidx+i) w
248 | i >= count= return ()
249 | otherwise = do unsafeRead src (sidx+i) >>= unsafeWrite dest (didx+i)
252-- | Copy some elements of an immutable array.
253copyI :: MArray s -- ^ Destination
254 -> Int -- ^ Destination offset
256 -> Int -- ^ Source offset
257 -> Int -- ^ First offset in source /not/ to
258 -- copy (i.e. /not/ length)
260copyI dest i0 src j0 top
261 | wordAligned i0 && wordAligned j0 = fast (i0 `div` wordFactor) (j0 `div` wordFactor)
262 | otherwise = slow i0 j0
264 topwds = top `div` wordFactor
266 | i >= topwds = slow (i * wordFactor) (j * wordFactor)
267 | otherwise = do unsafeWriteWord dest i (src `unsafeIndexWord` j)
270 | i >= top = return ()
271 | otherwise = do unsafeWrite dest i (src `unsafeIndex` j)
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