@@ -121,6 +121,8 @@ module Streamly.Internal.Data.Array.Foreign
121 -- , foldtWith
121 -- , foldtWith
122 -- , foldbWith
122 -- , foldbWith
124 -- * Immutable Transformations
126 -- * Immutable Transformations
125 , streamTransform
127 , streamTransform
@@ -149,6 +151,7 @@ import Data.Word (Word8)
149-- import Data.Functor.Identity (Identity)
151-- import Data.Functor.Identity (Identity)
150import Foreign.C.String (CString)
152import Foreign.C.String (CString)
151import Foreign.ForeignPtr (withForeignPtr, castForeignPtr)
153import Foreign.ForeignPtr (withForeignPtr, castForeignPtr)
154+import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
152import Foreign.Ptr (plusPtr, castPtr)
155import Foreign.Ptr (plusPtr, castPtr)
153import Foreign.Storable (Storable(..))
156import Foreign.Storable (Storable(..))
154import Prelude hiding (length, null, last, map, (!!), read, concat)
157import Prelude hiding (length, null, last, map, (!!), read, concat)
@@ -175,6 +178,15 @@ import qualified Streamly.Internal.Data.Unfold as Unfold
175import qualified Streamly.Internal.Data.Producer.Type as Producer
178import qualified Streamly.Internal.Data.Producer.Type as Producer
176import qualified Streamly.Internal.Ring.Foreign as RB
179import qualified Streamly.Internal.Ring.Foreign as RB
181+#if MIN_VERSION_base(4,10,0)
182+import Foreign.ForeignPtr (plusForeignPtr)
184+import GHC.Base (Int(..), plusAddr#)
185+import GHC.ForeignPtr (ForeignPtr(..))
186+plusForeignPtr :: ForeignPtr a -> Int -> ForeignPtr b
187+plusForeignPtr (ForeignPtr addr c) (I# d) = ForeignPtr (plusAddr# addr d) c
178-------------------------------------------------------------------------------
190-------------------------------------------------------------------------------
179-- Construction
191-- Construction
180-------------------------------------------------------------------------------
192-------------------------------------------------------------------------------
@@ -334,9 +346,27 @@ findIndices = undefined
334-- Slice and splice
346-- Slice and splice
335-------------------------------------------------------------------------------
347-------------------------------------------------------------------------------
349+-- | /O(1)/ Slice an array in constant time.
351+-- Caution: The bounds of the slice are not checked.
356+{-# INLINE unsafeSlice #-}
358+ forall a. Storable a
359+ => Int -- ^ starting index
360+ -> Int -- ^ length of the slice
363+unsafeSlice start len (Array fp _) =
364+ let size = sizeOf (undefined :: a)
365+ fp1 = fp `plusForeignPtr` (start * size)
366+ end = unsafeForeignPtrToPtr fp1 `plusPtr` (len * size)
338−slice :: Int -> Int -> Array a
339−slice begin end arr = undefined
341splitAt :: Int -> Array a -> (Array a, Array a)
371splitAt :: Int -> Array a -> (Array a, Array a)
342splitAt i arr = undefined
372splitAt i arr = undefined