on master help sign in
Add signatures for some in-place mutation combinators for arrays
4211fe2Harendra Kumar
src/Streamly/FileSystem/File.hs+4
@@ -278,6 +278,10 @@ write :: (MonadIO m, Storable a) => Handle -> SerialT m a -> m ()
278write = toHandleWith A.defaultChunkSize
278write = toHandleWith A.defaultChunkSize
279-}
279-}
280
280
281+-- XXX mmap a file into an array. This could be useful for in-place operations
282+-- on a file. For example, we can quicksort the contents of a file by mmapping
283+-- it.
284+
281-------------------------------------------------------------------------------
285-------------------------------------------------------------------------------
282-- IO with encoding/decoding Unicode characters
286-- IO with encoding/decoding Unicode characters
283-------------------------------------------------------------------------------
287-------------------------------------------------------------------------------
src/Streamly/Mem/Array.hs+92
@@ -202,6 +202,94 @@ _last arr = readIndex arr (length arr - 1)
202-- Random Access
202-- Random Access
203-------------------------------------------------------------------------------
203-------------------------------------------------------------------------------
204
204
205+-------------------------------------------------------------------------------
206+-- Searching
207+-------------------------------------------------------------------------------
208+
209+-- | Perform a binary search in the array to find an element.
210+elem :: a -> Array a -> Maybe Bool
211+
212+-- | Perform a binary search in the array to find an element index.
213+{-# INLINE elemIndex #-}
214+elemIndex :: a -> Array a -> Maybe Int
215+elemIndex elem arr = undefined
216+
217+-- find/findIndex etc can potentially be implemented more efficiently on arrays
218+-- compared to streams by using SIMD instructions.
219+
220+find :: (a -> Bool) -> Array a -> Bool
221+findIndex :: (a -> Bool) -> Array a -> Maybe Int
222+findIndices :: (a -> Bool) -> Array a -> Array Int
223+
224+-------------------------------------------------------------------------------
225+-- Folds
226+-------------------------------------------------------------------------------
227+
228+-- XXX We can potentially use SIMD instructions on arrays to fold faster.
229+
230+-------------------------------------------------------------------------------
231+-- Slice and splice
232+-------------------------------------------------------------------------------
233+
234+{-
235+slice :: Int -> Int -> Array a
236+slice begin end arr = undefined
237+
238+splitAt :: Int -> Array a -> (Array a, Array a)
239+splitAt i arr = undefined
240+
241+-- XXX This operation can be performed efficiently via streams.
242+-- | Append two arrays together to create a single array.
243+splice :: Array a -> Array a -> Array a
244+splice arr1 arr2 = undefined
245+
246+-------------------------------------------------------------------------------
247+-- In-place mutation APIs
248+-------------------------------------------------------------------------------
249+
250+-- | Partition an array into two halves using a partitioning predicate. The
251+-- first half retains values where the predicate is 'False' and the second half
252+-- retains values where the predicate is 'True'.
253+{-# INLINE partitionBy #-}
254+partitionBy :: (a -> Bool) -> Array a -> (Array a, Array a)
255+partitionBy f arr = undefined
256+
257+-- | Shuffle corresponding elements from two arrays using a shuffle function.
258+-- If the shuffle function returns 'False' then do nothing otherwise swap the
259+-- elements. This can be used in a bottom up fold to shuffle or reorder the
260+-- elements.
261+shuffleBy :: (a -> a -> m Bool) -> Array a -> Array a -> m (Array a)
262+shuffleBy f arr1 arr2 = undefined
263+
264+-- XXX we can also make the folds partial by stopping at a certain level.
265+--
266+-- | Perform a top down hierarchical recursive partitioning fold of items in
267+-- the container using the given function as the partition function.
268+--
269+-- This will perform a quick sort if the partition function is
270+-- 'partitionBy (< pivot)'.
271+--
272+-- @since 0.7.0
273+{-# INLINABLE foldtWith #-}
274+foldtWith :: Int -> (Array a -> Array a -> m (Array a)) -> Array a -> m (Array a)
275+foldtWith level f = undefined
276+
277+-- | Perform a pairwise bottom up fold recursively merging the pairs. Level
278+-- indicates the level in the tree where the fold would stop.
279+--
280+-- This will perform a random shuffle if the shuffle function is random.
281+-- If we stop at level 0 and repeatedly apply the function then we can do a
282+-- bubble sort.
283+foldbWith :: Int -> (Array a -> Array a -> m (Array a)) -> Array a -> m (Array a)
284+foldbWith level f = undefined
285+-}
286+
287+-- XXX consider the bulk update/accumulation/permutation APIs from vector.
288+
289+-------------------------------------------------------------------------------
290+-- Random reads and writes
291+-------------------------------------------------------------------------------
292+
205-- | /O(1)/ Lookup the element at the given index.
293-- | /O(1)/ Lookup the element at the given index.
206--
294--
207-- @since 0.7.0
295-- @since 0.7.0
@@ -328,3 +416,7 @@ spliceArrays s = do
328{-# INLINE write #-}
416{-# INLINE write #-}
329write :: (MonadIO m, Storable a) => SerialT m a -> m (Array a)
417write :: (MonadIO m, Storable a) => SerialT m a -> m (Array a)
330write m = A.fromStreamD $ D.toStreamD m
418write m = A.fromStreamD $ D.toStreamD m
419+
420+-- XXX efficiently compare two streams of arrays. Two streams can have chunks
421+-- of different sizes, we can handle that in the stream comparison abstraction.
422+-- This could be useful e.g. to fast compare whether two files differ.