on master help sign in
Implement reverse
7b8d5c0Ranjeet Kumar Ranjan
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+15−3
@@ -1163,10 +1163,22 @@ getSlice index len (Array contents start e _) =
1163-- to another array. However, in-place reverse can be useful to take adavantage
1163-- to another array. However, in-place reverse can be useful to take adavantage
1164-- of cache locality and when you do not want to allocate additional memory.
1164-- of cache locality and when you do not want to allocate additional memory.
1165--
1165--
1166−-- /Unimplemented/
1166+-- /Pre-release/
1167{-# INLINE reverse #-}
1167{-# INLINE reverse #-}
1168−reverse :: Array a -> m Bool
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+
1169−reverse = undefined
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))
1170
1182
1171-- | Generate the next permutation of the sequence, returns False if this is
1183-- | Generate the next permutation of the sequence, returns False if this is
1172-- the last permutation.
1184-- the last permutation.