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
11-- Unboxed pinned mutable array type for 'Storable' types with an option to use
12-- foreign (non-GHC) memory allocators. Fulfils the following goals:
14-- * Random access (array)
15-- * Efficient storage (unboxed)
16-- * Performance (unboxed access)
17-- * Performance - in-place operations (mutable)
18-- * Performance - GC (pinned, mutable)
19-- * interfacing with OS (pinned)
20-- * Fragmentation control (foreign allocators)
22-- Stream and Fold APIs allow easy, efficient and convenient operations on
25module Streamly.Internal.Data.Array.Foreign.Mut
30 -- , writeFrom -- start writing at the given position
32 -- , writeTo -- write from beginning up to the given position
37 -- , writeChunksOfFrom
47 -- -- * In-pace mutation (for Mutable Array type)
78import Control.Monad.IO.Class (MonadIO(..))
79import Foreign.Storable (Storable(..))
80import Streamly.Internal.BaseCompat (unsafeWithForeignPtr)
82import Prelude hiding (foldr, length, read, splitAt)
84import Streamly.Internal.Data.Array.Foreign.Mut.Type
86-- | /O(1)/ Write the given element at the given index in the array.
87-- Performs in-place mutation of the array.
90{-# INLINE writeIndex #-}
91writeIndex :: (MonadIO m, Storable a) => Array a -> Int -> a -> m ()
92writeIndex arr i a = do
93 let maxIndex = length arr - 1
95 then error "writeIndex: negative array index"
97 then error $ "writeIndex: specified array index " ++ show i
98 ++ " is beyond the maximum index " ++ show maxIndex
100 liftIO $ unsafeWithForeignPtr (aStart arr) $ \p ->
105-------------------------------------------------------------------------------
106-- In-place mutation APIs
107-------------------------------------------------------------------------------
109-- | Partition an array into two halves using a partitioning predicate. The
110-- first half retains values where the predicate is 'False' and the second half
111-- retains values where the predicate is 'True'.
112{-# INLINE partitionBy #-}
113partitionBy :: (a -> Bool) -> Array a -> (Array a, Array a)
114partitionBy f arr = undefined
116-- | Shuffle corresponding elements from two arrays using a shuffle function.
117-- If the shuffle function returns 'False' then do nothing otherwise swap the
118-- elements. This can be used in a bottom up fold to shuffle or reorder the
120shuffleBy :: (a -> a -> m Bool) -> Array a -> Array a -> m (Array a)
121shuffleBy f arr1 arr2 = undefined
123-- XXX we can also make the folds partial by stopping at a certain level.
125-- | Perform a top down hierarchical recursive partitioning fold of items in
126-- the container using the given function as the partition function.
128-- This will perform a quick sort if the partition function is
129-- 'partitionBy (< pivot)'.
132{-# INLINABLE foldtWith #-}
133foldtWith :: Int -> (Array a -> Array a -> m (Array a)) -> Array a -> m (Array a)
134foldtWith level f = undefined
136-- | Perform a pairwise bottom up fold recursively merging the pairs. Level
137-- indicates the level in the tree where the fold would stop.
139-- This will perform a random shuffle if the shuffle function is random.
140-- If we stop at level 0 and repeatedly apply the function then we can do a
142foldbWith :: Int -> (Array a -> Array a -> m (Array a)) -> Array a -> m (Array a)
143foldbWith level f = undefined
146-- XXX consider the bulk update/accumulation/permutation APIs from vector.
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list