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