1{-# LANGUAGE BangPatterns, CPP, MagicHash, Rank2Types, UnboxedTuples #-}
2{-# OPTIONS_GHC -fno-warn-unused-matches #-}
4-- Module : Data.Text.Array
5-- Copyright : (c) 2009, 2010 Bryan O'Sullivan
8-- Maintainer : bos@serpentine.com, rtomharper@googlemail.com,
10-- Stability : experimental
11-- Portability : portable
13-- Packed, unboxed, heap-resident arrays. Suitable for performance
14-- critical use, both in terms of large data quantities and high
17-- This module is intended to be imported @qualified@, to avoid name
18-- clashes with "Prelude" functions, e.g.
20-- > import qualified Data.Text.Array as A
22-- The names in this module resemble those in the 'Data.Array' family
23-- of modules, but are shorter due to the assumption of qualifid
47-- This fugly hack is brought by GHC's apparent reluctance to deal
48-- with MagicHash and UnboxedTuples when inferring types. Eek!
49# define CHECK_BOUNDS(_func_,_len_,_k_) \
50if (_k_) < 0 || (_k_) >= (_len_) then error ("Data.Text.Array." ++ (_func_) ++ ": bounds error, offset " ++ show (_k_) ++ ", length " ++ show (_len_)) else
52# define CHECK_BOUNDS(_func_,_len_,_k_)
58import Control.Exception (assert)
60import Data.Bits ((.&.))
61import Data.Text.UnsafeShift (shiftL, shiftR)
62import GHC.Base (ByteArray#, MutableByteArray#, Int(..),
63 indexWord16Array#, indexWordArray#, newByteArray#,
64 readWord16Array#, readWordArray#, unsafeCoerce#,
65 writeWord16Array#, writeWordArray#)
66import GHC.ST (ST(..), runST)
67import GHC.Word (Word16(..), Word(..))
68import Prelude hiding (length, read)
70-- | Immutable array type.
72 {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
75-- | Mutable array type, for use in the ST monad.
77 {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
80-- | Operations supported by all arrays.
82 -- | Return the length of an array.
85instance IArray Array where
86 length (Array len _ba) = len
89instance IArray (MArray s) where
90 length (MArray len _ba) = len
93-- | Create an uninitialized mutable array.
94unsafeNew :: forall s. Int -> ST s (MArray s)
100 case bytesInArray n of
103 if len < 0 then error (show ("unsafeNew",len)) else
105 case newByteArray# len# s1# of
106 (# s2#, marr# #) -> (# s2#, MArray n marr# #)
107{-# INLINE unsafeNew #-}
109-- | Freeze a mutable array. Do not mutate the 'MArray' afterwards!
110unsafeFreeze :: MArray s -> ST s (Array)
111unsafeFreeze (MArray len mba#) = ST $ \s# ->
112 (# s#, Array len (unsafeCoerce# mba#) #)
113{-# INLINE unsafeFreeze #-}
115-- | Indicate how many bytes would be used for an array of the given
117bytesInArray :: Int -> Int
118bytesInArray n = n `shiftL` 1
119{-# INLINE bytesInArray #-}
121-- | Unchecked read of an immutable array. May return garbage or
122-- crash on an out-of-bounds access.
123unsafeIndex :: Array -> Int -> Word16
124unsafeIndex (Array len ba#) i@(I# i#) =
125 CHECK_BOUNDS("unsafeIndex",len,i)
126 case indexWord16Array# ba# i# of r# -> (W16# r#)
127{-# INLINE unsafeIndex #-}
129-- | Unchecked read of an immutable array. May return garbage or
130-- crash on an out-of-bounds access.
131unsafeIndexWord :: Array -> Int -> Word
132unsafeIndexWord (Array len ba#) i@(I# i#) =
133 CHECK_BOUNDS("unsafeIndexWord",len,i)
134 case indexWordArray# ba# i# of r# -> (W# r#)
135{-# INLINE unsafeIndexWord #-}
137-- | Unchecked read of a mutable array. May return garbage or
138-- crash on an out-of-bounds access.
139unsafeRead :: MArray s -> Int -> ST s Word16
140unsafeRead (MArray len mba#) i@(I# i#) = ST $ \s# ->
141 CHECK_BOUNDS("unsafeRead",len,i)
142 case readWord16Array# mba# i# s# of
143 (# s2#, r# #) -> (# s2#, W16# r# #)
144{-# INLINE unsafeRead #-}
146-- | Unchecked write of a mutable array. May return garbage or crash
147-- on an out-of-bounds access.
148unsafeWrite :: MArray s -> Int -> Word16 -> ST s ()
149unsafeWrite (MArray len marr#) i@(I# i#) (W16# e#) = ST $ \s1# ->
150 CHECK_BOUNDS("unsafeWrite",len,i)
151 case writeWord16Array# marr# i# e# s1# of
153{-# INLINE unsafeWrite #-}
155-- | Unchecked read of a mutable array. May return garbage or
156-- crash on an out-of-bounds access.
157unsafeReadWord :: MArray s -> Int -> ST s Word
158unsafeReadWord (MArray len mba#) i@(I# i#) = ST $ \s# ->
159 CHECK_BOUNDS("unsafeRead64",len,i)
160 case readWordArray# mba# i# s# of
161 (# s2#, r# #) -> (# s2#, W# r# #)
162{-# INLINE unsafeReadWord #-}
164-- | Unchecked write of a mutable array. May return garbage or crash
165-- on an out-of-bounds access.
166unsafeWriteWord :: MArray s -> Int -> Word -> ST s ()
167unsafeWriteWord (MArray len marr#) i@(I# i#) (W# e#) = ST $ \s1# ->
168 CHECK_BOUNDS("unsafeWriteWord",len,i)
169 case writeWordArray# marr# i# e# s1# of
171{-# INLINE unsafeWriteWord #-}
173-- | Convert an immutable array to a list.
174toList :: Array -> [Word16]
176 where loop i | i < len = unsafeIndex a i : loop (i+1)
180-- | An empty immutable array.
182empty = runST (unsafeNew 0 >>= unsafeFreeze)
184-- | Run an action in the ST monad and return an immutable array of
186run :: (forall s. ST s (MArray s)) -> Array
187run k = runST (k >>= unsafeFreeze)
189-- | Run an action in the ST monad and return an immutable array of
190-- its result paired with whatever else the action returns.
191run2 :: (forall s. ST s (MArray s, a)) -> (Array, a)
194 arr <- unsafeFreeze marr
197-- | The amount to divide or multiply by to switch between units of
198-- 'Word16' and units of 'Word'.
200wordFactor = SIZEOF_HSWORD `shiftR` 1
202-- | Indicate whether an offset is word-aligned.
203wordAligned :: Int -> Bool
204wordAligned i = i .&. (wordFactor - 1) == 0
206-- | Copy an array in its entirety. The destination array must be at
207-- least as big as the source.
208copy :: MArray s -- ^ source array
209 -> MArray s -- ^ destination array
211copy dest@(MArray dlen _) src@(MArray slen _)
212 | dlen >= slen = fast_loop 0
213 | otherwise = fail "Data.Text.Array.copy: array too small"
215 nwds = slen `div` wordFactor
217 | i >= nwds = copy_loop (i * wordFactor)
218 | otherwise = do unsafeReadWord src i >>= unsafeWriteWord dest i
221 | i >= slen = return ()
222 | otherwise = do unsafeRead src i >>= unsafeWrite dest i
225-- | Copy some elements of a mutable array.
226partialCopyM :: MArray s -- ^ Destination
227 -> Int -- ^ Destination offset
228 -> MArray s -- ^ Source
229 -> Int -- ^ Source offset
232partialCopyM dest didx src sidx count =
234 assert (sidx + count <= length src) .
235 assert (didx + count <= length dest) $
237 copy_loop sidx didx 0
240 | c >= count = return ()
241 | otherwise = do unsafeRead src i >>= unsafeWrite dest j
242 copy_loop (i+1) (j+1) (c+1)
244-- | Copy some elements of an immutable array.
245partialCopyI :: MArray s -- ^ Destination
246 -> Int -- ^ Destination offset
248 -> Int -- ^ Source offset
249 -> Int -- ^ First offset in source /not/ to
250 -- copy (i.e. /not/ length)
252partialCopyI dest i0 src j0 top
253 | wordAligned i0 && wordAligned j0 = fast (i0 `div` wordFactor) (j0 `div` wordFactor)
254 | otherwise = slow i0 j0
256 topwds = top `div` wordFactor
258 | i >= topwds = slow (i * wordFactor) (j * wordFactor)
259 | otherwise = do unsafeWriteWord dest i (src `unsafeIndexWord` j)
262 | i >= top = return ()
263 | 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