@@ -235,11 +235,8 @@ import Foreign.C.Types (CSize(..), CInt(..))
235235import Foreign.Ptr (plusPtr, minusPtr, nullPtr)
236236import Streamly.Internal.Data.Unbox
237237 ( MutableByteArray(..)
238− , Unbox
238+ , Unbox(..)
239239 , getMutableByteArray#
240− , peekWith
241− , pokeWith
242− , sizeOf
243240 , touch
244241 )
245242import GHC.Base
@@ -497,7 +494,7 @@ putIndexUnsafe :: forall m a. (MonadIO m, Unbox a)
497494putIndexUnsafe i MutArray{..} x = do
498495 let index = INDEX_OF(arrStart, i, a)
499496 assert (i >= 0 && INDEX_VALID(index, arrEnd, a)) (return ())
500− liftIO $ pokeWith arrContents index x
497+ liftIO $ pokeByteIndex index arrContents x
501498
502499invalidIndex :: String -> Int -> a
503500invalidIndex label i =
@@ -515,7 +512,7 @@ putIndex :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> a -> m ()
515512putIndex i MutArray{..} x = do
516513 let index = INDEX_OF(arrStart,i,a)
517514 if i >= 0 && INDEX_VALID(index,arrEnd,a)
518− then liftIO $ pokeWith arrContents index x
515+ then liftIO $ pokeByteIndex index arrContents x
519516 else invalidIndex "putIndex" i
520517
521518-- | Write an input stream of (index, value) pairs to an array. Throws an
@@ -541,9 +538,9 @@ modifyIndexUnsafe :: forall m a b. (MonadIO m, Unbox a) =>
541538modifyIndexUnsafe i MutArray{..} f = liftIO $ do
542539 let index = INDEX_OF(arrStart,i,a)
543540 assert (i >= 0 && INDEX_NEXT(index,a) <= arrEnd) (return ())
544− r <- peekWith arrContents index
541+ r <- peekByteIndex index arrContents
545542 let (x, res) = f r
546− pokeWith arrContents index x
543+ pokeByteIndex index arrContents x
547544 return res
548545
549546-- | Modify a given index of an array using a modifier function.
@@ -555,9 +552,9 @@ modifyIndex i MutArray{..} f = do
555552 let index = INDEX_OF(arrStart,i,a)
556553 if i >= 0 && INDEX_VALID(index,arrEnd,a)
557554 then liftIO $ do
558− r <- peekWith arrContents index
555+ r <- peekByteIndex index arrContents
559556 let (x, res) = f r
560− pokeWith arrContents index x
557+ pokeByteIndex index arrContents x
561558 return res
562559 else invalidIndex "modifyIndex" i
563560
@@ -591,8 +588,8 @@ modify MutArray{..} f = liftIO $
591588
592589 go i =
593590 when (INDEX_VALID(i,arrEnd,a)) $ do
594− r <- peekWith arrContents i
595− pokeWith arrContents i (f r)
591+ r <- peekByteIndex i arrContents
592+ pokeByteIndex i arrContents (f r)
596593 go (INDEX_NEXT(i,a))
597594
598595-- XXX We could specify the number of bytes to swap instead of Proxy. Need
@@ -606,10 +603,10 @@ swapArrayByteIndices ::
606603 -> Int
607604 -> IO ()
608605swapArrayByteIndices _ arrContents i1 i2 = do
609− r1 <- peekWith arrContents i1
610− r2 <- peekWith arrContents i2
611− pokeWith arrContents i1 (r2 :: a)
612− pokeWith arrContents i2 (r1 :: a)
606+ r1 <- peekByteIndex i1 arrContents
607+ r2 <- peekByteIndex i2 arrContents
608+ pokeByteIndex i1 arrContents (r2 :: a)
609+ pokeByteIndex i2 arrContents (r1 :: a)
613610
614611-- | Swap the elements at two indices without validating the indices.
615612--
@@ -882,7 +879,7 @@ rightSize arr@MutArray{..} = do
882879snocNewEnd :: (MonadIO m, Unbox a) => Int -> MutArray a -> a -> m (MutArray a)
883880snocNewEnd newEnd arr@MutArray{..} x = liftIO $ do
884881 assert (newEnd <= arrBound) (return ())
885− pokeWith arrContents arrEnd x
882+ pokeByteIndex arrEnd arrContents x
886883 return $ arr {arrEnd = newEnd}
887884
888885-- | Really really unsafe, appends the element into the first array, may
@@ -996,7 +993,7 @@ getIndexUnsafe :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> m a
996993getIndexUnsafe i MutArray{..} = do
997994 let index = INDEX_OF(arrStart,i,a)
998995 assert (i >= 0 && INDEX_VALID(index,arrEnd,a)) (return ())
999− liftIO $ peekWith arrContents index
996+ liftIO $ peekByteIndex index arrContents
1000997
1001998-- | /O(1)/ Lookup the element at the given index. Index starts from 0.
1002999--
@@ -1005,7 +1002,7 @@ getIndex :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> m (Maybe a)
10051002getIndex i MutArray{..} = do
10061003 let index = INDEX_OF(arrStart,i,a)
10071004 if i >= 0 && INDEX_VALID(index,arrEnd,a)
1008− then liftIO $ Just <$> peekWith arrContents index
1005+ then liftIO $ Just <$> peekByteIndex index arrContents
10091006 else return Nothing
10101007
10111008-- | /O(1)/ Lookup the element at the given index from the end of the array.
@@ -1018,7 +1015,7 @@ getIndexRev :: forall m a. (MonadIO m, Unbox a) => Int -> MutArray a -> m a
10181015getIndexRev i MutArray{..} = do
10191016 let index = RINDEX_OF(arrEnd,i,a)
10201017 if i >= 0 && index >= arrStart
1021− then liftIO $ peekWith arrContents index
1018+ then liftIO $ peekByteIndex index arrContents
10221019 else invalidIndex "getIndexRev" i
10231020
10241021data GetIndicesState contents start end st =
@@ -1158,7 +1155,7 @@ partitionBy f arr@MutArray{..} = liftIO $ do
11581155
11591156 -- Invariant low < high on entry, and on return as well
11601157 moveHigh low high = do
1161− h <- peekWith arrContents high
1158+ h <- peekByteIndex high arrContents
11621159 if f h
11631160 then
11641161 -- Correctly classified, continue the loop
@@ -1178,7 +1175,7 @@ partitionBy f arr@MutArray{..} = liftIO $ do
11781175 -- low <= high
11791176 -- Both low and high are valid locations within the array
11801177 go low high = do
1181− l <- peekWith arrContents low
1178+ l <- peekByteIndex low arrContents
11821179 if f l
11831180 then
11841181 -- low is wrongly classified
@@ -1189,8 +1186,8 @@ partitionBy f arr@MutArray{..} = liftIO $ do
11891186 case r of
11901187 Nothing -> return low
11911188 Just (high1, h) -> do -- low < high1
1192− pokeWith arrContents low h
1193− pokeWith arrContents high1 l
1189+ pokeByteIndex low arrContents h
1190+ pokeByteIndex high1 arrContents l
11941191 let low1 = INDEX_NEXT(low,a)
11951192 high2 = INDEX_PREV(high1,a)
11961193 if low1 <= high2
@@ -1334,7 +1331,7 @@ chunksOf n (D.Stream step state) =
13341331 r <- step (adaptState gst) st
13351332 case r of
13361333 D.Yield x s -> do
1337− liftIO $ pokeWith contents end x
1334+ liftIO $ pokeByteIndex end contents x
13381335 let end1 = INDEX_NEXT(end,a)
13391336 return $
13401337 if end1 >= bound
@@ -1396,7 +1393,7 @@ flattenArrays (D.Stream step state) = D.Stream step' (OuterLoop state)
13961393 return $ D.Skip $ OuterLoop st
13971394
13981395 step' _ (InnerLoop st contents p end) = do
1399− x <- liftIO $ peekWith contents p
1396+ x <- liftIO $ peekByteIndex p contents
14001397 return $ D.Yield x (InnerLoop st contents (INDEX_NEXT(p,a)) end)
14011398
14021399-- | Use the "readerRev" unfold instead.
@@ -1426,7 +1423,7 @@ flattenArraysRev (D.Stream step state) = D.Stream step' (OuterLoop state)
14261423 return $ D.Skip $ OuterLoop st
14271424
14281425 step' _ (InnerLoop st contents p start) = do
1429− x <- liftIO $ peekWith contents p
1426+ x <- liftIO $ peekByteIndex p contents
14301427 let cur = INDEX_PREV(p,a)
14311428 return $ D.Yield x (InnerLoop st contents cur start)
14321429
@@ -1465,7 +1462,7 @@ producerWith liftio = Producer step (return . toArrayUnsafe) extract
14651462 -- few actions for correctness and execution order sanity. We want
14661463 -- the peek to occur right here and not lazily at some later point
14671464 -- because we want the peek to be ordered with respect to the touch.
1468− !x <- liftio $ peekWith contents cur
1465+ !x <- liftio $ peekByteIndex cur contents
14691466 return $ D.Yield x (ArrayUnsafe contents (INDEX_NEXT(cur,a)) end)
14701467
14711468 extract = return . fromArrayUnsafe
@@ -1496,7 +1493,7 @@ readerRevWith liftio = Unfold step inject
14961493 {-# INLINE_LATE step #-}
14971494 step (ArrayUnsafe _ start p) | p < start = return D.Stop
14981495 step (ArrayUnsafe contents start p) = do
1499− !x <- liftio $ peekWith contents p
1496+ !x <- liftio $ peekByteIndex p contents
15001497 return $ D.Yield x (ArrayUnsafe contents start (INDEX_PREV(p,a)))
15011498
15021499-- | Unfold an array into a stream in reverse order.
@@ -1527,7 +1524,7 @@ toListFB c n MutArray{..} = go arrStart
15271524 -- evaluated/written to before we peek at them.
15281525 -- XXX
15291526 let !x = unsafeInlineIO $ do
1530− r <- peekWith arrContents p
1527+ r <- peekByteIndex arrContents p
15311528 return r
15321529 in c x (go (PTR_NEXT(p,a)))
15331530-}
@@ -1544,7 +1541,7 @@ toList MutArray{..} = liftIO $ go arrStart
15441541
15451542 go p | assert (p <= arrEnd) (p == arrEnd) = return []
15461543 go p = do
1547− x <- peekWith arrContents p
1544+ x <- peekByteIndex p arrContents
15481545 (:) x <$> go (INDEX_NEXT(p,a))
15491546
15501547{-# INLINE_NORMAL toStreamDWith #-}
@@ -1558,7 +1555,7 @@ toStreamDWith liftio MutArray{..} = D.Stream step arrStart
15581555 {-# INLINE_LATE step #-}
15591556 step _ p | assert (p <= arrEnd) (p == arrEnd) = return D.Stop
15601557 step _ p = liftio $ do
1561− r <- peekWith arrContents p
1558+ r <- peekByteIndex p arrContents
15621559 return $ D.Yield r (INDEX_NEXT(p,a))
15631560
15641561-- | Convert a 'MutArray' into a stream.
@@ -1579,7 +1576,7 @@ toStreamKWith liftio MutArray{..} = go arrStart
15791576
15801577 go p | assert (p <= arrEnd) (p == arrEnd) = K.nil
15811578 | otherwise =
1582− let elemM = peekWith arrContents p
1579+ let elemM = peekByteIndex p arrContents
15831580 in liftio elemM `K.consM` go (INDEX_NEXT(p,a))
15841581
15851582{-# INLINE toStreamK #-}
@@ -1599,7 +1596,7 @@ toStreamDRevWith liftio MutArray{..} =
15991596 {-# INLINE_LATE step #-}
16001597 step _ p | p < arrStart = return D.Stop
16011598 step _ p = liftio $ do
1602− r <- peekWith arrContents p
1599+ r <- peekByteIndex p arrContents
16031600 return $ D.Yield r (INDEX_PREV(p,a))
16041601
16051602-- | Convert a 'MutArray' into a stream in reverse order.
@@ -1622,7 +1619,7 @@ toStreamKRevWith liftio MutArray {..} =
16221619
16231620 go p | p < arrStart = K.nil
16241621 | otherwise =
1625− let elemM = peekWith arrContents p
1622+ let elemM = peekByteIndex p arrContents
16261623 in liftio elemM `K.consM` go (INDEX_PREV(p,a))
16271624
16281625{-# INLINE toStreamKRev #-}
@@ -1690,7 +1687,7 @@ writeAppendNUnsafe n action =
16901687 return $ toArrayUnsafe arr1
16911688
16921689 step (ArrayUnsafe contents start end) x = do
1693− liftIO $ pokeWith contents end x
1690+ liftIO $ pokeByteIndex end contents x
16941691 return $ ArrayUnsafe contents start (INDEX_NEXT(end,a))
16951692
16961693-- | Append @n@ elements to an existing array. Any free space left in the array
@@ -1750,7 +1747,7 @@ writeNWithUnsafe alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
17501747 initial = toArrayUnsafe <$> alloc (max n 0)
17511748
17521749 step (ArrayUnsafe contents start end) x = do
1753− liftIO $ pokeWith contents end x
1750+ liftIO $ pokeByteIndex end contents x
17541751 return
17551752 $ ArrayUnsafe contents start (INDEX_NEXT(end,a))
17561753
@@ -1806,7 +1803,7 @@ writeRevNWithUnsafe alloc n = fromArrayUnsafe <$> FL.foldlM' step initial
18061803
18071804 step (ArrayUnsafe contents start end) x = do
18081805 let ptr = INDEX_PREV(start,a)
1809− liftIO $ pokeWith contents ptr x
1806+ liftIO $ pokeByteIndex ptr contents x
18101807 return
18111808 $ ArrayUnsafe contents ptr end
18121809
@@ -1938,7 +1935,7 @@ fromStreamDN limit str = do
19381935 where
19391936
19401937 fwrite arrContents ptr x = do
1941− liftIO $ pokeWith arrContents ptr x
1938+ liftIO $ pokeByteIndex ptr arrContents x
19421939 return $ INDEX_NEXT(ptr,a)
19431940
19441941-- | Create a 'MutArray' from the first N elements of a list. The array is
@@ -2328,7 +2325,7 @@ strip eq arr@MutArray{..} = liftIO $ do
23282325 getStart cur = do
23292326 if cur < arrEnd
23302327 then do
2331− r <- peekWith arrContents cur
2328+ r <- peekByteIndex cur arrContents
23322329 if eq r
23332330 then getStart (INDEX_NEXT(cur,a))
23342331 else return cur
@@ -2338,7 +2335,7 @@ strip eq arr@MutArray{..} = liftIO $ do
23382335 if cur > low
23392336 then do
23402337 let prev = INDEX_PREV(cur,a)
2341− r <- peekWith arrContents prev
2338+ r <- peekByteIndex prev arrContents
23422339 if eq r
23432340 then getLast prev low
23442341 else return cur