{-# LANGUAGE BangPatterns #-}
module Control.Foldl.Util.MVector
where

import Data.Vector.Generic.Mutable
import Control.Monad.ST


{-# INLINE writeListInReverseOrderStartingFrom #-}
writeListInReverseOrderStartingFrom :: MVector v a => v s a -> Int -> [a] -> ST s ()
writeListInReverseOrderStartingFrom :: forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> [a] -> ST s ()
writeListInReverseOrderStartingFrom v s a
v = let
  loop :: Int -> [a] -> ST s ()
loop !Int
index [a]
list = case [a]
list of
    a
h : [a]
t -> do
      forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v s a
v Int
index a
h
      Int -> [a] -> ST s ()
loop (forall a. Enum a => a -> a
pred Int
index) [a]
t
    [a]
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
  in Int -> [a] -> ST s ()
loop