1{-# LANGUAGE BangPatterns, CPP, ForeignFunctionInterface, MagicHash, Rank2Types,
2 RecordWildCards, UnboxedTuples, UnliftedFFITypes #-}
3{-# OPTIONS_GHC -fno-warn-unused-matches #-}
5-- Module : Data.Text.Array
6-- Copyright : (c) 2009, 2010, 2011 Bryan O'Sullivan
9-- Maintainer : bos@serpentine.com
10-- Portability : portable
12-- Packed, unboxed, heap-resident arrays. Suitable for performance
13-- critical use, both in terms of large data quantities and high
16-- This module is intended to be imported @qualified@, to avoid name
17-- clashes with "Prelude" functions, e.g.
19-- > import qualified Data.Text.Array as A
21-- The names in this module resemble those in the 'Data.Array' family
22-- of modules, but are shorter due to the assumption of qualified
28 , MArray(MArray, maBA)
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_)
59import Control.Exception (assert)
61#if __GLASGOW_HASKELL__ >= 702
62import Control.Monad.ST.Unsafe (unsafeIOToST)
64import Control.Monad.ST (unsafeIOToST)
66import Data.Bits ((.&.), xor)
67import Data.Text.Internal.Unsafe (inlinePerformIO)
68import Data.Text.Internal.Unsafe.Shift (shiftL, shiftR)
69#if __GLASGOW_HASKELL__ >= 703
70import Foreign.C.Types (CInt(CInt), CSize(CSize))
72import Foreign.C.Types (CInt, CSize)
74import GHC.Base (ByteArray#, MutableByteArray#, Int(..),
75 indexWord16Array#, newByteArray#,
76 unsafeFreezeByteArray#, writeWord16Array#)
77import GHC.ST (ST(..), runST)
78import GHC.Word (Word16(..))
79import Prelude hiding (length, read)
81-- | Immutable array type.
83-- The 'Array' constructor is exposed since @text-1.1.1.3@
87 , aLen :: {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
91-- | Mutable array type, for use in the ST monad.
93-- The 'MArray' constructor is exposed since @text-1.1.1.3@
94data MArray s = MArray {
95 maBA :: MutableByteArray# s
97 , maLen :: {-# UNPACK #-} !Int -- length (in units of Word16, not bytes)
102-- | Operations supported by all arrays.
104 -- | Return the length of an array.
107instance IArray Array where
109 {-# INLINE length #-}
111instance IArray (MArray s) where
113 {-# INLINE length #-}
116-- | Create an uninitialized mutable array.
117new :: forall s. Int -> ST s (MArray s)
119 | n < 0 || n .&. highBit /= 0 = array_size_error
120 | otherwise = ST $ \s1# ->
121 case newByteArray# len# s1# of
122 (# s2#, marr# #) -> (# s2#, MArray marr#
127 where !(I# len#) = bytesInArray n
128 highBit = maxBound `xor` (maxBound `shiftR` 1)
132array_size_error = error "Data.Text.Array.new: size overflow"
134-- | Freeze a mutable array. Do not mutate the 'MArray' afterwards!
135unsafeFreeze :: MArray s -> ST s Array
136unsafeFreeze MArray{..} = ST $ \s1# ->
137 case unsafeFreezeByteArray# maBA s1# of
138 (# s2#, ba# #) -> (# s2#, Array ba#
143{-# INLINE unsafeFreeze #-}
145-- | Indicate how many bytes would be used for an array of the given
147bytesInArray :: Int -> Int
148bytesInArray n = n `shiftL` 1
149{-# INLINE bytesInArray #-}
151-- | Unchecked read of an immutable array. May return garbage or
152-- crash on an out-of-bounds access.
153unsafeIndex :: Array -> Int -> Word16
154unsafeIndex Array{..} i@(I# i#) =
155 CHECK_BOUNDS("unsafeIndex",aLen,i)
156 case indexWord16Array# aBA i# of r# -> (W16# r#)
157{-# INLINE unsafeIndex #-}
159-- | Unchecked write of a mutable array. May return garbage or crash
160-- on an out-of-bounds access.
161unsafeWrite :: MArray s -> Int -> Word16 -> ST s ()
162unsafeWrite MArray{..} i@(I# i#) (W16# e#) = ST $ \s1# ->
163 CHECK_BOUNDS("unsafeWrite",maLen,i)
164 case writeWord16Array# maBA i# e# s1# of
166{-# INLINE unsafeWrite #-}
168-- | Convert an immutable array to a list.
169toList :: Array -> Int -> Int -> [Word16]
170toList ary off len = loop 0
171 where loop i | i < len = unsafeIndex ary (off+i) : loop (i+1)
174-- | An empty immutable array.
176empty = runST (new 0 >>= unsafeFreeze)
178-- | Run an action in the ST monad and return an immutable array of
180run :: (forall s. ST s (MArray s)) -> Array
181run k = runST (k >>= unsafeFreeze)
183-- | Run an action in the ST monad and return an immutable array of
184-- its result paired with whatever else the action returns.
185run2 :: (forall s. ST s (MArray s, a)) -> (Array, a)
188 arr <- unsafeFreeze marr
192-- | Copy some elements of a mutable array.
193copyM :: MArray s -- ^ Destination
194 -> Int -- ^ Destination offset
195 -> MArray s -- ^ Source
196 -> Int -- ^ Source offset
199copyM dest didx src sidx count
200 | count <= 0 = return ()
203 assert (sidx + count <= length src) .
204 assert (didx + count <= length dest) .
206 unsafeIOToST $ memcpyM (maBA dest) (fromIntegral didx)
207 (maBA src) (fromIntegral sidx)
211-- | Copy some elements of an immutable array.
212copyI :: MArray s -- ^ Destination
213 -> Int -- ^ Destination offset
215 -> Int -- ^ Source offset
216 -> Int -- ^ First offset in destination /not/ to
217 -- copy (i.e. /not/ length)
219copyI dest i0 src j0 top
220 | i0 >= top = return ()
221 | otherwise = unsafeIOToST $
222 memcpyI (maBA dest) (fromIntegral i0)
223 (aBA src) (fromIntegral j0)
224 (fromIntegral (top-i0))
227-- | Compare portions of two arrays for equality. No bounds checking
229equal :: Array -- ^ First
230 -> Int -- ^ Offset into first
232 -> Int -- ^ Offset into second
235equal arrA offA arrB offB count = inlinePerformIO $ do
236 i <- memcmp (aBA arrA) (fromIntegral offA)
237 (aBA arrB) (fromIntegral offB) (fromIntegral count)
241foreign import ccall unsafe "_hs_text_memcpy" memcpyI
242 :: MutableByteArray# s -> CSize -> ByteArray# -> CSize -> CSize -> IO ()
244foreign import ccall unsafe "_hs_text_memcmp" memcmp
245 :: ByteArray# -> CSize -> ByteArray# -> CSize -> CSize -> IO CInt
247foreign import ccall unsafe "_hs_text_memcpy" memcpyM
248 :: MutableByteArray# s -> CSize -> MutableByteArray# s -> CSize -> CSize
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