Skip to content

Commit 36b12ef

Browse files
committed
Implement conversion between strict and lazy vectors
1 parent 3dcf821 commit 36b12ef

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

vector/src/Data/Vector/Strict.hs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ module Data.Vector.Strict (
162162

163163
-- ** Lists
164164
toList, Data.Vector.Strict.fromList, Data.Vector.Strict.fromListN,
165-
165+
-- ** Lazy vectors
166+
toLazy, fromLazy, lazyFromLazy,
166167
-- ** Arrays
167168
toArray, fromArray, lazyFromArray, toArraySlice, unsafeFromArraySlice, unsafeLazyFromArraySlice,
168169

@@ -2463,6 +2464,24 @@ fromListN :: Int -> [a] -> Vector a
24632464
{-# INLINE fromListN #-}
24642465
fromListN = G.fromListN
24652466

2467+
-- Conversions - Lazy vectors
2468+
-- -----------------------------
2469+
2470+
-- | /O(1)/ Convert strict array to lazy array
2471+
toLazy :: Vector a -> V.Vector a
2472+
toLazy (Vector v) = v
2473+
2474+
-- | /O(n)/ Convert lazy array to strict array. This function reduces
2475+
-- each element of vector to WHNF.
2476+
fromLazy :: V.Vector a -> Vector a
2477+
fromLazy vec = liftRnf (`seq` ()) v `seq` v where v = Vector vec
2478+
2479+
-- | /O(1)/ Convert lazy array to strict array. This function does not
2480+
-- evaluate vector elements.
2481+
lazyFromLazy :: V.Vector a -> Vector a
2482+
lazyFromLazy = Vector
2483+
2484+
24662485
-- Conversions - Arrays
24672486
-- -----------------------------
24682487

vector/src/Data/Vector/Strict/Mutable.hs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ module Data.Vector.Strict.Mutable (
6666

6767
-- ** Filling and copying
6868
set, copy, move, unsafeCopy, unsafeMove,
69-
69+
-- ** Lazy arrays
70+
toLazy, fromLazy, lazyFromLazy,
7071
-- ** Arrays
7172
fromMutableArray, toMutableArray,
7273

@@ -707,6 +708,31 @@ ifoldrM' :: (PrimMonad m) => (Int -> a -> b -> m b) -> b -> MVector (PrimState m
707708
{-# INLINE ifoldrM' #-}
708709
ifoldrM' = G.ifoldrM'
709710

711+
-- Conversions - Lazy vectors
712+
-- -----------------------------
713+
714+
-- | /O(1)/ Convert strict mutable vector to lazy mutable
715+
-- vector. Vectors will share mutable buffer
716+
toLazy :: MVector s a -> MV.MVector s a
717+
{-# INLINE toLazy #-}
718+
toLazy (MVector vec) = vec
719+
720+
-- | /O(1)/ Convert lazy mutable vector to strict mutable
721+
-- vector. Vectors will share mutable buffer. This function does not
722+
-- evaluate vector elements to WHNF.
723+
lazyFromLazy :: MV.MVector s a -> MVector s a
724+
{-# INLINE lazyFromLazy #-}
725+
lazyFromLazy = MVector
726+
727+
-- | /O(1)/ Convert lazy mutable vector to strict mutable
728+
-- vector. Vectors will share mutable buffer. This function evaluates
729+
-- vector elements to WHNF.
730+
fromLazy :: PrimMonad m => MV.MVector (PrimState m) a -> m (MVector (PrimState m) a)
731+
fromLazy mvec = stToPrim $ do
732+
G.foldM' (\_ !_ -> return ()) () mvec
733+
return $ MVector mvec
734+
735+
710736
-- Conversions - Arrays
711737
-- -----------------------------
712738

0 commit comments

Comments
 (0)