Tie New to a particular vector type
[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(..), run, apply, modify, unstream, transform, unstreamR, transformR,
17 accum, update, reverse,
18 slice, init, tail, take, drop,
19 unsafeSlice, unsafeInit, unsafeTail,
20 unsafeAccum, unsafeUpdate
21 ) where
22
23 import qualified Data.Vector.Generic.Mutable as MVector
24 import Data.Vector.Generic.Mutable ( MVector )
25
26 import Data.Vector.Generic.Base ( Vector, Mutable )
27
28 import Data.Vector.Fusion.Stream ( Stream, MStream )
29 import qualified Data.Vector.Fusion.Stream as Stream
30
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 run :: New v a -> ST s (Mutable v s a)
40 {-# INLINE run #-}
41 run (New p) = p
42
43 apply :: (forall s. Mutable v s a -> Mutable v s a) -> New v a -> New v a
44 {-# INLINE apply #-}
45 apply f (New p) = New (liftM f p)
46
47 modify :: (forall s. Mutable v s a -> ST s ()) -> New v a -> New v a
48 {-# INLINE modify #-}
49 modify f (New p) = New (do { v <- p; f v; return v })
50
51 unstream :: Vector v a => Stream a -> New v a
52 {-# INLINE_STREAM unstream #-}
53 unstream s = s `seq` New (MVector.unstream s)
54
55 transform :: Vector v a =>
56 (forall m. Monad m => MStream m a -> MStream m a) -> New v a -> New v a
57 {-# INLINE_STREAM transform #-}
58 transform f (New p) = New (MVector.transform f =<< p)
59
60 {-# RULES
61
62 "transform/transform [New]"
63 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
64 (g :: forall m. Monad m => MStream m a -> MStream m a)
65 p .
66 transform f (transform g p) = transform (f . g) p
67
68 "transform/unstream [New]"
69 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
70 s.
71 transform f (unstream s) = unstream (f s)
72
73 #-}
74
75
76 unstreamR :: Vector v a => Stream a -> New v a
77 {-# INLINE_STREAM unstreamR #-}
78 unstreamR s = s `seq` New (MVector.unstreamR s)
79
80 transformR :: Vector v a =>
81 (forall m. Monad m => MStream m a -> MStream m a) -> New v a -> New v a
82 {-# INLINE_STREAM transformR #-}
83 transformR f (New p) = New (MVector.transformR f =<< p)
84
85 {-# RULES
86
87 "transformR/transformR [New]"
88 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
89 (g :: forall m. Monad m => MStream m a -> MStream m a)
90 p .
91 transformR f (transformR g p) = transformR (f . g) p
92
93 "transformR/unstreamR [New]"
94 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
95 s.
96 transformR f (unstreamR s) = unstreamR (f s)
97
98 #-}
99
100 slice :: Vector v a => Int -> Int -> New v a -> New v a
101 {-# INLINE_STREAM slice #-}
102 slice i n m = apply (MVector.slice i n) m
103
104 init :: Vector v a => New v a -> New v a
105 {-# INLINE_STREAM init #-}
106 init m = apply MVector.init m
107
108 tail :: Vector v a => New v a -> New v a
109 {-# INLINE_STREAM tail #-}
110 tail m = apply MVector.tail m
111
112 take :: Vector v a => Int -> New v a -> New v a
113 {-# INLINE_STREAM take #-}
114 take n m = apply (MVector.take n) m
115
116 drop :: Vector v a => Int -> New v a -> New v a
117 {-# INLINE_STREAM drop #-}
118 drop n m = apply (MVector.drop n) m
119
120 unsafeSlice :: Vector v a => Int -> Int -> New v a -> New v a
121 {-# INLINE_STREAM unsafeSlice #-}
122 unsafeSlice i n m = apply (MVector.unsafeSlice i n) m
123
124 unsafeInit :: Vector v a => New v a -> New v a
125 {-# INLINE_STREAM unsafeInit #-}
126 unsafeInit m = apply MVector.unsafeInit m
127
128 unsafeTail :: Vector v a => New v a -> New v a
129 {-# INLINE_STREAM unsafeTail #-}
130 unsafeTail m = apply MVector.unsafeTail m
131
132 {-# RULES
133
134 "slice/unstream [New]" forall i n s.
135 slice i n (unstream s) = unstream (Stream.slice i n s)
136
137 "init/unstream [New]" forall s.
138 init (unstream s) = unstream (Stream.init s)
139
140 "tail/unstream [New]" forall s.
141 tail (unstream s) = unstream (Stream.tail s)
142
143 "take/unstream [New]" forall n s.
144 take n (unstream s) = unstream (Stream.take n s)
145
146 "drop/unstream [New]" forall n s.
147 drop n (unstream s) = unstream (Stream.drop n s)
148
149 "unsafeSlice/unstream [New]" forall i n s.
150 unsafeSlice i n (unstream s) = unstream (Stream.slice i n s)
151
152 "unsafeInit/unstream [New]" forall s.
153 unsafeInit (unstream s) = unstream (Stream.init s)
154
155 "unsafeTail/unstream [New]" forall s.
156 unsafeTail (unstream s) = unstream (Stream.tail s)
157
158 #-}
159
160 unsafeAccum
161 :: Vector v a => (a -> b -> a) -> New v a -> Stream (Int, b) -> New v a
162 {-# INLINE_STREAM unsafeAccum #-}
163 unsafeAccum f m s = s `seq` modify (\v -> MVector.unsafeAccum f v s) m
164
165 accum :: Vector v a => (a -> b -> a) -> New v a -> Stream (Int, b) -> New v a
166 {-# INLINE_STREAM accum #-}
167 accum f m s = s `seq` modify (\v -> MVector.accum f v s) m
168
169 unsafeUpdate :: Vector v a => New v a -> Stream (Int, a) -> New v a
170 {-# INLINE_STREAM unsafeUpdate #-}
171 unsafeUpdate m s = s `seq` modify (\v -> MVector.unsafeUpdate v s) m
172
173 update :: Vector v a => New v a -> Stream (Int, a) -> New v a
174 {-# INLINE_STREAM update #-}
175 update m s = s `seq` modify (\v -> MVector.update v s) m
176
177 reverse :: Vector v a => New v a -> New v a
178 {-# INLINE_STREAM reverse #-}
179 reverse m = modify (MVector.reverse) m
180