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