Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How can I ensure amortized O(n) concatenation from Data.Vector?
- import Data.Vector((++),Vector)
- import Prelude hiding ((++))
- import Control.Monad.State.Strict
- data App = S (Vector Int)
- add :: Vector Int -> State App ()
- add v1 = modify (S v2 -> S (v2 ++ v1))
- add v1 = do
- S v2 <- get
- let v3 = runST $ do
- m1 <- unsafeThaw v2
- m2 <- unsafeGrow m1 (length v1)
- -- copy contents of v1 behind contents of v2
- unsafeFreeze m2
- put (S v3)
Add Comment
Please, Sign In to add comment