File: Update.hs

package info (click to toggle)
haskell-vector-builder 0.3.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 84 kB
  • sloc: haskell: 364; makefile: 3
file content (33 lines) | stat: -rw-r--r-- 1,255 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module VectorBuilder.Core.Update where

import qualified Data.Vector.Generic as B
import qualified Data.Vector.Generic.Mutable as A
import VectorBuilder.Prelude

newtype Update element
  = Update (forall s vector. (A.MVector vector element) => vector s element -> Int -> ST s ())

{-# INLINE write #-}
write :: element -> Update element
write element =
  Update (\mVector offset -> A.unsafeWrite mVector offset element)

{-# INLINE writeMany #-}
writeMany :: (B.Vector vector element) => vector element -> Update element
writeMany appendedVector =
  Update (\mVector offset -> B.ifoldM' (\_ index element -> A.unsafeWrite mVector (strict (offset + index)) element) () appendedVector)

{-# INLINE prepend #-}
prepend :: Int -> Update element -> Update element -> Update element
prepend size (Update leftST) (Update rightST) =
  Update (\mVector offset -> leftST mVector offset >> rightST mVector (strict (size + offset)))

{-# INLINE empty #-}
empty :: Update element
empty =
  Update (\_ _ -> pure ())

{-# INLINE writeFoldable #-}
writeFoldable :: (Foldable foldable) => foldable element -> Update element
writeFoldable foldable =
  Update (\mVector offset -> foldM_ (\index element -> A.unsafeWrite mVector index element $> succ index) offset foldable)