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