@@ -242,19 +242,38 @@ unsafeRead = Unfold step inject
243 touch r = IO $ \s -> case touch# r s of s' -> (# s', () #)
243 touch r = IO $ \s -> case touch# r s of s' -> (# s', () #)
247+-- >>> import qualified Streamly.Internal.Data.Array.Foreign.Type as Array
245−-- | > null arr = length arr == 0
248+-- >>> null arr = Array.byteLength arr == 0
247-- /Pre-release/
250-- /Pre-release/
248{-# INLINE null #-}
251{-# INLINE null #-}
252+null :: Array a -> Bool
253+null arr = A.byteLength arr == 0
255+-- | Like 'getIndex' but indexes the array in reverse from the end.
258+{-# INLINE getIndexRev #-}
249−null :: Storable a => Array a -> Bool
259+getIndexRev :: forall a. Storable a => Array a -> Int -> Maybe a
250−null arr = length arr == 0
260+getIndexRev arr i =
262+ unsafeWithForeignPtr (aStart arr) $ \ptr -> do
263+ let elemSize = sizeOf (undefined :: a)
264+ elemPtr = aEnd arr `plusPtr` negate (elemSize * (i + 1))
265+ if i >= 0 && elemPtr >= ptr
266+ then Just <$> peek elemPtr
252−-- | > last arr = getIndex arr (length arr - 1)
271+-- >>> last arr = getIndexRev arr 0
254-- /Pre-release/
273-- /Pre-release/
255{-# INLINE last #-}
274{-# INLINE last #-}
256last :: Storable a => Array a -> Maybe a
275last :: Storable a => Array a -> Maybe a
257−last arr = getIndex arr (length arr - 1)
276+last arr = getIndexRev arr 0
259-------------------------------------------------------------------------------
278-------------------------------------------------------------------------------
260-- Folds with Array as the container
279-- Folds with Array as the container
@@ -355,16 +374,22 @@ unsafeSlice start len (Array fp _) =
355-- Random reads and writes
374-- Random reads and writes
356-------------------------------------------------------------------------------
375-------------------------------------------------------------------------------
377+-- XXX Change this to a partial function instead of a Maybe type? And use
378+-- MA.getIndex instead.
358−-- | /O(1)/ Lookup the element at the given index, starting from 0.
380+-- | /O(1)/ Lookup the element at the given index. Index starts from 0.
360-- @since 0.8.0
382-- @since 0.8.0
361{-# INLINE getIndex #-}
383{-# INLINE getIndex #-}
362−getIndex :: Storable a => Array a -> Int -> Maybe a
384+getIndex :: forall a. Storable a => Array a -> Int -> Maybe a
363getIndex arr i =
385getIndex arr i =
364− if i < 0 || i > length arr - 1
366− else unsafeInlineIO $
386+ unsafeInlineIO $
367− unsafeWithForeignPtr (aStart arr) $ \p -> Just <$> peekElemOff p i
387+ unsafeWithForeignPtr (aStart arr) $ \ptr -> do
388+ let elemSize = sizeOf (undefined :: a)
389+ elemPtr = ptr `plusPtr` (elemSize * i)
390+ if i >= 0 && elemPtr `plusPtr` elemSize <= aEnd arr
391+ then Just <$> peek elemPtr
370-- | @readSlice arr i count@ streams a slice of the array @arr@ starting
395-- | @readSlice arr i count@ streams a slice of the array @arr@ starting
@@ -429,6 +454,8 @@ runPipe :: (MonadIO m, Storable a, Storable b)
429runPipe f arr = P.runPipe (toArrayMinChunk (length arr)) $ f (A.read arr)
454runPipe f arr = P.runPipe (toArrayMinChunk (length arr)) $ f (A.read arr)
457+-- XXX Use byteLength instead of length.
432-- | Transform an array into another array using a stream transformation
459-- | Transform an array into another array using a stream transformation
433-- operation.
460-- operation.