on 24af690 help sign in
text/Data/Text/Array.hs
245 lines 7.8 kB history raw
1{-# LANGUAGE BangPatterns, CPP, ForeignFunctionInterface, MagicHash, Rank2Types,
2 RecordWildCards, UnboxedTuples, UnliftedFFITypes #-}
3{-# OPTIONS_GHC -fno-warn-unused-matches #-}
4-- |
5-- Module : Data.Text.Array
6-- Copyright : (c) 2009, 2010, 2011 Bryan O'Sullivan
7--
8-- License : BSD-style
9-- Maintainer : bos@serpentine.com
10-- Stability : experimental
11-- Portability : portable
12--
13-- Packed, unboxed, heap-resident arrays. Suitable for performance
14-- critical use, both in terms of large data quantities and high
15-- speed.
16--
17-- This module is intended to be imported @qualified@, to avoid name
18-- clashes with "Prelude" functions, e.g.
19--
20-- > import qualified Data.Text.Array as A
21--
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
24-- naming.
25module Data.Text.Array
26 (
27 -- * Types
28 Array(Array, aBA)
29 , MArray(MArray, maBA)
31 -- * Functions
32 , copyM
33 , copyI
34 , empty
35 , equal
36#if defined(ASSERTS)
37 , length
38#endif
39 , run
40 , run2
41 , toList
42 , unsafeFreeze
43 , unsafeIndex
44 , new
45 , unsafeWrite
46 ) where
48#if defined(ASSERTS)
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
53#else
54# define CHECK_BOUNDS(_func_,_len_,_k_)
55#endif
57#include "MachDeps.h"
59#if defined(ASSERTS)
60import Control.Exception (assert)
61#endif
62#if __GLASGOW_HASKELL__ >= 702
63import Control.Monad.ST.Unsafe (unsafeIOToST)
64#else
65import Control.Monad.ST (unsafeIOToST)
66#endif
67import Data.Bits ((.&.), xor)
68import Data.Text.Internal.Unsafe (inlinePerformIO)
69import Data.Text.Internal.Unsafe.Shift (shiftL, shiftR)
70#if __GLASGOW_HASKELL__ >= 703
71import Foreign.C.Types (CInt(CInt), CSize(CSize))
72#else
73import Foreign.C.Types (CInt, CSize)
74#endif
75import GHC.Base (ByteArray#, MutableByteArray#, Int(..),
76 indexWord16Array#, newByteArray#,
77 unsafeCoerce#, writeWord16Array#)
78import GHC.ST (ST(..), runST)
79import GHC.Word (Word16(..))
80import Prelude hiding (length, read)
82-- | Immutable array type.
83data Array = Array {
84 aBA :: ByteArray#
85#if defined(ASSERTS)
86 , aLen :: {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
87#endif
88 }
90-- | Mutable array type, for use in the ST monad.
91data MArray s = MArray {
92 maBA :: MutableByteArray# s
93#if defined(ASSERTS)
94 , maLen :: {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
95#endif
96 }
98#if defined(ASSERTS)
99-- | Operations supported by all arrays.
100class IArray a where
101 -- | Return the length of an array.
102 length :: a -> Int
104instance IArray Array where
105 length = aLen
106 {-# INLINE length #-}
108instance IArray (MArray s) where
109 length = maLen
110 {-# INLINE length #-}
111#endif
113-- | Create an uninitialized mutable array.
114new :: forall s. Int -> ST s (MArray s)
115new n
116 | n < 0 || n .&. highBit /= 0 = array_size_error
117 | otherwise = ST $ \s1# ->
118 case newByteArray# len# s1# of
119 (# s2#, marr# #) -> (# s2#, MArray marr#
120#if defined(ASSERTS)
121 n
122#endif
123 #)
124 where !(I# len#) = bytesInArray n
125 highBit = maxBound `xor` (maxBound `shiftR` 1)
126{-# INLINE new #-}
128array_size_error :: a
129array_size_error = error "Data.Text.Array.new: size overflow"
131-- | Freeze a mutable array. Do not mutate the 'MArray' afterwards!
132unsafeFreeze :: MArray s -> ST s Array
133unsafeFreeze MArray{..} = ST $ \s# ->
134 (# s#, Array (unsafeCoerce# maBA)
135#if defined(ASSERTS)
136 maLen
137#endif
138 #)
139{-# INLINE unsafeFreeze #-}
141-- | Indicate how many bytes would be used for an array of the given
142-- size.
143bytesInArray :: Int -> Int
144bytesInArray n = n `shiftL` 1
145{-# INLINE bytesInArray #-}
147-- | Unchecked read of an immutable array. May return garbage or
148-- crash on an out-of-bounds access.
149unsafeIndex :: Array -> Int -> Word16
150unsafeIndex Array{..} i@(I# i#) =
151 CHECK_BOUNDS("unsafeIndex",aLen,i)
152 case indexWord16Array# aBA i# of r# -> (W16# r#)
153{-# INLINE unsafeIndex #-}
155-- | Unchecked write of a mutable array. May return garbage or crash
156-- on an out-of-bounds access.
157unsafeWrite :: MArray s -> Int -> Word16 -> ST s ()
158unsafeWrite MArray{..} i@(I# i#) (W16# e#) = ST $ \s1# ->
159 CHECK_BOUNDS("unsafeWrite",maLen,i)
160 case writeWord16Array# maBA i# e# s1# of
161 s2# -> (# s2#, () #)
162{-# INLINE unsafeWrite #-}
164-- | Convert an immutable array to a list.
165toList :: Array -> Int -> Int -> [Word16]
166toList ary off len = loop 0
167 where loop i | i < len = unsafeIndex ary (off+i) : loop (i+1)
168 | otherwise = []
170-- | An empty immutable array.
171empty :: Array
172empty = runST (new 0 >>= unsafeFreeze)
174-- | Run an action in the ST monad and return an immutable array of
175-- its result.
176run :: (forall s. ST s (MArray s)) -> Array
177run k = runST (k >>= unsafeFreeze)
179-- | Run an action in the ST monad and return an immutable array of
180-- its result paired with whatever else the action returns.
181run2 :: (forall s. ST s (MArray s, a)) -> (Array, a)
182run2 k = runST (do
183 (marr,b) <- k
184 arr <- unsafeFreeze marr
185 return (arr,b))
186{-# INLINE run2 #-}
188-- | Copy some elements of a mutable array.
189copyM :: MArray s -- ^ Destination
190 -> Int -- ^ Destination offset
191 -> MArray s -- ^ Source
192 -> Int -- ^ Source offset
193 -> Int -- ^ Count
194 -> ST s ()
195copyM dest didx src sidx count
196 | count <= 0 = return ()
197 | otherwise =
198#if defined(ASSERTS)
199 assert (sidx + count <= length src) .
200 assert (didx + count <= length dest) .
201#endif
202 unsafeIOToST $ memcpyM (maBA dest) (fromIntegral didx)
203 (maBA src) (fromIntegral sidx)
204 (fromIntegral count)
205{-# INLINE copyM #-}
207-- | Copy some elements of an immutable array.
208copyI :: MArray s -- ^ Destination
209 -> Int -- ^ Destination offset
210 -> Array -- ^ Source
211 -> Int -- ^ Source offset
212 -> Int -- ^ First offset in destination /not/ to
213 -- copy (i.e. /not/ length)
214 -> ST s ()
215copyI dest i0 src j0 top
216 | i0 >= top = return ()
217 | otherwise = unsafeIOToST $
218 memcpyI (maBA dest) (fromIntegral i0)
219 (aBA src) (fromIntegral j0)
220 (fromIntegral (top-i0))
221{-# INLINE copyI #-}
223-- | Compare portions of two arrays for equality. No bounds checking
224-- is performed.
225equal :: Array -- ^ First
226 -> Int -- ^ Offset into first
227 -> Array -- ^ Second
228 -> Int -- ^ Offset into second
229 -> Int -- ^ Count
230 -> Bool
231equal arrA offA arrB offB count = inlinePerformIO $ do
232 i <- memcmp (aBA arrA) (fromIntegral offA)
233 (aBA arrB) (fromIntegral offB) (fromIntegral count)
234 return $! i == 0
235{-# INLINE equal #-}
237foreign import ccall unsafe "_hs_text_memcpy" memcpyI
238 :: MutableByteArray# s -> CSize -> ByteArray# -> CSize -> CSize -> IO ()
240foreign import ccall unsafe "_hs_text_memcmp" memcmp
241 :: ByteArray# -> CSize -> ByteArray# -> CSize -> CSize -> IO CInt
243foreign import ccall unsafe "_hs_text_memcpy" memcpyM
244 :: MutableByteArray# s -> CSize -> MutableByteArray# s -> CSize -> CSize
245 -> IO ()