Eta-expand create to work around GHC bug 4120
[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. The use of this module is not
13 -- recommended except in very special cases. Adaptive unboxed vectors defined
14 -- in "Data.Vector.Unboxed" are significantly more flexible at no performance
15 -- cost.
16 --
17
18 module Data.Vector.Primitive (
19 -- * Primitive vectors
20 Vector, MVector(..), Prim,
21
22 -- * Accessors
23
24 -- ** Length information
25 length, null,
26
27 -- ** Indexing
28 (!), head, last,
29 unsafeIndex, unsafeHead, unsafeLast,
30
31 -- ** Monadic indexing
32 indexM, headM, lastM,
33 unsafeIndexM, unsafeHeadM, unsafeLastM,
34
35 -- ** Extracting subvectors (slicing)
36 slice, init, tail, take, drop,
37 unsafeSlice, unsafeInit, unsafeTail, unsafeTake, unsafeDrop,
38
39 -- * Construction
40
41 -- ** Initialisation
42 empty, singleton, replicate, generate,
43
44 -- ** Monadic initialisation
45 replicateM, create,
46
47 -- ** Unfolding
48 unfoldr, unfoldrN,
49
50 -- ** Enumeration
51 enumFromN, enumFromStepN, enumFromTo, enumFromThenTo,
52
53 -- ** Concatenation
54 cons, snoc, (++), concat,
55
56 -- ** Restricting memory usage
57 force,
58
59 -- * Modifying vectors
60
61 -- ** Bulk updates
62 (//), update_,
63 unsafeUpd, unsafeUpdate_,
64
65 -- ** Accumulations
66 accum, accumulate_,
67 unsafeAccum, unsafeAccumulate_,
68
69 -- ** Permutations
70 reverse, backpermute, unsafeBackpermute,
71
72 -- ** Safe destructive updates
73 modify,
74
75 -- * Elementwise operations
76
77 -- ** Mapping
78 map, imap, concatMap,
79
80 -- ** Monadic mapping
81 mapM, mapM_, forM, forM_,
82
83 -- ** Zipping
84 zipWith, zipWith3, zipWith4, zipWith5, zipWith6,
85 izipWith, izipWith3, izipWith4, izipWith5, izipWith6,
86
87 -- ** Monadic zipping
88 zipWithM, zipWithM_,
89
90 -- * Working with predicates
91
92 -- ** Filtering
93 filter, ifilter, filterM,
94 takeWhile, dropWhile,
95
96 -- ** Partitioning
97 partition, unstablePartition, span, break,
98
99 -- ** Searching
100 elem, notElem, find, findIndex, findIndices, elemIndex, elemIndices,
101
102 -- * Folding
103 foldl, foldl1, foldl', foldl1', foldr, foldr1, foldr', foldr1',
104 ifoldl, ifoldl', ifoldr, ifoldr',
105
106 -- ** Specialised folds
107 all, any,
108 sum, product,
109 maximum, maximumBy, minimum, minimumBy,
110 minIndex, minIndexBy, maxIndex, maxIndexBy,
111
112 -- ** Monadic folds
113 foldM, foldM', fold1M, fold1M',
114
115 -- * Prefix sums (scans)
116 prescanl, prescanl',
117 postscanl, postscanl',
118 scanl, scanl', scanl1, scanl1',
119 prescanr, prescanr',
120 postscanr, postscanr',
121 scanr, scanr', scanr1, scanr1',
122
123 -- * Conversions
124
125 -- ** Lists
126 toList, fromList, fromListN,
127
128 -- ** Other vector types
129 G.convert,
130
131 -- ** Mutable vectors
132 freeze, thaw, copy, unsafeFreeze, unsafeThaw, unsafeCopy
133 ) where
134
135 import qualified Data.Vector.Generic as G
136 import Data.Vector.Primitive.Mutable ( MVector(..) )
137 import qualified Data.Vector.Fusion.Stream as Stream
138 import Data.Primitive.ByteArray
139 import Data.Primitive ( Prim, sizeOf )
140
141 import Control.Monad ( liftM )
142 import Control.Monad.ST ( ST )
143 import Control.Monad.Primitive
144
145 import Prelude hiding ( length, null,
146 replicate, (++), concat,
147 head, last,
148 init, tail, take, drop, reverse,
149 map, concatMap,
150 zipWith, zipWith3, zip, zip3, unzip, unzip3,
151 filter, takeWhile, dropWhile, span, break,
152 elem, notElem,
153 foldl, foldl1, foldr, foldr1,
154 all, any, sum, product, minimum, maximum,
155 scanl, scanl1, scanr, scanr1,
156 enumFromTo, enumFromThenTo,
157 mapM, mapM_ )
158
159 import qualified Prelude
160
161 import Data.Typeable ( Typeable )
162 import Data.Data ( Data(..) )
163
164 import Data.Monoid ( Monoid(..) )
165
166 -- | Unboxed vectors of primitive types
167 data Vector a = Vector {-# UNPACK #-} !Int
168 {-# UNPACK #-} !Int
169 {-# UNPACK #-} !ByteArray
170 deriving ( Typeable )
171
172 instance (Show a, Prim a) => Show (Vector a) where
173 show = (Prelude.++ " :: Data.Vector.Primitive.Vector") . ("fromList " Prelude.++) . show . toList
174
175 instance (Data a, Prim a) => Data (Vector a) where
176 gfoldl = G.gfoldl
177 toConstr _ = error "toConstr"
178 gunfold _ _ = error "gunfold"
179 dataTypeOf _ = G.mkType "Data.Vector.Primitive.Vector"
180 dataCast1 = G.dataCast
181
182
183 type instance G.Mutable Vector = MVector
184
185 instance Prim a => G.Vector Vector a where
186 {-# INLINE basicUnsafeFreeze #-}
187 basicUnsafeFreeze (MVector i n marr)
188 = Vector i n `liftM` unsafeFreezeByteArray marr
189
190 {-# INLINE basicUnsafeThaw #-}
191 basicUnsafeThaw (Vector i n arr)
192 = MVector i n `liftM` unsafeThawByteArray arr
193
194 {-# INLINE basicLength #-}
195 basicLength (Vector _ n _) = n
196
197 {-# INLINE basicUnsafeSlice #-}
198 basicUnsafeSlice j n (Vector i _ arr) = Vector (i+j) n arr
199
200 {-# INLINE basicUnsafeIndexM #-}
201 basicUnsafeIndexM (Vector i _ arr) j = return (indexByteArray arr (i+j))
202
203 {-# INLINE basicUnsafeCopy #-}
204 basicUnsafeCopy (MVector i n dst) (Vector j _ src)
205 = memcpyByteArray' dst (i * sz) src (j * sz) (n * sz)
206 where
207 sz = sizeOf (undefined :: a)
208
209 {-# INLINE elemseq #-}
210 elemseq _ = seq
211
212 -- See http://trac.haskell.org/vector/ticket/12
213 instance (Prim a, Eq a) => Eq (Vector a) where
214 {-# INLINE (==) #-}
215 xs == ys = Stream.eq (G.stream xs) (G.stream ys)
216
217 {-# INLINE (/=) #-}
218 xs /= ys = not (Stream.eq (G.stream xs) (G.stream ys))
219
220 -- See http://trac.haskell.org/vector/ticket/12
221 instance (Prim a, Ord a) => Ord (Vector a) where
222 {-# INLINE compare #-}
223 compare xs ys = Stream.cmp (G.stream xs) (G.stream ys)
224
225 {-# INLINE (<) #-}
226 xs < ys = Stream.cmp (G.stream xs) (G.stream ys) == LT
227
228 {-# INLINE (<=) #-}
229 xs <= ys = Stream.cmp (G.stream xs) (G.stream ys) /= GT
230
231 {-# INLINE (>) #-}
232 xs > ys = Stream.cmp (G.stream xs) (G.stream ys) == GT
233
234 {-# INLINE (>=) #-}
235 xs >= ys = Stream.cmp (G.stream xs) (G.stream ys) /= LT
236
237 instance Prim a => Monoid (Vector a) where
238 {-# INLINE mempty #-}
239 mempty = empty
240
241 {-# INLINE mappend #-}
242 mappend = (++)
243
244 {-# INLINE mconcat #-}
245 mconcat = concat
246
247 -- Length
248 -- ------
249
250 -- | /O(1)/ Yield the length of the vector.
251 length :: Prim a => Vector a -> Int
252 {-# INLINE length #-}
253 length = G.length
254
255 -- | /O(1)/ Test whether a vector if empty
256 null :: Prim a => Vector a -> Bool
257 {-# INLINE null #-}
258 null = G.null
259
260 -- Indexing
261 -- --------
262
263 -- | O(1) Indexing
264 (!) :: Prim a => Vector a -> Int -> a
265 {-# INLINE (!) #-}
266 (!) = (G.!)
267
268 -- | /O(1)/ First element
269 head :: Prim a => Vector a -> a
270 {-# INLINE head #-}
271 head = G.head
272
273 -- | /O(1)/ Last element
274 last :: Prim a => Vector a -> a
275 {-# INLINE last #-}
276 last = G.last
277
278 -- | /O(1)/ Unsafe indexing without bounds checking
279 unsafeIndex :: Prim a => Vector a -> Int -> a
280 {-# INLINE unsafeIndex #-}
281 unsafeIndex = G.unsafeIndex
282
283 -- | /O(1)/ First element without checking if the vector is empty
284 unsafeHead :: Prim a => Vector a -> a
285 {-# INLINE unsafeHead #-}
286 unsafeHead = G.unsafeHead
287
288 -- | /O(1)/ Last element without checking if the vector is empty
289 unsafeLast :: Prim a => Vector a -> a
290 {-# INLINE unsafeLast #-}
291 unsafeLast = G.unsafeLast
292
293 -- Monadic indexing
294 -- ----------------
295
296 -- | /O(1)/ Indexing in a monad.
297 --
298 -- The monad allows operations to be strict in the vector when necessary.
299 -- Suppose vector copying is implemented like this:
300 --
301 -- > copy mv v = ... write mv i (v ! i) ...
302 --
303 -- For lazy vectors, @v ! i@ would not be evaluated which means that @mv@
304 -- would unnecessarily retain a reference to @v@ in each element written.
305 --
306 -- With 'indexM', copying can be implemented like this instead:
307 --
308 -- > copy mv v = ... do
309 -- > x <- indexM v i
310 -- > write mv i x
311 --
312 -- Here, no references to @v@ are retained because indexing (but /not/ the
313 -- elements) is evaluated eagerly.
314 --
315 indexM :: (Prim a, Monad m) => Vector a -> Int -> m a
316 {-# INLINE indexM #-}
317 indexM = G.indexM
318
319 -- | /O(1)/ First element of a vector in a monad. See 'indexM' for an
320 -- explanation of why this is useful.
321 headM :: (Prim a, Monad m) => Vector a -> m a
322 {-# INLINE headM #-}
323 headM = G.headM
324
325 -- | /O(1)/ Last element of a vector in a monad. See 'indexM' for an
326 -- explanation of why this is useful.
327 lastM :: (Prim a, Monad m) => Vector a -> m a
328 {-# INLINE lastM #-}
329 lastM = G.lastM
330
331 -- | /O(1)/ Indexing in a monad without bounds checks. See 'indexM' for an
332 -- explanation of why this is useful.
333 unsafeIndexM :: (Prim a, Monad m) => Vector a -> Int -> m a
334 {-# INLINE unsafeIndexM #-}
335 unsafeIndexM = G.unsafeIndexM
336
337 -- | /O(1)/ First element in a monad without checking for empty vectors.
338 -- See 'indexM' for an explanation of why this is useful.
339 unsafeHeadM :: (Prim a, Monad m) => Vector a -> m a
340 {-# INLINE unsafeHeadM #-}
341 unsafeHeadM = G.unsafeHeadM
342
343 -- | /O(1)/ Last element in a monad without checking for empty vectors.
344 -- See 'indexM' for an explanation of why this is useful.
345 unsafeLastM :: (Prim a, Monad m) => Vector a -> m a
346 {-# INLINE unsafeLastM #-}
347 unsafeLastM = G.unsafeLastM
348
349 -- Extracting subvectors (slicing)
350 -- -------------------------------
351
352 -- | /O(1)/ Yield a slice of the vector without copying it. The vector must
353 -- contain at least @i+n@ elements.
354 slice :: Prim a
355 => Int -- ^ @i@ starting index
356 -> Int -- ^ @n@ length
357 -> Vector a
358 -> Vector a
359 {-# INLINE slice #-}
360 slice = G.slice
361
362 -- | /O(1)/ Yield all but the last element without copying. The vector may not
363 -- be empty.
364 init :: Prim a => Vector a -> Vector a
365 {-# INLINE init #-}
366 init = G.init
367
368 -- | /O(1)/ Yield all but the first element without copying. The vector may not
369 -- be empty.
370 tail :: Prim a => Vector a -> Vector a
371 {-# INLINE tail #-}
372 tail = G.tail
373
374 -- | /O(1)/ Yield at the first @n@ elements without copying. The vector may
375 -- contain less than @n@ elements in which case it is returned unchanged.
376 take :: Prim a => Int -> Vector a -> Vector a
377 {-# INLINE take #-}
378 take = G.take
379
380 -- | /O(1)/ Yield all but the first @n@ elements without copying. The vector may
381 -- contain less than @n@ elements in which case an empty vector is returned.
382 drop :: Prim a => Int -> Vector a -> Vector a
383 {-# INLINE drop #-}
384 drop = G.drop
385
386 -- | /O(1)/ Yield a slice of the vector without copying. The vector must
387 -- contain at least @i+n@ elements but this is not checked.
388 unsafeSlice :: Prim a => Int -- ^ @i@ starting index
389 -> Int -- ^ @n@ length
390 -> Vector a
391 -> Vector a
392 {-# INLINE unsafeSlice #-}
393 unsafeSlice = G.unsafeSlice
394
395 -- | /O(1)/ Yield all but the last element without copying. The vector may not
396 -- be empty but this is not checked.
397 unsafeInit :: Prim a => Vector a -> Vector a
398 {-# INLINE unsafeInit #-}
399 unsafeInit = G.unsafeInit
400
401 -- | /O(1)/ Yield all but the first element without copying. The vector may not
402 -- be empty but this is not checked.
403 unsafeTail :: Prim a => Vector a -> Vector a
404 {-# INLINE unsafeTail #-}
405 unsafeTail = G.unsafeTail
406
407 -- | /O(1)/ Yield the first @n@ elements without copying. The vector must
408 -- contain at least @n@ elements but this is not checked.
409 unsafeTake :: Prim a => Int -> Vector a -> Vector a
410 {-# INLINE unsafeTake #-}
411 unsafeTake = G.unsafeTake
412
413 -- | /O(1)/ Yield all but the first @n@ elements without copying. The vector
414 -- must contain at least @n@ elements but this is not checked.
415 unsafeDrop :: Prim a => Int -> Vector a -> Vector a
416 {-# INLINE unsafeDrop #-}
417 unsafeDrop = G.unsafeDrop
418
419 -- Initialisation
420 -- --------------
421
422 -- | /O(1)/ Empty vector
423 empty :: Prim a => Vector a
424 {-# INLINE empty #-}
425 empty = G.empty
426
427 -- | /O(1)/ Vector with exactly one element
428 singleton :: Prim a => a -> Vector a
429 {-# INLINE singleton #-}
430 singleton = G.singleton
431
432 -- | /O(n)/ Vector of the given length with the same value in each position
433 replicate :: Prim a => Int -> a -> Vector a
434 {-# INLINE replicate #-}
435 replicate = G.replicate
436
437 -- | /O(n)/ Construct a vector of the given length by applying the function to
438 -- each index
439 generate :: Prim a => Int -> (Int -> a) -> Vector a
440 {-# INLINE generate #-}
441 generate = G.generate
442
443 -- Unfolding
444 -- ---------
445
446 -- | /O(n)/ Construct a vector by repeatedly applying the generator function
447 -- to a seed. The generator function yields 'Just' the next element and the
448 -- new seed or 'Nothing' if there are no more elements.
449 --
450 -- > unfoldr (\n -> if n == 0 then Nothing else Just (n,n-1)) 10
451 -- > = <10,9,8,7,6,5,4,3,2,1>
452 unfoldr :: Prim a => (b -> Maybe (a, b)) -> b -> Vector a
453 {-# INLINE unfoldr #-}
454 unfoldr = G.unfoldr
455
456 -- | /O(n)/ Construct a vector with at most @n@ by repeatedly applying the
457 -- generator function to the a seed. The generator function yields 'Just' the
458 -- next element and the new seed or 'Nothing' if there are no more elements.
459 --
460 -- > unfoldrN 3 (\n -> Just (n,n-1)) 10 = <10,9,8>
461 unfoldrN :: Prim a => Int -> (b -> Maybe (a, b)) -> b -> Vector a
462 {-# INLINE unfoldrN #-}
463 unfoldrN = G.unfoldrN
464
465 -- Enumeration
466 -- -----------
467
468 -- | /O(n)/ Yield a vector of the given length containing the values @x@, @x+1@
469 -- etc. This operation is usually more efficient than 'enumFromTo'.
470 --
471 -- > enumFromN 5 3 = <5,6,7>
472 enumFromN :: (Prim a, Num a) => a -> Int -> Vector a
473 {-# INLINE enumFromN #-}
474 enumFromN = G.enumFromN
475
476 -- | /O(n)/ Yield a vector of the given length containing the values @x@, @x+y@,
477 -- @x+y+y@ etc. This operations is usually more efficient than 'enumFromThenTo'.
478 --
479 -- > enumFromStepN 1 0.1 5 = <1,1.1,1.2,1.3,1.4>
480 enumFromStepN :: (Prim a, Num a) => a -> a -> Int -> Vector a
481 {-# INLINE enumFromStepN #-}
482 enumFromStepN = G.enumFromStepN
483
484 -- | /O(n)/ Enumerate values from @x@ to @y@.
485 --
486 -- /WARNING:/ This operation can be very inefficient. If at all possible, use
487 -- 'enumFromN' instead.
488 enumFromTo :: (Prim a, Enum a) => a -> a -> Vector a
489 {-# INLINE enumFromTo #-}
490 enumFromTo = G.enumFromTo
491
492 -- | /O(n)/ Enumerate values from @x@ to @y@ with a specific step @z@.
493 --
494 -- /WARNING:/ This operation can be very inefficient. If at all possible, use
495 -- 'enumFromStepN' instead.
496 enumFromThenTo :: (Prim a, Enum a) => a -> a -> a -> Vector a
497 {-# INLINE enumFromThenTo #-}
498 enumFromThenTo = G.enumFromThenTo
499
500 -- Concatenation
501 -- -------------
502
503 -- | /O(n)/ Prepend an element
504 cons :: Prim a => a -> Vector a -> Vector a
505 {-# INLINE cons #-}
506 cons = G.cons
507
508 -- | /O(n)/ Append an element
509 snoc :: Prim a => Vector a -> a -> Vector a
510 {-# INLINE snoc #-}
511 snoc = G.snoc
512
513 infixr 5 ++
514 -- | /O(m+n)/ Concatenate two vectors
515 (++) :: Prim a => Vector a -> Vector a -> Vector a
516 {-# INLINE (++) #-}
517 (++) = (G.++)
518
519 -- | /O(n)/ Concatenate all vectors in the list
520 concat :: Prim a => [Vector a] -> Vector a
521 {-# INLINE concat #-}
522 concat = G.concat
523
524 -- Monadic initialisation
525 -- ----------------------
526
527 -- | /O(n)/ Execute the monadic action the given number of times and store the
528 -- results in a vector.
529 replicateM :: (Monad m, Prim a) => Int -> m a -> m (Vector a)
530 {-# INLINE replicateM #-}
531 replicateM = G.replicateM
532
533 -- | Execute the monadic action and freeze the resulting vector.
534 --
535 -- @
536 -- create (do { v \<- new 2; write v 0 \'a\'; write v 1 \'b\' }) = \<'a','b'\>
537 -- @
538 create :: Prim a => (forall s. ST s (MVector s a)) -> Vector a
539 {-# INLINE create #-}
540 -- NOTE: eta-expanded due to http://hackage.haskell.org/trac/ghc/ticket/4120
541 create p = G.create p
542
543 -- Restricting memory usage
544 -- ------------------------
545
546 -- | /O(n)/ Yield the argument but force it not to retain any extra memory,
547 -- possibly by copying it.
548 --
549 -- This is especially useful when dealing with slices. For example:
550 --
551 -- > force (slice 0 2 <huge vector>)
552 --
553 -- Here, the slice retains a reference to the huge vector. Forcing it creates
554 -- a copy of just the elements that belong to the slice and allows the huge
555 -- vector to be garbage collected.
556 force :: Prim a => Vector a -> Vector a
557 {-# INLINE force #-}
558 force = G.force
559
560 -- Bulk updates
561 -- ------------
562
563 -- | /O(m+n)/ For each pair @(i,a)@ from the list, replace the vector
564 -- element at position @i@ by @a@.
565 --
566 -- > <5,9,2,7> // [(2,1),(0,3),(2,8)] = <3,9,8,7>
567 --
568 (//) :: Prim a => Vector a -- ^ initial vector (of length @m@)
569 -> [(Int, a)] -- ^ list of index/value pairs (of length @n@)
570 -> Vector a
571 {-# INLINE (//) #-}
572 (//) = (G.//)
573
574 -- | /O(m+min(n1,n2))/ For each index @i@ from the index vector and the
575 -- corresponding value @a@ from the value vector, replace the element of the
576 -- initial vector at position @i@ by @a@.
577 --
578 -- > update_ <5,9,2,7> <2,0,2> <1,3,8> = <3,9,8,7>
579 --
580 update_ :: Prim a
581 => Vector a -- ^ initial vector (of length @m@)
582 -> Vector Int -- ^ index vector (of length @n1@)
583 -> Vector a -- ^ value vector (of length @n2@)
584 -> Vector a
585 {-# INLINE update_ #-}
586 update_ = G.update_
587
588 -- | Same as ('//') but without bounds checking.
589 unsafeUpd :: Prim a => Vector a -> [(Int, a)] -> Vector a
590 {-# INLINE unsafeUpd #-}
591 unsafeUpd = G.unsafeUpd
592
593 -- | Same as 'update_' but without bounds checking.
594 unsafeUpdate_ :: Prim a => Vector a -> Vector Int -> Vector a -> Vector a
595 {-# INLINE unsafeUpdate_ #-}
596 unsafeUpdate_ = G.unsafeUpdate_
597
598 -- Accumulations
599 -- -------------
600
601 -- | /O(m+n)/ For each pair @(i,b)@ from the list, replace the vector element
602 -- @a@ at position @i@ by @f a b@.
603 --
604 -- > accum (+) <5,9,2> [(2,4),(1,6),(0,3),(1,7)] = <5+3, 9+6+7, 2+4>
605 accum :: Prim a
606 => (a -> b -> a) -- ^ accumulating function @f@
607 -> Vector a -- ^ initial vector (of length @m@)
608 -> [(Int,b)] -- ^ list of index/value pairs (of length @n@)
609 -> Vector a
610 {-# INLINE accum #-}
611 accum = G.accum
612
613 -- | /O(m+min(n1,n2))/ For each index @i@ from the index vector and the
614 -- corresponding value @b@ from the the value vector,
615 -- replace the element of the initial vector at
616 -- position @i@ by @f a b@.
617 --
618 -- > accumulate_ (+) <5,9,2> <2,1,0,1> <4,6,3,7> = <5+3, 9+6+7, 2+4>
619 --
620 accumulate_ :: (Prim a, Prim b)
621 => (a -> b -> a) -- ^ accumulating function @f@
622 -> Vector a -- ^ initial vector (of length @m@)
623 -> Vector Int -- ^ index vector (of length @n1@)
624 -> Vector b -- ^ value vector (of length @n2@)
625 -> Vector a
626 {-# INLINE accumulate_ #-}
627 accumulate_ = G.accumulate_
628
629 -- | Same as 'accum' but without bounds checking.
630 unsafeAccum :: Prim a => (a -> b -> a) -> Vector a -> [(Int,b)] -> Vector a
631 {-# INLINE unsafeAccum #-}
632 unsafeAccum = G.unsafeAccum
633
634 -- | Same as 'accumulate_' but without bounds checking.
635 unsafeAccumulate_ :: (Prim a, Prim b) =>
636 (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a
637 {-# INLINE unsafeAccumulate_ #-}
638 unsafeAccumulate_ = G.unsafeAccumulate_
639
640 -- Permutations
641 -- ------------
642
643 -- | /O(n)/ Reverse a vector
644 reverse :: Prim a => Vector a -> Vector a
645 {-# INLINE reverse #-}
646 reverse = G.reverse
647
648 -- | /O(n)/ Yield the vector obtained by replacing each element @i@ of the
649 -- index vector by @xs'!'i@. This is equivalent to @'map' (xs'!') is@ but is
650 -- often much more efficient.
651 --
652 -- > backpermute <a,b,c,d> <0,3,2,3,1,0> = <a,d,c,d,b,a>
653 backpermute :: Prim a => Vector a -> Vector Int -> Vector a
654 {-# INLINE backpermute #-}
655 backpermute = G.backpermute
656
657 -- | Same as 'backpermute' but without bounds checking.
658 unsafeBackpermute :: Prim a => Vector a -> Vector Int -> Vector a
659 {-# INLINE unsafeBackpermute #-}
660 unsafeBackpermute = G.unsafeBackpermute
661
662 -- Safe destructive updates
663 -- ------------------------
664
665 -- | Apply a destructive operation to a vector. The operation will be
666 -- performed in place if it is safe to do so and will modify a copy of the
667 -- vector otherwise.
668 --
669 -- @
670 -- modify (\\v -> write v 0 \'x\') ('replicate' 3 \'a\') = \<\'x\',\'a\',\'a\'\>
671 -- @
672 modify :: Prim a => (forall s. MVector s a -> ST s ()) -> Vector a -> Vector a
673 {-# INLINE modify #-}
674 modify = G.modify
675
676 -- Mapping
677 -- -------
678
679 -- | /O(n)/ Map a function over a vector
680 map :: (Prim a, Prim b) => (a -> b) -> Vector a -> Vector b
681 {-# INLINE map #-}
682 map = G.map
683
684 -- | /O(n)/ Apply a function to every element of a vector and its index
685 imap :: (Prim a, Prim b) => (Int -> a -> b) -> Vector a -> Vector b
686 {-# INLINE imap #-}
687 imap = G.imap
688
689 -- | Map a function over a vector and concatenate the results.
690 concatMap :: (Prim a, Prim b) => (a -> Vector b) -> Vector a -> Vector b
691 {-# INLINE concatMap #-}
692 concatMap = G.concatMap
693
694 -- Monadic mapping
695 -- ---------------
696
697 -- | /O(n)/ Apply the monadic action to all elements of the vector, yielding a
698 -- vector of results
699 mapM :: (Monad m, Prim a, Prim b) => (a -> m b) -> Vector a -> m (Vector b)
700 {-# INLINE mapM #-}
701 mapM = G.mapM
702
703 -- | /O(n)/ Apply the monadic action to all elements of a vector and ignore the
704 -- results
705 mapM_ :: (Monad m, Prim a) => (a -> m b) -> Vector a -> m ()
706 {-# INLINE mapM_ #-}
707 mapM_ = G.mapM_
708
709 -- | /O(n)/ Apply the monadic action to all elements of the vector, yielding a
710 -- vector of results. Equvalent to @flip 'mapM'@.
711 forM :: (Monad m, Prim a, Prim b) => Vector a -> (a -> m b) -> m (Vector b)
712 {-# INLINE forM #-}
713 forM = G.forM
714
715 -- | /O(n)/ Apply the monadic action to all elements of a vector and ignore the
716 -- results. Equivalent to @flip 'mapM_'@.
717 forM_ :: (Monad m, Prim a) => Vector a -> (a -> m b) -> m ()
718 {-# INLINE forM_ #-}
719 forM_ = G.forM_
720
721 -- Zipping
722 -- -------
723
724 -- | /O(min(m,n))/ Zip two vectors with the given function.
725 zipWith :: (Prim a, Prim b, Prim c)
726 => (a -> b -> c) -> Vector a -> Vector b -> Vector c
727 {-# INLINE zipWith #-}
728 zipWith = G.zipWith
729
730 -- | Zip three vectors with the given function.
731 zipWith3 :: (Prim a, Prim b, Prim c, Prim d)
732 => (a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d
733 {-# INLINE zipWith3 #-}
734 zipWith3 = G.zipWith3
735
736 zipWith4 :: (Prim a, Prim b, Prim c, Prim d, Prim e)
737 => (a -> b -> c -> d -> e)
738 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
739 {-# INLINE zipWith4 #-}
740 zipWith4 = G.zipWith4
741
742 zipWith5 :: (Prim a, Prim b, Prim c, Prim d, Prim e,
743 Prim f)
744 => (a -> b -> c -> d -> e -> f)
745 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
746 -> Vector f
747 {-# INLINE zipWith5 #-}
748 zipWith5 = G.zipWith5
749
750 zipWith6 :: (Prim a, Prim b, Prim c, Prim d, Prim e,
751 Prim f, Prim g)
752 => (a -> b -> c -> d -> e -> f -> g)
753 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
754 -> Vector f -> Vector g
755 {-# INLINE zipWith6 #-}
756 zipWith6 = G.zipWith6
757
758 -- | /O(min(m,n))/ Zip two vectors with a function that also takes the
759 -- elements' indices.
760 izipWith :: (Prim a, Prim b, Prim c)
761 => (Int -> a -> b -> c) -> Vector a -> Vector b -> Vector c
762 {-# INLINE izipWith #-}
763 izipWith = G.izipWith
764
765 -- | Zip three vectors and their indices with the given function.
766 izipWith3 :: (Prim a, Prim b, Prim c, Prim d)
767 => (Int -> a -> b -> c -> d)
768 -> Vector a -> Vector b -> Vector c -> Vector d
769 {-# INLINE izipWith3 #-}
770 izipWith3 = G.izipWith3
771
772 izipWith4 :: (Prim a, Prim b, Prim c, Prim d, Prim e)
773 => (Int -> a -> b -> c -> d -> e)
774 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
775 {-# INLINE izipWith4 #-}
776 izipWith4 = G.izipWith4
777
778 izipWith5 :: (Prim a, Prim b, Prim c, Prim d, Prim e,
779 Prim f)
780 => (Int -> a -> b -> c -> d -> e -> f)
781 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
782 -> Vector f
783 {-# INLINE izipWith5 #-}
784 izipWith5 = G.izipWith5
785
786 izipWith6 :: (Prim a, Prim b, Prim c, Prim d, Prim e,
787 Prim f, Prim g)
788 => (Int -> a -> b -> c -> d -> e -> f -> g)
789 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
790 -> Vector f -> Vector g
791 {-# INLINE izipWith6 #-}
792 izipWith6 = G.izipWith6
793
794 -- Monadic zipping
795 -- ---------------
796
797 -- | /O(min(m,n))/ Zip the two vectors with the monadic action and yield a
798 -- vector of results
799 zipWithM :: (Monad m, Prim a, Prim b, Prim c)
800 => (a -> b -> m c) -> Vector a -> Vector b -> m (Vector c)
801 {-# INLINE zipWithM #-}
802 zipWithM = G.zipWithM
803
804 -- | /O(min(m,n))/ Zip the two vectors with the monadic action and ignore the
805 -- results
806 zipWithM_ :: (Monad m, Prim a, Prim b)
807 => (a -> b -> m c) -> Vector a -> Vector b -> m ()
808 {-# INLINE zipWithM_ #-}
809 zipWithM_ = G.zipWithM_
810
811 -- Filtering
812 -- ---------
813
814 -- | /O(n)/ Drop elements that do not satisfy the predicate
815 filter :: Prim a => (a -> Bool) -> Vector a -> Vector a
816 {-# INLINE filter #-}
817 filter = G.filter
818
819 -- | /O(n)/ Drop elements that do not satisfy the predicate which is applied to
820 -- values and their indices
821 ifilter :: Prim a => (Int -> a -> Bool) -> Vector a -> Vector a
822 {-# INLINE ifilter #-}
823 ifilter = G.ifilter
824
825 -- | /O(n)/ Drop elements that do not satisfy the monadic predicate
826 filterM :: (Monad m, Prim a) => (a -> m Bool) -> Vector a -> m (Vector a)
827 {-# INLINE filterM #-}
828 filterM = G.filterM
829
830 -- | /O(n)/ Yield the longest prefix of elements satisfying the predicate
831 -- without copying.
832 takeWhile :: Prim a => (a -> Bool) -> Vector a -> Vector a
833 {-# INLINE takeWhile #-}
834 takeWhile = G.takeWhile
835
836 -- | /O(n)/ Drop the longest prefix of elements that satisfy the predicate
837 -- without copying.
838 dropWhile :: Prim a => (a -> Bool) -> Vector a -> Vector a
839 {-# INLINE dropWhile #-}
840 dropWhile = G.dropWhile
841
842 -- Parititioning
843 -- -------------
844
845 -- | /O(n)/ Split the vector in two parts, the first one containing those
846 -- elements that satisfy the predicate and the second one those that don't. The
847 -- relative order of the elements is preserved at the cost of a sometimes
848 -- reduced performance compared to 'unstablePartition'.
849 partition :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
850 {-# INLINE partition #-}
851 partition = G.partition
852
853 -- | /O(n)/ Split the vector in two parts, the first one containing those
854 -- elements that satisfy the predicate and the second one those that don't.
855 -- The order of the elements is not preserved but the operation is often
856 -- faster than 'partition'.
857 unstablePartition :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
858 {-# INLINE unstablePartition #-}
859 unstablePartition = G.unstablePartition
860
861 -- | /O(n)/ Split the vector into the longest prefix of elements that satisfy
862 -- the predicate and the rest without copying.
863 span :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
864 {-# INLINE span #-}
865 span = G.span
866
867 -- | /O(n)/ Split the vector into the longest prefix of elements that do not
868 -- satisfy the predicate and the rest without copying.
869 break :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
870 {-# INLINE break #-}
871 break = G.break
872
873 -- Searching
874 -- ---------
875
876 infix 4 `elem`
877 -- | /O(n)/ Check if the vector contains an element
878 elem :: (Prim a, Eq a) => a -> Vector a -> Bool
879 {-# INLINE elem #-}
880 elem = G.elem
881
882 infix 4 `notElem`
883 -- | /O(n)/ Check if the vector does not contain an element (inverse of 'elem')
884 notElem :: (Prim a, Eq a) => a -> Vector a -> Bool
885 {-# INLINE notElem #-}
886 notElem = G.notElem
887
888 -- | /O(n)/ Yield 'Just' the first element matching the predicate or 'Nothing'
889 -- if no such element exists.
890 find :: Prim a => (a -> Bool) -> Vector a -> Maybe a
891 {-# INLINE find #-}
892 find = G.find
893
894 -- | /O(n)/ Yield 'Just' the index of the first element matching the predicate
895 -- or 'Nothing' if no such element exists.
896 findIndex :: Prim a => (a -> Bool) -> Vector a -> Maybe Int
897 {-# INLINE findIndex #-}
898 findIndex = G.findIndex
899
900 -- | /O(n)/ Yield the indices of elements satisfying the predicate in ascending
901 -- order.
902 findIndices :: Prim a => (a -> Bool) -> Vector a -> Vector Int
903 {-# INLINE findIndices #-}
904 findIndices = G.findIndices
905
906 -- | /O(n)/ Yield 'Just' the index of the first occurence of the given element or
907 -- 'Nothing' if the vector does not contain the element. This is a specialised
908 -- version of 'findIndex'.
909 elemIndex :: (Prim a, Eq a) => a -> Vector a -> Maybe Int
910 {-# INLINE elemIndex #-}
911 elemIndex = G.elemIndex
912
913 -- | /O(n)/ Yield the indices of all occurences of the given element in
914 -- ascending order. This is a specialised version of 'findIndices'.
915 elemIndices :: (Prim a, Eq a) => a -> Vector a -> Vector Int
916 {-# INLINE elemIndices #-}
917 elemIndices = G.elemIndices
918
919 -- Folding
920 -- -------
921
922 -- | /O(n)/ Left fold
923 foldl :: Prim b => (a -> b -> a) -> a -> Vector b -> a
924 {-# INLINE foldl #-}
925 foldl = G.foldl
926
927 -- | /O(n)/ Left fold on non-empty vectors
928 foldl1 :: Prim a => (a -> a -> a) -> Vector a -> a
929 {-# INLINE foldl1 #-}
930 foldl1 = G.foldl1
931
932 -- | /O(n)/ Left fold with strict accumulator
933 foldl' :: Prim b => (a -> b -> a) -> a -> Vector b -> a
934 {-# INLINE foldl' #-}
935 foldl' = G.foldl'
936
937 -- | /O(n)/ Left fold on non-empty vectors with strict accumulator
938 foldl1' :: Prim a => (a -> a -> a) -> Vector a -> a
939 {-# INLINE foldl1' #-}
940 foldl1' = G.foldl1'
941
942 -- | /O(n)/ Right fold
943 foldr :: Prim a => (a -> b -> b) -> b -> Vector a -> b
944 {-# INLINE foldr #-}
945 foldr = G.foldr
946
947 -- | /O(n)/ Right fold on non-empty vectors
948 foldr1 :: Prim a => (a -> a -> a) -> Vector a -> a
949 {-# INLINE foldr1 #-}
950 foldr1 = G.foldr1
951
952 -- | /O(n)/ Right fold with a strict accumulator
953 foldr' :: Prim a => (a -> b -> b) -> b -> Vector a -> b
954 {-# INLINE foldr' #-}
955 foldr' = G.foldr'
956
957 -- | /O(n)/ Right fold on non-empty vectors with strict accumulator
958 foldr1' :: Prim a => (a -> a -> a) -> Vector a -> a
959 {-# INLINE foldr1' #-}
960 foldr1' = G.foldr1'
961
962 -- | /O(n)/ Left fold (function applied to each element and its index)
963 ifoldl :: Prim b => (a -> Int -> b -> a) -> a -> Vector b -> a
964 {-# INLINE ifoldl #-}
965 ifoldl = G.ifoldl
966
967 -- | /O(n)/ Left fold with strict accumulator (function applied to each element
968 -- and its index)
969 ifoldl' :: Prim b => (a -> Int -> b -> a) -> a -> Vector b -> a
970 {-# INLINE ifoldl' #-}
971 ifoldl' = G.ifoldl'
972
973 -- | /O(n)/ Right fold (function applied to each element and its index)
974 ifoldr :: Prim a => (Int -> a -> b -> b) -> b -> Vector a -> b
975 {-# INLINE ifoldr #-}
976 ifoldr = G.ifoldr
977
978 -- | /O(n)/ Right fold with strict accumulator (function applied to each
979 -- element and its index)
980 ifoldr' :: Prim a => (Int -> a -> b -> b) -> b -> Vector a -> b
981 {-# INLINE ifoldr' #-}
982 ifoldr' = G.ifoldr'
983
984 -- Specialised folds
985 -- -----------------
986
987 -- | /O(n)/ Check if all elements satisfy the predicate.
988 all :: Prim a => (a -> Bool) -> Vector a -> Bool
989 {-# INLINE all #-}
990 all = G.all
991
992 -- | /O(n)/ Check if any element satisfies the predicate.
993 any :: Prim a => (a -> Bool) -> Vector a -> Bool
994 {-# INLINE any #-}
995 any = G.any
996
997 -- | /O(n)/ Compute the sum of the elements
998 sum :: (Prim a, Num a) => Vector a -> a
999 {-# INLINE sum #-}
1000 sum = G.sum
1001
1002 -- | /O(n)/ Compute the produce of the elements
1003 product :: (Prim a, Num a) => Vector a -> a
1004 {-# INLINE product #-}
1005 product = G.product
1006
1007 -- | /O(n)/ Yield the maximum element of the vector. The vector may not be
1008 -- empty.
1009 maximum :: (Prim a, Ord a) => Vector a -> a
1010 {-# INLINE maximum #-}
1011 maximum = G.maximum
1012
1013 -- | /O(n)/ Yield the maximum element of the vector according to the given
1014 -- comparison function. The vector may not be empty.
1015 maximumBy :: Prim a => (a -> a -> Ordering) -> Vector a -> a
1016 {-# INLINE maximumBy #-}
1017 maximumBy = G.maximumBy
1018
1019 -- | /O(n)/ Yield the minimum element of the vector. The vector may not be
1020 -- empty.
1021 minimum :: (Prim a, Ord a) => Vector a -> a
1022 {-# INLINE minimum #-}
1023 minimum = G.minimum
1024
1025 -- | /O(n)/ Yield the minimum element of the vector according to the given
1026 -- comparison function. The vector may not be empty.
1027 minimumBy :: Prim a => (a -> a -> Ordering) -> Vector a -> a
1028 {-# INLINE minimumBy #-}
1029 minimumBy = G.minimumBy
1030
1031 -- | /O(n)/ Yield the index of the maximum element of the vector. The vector
1032 -- may not be empty.
1033 maxIndex :: (Prim a, Ord a) => Vector a -> Int
1034 {-# INLINE maxIndex #-}
1035 maxIndex = G.maxIndex
1036
1037 -- | /O(n)/ Yield the index of the maximum element of the vector according to
1038 -- the given comparison function. The vector may not be empty.
1039 maxIndexBy :: Prim a => (a -> a -> Ordering) -> Vector a -> Int
1040 {-# INLINE maxIndexBy #-}
1041 maxIndexBy = G.maxIndexBy
1042
1043 -- | /O(n)/ Yield the index of the minimum element of the vector. The vector
1044 -- may not be empty.
1045 minIndex :: (Prim a, Ord a) => Vector a -> Int
1046 {-# INLINE minIndex #-}
1047 minIndex = G.minIndex
1048
1049 -- | /O(n)/ Yield the index of the minimum element of the vector according to
1050 -- the given comparison function. The vector may not be empty.
1051 minIndexBy :: Prim a => (a -> a -> Ordering) -> Vector a -> Int
1052 {-# INLINE minIndexBy #-}
1053 minIndexBy = G.minIndexBy
1054
1055 -- Monadic folds
1056 -- -------------
1057
1058 -- | /O(n)/ Monadic fold
1059 foldM :: (Monad m, Prim b) => (a -> b -> m a) -> a -> Vector b -> m a
1060 {-# INLINE foldM #-}
1061 foldM = G.foldM
1062
1063 -- | /O(n)/ Monadic fold over non-empty vectors
1064 fold1M :: (Monad m, Prim a) => (a -> a -> m a) -> Vector a -> m a
1065 {-# INLINE fold1M #-}
1066 fold1M = G.fold1M
1067
1068 -- | /O(n)/ Monadic fold with strict accumulator
1069 foldM' :: (Monad m, Prim b) => (a -> b -> m a) -> a -> Vector b -> m a
1070 {-# INLINE foldM' #-}
1071 foldM' = G.foldM'
1072
1073 -- | /O(n)/ Monad fold over non-empty vectors with strict accumulator
1074 fold1M' :: (Monad m, Prim a) => (a -> a -> m a) -> Vector a -> m a
1075 {-# INLINE fold1M' #-}
1076 fold1M' = G.fold1M'
1077
1078 -- Prefix sums (scans)
1079 -- -------------------
1080
1081 -- | /O(n)/ Prescan
1082 --
1083 -- @
1084 -- prescanl f z = 'init' . 'scanl' f z
1085 -- @
1086 --
1087 -- Example: @prescanl (+) 0 \<1,2,3,4\> = \<0,1,3,6\>@
1088 --
1089 prescanl :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
1090 {-# INLINE prescanl #-}
1091 prescanl = G.prescanl
1092
1093 -- | /O(n)/ Prescan with strict accumulator
1094 prescanl' :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
1095 {-# INLINE prescanl' #-}
1096 prescanl' = G.prescanl'
1097
1098 -- | /O(n)/ Scan
1099 --
1100 -- @
1101 -- postscanl f z = 'tail' . 'scanl' f z
1102 -- @
1103 --
1104 -- Example: @postscanl (+) 0 \<1,2,3,4\> = \<1,3,6,10\>@
1105 --
1106 postscanl :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
1107 {-# INLINE postscanl #-}
1108 postscanl = G.postscanl
1109
1110 -- | /O(n)/ Scan with strict accumulator
1111 postscanl' :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
1112 {-# INLINE postscanl' #-}
1113 postscanl' = G.postscanl'
1114
1115 -- | /O(n)/ Haskell-style scan
1116 --
1117 -- > scanl f z <x1,...,xn> = <y1,...,y(n+1)>
1118 -- > where y1 = z
1119 -- > yi = f y(i-1) x(i-1)
1120 --
1121 -- Example: @scanl (+) 0 \<1,2,3,4\> = \<0,1,3,6,10\>@
1122 --
1123 scanl :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
1124 {-# INLINE scanl #-}
1125 scanl = G.scanl
1126
1127 -- | /O(n)/ Haskell-style scan with strict accumulator
1128 scanl' :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
1129 {-# INLINE scanl' #-}
1130 scanl' = G.scanl'
1131
1132 -- | /O(n)/ Scan over a non-empty vector
1133 --
1134 -- > scanl f <x1,...,xn> = <y1,...,yn>
1135 -- > where y1 = x1
1136 -- > yi = f y(i-1) xi
1137 --
1138 scanl1 :: Prim a => (a -> a -> a) -> Vector a -> Vector a
1139 {-# INLINE scanl1 #-}
1140 scanl1 = G.scanl1
1141
1142 -- | /O(n)/ Scan over a non-empty vector with a strict accumulator
1143 scanl1' :: Prim a => (a -> a -> a) -> Vector a -> Vector a
1144 {-# INLINE scanl1' #-}
1145 scanl1' = G.scanl1'
1146
1147 -- | /O(n)/ Right-to-left prescan
1148 --
1149 -- @
1150 -- prescanr f z = 'reverse' . 'prescanl' (flip f) z . 'reverse'
1151 -- @
1152 --
1153 prescanr :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b
1154 {-# INLINE prescanr #-}
1155 prescanr = G.prescanr
1156
1157 -- | /O(n)/ Right-to-left prescan with strict accumulator
1158 prescanr' :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b
1159 {-# INLINE prescanr' #-}
1160 prescanr' = G.prescanr'
1161
1162 -- | /O(n)/ Right-to-left scan
1163 postscanr :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b
1164 {-# INLINE postscanr #-}
1165 postscanr = G.postscanr
1166
1167 -- | /O(n)/ Right-to-left scan with strict accumulator
1168 postscanr' :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b
1169 {-# INLINE postscanr' #-}
1170 postscanr' = G.postscanr'
1171
1172 -- | /O(n)/ Right-to-left Haskell-style scan
1173 scanr :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b
1174 {-# INLINE scanr #-}
1175 scanr = G.scanr
1176
1177 -- | /O(n)/ Right-to-left Haskell-style scan with strict accumulator
1178 scanr' :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b
1179 {-# INLINE scanr' #-}
1180 scanr' = G.scanr'
1181
1182 -- | /O(n)/ Right-to-left scan over a non-empty vector
1183 scanr1 :: Prim a => (a -> a -> a) -> Vector a -> Vector a
1184 {-# INLINE scanr1 #-}
1185 scanr1 = G.scanr1
1186
1187 -- | /O(n)/ Right-to-left scan over a non-empty vector with a strict
1188 -- accumulator
1189 scanr1' :: Prim a => (a -> a -> a) -> Vector a -> Vector a
1190 {-# INLINE scanr1' #-}
1191 scanr1' = G.scanr1'
1192
1193 -- Conversions - Lists
1194 -- ------------------------
1195
1196 -- | /O(n)/ Convert a vector to a list
1197 toList :: Prim a => Vector a -> [a]
1198 {-# INLINE toList #-}
1199 toList = G.toList
1200
1201 -- | /O(n)/ Convert a list to a vector
1202 fromList :: Prim a => [a] -> Vector a
1203 {-# INLINE fromList #-}
1204 fromList = G.fromList
1205
1206 -- | /O(n)/ Convert the first @n@ elements of a list to a vector
1207 --
1208 -- @
1209 -- fromListN n xs = 'fromList' ('take' n xs)
1210 -- @
1211 fromListN :: Prim a => Int -> [a] -> Vector a
1212 {-# INLINE fromListN #-}
1213 fromListN = G.fromListN
1214
1215 -- Conversions - Mutable vectors
1216 -- -----------------------------
1217
1218 -- | /O(1)/ Unsafe convert a mutable vector to an immutable one without
1219 -- copying. The mutable vector may not be used after this operation.
1220 unsafeFreeze :: (Prim a, PrimMonad m) => MVector (PrimState m) a -> m (Vector a)
1221 {-# INLINE unsafeFreeze #-}
1222 unsafeFreeze = G.unsafeFreeze
1223
1224 -- | /O(1)/ Unsafely convert an immutable vector to a mutable one without
1225 -- copying. The immutable vector may not be used after this operation.
1226 unsafeThaw :: (Prim a, PrimMonad m) => Vector a -> m (MVector (PrimState m) a)
1227 {-# INLINE unsafeThaw #-}
1228 unsafeThaw = G.unsafeThaw
1229
1230 -- | /O(n)/ Yield a mutable copy of the immutable vector.
1231 thaw :: (Prim a, PrimMonad m) => Vector a -> m (MVector (PrimState m) a)
1232 {-# INLINE thaw #-}
1233 thaw = G.thaw
1234
1235 -- | /O(n)/ Yield an immutable copy of the mutable vector.
1236 freeze :: (Prim a, PrimMonad m) => MVector (PrimState m) a -> m (Vector a)
1237 {-# INLINE freeze #-}
1238 freeze = G.freeze
1239
1240 -- | /O(n)/ Copy an immutable vector into a mutable one. The two vectors must
1241 -- have the same length. This is not checked.
1242 unsafeCopy
1243 :: (Prim a, PrimMonad m) => MVector (PrimState m) a -> Vector a -> m ()
1244 {-# INLINE unsafeCopy #-}
1245 unsafeCopy = G.unsafeCopy
1246
1247 -- | /O(n)/ Copy an immutable vector into a mutable one. The two vectors must
1248 -- have the same length.
1249 copy :: (Prim a, PrimMonad m) => MVector (PrimState m) a -> Vector a -> m ()
1250 {-# INLINE copy #-}
1251 copy = G.copy
1252
1253