d5bb50c6281b6da94833cc10875c5d0f538496d9
[darcs-mirrors/vector.git] / Data / Vector / Generic / New.hs
1 {-# LANGUAGE Rank2Types, FlexibleContexts #-}
2
3 -- |
4 -- Module : Data.Vector.Generic.New
5 -- Copyright : (c) Roman Leshchinskiy 2008-2010
6 -- License : BSD-style
7 --
8 -- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
9 -- Stability : experimental
10 -- Portability : non-portable
11 --
12 -- Purely functional interface to initialisation of mutable vectors
13 --
14
15 module Data.Vector.Generic.New (
16 New(..), create, run, runPrim, apply, modify, modifyWithStream,
17 unstream, transform, unstreamR, transformR,
18 slice, init, tail, take, drop,
19 unsafeSlice, unsafeInit, unsafeTail
20 ) where
21
22 import qualified Data.Vector.Generic.Mutable as MVector
23 import Data.Vector.Generic.Mutable ( MVector )
24
25 import Data.Vector.Generic.Base ( Vector, Mutable )
26
27 import Data.Vector.Fusion.Stream ( Stream, MStream )
28 import qualified Data.Vector.Fusion.Stream as Stream
29
30 import Control.Monad.Primitive
31 import Control.Monad.ST ( ST )
32 import Control.Monad ( liftM )
33 import Prelude hiding ( init, tail, take, drop, reverse, map, filter )
34
35 #include "vector.h"
36
37 data New v a = New (forall s. ST s (Mutable v s a))
38
39 create :: (forall s. ST s (Mutable v s a)) -> New v a
40 {-# INLINE create #-}
41 create p = New p
42
43 run :: New v a -> ST s (Mutable v s a)
44 {-# INLINE run #-}
45 run (New p) = p
46
47 runPrim :: PrimMonad m => New v a -> m (Mutable v (PrimState m) a)
48 {-# INLINE runPrim #-}
49 runPrim (New p) = primToPrim p
50
51 apply :: (forall s. Mutable v s a -> Mutable v s a) -> New v a -> New v a
52 {-# INLINE apply #-}
53 apply f (New p) = New (liftM f p)
54
55 modify :: (forall s. Mutable v s a -> ST s ()) -> New v a -> New v a
56 {-# INLINE modify #-}
57 modify f (New p) = New (do { v <- p; f v; return v })
58
59 modifyWithStream :: (forall s. Mutable v s a -> Stream b -> ST s ())
60 -> New v a -> Stream b -> New v a
61 {-# INLINE_STREAM modifyWithStream #-}
62 modifyWithStream f (New p) s = s `seq` New (do { v <- p; f v s; return v })
63
64 unstream :: Vector v a => Stream a -> New v a
65 {-# INLINE_STREAM unstream #-}
66 unstream s = s `seq` New (MVector.unstream s)
67
68 transform :: Vector v a =>
69 (forall m. Monad m => MStream m a -> MStream m a) -> New v a -> New v a
70 {-# INLINE_STREAM transform #-}
71 transform f (New p) = New (MVector.transform f =<< p)
72
73 {-# RULES
74
75 "transform/transform [New]"
76 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
77 (g :: forall m. Monad m => MStream m a -> MStream m a)
78 p .
79 transform f (transform g p) = transform (f . g) p
80
81 "transform/unstream [New]"
82 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
83 s.
84 transform f (unstream s) = unstream (f s)
85
86 #-}
87
88
89 unstreamR :: Vector v a => Stream a -> New v a
90 {-# INLINE_STREAM unstreamR #-}
91 unstreamR s = s `seq` New (MVector.unstreamR s)
92
93 transformR :: Vector v a =>
94 (forall m. Monad m => MStream m a -> MStream m a) -> New v a -> New v a
95 {-# INLINE_STREAM transformR #-}
96 transformR f (New p) = New (MVector.transformR f =<< p)
97
98 {-# RULES
99
100 "transformR/transformR [New]"
101 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
102 (g :: forall m. Monad m => MStream m a -> MStream m a)
103 p .
104 transformR f (transformR g p) = transformR (f . g) p
105
106 "transformR/unstreamR [New]"
107 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
108 s.
109 transformR f (unstreamR s) = unstreamR (f s)
110
111 #-}
112
113 slice :: Vector v a => Int -> Int -> New v a -> New v a
114 {-# INLINE_STREAM slice #-}
115 slice i n m = apply (MVector.slice i n) m
116
117 init :: Vector v a => New v a -> New v a
118 {-# INLINE_STREAM init #-}
119 init m = apply MVector.init m
120
121 tail :: Vector v a => New v a -> New v a
122 {-# INLINE_STREAM tail #-}
123 tail m = apply MVector.tail m
124
125 take :: Vector v a => Int -> New v a -> New v a
126 {-# INLINE_STREAM take #-}
127 take n m = apply (MVector.take n) m
128
129 drop :: Vector v a => Int -> New v a -> New v a
130 {-# INLINE_STREAM drop #-}
131 drop n m = apply (MVector.drop n) m
132
133 unsafeSlice :: Vector v a => Int -> Int -> New v a -> New v a
134 {-# INLINE_STREAM unsafeSlice #-}
135 unsafeSlice i n m = apply (MVector.unsafeSlice i n) m
136
137 unsafeInit :: Vector v a => New v a -> New v a
138 {-# INLINE_STREAM unsafeInit #-}
139 unsafeInit m = apply MVector.unsafeInit m
140
141 unsafeTail :: Vector v a => New v a -> New v a
142 {-# INLINE_STREAM unsafeTail #-}
143 unsafeTail m = apply MVector.unsafeTail m
144
145 {-# RULES
146
147 "slice/unstream [New]" forall i n s.
148 slice i n (unstream s) = unstream (Stream.slice i n s)
149
150 "init/unstream [New]" forall s.
151 init (unstream s) = unstream (Stream.init s)
152
153 "tail/unstream [New]" forall s.
154 tail (unstream s) = unstream (Stream.tail s)
155
156 "take/unstream [New]" forall n s.
157 take n (unstream s) = unstream (Stream.take n s)
158
159 "drop/unstream [New]" forall n s.
160 drop n (unstream s) = unstream (Stream.drop n s)
161
162 "unsafeSlice/unstream [New]" forall i n s.
163 unsafeSlice i n (unstream s) = unstream (Stream.slice i n s)
164
165 "unsafeInit/unstream [New]" forall s.
166 unsafeInit (unstream s) = unstream (Stream.init s)
167
168 "unsafeTail/unstream [New]" forall s.
169 unsafeTail (unstream s) = unstream (Stream.tail s)
170
171 #-}
172