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