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