on master help sign in
Refactor splitAt, add unsafeSplitAt
63f9adfHarendra Kumar
core/src/Streamly/Internal/Data/MutArray/Type.hs+24−16
@@ -2603,33 +2603,41 @@ breakOn sep arr@MutArray{..} = asUnpinnedPtrUnsafe arr $ \p -> liftIO $ do
2603 }
2603 }
2604 )
2604 )
2605
2605
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+
2606-- | Create two slices of an array without copying the original array. The
2628-- | Create two slices of an array without copying the original array. The
2607-- specified index @i@ is the first index of the second slice.
2629-- specified index @i@ is the first index of the second slice.
2608--
2630--
2609{-# INLINE splitAt #-}
2631{-# INLINE splitAt #-}
2610splitAt :: forall a. Unbox a => Int -> MutArray a -> (MutArray a, MutArray a)
2632splitAt :: forall a. Unbox a => Int -> MutArray a -> (MutArray a, MutArray a)
2611−splitAt i arr@MutArray{..} =
2633+splitAt i arr =
2612 let maxIndex = length arr - 1
2634 let maxIndex = length arr - 1
2613 in if i < 0
2635 in if i < 0
2614 then error "sliceAt: negative array index"
2636 then error "sliceAt: negative array index"
2615 else if i > maxIndex
2637 else if i > maxIndex
2616 then error $ "sliceAt: specified array index " ++ show i
2638 then error $ "sliceAt: specified array index " ++ show i
2617 ++ " is beyond the maximum index " ++ show maxIndex
2639 ++ " is beyond the maximum index " ++ show maxIndex
2618− else let off = i * SIZE_OF(a)
2640+ else unsafeSplitAt i arr
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− )
2633
2641
2634-------------------------------------------------------------------------------
2642-------------------------------------------------------------------------------
2635-- Casting
2643-- Casting