Fusible length
[darcs-mirrors/vector.git] / Data / Vector / IVector.hs
1 {-# LANGUAGE Rank2Types, MultiParamTypeClasses #-}
2 -- |
3 -- Module : Data.Vector.IVector
4 -- Copyright : (c) Roman Leshchinskiy 2008
5 -- License : BSD-style
6 --
7 -- Maintainer : rl@cse.unsw.edu.au
8 -- Stability : experimental
9 -- Portability : non-portable
10 --
11 -- Generic interface to pure vectors
12 --
13
14 #include "phases.h"
15
16 module Data.Vector.IVector (
17 -- * Immutable vectors
18 IVector,
19
20 -- * Length information
21 length,
22
23 -- * Construction
24 empty, singleton, cons, snoc, replicate, (++),
25
26 -- * Accessing individual elements
27 (!), head, last,
28
29 -- * Subvectors
30 slice, takeSlice, take, dropSlice, drop,
31
32 -- * Mapping and zipping
33 map, zipWith,
34
35 -- * Filtering
36 filter, takeWhileSlice, takeWhile, dropWhileSlice, dropWhile,
37
38 -- * Searching
39 elem, notElem, find, findIndex,
40
41 -- * Folding
42 foldl, foldl1, foldl', foldl1', foldr, foldr1,
43
44 -- * Conversion to/from lists
45 toList, fromList,
46
47 -- * Conversion to/from Streams
48 stream, unstream,
49
50 -- * MVector-based initialisation
51 create,
52
53 -- * Unsafe functions
54 unsafeSlice, unsafeIndex,
55
56 -- * Utility functions
57 vlength
58 ) where
59
60 import qualified Data.Vector.MVector as MVector
61 import Data.Vector.MVector ( MVector )
62
63 import qualified Data.Vector.Stream as Stream
64 import Data.Vector.Stream ( Stream )
65 import Data.Vector.Stream.Size
66
67 import Control.Exception ( assert )
68
69 import Prelude hiding ( length,
70 replicate, (++),
71 head, last,
72 init, tail, take, drop,
73 map, zipWith,
74 filter, takeWhile, dropWhile,
75 elem, notElem,
76 foldl, foldl1, foldr, foldr1 )
77
78 -- | Class of immutable vectors. Just like with 'MVector', the type of the
79 -- elements can be restricted by using GADTs.
80 --
81 class IVector v a where
82 -- | Construct a pure vector from a monadic initialiser.
83 create :: (forall mv m. MVector mv m a => m (mv m a)) -> v a
84
85 -- | Length of the vector (not fusible!)
86 vlength :: v a -> Int
87
88 -- | Yield a part of the vector without copying it. No range checks!
89 unsafeSlice :: v a -> Int -> Int -> v a
90
91 -- | Apply the given function to the element at the given position. This
92 -- interface prevents us from being too lazy. Suppose we had
93 --
94 -- > unsafeIndex' :: v a -> Int -> a
95 --
96 -- instead. Now, if we wanted to copy a vector, we'd do something like
97 --
98 -- > copy mv v ... = ... unsafeWrite mv i (unsafeIndex' v i) ...
99 --
100 -- For lazy vectors, the indexing would not be evaluated which means that we
101 -- would retain a reference to the original vector in each element we write.
102 -- This would be bad!
103 --
104 -- With 'unsafeIndex', we can do
105 --
106 -- > copy mv v ... = ... unsafeIndex v i (unsafeWrite mv i) ...
107 --
108 -- which does not have this problem.
109 --
110 unsafeIndex :: v a -> Int -> (a -> b) -> b
111
112 -- | Convert a vector to a 'Stream'
113 stream :: IVector v a => v a -> Stream a
114 {-# INLINE_STREAM stream #-}
115 stream v = v `seq` (Stream.unfold get 0 `Stream.sized` Exact n)
116 where
117 n = length v
118
119 {-# INLINE get #-}
120 get i | i < n = unsafeIndex v i $ \x -> Just (x, i+1)
121 | otherwise = Nothing
122
123 -- | Create a vector from a 'Stream'
124 unstream :: IVector v a => Stream a -> v a
125 {-# INLINE_STREAM unstream #-}
126 unstream s = create (MVector.unstream s)
127
128 {-# RULES
129
130 "stream/unstream [IVector]" forall s.
131 stream (unstream s) = s
132
133 #-}
134
135 -- Length
136 -- ------
137
138 length :: IVector v a => v a -> Int
139 {-# INLINE_STREAM length #-}
140 length v = vlength v
141
142 {-# RULES
143
144 "length/unstream [IVector]" forall s.
145 length (unstream s) = Stream.length s
146
147 #-}
148
149 -- Construction
150 -- ------------
151
152 -- | Empty vector
153 empty :: IVector v a => v a
154 {-# INLINE empty #-}
155 empty = unstream Stream.empty
156
157 -- | Vector with exaclty one element
158 singleton :: IVector v a => a -> v a
159 {-# INLINE singleton #-}
160 singleton x = unstream (Stream.singleton x)
161
162 -- | Vector of the given length with the given value in each position
163 replicate :: IVector v a => Int -> a -> v a
164 {-# INLINE replicate #-}
165 replicate n = unstream . Stream.replicate n
166
167 -- | Prepend an element
168 cons :: IVector v a => a -> v a -> v a
169 {-# INLINE cons #-}
170 cons x = unstream . Stream.cons x . stream
171
172 -- | Append an element
173 snoc :: IVector v a => v a -> a -> v a
174 {-# INLINE snoc #-}
175 snoc v = unstream . Stream.snoc (stream v)
176
177 infixr 5 ++
178 -- | Concatenate two vectors
179 (++) :: IVector v a => v a -> v a -> v a
180 {-# INLINE (++) #-}
181 v ++ w = unstream (stream v Stream.++ stream w)
182
183 -- Accessing individual elements
184 -- -----------------------------
185
186 -- | Indexing
187 (!) :: IVector v a => v a -> Int -> a
188 {-# INLINE (!) #-}
189 v ! i = assert (i >= 0 && i < length v)
190 $ unsafeIndex v i id
191
192 -- | First element
193 head :: IVector v a => v a -> a
194 {-# INLINE head #-}
195 head v = v ! 0
196
197 -- | Last element
198 last :: IVector v a => v a -> a
199 {-# INLINE last #-}
200 last v = v ! (length v - 1)
201
202 -- Subarrays
203 -- ---------
204
205 -- | Yield a part of the vector without copying it. Safer version of
206 -- 'unsafeSlice'.
207 slice :: IVector v a => v a -> Int -- ^ starting index
208 -> Int -- ^ length
209 -> v a
210 {-# INLINE slice #-}
211 slice v i n = assert (i >= 0 && n >= 0 && i+n <= length v)
212 $ unsafeSlice v i n
213
214 -- | Yield the first @n@ elements without copying.
215 takeSlice :: IVector v a => Int -> v a -> v a
216 {-# INLINE takeSlice #-}
217 takeSlice n v = slice v 0 n
218
219 -- | Copy the first @n@ elements to a new vector.
220 take :: IVector v a => Int -> v a -> v a
221 {-# INLINE take #-}
222 take n = unstream . Stream.take n . stream
223
224 -- | Yield all but the first @n@ elements without copying.
225 dropSlice :: IVector v a => Int -> v a -> v a
226 {-# INLINE dropSlice #-}
227 dropSlice n v = slice v n (length v - n)
228
229 -- | Copy all but the first @n@ elements to a new vectors.
230 drop :: IVector v a => Int -> v a -> v a
231 {-# INLINE drop #-}
232 drop n = unstream . Stream.drop n . stream
233
234 -- Mapping/zipping
235 -- ---------------
236
237 -- | Map a function over a vector
238 map :: (IVector v a, IVector v b) => (a -> b) -> v a -> v b
239 {-# INLINE map #-}
240 map f = unstream . Stream.map f . stream
241
242 -- | Zip two vectors with the given function.
243 zipWith :: (IVector v a, IVector v b, IVector v c) => (a -> b -> c) -> v a -> v b -> v c
244 {-# INLINE zipWith #-}
245 zipWith f xs ys = unstream (Stream.zipWith f (stream xs) (stream ys))
246
247 -- Filtering
248 -- ---------
249
250 -- | Drop elements which do not satisfy the predicate
251 filter :: IVector v a => (a -> Bool) -> v a -> v a
252 {-# INLINE filter #-}
253 filter f = unstream . Stream.filter f . stream
254
255 -- | Yield the longest prefix of elements satisfying the predicate without
256 -- copying.
257 takeWhileSlice :: IVector v a => (a -> Bool) -> v a -> v a
258 {-# INLINE takeWhileSlice #-}
259 takeWhileSlice f v = case findIndex (not . f) v of
260 Just n -> takeSlice n v
261 Nothing -> v
262
263 -- | Copy the longest prefix of elements satisfying the predicate to a new
264 -- vector
265 takeWhile :: IVector v a => (a -> Bool) -> v a -> v a
266 {-# INLINE takeWhile #-}
267 takeWhile f = unstream . Stream.takeWhile f . stream
268
269 -- | Drop the longest prefix of elements that satisfy the predicate without
270 -- copying
271 dropWhileSlice :: IVector v a => (a -> Bool) -> v a -> v a
272 {-# INLINE dropWhileSlice #-}
273 dropWhileSlice f v = case findIndex (not . f) v of
274 Just n -> dropSlice n v
275 Nothing -> v
276
277 -- | Drop the longest prefix of elements that satisfy the predicate and copy
278 -- the rest to a new vector.
279 dropWhile :: IVector v a => (a -> Bool) -> v a -> v a
280 {-# INLINE dropWhile #-}
281 dropWhile f = unstream . Stream.dropWhile f . stream
282
283 -- Searching
284 -- ---------
285
286 infix 4 `elem`
287 -- | Check whether the vector contains an element
288 elem :: (IVector v a, Eq a) => a -> v a -> Bool
289 {-# INLINE elem #-}
290 elem x = Stream.elem x . stream
291
292 infix 4 `notElem`
293 -- | Inverse of `elem`
294 notElem :: (IVector v a, Eq a) => a -> v a -> Bool
295 {-# INLINE notElem #-}
296 notElem x = Stream.notElem x . stream
297
298 -- | Yield 'Just' the first element matching the predicate or 'Nothing' if no
299 -- such element exists.
300 find :: IVector v a => (a -> Bool) -> v a -> Maybe a
301 {-# INLINE find #-}
302 find f = Stream.find f . stream
303
304 -- | Yield 'Just' the index of the first element matching the predicate or
305 -- 'Nothing' if no such element exists.
306 findIndex :: IVector v a => (a -> Bool) -> v a -> Maybe Int
307 {-# INLINE findIndex #-}
308 findIndex f = Stream.findIndex f . stream
309
310 -- Folding
311 -- -------
312
313 -- | Left fold
314 foldl :: IVector v b => (a -> b -> a) -> a -> v b -> a
315 {-# INLINE foldl #-}
316 foldl f z = Stream.foldl f z . stream
317
318 -- | Lefgt fold on non-empty vectors
319 foldl1 :: IVector v a => (a -> a -> a) -> v a -> a
320 {-# INLINE foldl1 #-}
321 foldl1 f = Stream.foldl1 f . stream
322
323 -- | Left fold with strict accumulator
324 foldl' :: IVector v b => (a -> b -> a) -> a -> v b -> a
325 {-# INLINE foldl' #-}
326 foldl' f z = Stream.foldl' f z . stream
327
328 -- | Left fold on non-empty vectors with strict accumulator
329 foldl1' :: IVector v a => (a -> a -> a) -> v a -> a
330 {-# INLINE foldl1' #-}
331 foldl1' f = Stream.foldl1' f . stream
332
333 -- | Right fold
334 foldr :: IVector v a => (a -> b -> b) -> b -> v a -> b
335 {-# INLINE foldr #-}
336 foldr f z = Stream.foldr f z . stream
337
338 -- | Right fold on non-empty vectors
339 foldr1 :: IVector v a => (a -> a -> a) -> v a -> a
340 {-# INLINE foldr1 #-}
341 foldr1 f = Stream.foldr1 f . stream
342
343 -- | Convert a vector to a list
344 toList :: IVector v a => v a -> [a]
345 {-# INLINE toList #-}
346 toList = Stream.toList . stream
347
348 -- | Convert a list to a vector
349 fromList :: IVector v a => [a] -> v a
350 {-# INLINE fromList #-}
351 fromList = unstream . Stream.fromList
352