Add INLINE pragma
[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 : Roman Leshchinskiy <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, null,
23
24 -- * Construction
25 empty, singleton, cons, snoc, replicate, (++), copy,
26
27 -- * Accessing individual elements
28 (!), head, last,
29
30 -- * Subvectors
31 slice, init, tail, take, drop,
32
33 -- * Permutations
34 accum, (//), update, backpermute, reverse,
35
36 -- * Mapping
37 map, concatMap,
38
39 -- * Zipping and unzipping
40 zipWith, zipWith3, zip, zip3, unzip, unzip3,
41
42 -- * Comparisons
43 eq, cmp,
44
45 -- * Filtering
46 filter, takeWhile, dropWhile,
47
48 -- * Searching
49 elem, notElem, find, findIndex,
50
51 -- * Folding
52 foldl, foldl1, foldl', foldl1', foldr, foldr1,
53
54 -- * Specialised folds
55 and, or, sum, product, maximum, minimum,
56
57 -- * Enumeration
58 enumFromTo, enumFromThenTo,
59
60 -- * Unfolding
61 unfoldr,
62
63 -- * Scans
64 prescanl, prescanl',
65
66 -- * Conversion to/from lists
67 toList, fromList,
68
69 -- * Conversion to/from Streams
70 stream, unstream,
71
72 -- * MVector-based initialisation
73 new,
74
75 -- * Unsafe functions
76 unsafeSlice, unsafeIndex,
77
78 -- * Utility functions
79 vlength, vnew
80 ) where
81
82 import qualified Data.Vector.MVector as MVector
83 import Data.Vector.MVector ( MVector )
84
85 import qualified Data.Vector.MVector.New as New
86 import Data.Vector.MVector.New ( New )
87
88 import qualified Data.Vector.Fusion.Stream as Stream
89 import Data.Vector.Fusion.Stream ( Stream, MStream )
90 import qualified Data.Vector.Fusion.Stream.Monadic as MStream
91 import Data.Vector.Fusion.Stream.Size
92
93 import Control.Exception ( assert )
94
95 import Prelude hiding ( length, null,
96 replicate, (++),
97 head, last,
98 init, tail, take, drop, reverse,
99 map, concatMap,
100 zipWith, zipWith3, zip, zip3, unzip, unzip3,
101 filter, takeWhile, dropWhile,
102 elem, notElem,
103 foldl, foldl1, foldr, foldr1,
104 and, or, sum, product, maximum, minimum,
105 enumFromTo, enumFromThenTo )
106
107 -- | Class of immutable vectors.
108 --
109 class IVector v a where
110 -- | Construct a pure vector from a monadic initialiser (not fusible!)
111 vnew :: (forall mv m. MVector mv m a => m (mv a)) -> v a
112
113 -- | Length of the vector (not fusible!)
114 vlength :: v a -> Int
115
116 -- | Yield a part of the vector without copying it. No range checks!
117 unsafeSlice :: v a -> Int -> Int -> v a
118
119 -- | Apply the given function to the element at the given position. This
120 -- interface prevents us from being too lazy. Suppose we had
121 --
122 -- > unsafeIndex' :: v a -> Int -> a
123 --
124 -- instead. Now, if we wanted to copy a vector, we'd do something like
125 --
126 -- > copy mv v ... = ... unsafeWrite mv i (unsafeIndex' v i) ...
127 --
128 -- For lazy vectors, the indexing would not be evaluated which means that we
129 -- would retain a reference to the original vector in each element we write.
130 -- This would be bad!
131 --
132 -- With 'unsafeIndex', we can do
133 --
134 -- > copy mv v ... = ... unsafeIndex v i (unsafeWrite mv i) ...
135 --
136 -- which does not have this problem.
137 --
138 unsafeIndex :: v a -> Int -> (a -> b) -> b
139
140 -- Fusion
141 -- ------
142
143 -- | Construct a pure vector from a monadic initialiser
144 new :: IVector v a => New a -> v a
145 {-# INLINE new #-}
146 new m = new' undefined m
147
148 -- | Same as 'new' but with a dummy argument necessary for correctly typing
149 -- the rule @uninplace@.
150 --
151 -- See http://hackage.haskell.org/trac/ghc/ticket/2600
152 new' :: IVector v a => v a -> New a -> v a
153 {-# INLINE_STREAM new' #-}
154 new' _ m = vnew (New.run m)
155
156 -- | Convert a vector to a 'Stream'
157 stream :: IVector v a => v a -> Stream a
158 {-# INLINE_STREAM stream #-}
159 stream v = v `seq` (Stream.unfoldr get 0 `Stream.sized` Exact n)
160 where
161 n = length v
162
163 {-# INLINE get #-}
164 get i | i < n = unsafeIndex v i $ \x -> Just (x, i+1)
165 | otherwise = Nothing
166
167 -- | Create a vector from a 'Stream'
168 unstream :: IVector v a => Stream a -> v a
169 {-# INLINE unstream #-}
170 unstream s = new (New.unstream s)
171
172 {-# RULES
173
174 "stream/unstream [IVector]" forall v s.
175 stream (new' v (New.unstream s)) = s
176
177 "New.unstream/stream/new [IVector]" forall v p.
178 New.unstream (stream (new' v p)) = p
179
180 #-}
181
182 inplace :: (forall m. Monad m => MStream m a -> MStream m a)
183 -> Stream a -> Stream a
184 {-# INLINE_STREAM inplace #-}
185 inplace f s = f s
186
187 {-# RULES
188
189 "inplace [IVector]"
190 forall (f :: forall m. Monad m => MStream m a -> MStream m a) v m.
191 New.unstream (inplace f (stream (new' v m))) = New.transform f m
192
193 "uninplace [IVector]"
194 forall (f :: forall m. Monad m => MStream m a -> MStream m a) v m.
195 stream (new' v (New.transform f m)) = inplace f (stream (new' v m))
196
197 "inplace/inplace [IVector]"
198 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
199 (g :: forall m. Monad m => MStream m a -> MStream m a)
200 s.
201 inplace f (inplace g s) = inplace (f . g) s
202
203 #-}
204
205 -- Length
206 -- ------
207
208 length :: IVector v a => v a -> Int
209 {-# INLINE_STREAM length #-}
210 length v = vlength v
211
212 {-# RULES
213
214 "length/unstream [IVector]" forall v s.
215 length (new' v (New.unstream s)) = Stream.length s
216
217 #-}
218
219 null :: IVector v a => v a -> Bool
220 {-# INLINE_STREAM null #-}
221 null v = vlength v == 0
222
223 {-# RULES
224
225 "null/unstream [IVector]" forall v s.
226 null (new' v (New.unstream s)) = Stream.null s
227
228 #-}
229
230 -- Construction
231 -- ------------
232
233 -- | Empty vector
234 empty :: IVector v a => v a
235 {-# INLINE empty #-}
236 empty = unstream Stream.empty
237
238 -- | Vector with exaclty one element
239 singleton :: IVector v a => a -> v a
240 {-# INLINE singleton #-}
241 singleton x = unstream (Stream.singleton x)
242
243 -- | Vector of the given length with the given value in each position
244 replicate :: IVector v a => Int -> a -> v a
245 {-# INLINE replicate #-}
246 replicate n = unstream . Stream.replicate n
247
248 -- | Prepend an element
249 cons :: IVector v a => a -> v a -> v a
250 {-# INLINE cons #-}
251 cons x = unstream . Stream.cons x . stream
252
253 -- | Append an element
254 snoc :: IVector v a => v a -> a -> v a
255 {-# INLINE snoc #-}
256 snoc v = unstream . Stream.snoc (stream v)
257
258 infixr 5 ++
259 -- | Concatenate two vectors
260 (++) :: IVector v a => v a -> v a -> v a
261 {-# INLINE (++) #-}
262 v ++ w = unstream (stream v Stream.++ stream w)
263
264 -- | Create a copy of a vector. Useful when dealing with slices.
265 copy :: IVector v a => v a -> v a
266 {-# INLINE_STREAM copy #-}
267 copy = unstream . stream
268
269 {-# RULES
270
271 "copy/unstream [IVector]" forall v s.
272 copy (new' v (New.unstream s)) = new' v (New.unstream s)
273
274 #-}
275
276 -- Accessing individual elements
277 -- -----------------------------
278
279 -- | Indexing
280 (!) :: IVector v a => v a -> Int -> a
281 {-# INLINE_STREAM (!) #-}
282 v ! i = assert (i >= 0 && i < length v)
283 $ unsafeIndex v i id
284
285 -- | First element
286 head :: IVector v a => v a -> a
287 {-# INLINE_STREAM head #-}
288 head v = v ! 0
289
290 -- | Last element
291 last :: IVector v a => v a -> a
292 {-# INLINE_STREAM last #-}
293 last v = v ! (length v - 1)
294
295 {-# RULES
296
297 "(!)/unstream [IVector]" forall v i s.
298 new' v (New.unstream s) ! i = s Stream.!! i
299
300 "head/unstream [IVector]" forall v s.
301 head (new' v (New.unstream s)) = Stream.head s
302
303 "last/unstream [IVector]" forall v s.
304 last (new' v (New.unstream s)) = Stream.last s
305
306 #-}
307
308 -- Subarrays
309 -- ---------
310
311 -- FIXME: slicing doesn't work with the inplace stuff at the moment
312
313 -- | Yield a part of the vector without copying it. Safer version of
314 -- 'unsafeSlice'.
315 slice :: IVector v a => v a -> Int -- ^ starting index
316 -> Int -- ^ length
317 -> v a
318 {-# INLINE_STREAM slice #-}
319 slice v i n = assert (i >= 0 && n >= 0 && i+n <= length v)
320 $ unsafeSlice v i n
321
322 -- | Yield all but the last element without copying.
323 init :: IVector v a => v a -> v a
324 {-# INLINE_STREAM init #-}
325 init v = slice v 0 (length v - 1)
326
327 -- | All but the first element (without copying).
328 tail :: IVector v a => v a -> v a
329 {-# INLINE_STREAM tail #-}
330 tail v = slice v 1 (length v - 1)
331
332 -- | Yield the first @n@ elements without copying.
333 take :: IVector v a => Int -> v a -> v a
334 {-# INLINE_STREAM take #-}
335 take n v = slice v 0 (min n' (length v))
336 where n' = max n 0
337
338 -- | Yield all but the first @n@ elements without copying.
339 drop :: IVector v a => Int -> v a -> v a
340 {-# INLINE_STREAM drop #-}
341 drop n v = slice v (min n' len) (max 0 (len - n'))
342 where n' = max n 0
343 len = length v
344
345 {-# RULES
346
347 "slice/new [IVector]" forall v p i n.
348 slice (new' v p) i n = new' v (New.slice p i n)
349
350 "init/new [IVector]" forall v p.
351 init (new' v p) = new' v (New.init p)
352
353 "tail/new [IVector]" forall v p.
354 tail (new' v p) = new' v (New.tail p)
355
356 "take/new [IVector]" forall n v p.
357 take n (new' v p) = new' v (New.take n p)
358
359 "drop/new [IVector]" forall n v p.
360 drop n (new' v p) = new' v (New.drop n p)
361
362 #-}
363
364 -- Permutations
365 -- ------------
366
367 accum :: IVector v a => (a -> b -> a) -> v a -> [(Int,b)] -> v a
368 {-# INLINE accum #-}
369 accum f v us = new (New.accum f (New.unstream (stream v))
370 (Stream.fromList us))
371
372 (//) :: IVector v a => v a -> [(Int, a)] -> v a
373 {-# INLINE (//) #-}
374 v // us = new (New.update (New.unstream (stream v))
375 (Stream.fromList us))
376
377 update :: (IVector v a, IVector v (Int, a)) => v a -> v (Int, a) -> v a
378 {-# INLINE update #-}
379 update v w = new (New.update (New.unstream (stream v)) (stream w))
380
381 backpermute :: (IVector v a, IVector v Int) => v a -> v Int -> v a
382 {-# INLINE backpermute #-}
383 backpermute v is = v `seq` map (v!) is
384
385 reverse :: (IVector v a) => v a -> v a
386 {-# INLINE reverse #-}
387 reverse = new . New.reverse . New.unstream . stream
388
389 -- Mapping
390 -- -------
391
392 -- | Map a function over a vector
393 map :: (IVector v a, IVector v b) => (a -> b) -> v a -> v b
394 {-# INLINE map #-}
395 map f = unstream . Stream.map f . stream
396
397 inplace_map :: IVector v a => (a -> a) -> v a -> v a
398 {-# INLINE inplace_map #-}
399 inplace_map f = unstream . inplace (MStream.map f) . stream
400
401 {-# RULES
402
403 "map->inplace_map [IVector]" map = inplace_map
404
405 #-}
406
407 concatMap :: (IVector v a, IVector v b) => (a -> v b) -> v a -> v b
408 {-# INLINE concatMap #-}
409 concatMap f = unstream . Stream.concatMap (stream . f) . stream
410
411 -- Zipping/unzipping
412 -- -----------------
413
414 -- | Zip two vectors with the given function.
415 zipWith :: (IVector v a, IVector v b, IVector v c) => (a -> b -> c) -> v a -> v b -> v c
416 {-# INLINE zipWith #-}
417 zipWith f xs ys = unstream (Stream.zipWith f (stream xs) (stream ys))
418
419 -- | Zip three vectors with the given function.
420 zipWith3 :: (IVector v a, IVector v b, IVector v c, IVector v d) => (a -> b -> c -> d) -> v a -> v b -> v c -> v d
421 {-# INLINE zipWith3 #-}
422 zipWith3 f xs ys zs = unstream (Stream.zipWith3 f (stream xs) (stream ys) (stream zs))
423
424 zip :: (IVector v a, IVector v b, IVector v (a,b)) => v a -> v b -> v (a, b)
425 {-# INLINE zip #-}
426 zip = zipWith (,)
427
428 zip3 :: (IVector v a, IVector v b, IVector v c, IVector v (a, b, c)) => v a -> v b -> v c -> v (a, b, c)
429 {-# INLINE zip3 #-}
430 zip3 = zipWith3 (,,)
431
432 unzip :: (IVector v a, IVector v b, IVector v (a,b)) => v (a, b) -> (v a, v b)
433 {-# INLINE unzip #-}
434 unzip xs = (map fst xs, map snd xs)
435
436 unzip3 :: (IVector v a, IVector v b, IVector v c, IVector v (a, b, c)) => v (a, b, c) -> (v a, v b, v c)
437 {-# INLINE unzip3 #-}
438 unzip3 xs = (map (\(a, b, c) -> a) xs, map (\(a, b, c) -> b) xs, map (\(a, b, c) -> c) xs)
439
440 -- Comparisons
441 -- -----------
442
443 eq :: (IVector v a, Eq a) => v a -> v a -> Bool
444 {-# INLINE eq #-}
445 xs `eq` ys = stream xs == stream ys
446
447 cmp :: (IVector v a, Ord a) => v a -> v a -> Ordering
448 {-# INLINE cmp #-}
449 cmp xs ys = compare (stream xs) (stream ys)
450
451 -- Filtering
452 -- ---------
453
454 -- | Drop elements which do not satisfy the predicate
455 filter :: IVector v a => (a -> Bool) -> v a -> v a
456 {-# INLINE filter #-}
457 filter f = unstream . inplace (MStream.filter f) . stream
458
459 -- | Yield the longest prefix of elements satisfying the predicate.
460 takeWhile :: IVector v a => (a -> Bool) -> v a -> v a
461 {-# INLINE takeWhile #-}
462 takeWhile f = unstream . Stream.takeWhile f . stream
463
464 -- | Drop the longest prefix of elements that satisfy the predicate.
465 dropWhile :: IVector v a => (a -> Bool) -> v a -> v a
466 {-# INLINE dropWhile #-}
467 dropWhile f = unstream . Stream.dropWhile f . stream
468
469 -- Searching
470 -- ---------
471
472 infix 4 `elem`
473 -- | Check whether the vector contains an element
474 elem :: (IVector v a, Eq a) => a -> v a -> Bool
475 {-# INLINE elem #-}
476 elem x = Stream.elem x . stream
477
478 infix 4 `notElem`
479 -- | Inverse of `elem`
480 notElem :: (IVector v a, Eq a) => a -> v a -> Bool
481 {-# INLINE notElem #-}
482 notElem x = Stream.notElem x . stream
483
484 -- | Yield 'Just' the first element matching the predicate or 'Nothing' if no
485 -- such element exists.
486 find :: IVector v a => (a -> Bool) -> v a -> Maybe a
487 {-# INLINE find #-}
488 find f = Stream.find f . stream
489
490 -- | Yield 'Just' the index of the first element matching the predicate or
491 -- 'Nothing' if no such element exists.
492 findIndex :: IVector v a => (a -> Bool) -> v a -> Maybe Int
493 {-# INLINE findIndex #-}
494 findIndex f = Stream.findIndex f . stream
495
496 -- Folding
497 -- -------
498
499 -- | Left fold
500 foldl :: IVector v b => (a -> b -> a) -> a -> v b -> a
501 {-# INLINE foldl #-}
502 foldl f z = Stream.foldl f z . stream
503
504 -- | Lefgt fold on non-empty vectors
505 foldl1 :: IVector v a => (a -> a -> a) -> v a -> a
506 {-# INLINE foldl1 #-}
507 foldl1 f = Stream.foldl1 f . stream
508
509 -- | Left fold with strict accumulator
510 foldl' :: IVector v b => (a -> b -> a) -> a -> v b -> a
511 {-# INLINE foldl' #-}
512 foldl' f z = Stream.foldl' f z . stream
513
514 -- | Left fold on non-empty vectors with strict accumulator
515 foldl1' :: IVector v a => (a -> a -> a) -> v a -> a
516 {-# INLINE foldl1' #-}
517 foldl1' f = Stream.foldl1' f . stream
518
519 -- | Right fold
520 foldr :: IVector v a => (a -> b -> b) -> b -> v a -> b
521 {-# INLINE foldr #-}
522 foldr f z = Stream.foldr f z . stream
523
524 -- | Right fold on non-empty vectors
525 foldr1 :: IVector v a => (a -> a -> a) -> v a -> a
526 {-# INLINE foldr1 #-}
527 foldr1 f = Stream.foldr1 f . stream
528
529 -- Specialised folds
530 -- -----------------
531
532 and :: IVector v Bool => v Bool -> Bool
533 {-# INLINE and #-}
534 and = Stream.and . stream
535
536 or :: IVector v Bool => v Bool -> Bool
537 {-# INLINE or #-}
538 or = Stream.or . stream
539
540 sum :: (IVector v a, Num a) => v a -> a
541 {-# INLINE sum #-}
542 sum = Stream.foldl' (+) 0 . stream
543
544 product :: (IVector v a, Num a) => v a -> a
545 {-# INLINE product #-}
546 product = Stream.foldl' (*) 1 . stream
547
548 maximum :: (IVector v a, Ord a) => v a -> a
549 {-# INLINE maximum #-}
550 maximum = Stream.foldl1' max . stream
551
552 minimum :: (IVector v a, Ord a) => v a -> a
553 {-# INLINE minimum #-}
554 minimum = Stream.foldl1' min . stream
555
556 -- Enumeration
557 -- -----------
558
559 enumFromTo :: (IVector v a, Enum a) => a -> a -> v a
560 {-# INLINE enumFromTo #-}
561 enumFromTo from to = from `seq` to `seq` unfoldr enumFromTo_go (fromEnum from)
562 where
563 to_i = fromEnum to
564 enumFromTo_go i | i <= to_i = Just (toEnum i, i + 1)
565 | otherwise = Nothing
566
567 enumFromThenTo :: (IVector v a, Enum a) => a -> a -> a -> v a
568 {-# INLINE enumFromThenTo #-}
569 enumFromThenTo from next to = from `seq` next `seq` to `seq` unfoldr enumFromThenTo_go from_i
570 where
571 from_i = fromEnum from
572 to_i = fromEnum to
573 step_i = fromEnum next - from_i
574 enumFromThenTo_go i | i <= to_i = Just (toEnum i, i + step_i)
575 | otherwise = Nothing
576
577 -- Unfolding
578 -- ---------
579
580 unfoldr :: IVector v a => (b -> Maybe (a, b)) -> b -> v a
581 {-# INLINE unfoldr #-}
582 unfoldr f = unstream . Stream.unfoldr f
583
584 -- Scans
585 -- -----
586
587 -- | Prefix scan
588 prescanl :: (IVector v a, IVector v b) => (a -> b -> a) -> a -> v b -> v a
589 {-# INLINE prescanl #-}
590 prescanl f z = unstream . Stream.prescanl f z . stream
591
592 inplace_prescanl :: IVector v a => (a -> a -> a) -> a -> v a -> v a
593 {-# INLINE inplace_prescanl #-}
594 inplace_prescanl f z = unstream . inplace (MStream.prescanl f z) . stream
595
596 {-# RULES
597
598 "prescanl -> inplace_prescanl [IVector]" prescanl = inplace_prescanl
599
600 #-}
601
602 -- | Prefix scan with strict accumulator
603 prescanl' :: (IVector v a, IVector v b) => (a -> b -> a) -> a -> v b -> v a
604 {-# INLINE prescanl' #-}
605 prescanl' f z = unstream . Stream.prescanl' f z . stream
606
607 inplace_prescanl' :: IVector v a => (a -> a -> a) -> a -> v a -> v a
608 {-# INLINE inplace_prescanl' #-}
609 inplace_prescanl' f z = unstream . inplace (MStream.prescanl' f z) . stream
610
611 {-# RULES
612
613 "prescanl' -> inplace_prescanl' [IVector]" prescanl' = inplace_prescanl'
614
615 #-}
616
617
618 -- | Convert a vector to a list
619 toList :: IVector v a => v a -> [a]
620 {-# INLINE toList #-}
621 toList = Stream.toList . stream
622
623 -- | Convert a list to a vector
624 fromList :: IVector v a => [a] -> v a
625 {-# INLINE fromList #-}
626 fromList = unstream . Stream.fromList
627