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