on master help sign in
Refactor splitAt, add unsafeSplitAt
commit63f9adfbbacd7bbfd103f9cd3bffb03779b9e272 authorHarendra Kumar <harendra@composewell.com> dateSun 7 Jan 2024, 06:45 (3 years ago) parenta290e6e
1 file changed+24−16
core/src/Streamly/Internal/Data/MutArray/Type.hs+24−16
core/src/Streamly/Internal/Data/MutArray/Type.hs+24−16
@@ -2603,33 +2603,41 @@ breakOn sep arr@MutArray{..} = asUnpinnedPtrUnsafe arr $ \p -> liftIO $ do
26032603 }
26042604 )
26052605
2606+-- | Like 'splitAt' but does not check whether the index is valid.
2607+--
2608+{-# INLINE unsafeSplitAt #-}
2609+unsafeSplitAt :: forall a. Unbox a =>
2610+ Int -> MutArray a -> (MutArray a, MutArray a)
2611+unsafeSplitAt i MutArray{..} =
2612+ let off = i * SIZE_OF(a)
2613+ p = arrStart + off
2614+ in ( MutArray
2615+ { arrContents = arrContents
2616+ , arrStart = arrStart
2617+ , arrEnd = p
2618+ , arrBound = p
2619+ }
2620+ , MutArray
2621+ { arrContents = arrContents
2622+ , arrStart = p
2623+ , arrEnd = arrEnd
2624+ , arrBound = arrBound
2625+ }
2626+ )
2627+
26062628-- | Create two slices of an array without copying the original array. The
26072629-- specified index @i@ is the first index of the second slice.
26082630--
26092631{-# INLINE splitAt #-}
26102632splitAt :: forall a. Unbox a => Int -> MutArray a -> (MutArray a, MutArray a)
2611−splitAt i arr@MutArray{..} =
2633+splitAt i arr =
26122634 let maxIndex = length arr - 1
26132635 in if i < 0
26142636 then error "sliceAt: negative array index"
26152637 else if i > maxIndex
26162638 then error $ "sliceAt: specified array index " ++ show i
26172639 ++ " is beyond the maximum index " ++ show maxIndex
2618− else let off = i * SIZE_OF(a)
2619− p = arrStart + off
2620− in ( MutArray
2621− { arrContents = arrContents
2622− , arrStart = arrStart
2623− , arrEnd = p
2624− , arrBound = p
2625− }
2626− , MutArray
2627− { arrContents = arrContents
2628− , arrStart = p
2629− , arrEnd = arrEnd
2630− , arrBound = arrBound
2631− }
2632− )
2640+ else unsafeSplitAt i arr
26332641
26342642-------------------------------------------------------------------------------
26352643-- Casting