on master help sign in
Add some unimplemented functions
58051d0Harendra Kumar
src/Streamly/Internal/Data/Array/Foreign.hs+63−64
@@ -72,10 +72,10 @@ module Streamly.Internal.Data.Array.Foreign
72 -- , (!!)
72 -- , (!!)
73 , getIndex
73 , getIndex
74 , A.unsafeIndex -- XXX Rename to getIndexUnsafe??
74 , A.unsafeIndex -- XXX Rename to getIndexUnsafe??
75− -- , getIndexRev
75+ , getIndexRev
76 , last -- XXX getIndexLast?
76 , last -- XXX getIndexLast?
77− -- , getIndices
77+ , getIndices
78− -- , getIndicesFromThenTo
78+ , getIndicesFromThenTo
79 -- , getIndicesFrom -- read from a given position to the end of file
79 -- , getIndicesFrom -- read from a given position to the end of file
80 -- , getIndicesUpto -- read from beginning up to the given position
80 -- , getIndicesUpto -- read from beginning up to the given position
81 -- , getIndicesFromTo
81 -- , getIndicesFromTo
@@ -87,8 +87,8 @@ module Streamly.Internal.Data.Array.Foreign
87 , null
87 , null
88
88
89 -- * Search
89 -- * Search
90− -- , binarySearch
90+ , binarySearch
91− -- , findIndicesOf
91+ , findIndicesOf
92 -- , findIndexOf
92 -- , findIndexOf
93 -- , find
93 -- , find
94
94
@@ -117,11 +117,11 @@ where
117
117
118import Control.Monad (when)
118import Control.Monad (when)
119import Control.Monad.IO.Class (MonadIO(..))
119import Control.Monad.IO.Class (MonadIO(..))
120+import Data.Functor.Identity (Identity)
120#if !(MIN_VERSION_base(4,11,0))
121#if !(MIN_VERSION_base(4,11,0))
121import Data.Semigroup ((<>))
122import Data.Semigroup ((<>))
122#endif
123#endif
123import Data.Word (Word8)
124import Data.Word (Word8)
124−-- import Data.Functor.Identity (Identity)
125import Foreign.C.String (CString)
125import Foreign.C.String (CString)
126import Foreign.ForeignPtr (castForeignPtr)
126import Foreign.ForeignPtr (castForeignPtr)
127import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
127import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
@@ -317,27 +317,31 @@ writeLastN n
317-- Searching
317-- Searching
318-------------------------------------------------------------------------------
318-------------------------------------------------------------------------------
319
319
320−{-
320+-- | Given a sorted array, perform a binary search to find the given element.
321−-- | Perform a binary search in the array to find an element.
321+-- Returns the index of the element if found.
322−bsearch :: a -> Array a -> Maybe Bool
322+--
323−bsearch = undefined
323+-- /Unimplemented/
324−
324+{-# INLINE binarySearch #-}
325−-- | Perform a binary search in the array to find an element index.
326−{-# INLINE elemIndex #-}
327−bsearchIndex :: a -> Array a -> Maybe Int
325+binarySearch :: a -> Array a -> Maybe Int
328−bsearchIndex elem arr = undefined
326+binarySearch = undefined
329
327
330-- find/findIndex etc can potentially be implemented more efficiently on arrays
328-- find/findIndex etc can potentially be implemented more efficiently on arrays
331-- compared to streams by using SIMD instructions.
329-- compared to streams by using SIMD instructions.
330+-- We can also return a bit array instead.
332
331
333−find :: (a -> Bool) -> Array a -> Bool
332+-- | Perform a linear search to find all the indices where a given element is
333+-- present in an array.
334+--
335+-- /Unimplemented/
336+findIndicesOf :: (a -> Bool) -> Unfold Identity (Array a) Int
334−find = undefined
337+findIndicesOf = undefined
335
338
339+{-
336−findIndex :: (a -> Bool) -> Array a -> Maybe Int
340+findIndexOf :: (a -> Bool) -> Array a -> Maybe Int
337−findIndex = undefined
341+findIndexOf p = Unfold.fold Fold.head . Stream.unfold (findIndicesOf p)
338
342
339−findIndices :: (a -> Bool) -> Array a -> Array Int
343+find :: (a -> Bool) -> Array a -> Bool
340−findIndices = undefined
344+find = Unfold.fold Fold.null . Stream.unfold (findIndicesOf p)
341-}
345-}
342
346
343-------------------------------------------------------------------------------
347-------------------------------------------------------------------------------
@@ -391,49 +395,44 @@ getIndex arr i =
391 then Just <$> peek elemPtr
395 then Just <$> peek elemPtr
392 else return Nothing
396 else return Nothing
393
397
394−{-
398+-- | Given a stream of array indices, read the elements on those indices from
395−-- | @readSlice arr i count@ streams a slice of the array @arr@ starting
399+-- the supplied Array. An exception is thrown if an index is out of bounds.
396−-- at index @i@ and reading up to @count@ elements in the forward direction
397−-- ending at the index @i + count - 1@.
398−--
400+--
399−-- @since 0.7.0
401+-- This is the most general operation. We can implement other operations in
400−{-# INLINE readSlice #-}
402+-- terms of this:
401−readSlice :: (IsStream t, Monad m, Storable a)
403+--
402− => Array a -> Int -> Int -> t m a
404+-- @
403−readSlice arr i len = undefined
405+-- read =
404−
406+-- let u = lmap (\arr -> (0, length arr - 1)) Unfold.enumerateFromTo
405−-- | @readSliceRev arr i count@ streams a slice of the array @arr@ starting at
407+-- in Unfold.lmap f (getIndices arr)
406−-- index @i@ and reading up to @count@ elements in the reverse direction ending
408+--
407−-- at the index @i - count + 1@.
409+-- readRev =
408−--
410+-- let i = length arr - 1
409−-- @since 0.7.0
411+-- in Unfold.lmap f (getIndicesFromThenTo i (i - 1) 0)
410−{-# INLINE readSliceRev #-}
412+-- @
411−readSliceRev :: (IsStream t, Monad m, Storable a)
412− => Array a -> Int -> Int -> t m a
413−readSliceRev arr i len = undefined
414−-}
415−
416−{-
417−-- | @writeSlice arr i count stream@ writes a stream to the array @arr@
418−-- starting at index @i@ and writing up to @count@ elements in the forward
419−-- direction ending at the index @i + count - 1@.
420−--
413+--
421−-- @since 0.7.0
414+-- /Unimplemented/
422−{-# INLINE writeSlice #-}
415+{-# INLINE getIndices #-}
423−writeSlice :: (IsStream t, Monad m, Storable a)
416+getIndices :: Unfold m (Array a) Int -> Unfold m (Array a) a
424− => Array a -> Int -> Int -> t m a -> m (Array a)
417+getIndices = undefined
425−writeSlice arr i len s = undefined
418+
426−
419+-- | Unfolds @(from, then, to, array)@ generating a finite stream whose first
427−-- | @writeSliceRev arr i count stream@ writes a stream to the array @arr@
420+-- element is the array value from the index @from@ and the successive elements
428−-- starting at index @i@ and writing up to @count@ elements in the reverse
421+-- are from the indices in increments of @then@ up to @to@. Index enumeration
429−-- direction ending at the index @i - count + 1@.
422+-- can occur downwards or upwards depending on whether @then@ comes before or
423+-- after @from@.
430−--
424+--
431−-- @since 0.7.0
425+-- @
432−{-# INLINE writeSliceRev #-}
426+-- getIndicesFromThenTo =
433−writeSliceRev :: (IsStream t, Monad m, Storable a)
427+-- let f (from, next, to, arr) =
434− => Array a -> Int -> Int -> t m a -> m (Array a)
428+-- (Stream.enumerateFromThenTo from next to, arr)
435−writeSliceRev arr i len s = undefined
429+-- in Unfold.lmap f getIndices
436−-}
430+-- @
431+--
432+-- /Unimplemented/
433+{-# INLINE getIndicesFromThenTo #-}
434+getIndicesFromThenTo :: Unfold m (Int, Int, Int, Array a) a
435+getIndicesFromThenTo = undefined
437
436
438-------------------------------------------------------------------------------
437-------------------------------------------------------------------------------
439-- Transform via stream operations
438-- Transform via stream operations