on bc4dba9 help sign in
52 lines 1.5 kB history raw
1#include "inline.hs"
3-- |
4-- Module : Streamly.Internal.Data.Array.Foreign.Mut
5-- Copyright : (c) 2020 Composewell Technologies
6-- License : BSD-3-Clause
7-- Maintainer : streamly@composewell.com
8-- Stability : experimental
9-- Portability : GHC
10--
11module Streamly.Internal.Data.Array.Foreign.Mut
12 (
13 -- , writeIndex
14 -- , writeFrom -- start writing at the given position
15 -- , writeFromRev
16 -- , writeTo -- write from beginning up to the given position
17 -- , writeToRev
18 -- , writeFromTo
19 -- , writeFromThenTo
20 --
21 -- , writeChunksOfFrom
22 -- , ...
24 writeIndex
25 --, writeIndices
26 --, writeRanges
27 )
28where
30import Control.Monad.IO.Class (MonadIO(..))
31import Foreign.Storable (Storable(..))
32import Streamly.Internal.BaseCompat (unsafeWithForeignPtr)
34import Prelude hiding (length)
35import Streamly.Internal.Data.Array.Foreign.Mut.Type
37-- | /O(1)/ Write the given element at the given index in the array.
38-- Performs in-place mutation of the array.
39--
40-- /Pre-release/
41{-# INLINE writeIndex #-}
42writeIndex :: (MonadIO m, Storable a) => Array a -> Int -> a -> m ()
43writeIndex arr i a = do
44 let maxIndex = length arr - 1
45 if i < 0
46 then error "writeIndex: negative array index"
47 else if i > maxIndex
48 then error $ "writeIndex: specified array index " ++ show i
49 ++ " is beyond the maximum index " ++ show maxIndex
50 else
51 liftIO $ unsafeWithForeignPtr (aStart arr) $ \p ->
52 pokeElemOff p i a