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