on master help sign in
Implement reverse
commit7b8d5c0c6f0561766c24771dde185664485a92f0 authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateMon 27 Dec 2021, 21:05 (5 years ago) parentd1146fb
1 file changed+15−3
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+15−3
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+15−3
@@ -1163,10 +1163,22 @@ getSlice index len (Array contents start e _) =
11631163-- to another array. However, in-place reverse can be useful to take adavantage
11641164-- of cache locality and when you do not want to allocate additional memory.
11651165--
1166−-- /Unimplemented/
1166+-- /Pre-release/
11671167{-# INLINE reverse #-}
1168−reverse :: Array a -> m Bool
1169−reverse = undefined
1168+reverse :: forall m a. (MonadIO m, Storable a) => Array a -> m ()
1169+reverse Array{..} = liftIO $ do
1170+ let l = arrStart
1171+ h = aEnd `plusPtr` (- elemSize)
1172+ in swap l h
1173+
1174+ where
1175+
1176+ elemSize = sizeOf (undefined :: a)
1177+
1178+ swap l h = do
1179+ when (l < h) $ do
1180+ swapPtrs l h
1181+ swap (l `plusPtr` elemSize) (h `plusPtr` (- elemSize))
11701182
11711183-- | Generate the next permutation of the sequence, returns False if this is
11721184-- the last permutation.