Data and Typeable instances for immutable vectors
[darcs-mirrors/vector.git] / Data / Vector / Primitive.hs
1 {-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, TypeFamilies #-}
2
3 -- |
4 -- Module : Data.Vector.Primitive
5 -- Copyright : (c) Roman Leshchinskiy 2008-2009
6 -- License : BSD-style
7 --
8 -- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
9 -- Stability : experimental
10 -- Portability : non-portable
11 --
12 -- Unboxed vectors of primitive types.
13 --
14
15 module Data.Vector.Primitive (
16 Vector, MVector(..), Prim,
17
18 -- * Length information
19 length, null,
20
21 -- * Construction
22 empty, singleton, cons, snoc, replicate, generate, (++), copy,
23
24 -- * Accessing individual elements
25 (!), head, last, indexM, headM, lastM,
26 unsafeIndex, unsafeHead, unsafeLast,
27 unsafeIndexM, unsafeHeadM, unsafeLastM,
28
29 -- * Subvectors
30 slice, init, tail, take, drop,
31 unsafeSlice, unsafeInit, unsafeTail, unsafeTake, unsafeDrop,
32
33 -- * Permutations
34 accum, accumulate_, (//), update_, backpermute, reverse,
35 unsafeAccum, unsafeAccumulate_,
36 unsafeUpd, unsafeUpdate_,
37 unsafeBackpermute,
38
39 -- * Mapping
40 map, imap, concatMap,
41
42 -- * Zipping and unzipping
43 zipWith, zipWith3, zipWith4, zipWith5, zipWith6,
44 izipWith, izipWith3, izipWith4, izipWith5, izipWith6,
45
46 -- * Filtering
47 filter, ifilter, takeWhile, dropWhile,
48 partition, unstablePartition, span, break,
49
50 -- * Searching
51 elem, notElem, find, findIndex, findIndices, elemIndex, elemIndices,
52
53 -- * Folding
54 foldl, foldl1, foldl', foldl1', foldr, foldr1, foldr', foldr1',
55 ifoldl, ifoldl', ifoldr, ifoldr',
56
57 -- * Specialised folds
58 all, any,
59 sum, product,
60 maximum, maximumBy, minimum, minimumBy,
61 minIndex, minIndexBy, maxIndex, maxIndexBy,
62
63 -- * Unfolding
64 unfoldr, unfoldrN,
65
66 -- * Scans
67 prescanl, prescanl',
68 postscanl, postscanl',
69 scanl, scanl', scanl1, scanl1',
70 prescanr, prescanr',
71 postscanr, postscanr',
72 scanr, scanr', scanr1, scanr1',
73
74 -- * Enumeration
75 enumFromN, enumFromStepN, enumFromTo, enumFromThenTo,
76
77 -- * Conversion to/from lists
78 toList, fromList, fromListN
79 ) where
80
81 import qualified Data.Vector.Generic as G
82 import Data.Vector.Primitive.Mutable ( MVector(..) )
83 import qualified Data.Vector.Fusion.Stream as Stream
84 import Data.Primitive.ByteArray
85 import Data.Primitive ( Prim )
86
87 import Control.Monad ( liftM )
88
89 import Prelude hiding ( length, null,
90 replicate, (++),
91 head, last,
92 init, tail, take, drop, reverse,
93 map, concatMap,
94 zipWith, zipWith3, zip, zip3, unzip, unzip3,
95 filter, takeWhile, dropWhile, span, break,
96 elem, notElem,
97 foldl, foldl1, foldr, foldr1,
98 all, any, sum, product, minimum, maximum,
99 scanl, scanl1, scanr, scanr1,
100 enumFromTo, enumFromThenTo )
101
102 import qualified Prelude
103
104 import Data.Typeable ( Typeable )
105 import Data.Data ( Data(..) )
106
107 -- | Unboxed vectors of primitive types
108 data Vector a = Vector {-# UNPACK #-} !Int
109 {-# UNPACK #-} !Int
110 {-# UNPACK #-} !ByteArray
111 deriving ( Typeable )
112
113 instance (Show a, Prim a) => Show (Vector a) where
114 show = (Prelude.++ " :: Data.Vector.Primitive.Vector") . ("fromList " Prelude.++) . show . toList
115
116 instance (Data a, Prim a) => Data (Vector a) where
117 gfoldl = G.gfoldl
118 toConstr _ = error "toConstr"
119 gunfold _ _ = error "gunfold"
120 dataTypeOf _ = G.mkType "Data.Vector.Primitive.Vector"
121 dataCast1 = G.dataCast
122
123
124 type instance G.Mutable Vector = MVector
125
126 instance Prim a => G.Vector Vector a where
127 {-# INLINE unsafeFreeze #-}
128 unsafeFreeze (MVector i n marr)
129 = Vector i n `liftM` unsafeFreezeByteArray marr
130
131 {-# INLINE basicLength #-}
132 basicLength (Vector _ n _) = n
133
134 {-# INLINE basicUnsafeSlice #-}
135 basicUnsafeSlice j n (Vector i _ arr) = Vector (i+j) n arr
136
137 {-# INLINE basicUnsafeIndexM #-}
138 basicUnsafeIndexM (Vector i _ arr) j = return (indexByteArray arr (i+j))
139
140 {-# INLINE elemseq #-}
141 elemseq _ = seq
142
143 -- See [HACKS:Eq and Ord instances]
144 instance (Prim a, Eq a) => Eq (Vector a) where
145 {-# INLINE (==) #-}
146 xs == ys = Stream.eq (G.stream xs) (G.stream ys)
147
148 {-# INLINE (/=) #-}
149 xs /= ys = not (Stream.eq (G.stream xs) (G.stream ys))
150
151 -- See [HACKS:Eq and Ord instances]
152 instance (Prim a, Ord a) => Ord (Vector a) where
153 {-# INLINE compare #-}
154 compare xs ys = Stream.cmp (G.stream xs) (G.stream ys)
155
156 {-# INLINE (<) #-}
157 xs < ys = Stream.cmp (G.stream xs) (G.stream ys) == LT
158
159 {-# INLINE (<=) #-}
160 xs <= ys = Stream.cmp (G.stream xs) (G.stream ys) /= GT
161
162 {-# INLINE (>) #-}
163 xs > ys = Stream.cmp (G.stream xs) (G.stream ys) == GT
164
165 {-# INLINE (>=) #-}
166 xs >= ys = Stream.cmp (G.stream xs) (G.stream ys) /= LT
167
168 -- Length
169 -- ------
170
171 length :: Prim a => Vector a -> Int
172 {-# INLINE length #-}
173 length = G.length
174
175 null :: Prim a => Vector a -> Bool
176 {-# INLINE null #-}
177 null = G.null
178
179 -- Construction
180 -- ------------
181
182 -- | Empty vector
183 empty :: Prim a => Vector a
184 {-# INLINE empty #-}
185 empty = G.empty
186
187 -- | Vector with exaclty one element
188 singleton :: Prim a => a -> Vector a
189 {-# INLINE singleton #-}
190 singleton = G.singleton
191
192 -- | Vector of the given length with the given value in each position
193 replicate :: Prim a => Int -> a -> Vector a
194 {-# INLINE replicate #-}
195 replicate = G.replicate
196
197 -- | Generate a vector of the given length by applying the function to each
198 -- index
199 generate :: Prim a => Int -> (Int -> a) -> Vector a
200 {-# INLINE generate #-}
201 generate = G.generate
202
203 -- | Prepend an element
204 cons :: Prim a => a -> Vector a -> Vector a
205 {-# INLINE cons #-}
206 cons = G.cons
207
208 -- | Append an element
209 snoc :: Prim a => Vector a -> a -> Vector a
210 {-# INLINE snoc #-}
211 snoc = G.snoc
212
213 infixr 5 ++
214 -- | Concatenate two vectors
215 (++) :: Prim a => Vector a -> Vector a -> Vector a
216 {-# INLINE (++) #-}
217 (++) = (G.++)
218
219 -- | Create a copy of a vector. Useful when dealing with slices.
220 copy :: Prim a => Vector a -> Vector a
221 {-# INLINE copy #-}
222 copy = G.copy
223
224 -- Accessing individual elements
225 -- -----------------------------
226
227 -- | Indexing
228 (!) :: Prim a => Vector a -> Int -> a
229 {-# INLINE (!) #-}
230 (!) = (G.!)
231
232 -- | First element
233 head :: Prim a => Vector a -> a
234 {-# INLINE head #-}
235 head = G.head
236
237 -- | Last element
238 last :: Prim a => Vector a -> a
239 {-# INLINE last #-}
240 last = G.last
241
242 -- | Unsafe indexing without bounds checking
243 unsafeIndex :: Prim a => Vector a -> Int -> a
244 {-# INLINE unsafeIndex #-}
245 unsafeIndex = G.unsafeIndex
246
247 -- | Yield the first element of a vector without checking if the vector is
248 -- empty
249 unsafeHead :: Prim a => Vector a -> a
250 {-# INLINE unsafeHead #-}
251 unsafeHead = G.unsafeHead
252
253 -- | Yield the last element of a vector without checking if the vector is
254 -- empty
255 unsafeLast :: Prim a => Vector a -> a
256 {-# INLINE unsafeLast #-}
257 unsafeLast = G.unsafeLast
258
259 -- | Monadic indexing which can be strict in the vector while remaining lazy in
260 -- the element
261 indexM :: (Prim a, Monad m) => Vector a -> Int -> m a
262 {-# INLINE indexM #-}
263 indexM = G.indexM
264
265 headM :: (Prim a, Monad m) => Vector a -> m a
266 {-# INLINE headM #-}
267 headM = G.headM
268
269 lastM :: (Prim a, Monad m) => Vector a -> m a
270 {-# INLINE lastM #-}
271 lastM = G.lastM
272
273 -- | Unsafe monadic indexing without bounds checks
274 unsafeIndexM :: (Prim a, Monad m) => Vector a -> Int -> m a
275 {-# INLINE unsafeIndexM #-}
276 unsafeIndexM = G.unsafeIndexM
277
278 unsafeHeadM :: (Prim a, Monad m) => Vector a -> m a
279 {-# INLINE unsafeHeadM #-}
280 unsafeHeadM = G.unsafeHeadM
281
282 unsafeLastM :: (Prim a, Monad m) => Vector a -> m a
283 {-# INLINE unsafeLastM #-}
284 unsafeLastM = G.unsafeLastM
285
286 -- Subarrays
287 -- ---------
288
289 -- | Yield a part of the vector without copying it. Safer version of
290 -- 'basicUnsafeSlice'.
291 slice :: Prim a => Int -- ^ starting index
292 -> Int -- ^ length
293 -> Vector a
294 -> Vector a
295 {-# INLINE slice #-}
296 slice = G.slice
297
298 -- | Yield all but the last element without copying.
299 init :: Prim a => Vector a -> Vector a
300 {-# INLINE init #-}
301 init = G.init
302
303 -- | All but the first element (without copying).
304 tail :: Prim a => Vector a -> Vector a
305 {-# INLINE tail #-}
306 tail = G.tail
307
308 -- | Yield the first @n@ elements without copying.
309 take :: Prim a => Int -> Vector a -> Vector a
310 {-# INLINE take #-}
311 take = G.take
312
313 -- | Yield all but the first @n@ elements without copying.
314 drop :: Prim a => Int -> Vector a -> Vector a
315 {-# INLINE drop #-}
316 drop = G.drop
317
318 -- | Unsafely yield a part of the vector without copying it and without
319 -- performing bounds checks.
320 unsafeSlice :: Prim a => Int -- ^ starting index
321 -> Int -- ^ length
322 -> Vector a
323 -> Vector a
324 {-# INLINE unsafeSlice #-}
325 unsafeSlice = G.unsafeSlice
326
327 unsafeInit :: Prim a => Vector a -> Vector a
328 {-# INLINE unsafeInit #-}
329 unsafeInit = G.unsafeInit
330
331 unsafeTail :: Prim a => Vector a -> Vector a
332 {-# INLINE unsafeTail #-}
333 unsafeTail = G.unsafeTail
334
335 unsafeTake :: Prim a => Int -> Vector a -> Vector a
336 {-# INLINE unsafeTake #-}
337 unsafeTake = G.unsafeTake
338
339 unsafeDrop :: Prim a => Int -> Vector a -> Vector a
340 {-# INLINE unsafeDrop #-}
341 unsafeDrop = G.unsafeDrop
342
343 -- Permutations
344 -- ------------
345
346 unsafeAccum :: Prim a => (a -> b -> a) -> Vector a -> [(Int,b)] -> Vector a
347 {-# INLINE unsafeAccum #-}
348 unsafeAccum = G.unsafeAccum
349
350 unsafeAccumulate_ :: (Prim a, Prim b) =>
351 (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a
352 {-# INLINE unsafeAccumulate_ #-}
353 unsafeAccumulate_ = G.unsafeAccumulate_
354
355 accum :: Prim a => (a -> b -> a) -> Vector a -> [(Int,b)] -> Vector a
356 {-# INLINE accum #-}
357 accum = G.accum
358
359 accumulate_ :: (Prim a, Prim b) =>
360 (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a
361 {-# INLINE accumulate_ #-}
362 accumulate_ = G.accumulate_
363
364 unsafeUpd :: Prim a => Vector a -> [(Int, a)] -> Vector a
365 {-# INLINE unsafeUpd #-}
366 unsafeUpd = G.unsafeUpd
367
368 unsafeUpdate_ :: Prim a => Vector a -> Vector Int -> Vector a -> Vector a
369 {-# INLINE unsafeUpdate_ #-}
370 unsafeUpdate_ = G.unsafeUpdate_
371
372 (//) :: Prim a => Vector a -> [(Int, a)] -> Vector a
373 {-# INLINE (//) #-}
374 (//) = (G.//)
375
376 update_ :: Prim a => Vector a -> Vector Int -> Vector a -> Vector a
377 {-# INLINE update_ #-}
378 update_ = G.update_
379
380 backpermute :: Prim a => Vector a -> Vector Int -> Vector a
381 {-# INLINE backpermute #-}
382 backpermute = G.backpermute
383
384 unsafeBackpermute :: Prim a => Vector a -> Vector Int -> Vector a
385 {-# INLINE unsafeBackpermute #-}
386 unsafeBackpermute = G.unsafeBackpermute
387
388 reverse :: Prim a => Vector a -> Vector a
389 {-# INLINE reverse #-}
390 reverse = G.reverse
391
392 -- Mapping
393 -- -------
394
395 -- | Map a function over a vector
396 map :: (Prim a, Prim b) => (a -> b) -> Vector a -> Vector b
397 {-# INLINE map #-}
398 map = G.map
399
400 -- | Apply a function to every index/value pair
401 imap :: (Prim a, Prim b) => (Int -> a -> b) -> Vector a -> Vector b
402 {-# INLINE imap #-}
403 imap = G.imap
404
405 concatMap :: (Prim a, Prim b) => (a -> Vector b) -> Vector a -> Vector b
406 {-# INLINE concatMap #-}
407 concatMap = G.concatMap
408
409 -- Zipping/unzipping
410 -- -----------------
411
412 -- | Zip two vectors with the given function.
413 zipWith :: (Prim a, Prim b, Prim c)
414 => (a -> b -> c) -> Vector a -> Vector b -> Vector c
415 {-# INLINE zipWith #-}
416 zipWith = G.zipWith
417
418 -- | Zip three vectors with the given function.
419 zipWith3 :: (Prim a, Prim b, Prim c, Prim d)
420 => (a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d
421 {-# INLINE zipWith3 #-}
422 zipWith3 = G.zipWith3
423
424 zipWith4 :: (Prim a, Prim b, Prim c, Prim d, Prim e)
425 => (a -> b -> c -> d -> e)
426 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
427 {-# INLINE zipWith4 #-}
428 zipWith4 = G.zipWith4
429
430 zipWith5 :: (Prim a, Prim b, Prim c, Prim d, Prim e, Prim f)
431 => (a -> b -> c -> d -> e -> f)
432 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
433 -> Vector f
434 {-# INLINE zipWith5 #-}
435 zipWith5 = G.zipWith5
436
437 zipWith6 :: (Prim a, Prim b, Prim c, Prim d, Prim e, Prim f, Prim g)
438 => (a -> b -> c -> d -> e -> f -> g)
439 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
440 -> Vector f -> Vector g
441 {-# INLINE zipWith6 #-}
442 zipWith6 = G.zipWith6
443
444 -- | Zip two vectors and their indices with the given function.
445 izipWith :: (Prim a, Prim b, Prim c)
446 => (Int -> a -> b -> c) -> Vector a -> Vector b -> Vector c
447 {-# INLINE izipWith #-}
448 izipWith = G.izipWith
449
450 -- | Zip three vectors and their indices with the given function.
451 izipWith3 :: (Prim a, Prim b, Prim c, Prim d)
452 => (Int -> a -> b -> c -> d)
453 -> Vector a -> Vector b -> Vector c -> Vector d
454 {-# INLINE izipWith3 #-}
455 izipWith3 = G.izipWith3
456
457 izipWith4 :: (Prim a, Prim b, Prim c, Prim d, Prim e)
458 => (Int -> a -> b -> c -> d -> e)
459 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
460 {-# INLINE izipWith4 #-}
461 izipWith4 = G.izipWith4
462
463 izipWith5 :: (Prim a, Prim b, Prim c, Prim d, Prim e, Prim f)
464 => (Int -> a -> b -> c -> d -> e -> f)
465 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
466 -> Vector f
467 {-# INLINE izipWith5 #-}
468 izipWith5 = G.izipWith5
469
470 izipWith6 :: (Prim a, Prim b, Prim c, Prim d, Prim e, Prim f, Prim g)
471 => (Int -> a -> b -> c -> d -> e -> f -> g)
472 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
473 -> Vector f -> Vector g
474 {-# INLINE izipWith6 #-}
475 izipWith6 = G.izipWith6
476
477 -- Filtering
478 -- ---------
479
480 -- | Drop elements which do not satisfy the predicate
481 filter :: Prim a => (a -> Bool) -> Vector a -> Vector a
482 {-# INLINE filter #-}
483 filter = G.filter
484
485 -- | Drop elements that do not satisfy the predicate (applied to values and
486 -- their indices)
487 ifilter :: Prim a => (Int -> a -> Bool) -> Vector a -> Vector a
488 {-# INLINE ifilter #-}
489 ifilter = G.ifilter
490
491 -- | Yield the longest prefix of elements satisfying the predicate.
492 takeWhile :: Prim a => (a -> Bool) -> Vector a -> Vector a
493 {-# INLINE takeWhile #-}
494 takeWhile = G.takeWhile
495
496 -- | Drop the longest prefix of elements that satisfy the predicate.
497 dropWhile :: Prim a => (a -> Bool) -> Vector a -> Vector a
498 {-# INLINE dropWhile #-}
499 dropWhile = G.dropWhile
500
501 -- | Split the vector in two parts, the first one containing those elements
502 -- that satisfy the predicate and the second one those that don't. The
503 -- relative order of the elements is preserved at the cost of a (sometimes)
504 -- reduced performance compared to 'unstablePartition'.
505 partition :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
506 {-# INLINE partition #-}
507 partition = G.partition
508
509 -- | Split the vector in two parts, the first one containing those elements
510 -- that satisfy the predicate and the second one those that don't. The order
511 -- of the elements is not preserved.
512 unstablePartition :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
513 {-# INLINE unstablePartition #-}
514 unstablePartition = G.unstablePartition
515
516 -- | Split the vector into the longest prefix of elements that satisfy the
517 -- predicate and the rest.
518 span :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
519 {-# INLINE span #-}
520 span = G.span
521
522 -- | Split the vector into the longest prefix of elements that do not satisfy
523 -- the predicate and the rest.
524 break :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
525 {-# INLINE break #-}
526 break = G.break
527
528 -- Searching
529 -- ---------
530
531 infix 4 `elem`
532 -- | Check whether the vector contains an element
533 elem :: (Prim a, Eq a) => a -> Vector a -> Bool
534 {-# INLINE elem #-}
535 elem = G.elem
536
537 infix 4 `notElem`
538 -- | Inverse of `elem`
539 notElem :: (Prim a, Eq a) => a -> Vector a -> Bool
540 {-# INLINE notElem #-}
541 notElem = G.notElem
542
543 -- | Yield 'Just' the first element matching the predicate or 'Nothing' if no
544 -- such element exists.
545 find :: Prim a => (a -> Bool) -> Vector a -> Maybe a
546 {-# INLINE find #-}
547 find = G.find
548
549 -- | Yield 'Just' the index of the first element matching the predicate or
550 -- 'Nothing' if no such element exists.
551 findIndex :: Prim a => (a -> Bool) -> Vector a -> Maybe Int
552 {-# INLINE findIndex #-}
553 findIndex = G.findIndex
554
555 -- | Yield the indices of elements satisfying the predicate
556 findIndices :: Prim a => (a -> Bool) -> Vector a -> Vector Int
557 {-# INLINE findIndices #-}
558 findIndices = G.findIndices
559
560 -- | Yield 'Just' the index of the first occurence of the given element or
561 -- 'Nothing' if the vector does not contain the element
562 elemIndex :: (Prim a, Eq a) => a -> Vector a -> Maybe Int
563 {-# INLINE elemIndex #-}
564 elemIndex = G.elemIndex
565
566 -- | Yield the indices of all occurences of the given element
567 elemIndices :: (Prim a, Eq a) => a -> Vector a -> Vector Int
568 {-# INLINE elemIndices #-}
569 elemIndices = G.elemIndices
570
571 -- Folding
572 -- -------
573
574 -- | Left fold
575 foldl :: Prim b => (a -> b -> a) -> a -> Vector b -> a
576 {-# INLINE foldl #-}
577 foldl = G.foldl
578
579 -- | Lefgt fold on non-empty vectors
580 foldl1 :: Prim a => (a -> a -> a) -> Vector a -> a
581 {-# INLINE foldl1 #-}
582 foldl1 = G.foldl1
583
584 -- | Left fold with strict accumulator
585 foldl' :: Prim b => (a -> b -> a) -> a -> Vector b -> a
586 {-# INLINE foldl' #-}
587 foldl' = G.foldl'
588
589 -- | Left fold on non-empty vectors with strict accumulator
590 foldl1' :: Prim a => (a -> a -> a) -> Vector a -> a
591 {-# INLINE foldl1' #-}
592 foldl1' = G.foldl1'
593
594 -- | Right fold
595 foldr :: Prim a => (a -> b -> b) -> b -> Vector a -> b
596 {-# INLINE foldr #-}
597 foldr = G.foldr
598
599 -- | Right fold on non-empty vectors
600 foldr1 :: Prim a => (a -> a -> a) -> Vector a -> a
601 {-# INLINE foldr1 #-}
602 foldr1 = G.foldr1
603
604 -- | Right fold with a strict accumulator
605 foldr' :: Prim a => (a -> b -> b) -> b -> Vector a -> b
606 {-# INLINE foldr' #-}
607 foldr' = G.foldr'
608
609 -- | Right fold on non-empty vectors with strict accumulator
610 foldr1' :: Prim a => (a -> a -> a) -> Vector a -> a
611 {-# INLINE foldr1' #-}
612 foldr1' = G.foldr1'
613
614 -- | Left fold (function applied to each element and its index)
615 ifoldl :: Prim b => (a -> Int -> b -> a) -> a -> Vector b -> a
616 {-# INLINE ifoldl #-}
617 ifoldl = G.ifoldl
618
619 -- | Left fold with strict accumulator (function applied to each element and
620 -- its index)
621 ifoldl' :: Prim b => (a -> Int -> b -> a) -> a -> Vector b -> a
622 {-# INLINE ifoldl' #-}
623 ifoldl' = G.ifoldl'
624
625 -- | Right fold (function applied to each element and its index)
626 ifoldr :: Prim a => (Int -> a -> b -> b) -> b -> Vector a -> b
627 {-# INLINE ifoldr #-}
628 ifoldr = G.ifoldr
629
630 -- | Right fold with strict accumulator (function applied to each element and
631 -- its index)
632 ifoldr' :: Prim a => (Int -> a -> b -> b) -> b -> Vector a -> b
633 {-# INLINE ifoldr' #-}
634 ifoldr' = G.ifoldr'
635
636 -- Specialised folds
637 -- -----------------
638
639 all :: Prim a => (a -> Bool) -> Vector a -> Bool
640 {-# INLINE all #-}
641 all = G.all
642
643 any :: Prim a => (a -> Bool) -> Vector a -> Bool
644 {-# INLINE any #-}
645 any = G.any
646
647 sum :: (Prim a, Num a) => Vector a -> a
648 {-# INLINE sum #-}
649 sum = G.sum
650
651 product :: (Prim a, Num a) => Vector a -> a
652 {-# INLINE product #-}
653 product = G.product
654
655 maximum :: (Prim a, Ord a) => Vector a -> a
656 {-# INLINE maximum #-}
657 maximum = G.maximum
658
659 maximumBy :: Prim a => (a -> a -> Ordering) -> Vector a -> a
660 {-# INLINE maximumBy #-}
661 maximumBy = G.maximumBy
662
663 minimum :: (Prim a, Ord a) => Vector a -> a
664 {-# INLINE minimum #-}
665 minimum = G.minimum
666
667 minimumBy :: Prim a => (a -> a -> Ordering) -> Vector a -> a
668 {-# INLINE minimumBy #-}
669 minimumBy = G.minimumBy
670
671 maxIndex :: (Prim a, Ord a) => Vector a -> Int
672 {-# INLINE maxIndex #-}
673 maxIndex = G.maxIndex
674
675 maxIndexBy :: Prim a => (a -> a -> Ordering) -> Vector a -> Int
676 {-# INLINE maxIndexBy #-}
677 maxIndexBy = G.maxIndexBy
678
679 minIndex :: (Prim a, Ord a) => Vector a -> Int
680 {-# INLINE minIndex #-}
681 minIndex = G.minIndex
682
683 minIndexBy :: Prim a => (a -> a -> Ordering) -> Vector a -> Int
684 {-# INLINE minIndexBy #-}
685 minIndexBy = G.minIndexBy
686
687 -- Unfolding
688 -- ---------
689
690 -- | The 'unfoldr' function is a \`dual\' to 'foldr': while 'foldr'
691 -- reduces a vector to a summary value, 'unfoldr' builds a list from
692 -- a seed value. The function takes the element and returns 'Nothing'
693 -- if it is done generating the vector or returns 'Just' @(a,b)@, in which
694 -- case, @a@ is a prepended to the vector and @b@ is used as the next
695 -- element in a recursive call.
696 --
697 -- A simple use of unfoldr:
698 --
699 -- > unfoldr (\b -> if b == 0 then Nothing else Just (b, b-1)) 10
700 -- > [10,9,8,7,6,5,4,3,2,1]
701 --
702 unfoldr :: Prim a => (b -> Maybe (a, b)) -> b -> Vector a
703 {-# INLINE unfoldr #-}
704 unfoldr = G.unfoldr
705
706 -- | Unfold at most @n@ elements
707 unfoldrN :: Prim a => Int -> (b -> Maybe (a, b)) -> b -> Vector a
708 {-# INLINE unfoldrN #-}
709 unfoldrN = G.unfoldrN
710
711 -- Scans
712 -- -----
713
714 -- | Prefix scan
715 prescanl :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
716 {-# INLINE prescanl #-}
717 prescanl = G.prescanl
718
719 -- | Prefix scan with strict accumulator
720 prescanl' :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
721 {-# INLINE prescanl' #-}
722 prescanl' = G.prescanl'
723
724 -- | Suffix scan
725 postscanl :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
726 {-# INLINE postscanl #-}
727 postscanl = G.postscanl
728
729 -- | Suffix scan with strict accumulator
730 postscanl' :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
731 {-# INLINE postscanl' #-}
732 postscanl' = G.postscanl'
733
734 -- | Haskell-style scan
735 scanl :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
736 {-# INLINE scanl #-}
737 scanl = G.scanl
738
739 -- | Haskell-style scan with strict accumulator
740 scanl' :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
741 {-# INLINE scanl' #-}
742 scanl' = G.scanl'
743
744 -- | Scan over a non-empty 'Vector'
745 scanl1 :: Prim a => (a -> a -> a) -> Vector a -> Vector a
746 {-# INLINE scanl1 #-}
747 scanl1 = G.scanl1
748
749 -- | Scan over a non-empty 'Vector' with a strict accumulator
750 scanl1' :: Prim a => (a -> a -> a) -> Vector a -> Vector a
751 {-# INLINE scanl1' #-}
752 scanl1' = G.scanl1'
753
754
755 -- | Prefix right-to-left scan
756 prescanr :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b
757 {-# INLINE prescanr #-}
758 prescanr = G.prescanr
759
760 -- | Prefix right-to-left scan with strict accumulator
761 prescanr' :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b
762 {-# INLINE prescanr' #-}
763 prescanr' = G.prescanr'
764
765 -- | Suffix right-to-left scan
766 postscanr :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b
767 {-# INLINE postscanr #-}
768 postscanr = G.postscanr
769
770 -- | Suffix right-to-left scan with strict accumulator
771 postscanr' :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b
772 {-# INLINE postscanr' #-}
773 postscanr' = G.postscanr'
774
775 -- | Haskell-style right-to-left scan
776 scanr :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b
777 {-# INLINE scanr #-}
778 scanr = G.scanr
779
780 -- | Haskell-style right-to-left scan with strict accumulator
781 scanr' :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b
782 {-# INLINE scanr' #-}
783 scanr' = G.scanr'
784
785 -- | Right-to-left scan over a non-empty vector
786 scanr1 :: Prim a => (a -> a -> a) -> Vector a -> Vector a
787 {-# INLINE scanr1 #-}
788 scanr1 = G.scanr1
789
790 -- | Right-to-left scan over a non-empty vector with a strict accumulator
791 scanr1' :: Prim a => (a -> a -> a) -> Vector a -> Vector a
792 {-# INLINE scanr1' #-}
793 scanr1' = G.scanr1'
794
795 -- Enumeration
796 -- -----------
797
798 -- | Yield a vector of the given length containing the values @x@, @x+1@ etc.
799 -- This operation is usually more efficient than 'enumFromTo'.
800 enumFromN :: (Prim a, Num a) => a -> Int -> Vector a
801 {-# INLINE enumFromN #-}
802 enumFromN = G.enumFromN
803
804 -- | Yield a vector of the given length containing the values @x@, @x+y@,
805 -- @x+y+y@ etc. This operations is usually more efficient than
806 -- 'enumFromThenTo'.
807 enumFromStepN :: (Prim a, Num a) => a -> a -> Int -> Vector a
808 {-# INLINE enumFromStepN #-}
809 enumFromStepN = G.enumFromStepN
810
811 -- | Enumerate values from @x@ to @y@.
812 --
813 -- /WARNING:/ This operation can be very inefficient. If at all possible, use
814 -- 'enumFromN' instead.
815 enumFromTo :: (Prim a, Enum a) => a -> a -> Vector a
816 {-# INLINE enumFromTo #-}
817 enumFromTo = G.enumFromTo
818
819 -- | Enumerate values from @x@ to @y@ with a specific step @z@.
820 --
821 -- /WARNING:/ This operation can be very inefficient. If at all possible, use
822 -- 'enumFromStepN' instead.
823 enumFromThenTo :: (Prim a, Enum a) => a -> a -> a -> Vector a
824 {-# INLINE enumFromThenTo #-}
825 enumFromThenTo = G.enumFromThenTo
826
827 -- Conversion to/from lists
828 -- ------------------------
829
830 -- | Convert a vector to a list
831 toList :: Prim a => Vector a -> [a]
832 {-# INLINE toList #-}
833 toList = G.toList
834
835 -- | Convert a list to a vector
836 fromList :: Prim a => [a] -> Vector a
837 {-# INLINE fromList #-}
838 fromList = G.fromList
839
840 -- | Convert the first @n@ elements of a list to a vector
841 --
842 -- > fromListN n xs = fromList (take n xs)
843 fromListN :: Prim a => Int -> [a] -> Vector a
844 {-# INLINE fromListN #-}
845 fromListN = G.fromListN
846