module Data.ProtoLens.Encoding.Growing (
Growing,
new,
append,
unsafeFreeze,
RealWorld,
) where
import Control.Monad.Primitive (PrimMonad, PrimState, RealWorld)
import qualified Data.Vector.Generic as V
import qualified Data.Vector.Generic.Mutable as MV
data Growing v s a = Growing
{-# UNPACK #-} !Int
!(V.Mutable v s a)
new :: (PrimMonad m, V.Vector v a) => m (Growing v (PrimState m) a)
new :: forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
m (Growing v (PrimState m) a)
new = forall (v :: * -> *) s a. Int -> Mutable v s a -> Growing v s a
Growing Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
MV.new Int
0
unsafeFreeze
:: (PrimMonad m, V.Vector v a)
=> Growing v (PrimState m) a -> m (v a)
unsafeFreeze :: forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Growing v (PrimState m) a -> m (v a)
unsafeFreeze (Growing Int
len Mutable v (PrimState m) a
m) = forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
V.unsafeFreeze (forall (v :: * -> * -> *) a s. MVector v a => Int -> v s a -> v s a
MV.take Int
len Mutable v (PrimState m) a
m)
append
:: (PrimMonad m, V.Vector v a)
=> Growing v (PrimState m) a
-> a
-> m (Growing v (PrimState m) a)
append :: forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Growing v (PrimState m) a -> a -> m (Growing v (PrimState m) a)
append (Growing Int
len Mutable v (PrimState m) a
v) a
x
| Int
len forall a. Ord a => a -> a -> Bool
< forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
MV.length Mutable v (PrimState m) a
v = do
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
MV.unsafeWrite Mutable v (PrimState m) a
v Int
len a
x
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (v :: * -> *) s a. Int -> Mutable v s a -> Growing v s a
Growing (Int
len forall a. Num a => a -> a -> a
+ Int
1) Mutable v (PrimState m) a
v
| Bool
otherwise = do
let len' :: Int
len' = Int
2 forall a. Num a => a -> a -> a
* Int
len forall a. Num a => a -> a -> a
+ Int
1
Mutable v (PrimState m) a
v' <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m (v (PrimState m) a)
MV.unsafeGrow Mutable v (PrimState m) a
v Int
len'
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
MV.unsafeWrite Mutable v (PrimState m) a
v' Int
len a
x
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (v :: * -> *) s a. Int -> Mutable v s a -> Growing v s a
Growing (Int
len forall a. Num a => a -> a -> a
+ Int
1) Mutable v (PrimState m) a
v'
{-# INLINE append #-}