on master help sign in
text/Data/Text/Array.hs
246 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 qualified
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 unsafeFreezeByteArray#, 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 $ \s1# ->
134 case unsafeFreezeByteArray# maBA s1# of
135 (# s2#, ba# #) -> (# s2#, Array ba#
136#if defined(ASSERTS)
137 maLen
138#endif
139 #)
140{-# INLINE unsafeFreeze #-}
142-- | Indicate how many bytes would be used for an array of the given
143-- size.
144bytesInArray :: Int -> Int
145bytesInArray n = n `shiftL` 1
146{-# INLINE bytesInArray #-}
148-- | Unchecked read of an immutable array. May return garbage or
149-- crash on an out-of-bounds access.
150unsafeIndex :: Array -> Int -> Word16
151unsafeIndex Array{..} i@(I# i#) =
152 CHECK_BOUNDS("unsafeIndex",aLen,i)
153 case indexWord16Array# aBA i# of r# -> (W16# r#)
154{-# INLINE unsafeIndex #-}
156-- | Unchecked write of a mutable array. May return garbage or crash
157-- on an out-of-bounds access.
158unsafeWrite :: MArray s -> Int -> Word16 -> ST s ()
159unsafeWrite MArray{..} i@(I# i#) (W16# e#) = ST $ \s1# ->
160 CHECK_BOUNDS("unsafeWrite",maLen,i)
161 case writeWord16Array# maBA i# e# s1# of
162 s2# -> (# s2#, () #)
163{-# INLINE unsafeWrite #-}
165-- | Convert an immutable array to a list.
166toList :: Array -> Int -> Int -> [Word16]
167toList ary off len = loop 0
168 where loop i | i < len = unsafeIndex ary (off+i) : loop (i+1)
169 | otherwise = []
171-- | An empty immutable array.
172empty :: Array
173empty = runST (new 0 >>= unsafeFreeze)
175-- | Run an action in the ST monad and return an immutable array of
176-- its result.
177run :: (forall s. ST s (MArray s)) -> Array
178run k = runST (k >>= unsafeFreeze)
180-- | Run an action in the ST monad and return an immutable array of
181-- its result paired with whatever else the action returns.
182run2 :: (forall s. ST s (MArray s, a)) -> (Array, a)
183run2 k = runST (do
184 (marr,b) <- k
185 arr <- unsafeFreeze marr
186 return (arr,b))
187{-# INLINE run2 #-}
189-- | Copy some elements of a mutable array.
190copyM :: MArray s -- ^ Destination
191 -> Int -- ^ Destination offset
192 -> MArray s -- ^ Source
193 -> Int -- ^ Source offset
194 -> Int -- ^ Count
195 -> ST s ()
196copyM dest didx src sidx count
197 | count <= 0 = return ()
198 | otherwise =
199#if defined(ASSERTS)
200 assert (sidx + count <= length src) .
201 assert (didx + count <= length dest) .
202#endif
203 unsafeIOToST $ memcpyM (maBA dest) (fromIntegral didx)
204 (maBA src) (fromIntegral sidx)
205 (fromIntegral count)
206{-# INLINE copyM #-}
208-- | Copy some elements of an immutable array.
209copyI :: MArray s -- ^ Destination
210 -> Int -- ^ Destination offset
211 -> Array -- ^ Source
212 -> Int -- ^ Source offset
213 -> Int -- ^ First offset in destination /not/ to
214 -- copy (i.e. /not/ length)
215 -> ST s ()
216copyI dest i0 src j0 top
217 | i0 >= top = return ()
218 | otherwise = unsafeIOToST $
219 memcpyI (maBA dest) (fromIntegral i0)
220 (aBA src) (fromIntegral j0)
221 (fromIntegral (top-i0))
222{-# INLINE copyI #-}
224-- | Compare portions of two arrays for equality. No bounds checking
225-- is performed.
226equal :: Array -- ^ First
227 -> Int -- ^ Offset into first
228 -> Array -- ^ Second
229 -> Int -- ^ Offset into second
230 -> Int -- ^ Count
231 -> Bool
232equal arrA offA arrB offB count = inlinePerformIO $ do
233 i <- memcmp (aBA arrA) (fromIntegral offA)
234 (aBA arrB) (fromIntegral offB) (fromIntegral count)
235 return $! i == 0
236{-# INLINE equal #-}
238foreign import ccall unsafe "_hs_text_memcpy" memcpyI
239 :: MutableByteArray# s -> CSize -> ByteArray# -> CSize -> CSize -> IO ()
241foreign import ccall unsafe "_hs_text_memcmp" memcmp
242 :: ByteArray# -> CSize -> ByteArray# -> CSize -> CSize -> IO CInt
244foreign import ccall unsafe "_hs_text_memcpy" memcpyM
245 :: MutableByteArray# s -> CSize -> MutableByteArray# s -> CSize -> CSize
246 -> IO ()