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 = Int -> Mutable v (PrimState m) a -> Growing v (PrimState m) a
forall (v :: * -> *) s a. Int -> Mutable v s a -> Growing v s a
Growing Int
0 (Mutable v (PrimState m) a -> Growing v (PrimState m) a)
-> m (Mutable v (PrimState m) a) -> m (Growing v (PrimState m) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> m (Mutable v (PrimState m) a)
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) = Mutable v (PrimState m) a -> m (v a)
forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
V.unsafeFreeze (Int -> Mutable v (PrimState m) a -> Mutable v (PrimState m) a
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Mutable v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
MV.length Mutable v (PrimState m) a
v = do
Mutable v (PrimState m) a -> Int -> a -> m ()
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
Growing v (PrimState m) a -> m (Growing v (PrimState m) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Growing v (PrimState m) a -> m (Growing v (PrimState m) a))
-> Growing v (PrimState m) a -> m (Growing v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ Int -> Mutable v (PrimState m) a -> Growing v (PrimState m) a
forall (v :: * -> *) s a. Int -> Mutable v s a -> Growing v s a
Growing (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Mutable v (PrimState m) a
v
| Bool
otherwise = do
let len' :: Int
len' = Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
Mutable v (PrimState m) a
v' <- Mutable v (PrimState m) a -> Int -> m (Mutable v (PrimState m) a)
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'
Mutable v (PrimState m) a -> Int -> a -> m ()
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
Growing v (PrimState m) a -> m (Growing v (PrimState m) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Growing v (PrimState m) a -> m (Growing v (PrimState m) a))
-> Growing v (PrimState m) a -> m (Growing v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ Int -> Mutable v (PrimState m) a -> Growing v (PrimState m) a
forall (v :: * -> *) s a. Int -> Mutable v s a -> Growing v s a
Growing (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Mutable v (PrimState m) a
v'
{-# INLINE append #-}