Rename D.V.IVector, D.V.MVector and D.V.MVector.New
[darcs-mirrors/vector.git] / Data / Vector / Generic / New.hs
1 {-# LANGUAGE Rank2Types, ScopedTypeVariables #-}
2
3 #include "phases.h"
4
5 module Data.Vector.Generic.New (
6 New(..), run, unstream, transform, accum, update, reverse,
7 slice, init, tail, take, drop
8 ) where
9
10 import qualified Data.Vector.Generic.Mutable as MVector
11 import Data.Vector.Generic.Mutable ( MVector, MVectorPure )
12
13 import Data.Vector.Fusion.Stream ( Stream, MStream )
14 import qualified Data.Vector.Fusion.Stream as Stream
15
16 import qualified Data.Vector.Fusion.Stream.Monadic as MStream
17
18 import Control.Monad ( liftM )
19 import Prelude hiding ( init, tail, take, drop, reverse, map, filter )
20
21 newtype New a = New (forall m mv. MVector mv m a => m (mv a))
22
23 run :: MVector mv m a => New a -> m (mv a)
24 {-# INLINE run #-}
25 run (New p) = p
26
27 apply :: (forall mv a. MVectorPure mv a => mv a -> mv a) -> New a -> New a
28 {-# INLINE apply #-}
29 apply f (New p) = New (liftM f p)
30
31 modify :: New a -> (forall m mv. MVector mv m a => mv a -> m ()) -> New a
32 {-# INLINE modify #-}
33 modify (New p) q = New (do { v <- p; q v; return v })
34
35 unstream :: Stream a -> New a
36 {-# INLINE_STREAM unstream #-}
37 unstream s = New (MVector.unstream s)
38
39 transform :: (forall m. Monad m => MStream m a -> MStream m a) -> New a -> New a
40 {-# INLINE_STREAM transform #-}
41 transform f (New p) = New (MVector.transform f =<< p)
42
43 {-# RULES
44
45 "transform/transform [New]"
46 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
47 (g :: forall m. Monad m => MStream m a -> MStream m a)
48 p .
49 transform f (transform g p) = transform (f . g) p
50
51 "transform/unstream [New]"
52 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
53 s.
54 transform f (unstream s) = unstream (f s)
55
56 #-}
57
58 slice :: New a -> Int -> Int -> New a
59 {-# INLINE_STREAM slice #-}
60 slice m i n = apply (\v -> MVector.slice v i n) m
61
62 init :: New a -> New a
63 {-# INLINE_STREAM init #-}
64 init m = apply (\v -> MVector.slice v 0 (MVector.length v - 1)) m
65
66 tail :: New a -> New a
67 {-# INLINE_STREAM tail #-}
68 tail m = apply (\v -> MVector.slice v 1 (MVector.length v - 1)) m
69
70 take :: Int -> New a -> New a
71 {-# INLINE_STREAM take #-}
72 take n m = apply (\v -> MVector.slice v 0 (min n (MVector.length v))) m
73
74 drop :: Int -> New a -> New a
75 {-# INLINE_STREAM drop #-}
76 drop n m = apply (\v -> MVector.slice v n (max 0 (MVector.length v - n))) m
77
78 {-# RULES
79
80 "slice/unstream [New]" forall s i n.
81 slice (unstream s) i n = unstream (Stream.extract s i n)
82
83 "init/unstream [New]" forall s.
84 init (unstream s) = unstream (Stream.init s)
85
86 "tail/unstream [New]" forall s.
87 tail (unstream s) = unstream (Stream.tail s)
88
89 "take/unstream [New]" forall n s.
90 take n (unstream s) = unstream (Stream.take n s)
91
92 "drop/unstream [New]" forall n s.
93 drop n (unstream s) = unstream (Stream.drop n s)
94
95 #-}
96
97 accum :: (a -> b -> a) -> New a -> Stream (Int, b) -> New a
98 {-# INLINE_STREAM accum #-}
99 accum f m s = modify m (\v -> MVector.accum f v s)
100
101 update :: New a -> Stream (Int, a) -> New a
102 {-# INLINE_STREAM update #-}
103 update m s = modify m (\v -> MVector.update v s)
104
105 reverse :: New a -> New a
106 {-# INLINE_STREAM reverse #-}
107 reverse m = modify m (MVector.reverse)
108