Add basicUnsafeThaw
[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 unsafeFreeze, thaw, copy, 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 create = G.create
541
542 -- Restricting memory usage
543 -- ------------------------
544
545 -- | /O(n)/ Yield the argument but force it not to retain any extra memory,
546 -- possibly by copying it.
547 --
548 -- This is especially useful when dealing with slices. For example:
549 --
550 -- > force (slice 0 2 <huge vector>)
551 --
552 -- Here, the slice retains a reference to the huge vector. Forcing it creates
553 -- a copy of just the elements that belong to the slice and allows the huge
554 -- vector to be garbage collected.
555 force :: Prim a => Vector a -> Vector a
556 {-# INLINE force #-}
557 force = G.force
558
559 -- Bulk updates
560 -- ------------
561
562 -- | /O(m+n)/ For each pair @(i,a)@ from the list, replace the vector
563 -- element at position @i@ by @a@.
564 --
565 -- > <5,9,2,7> // [(2,1),(0,3),(2,8)] = <3,9,8,7>
566 --
567 (//) :: Prim a => Vector a -- ^ initial vector (of length @m@)
568 -> [(Int, a)] -- ^ list of index/value pairs (of length @n@)
569 -> Vector a
570 {-# INLINE (//) #-}
571 (//) = (G.//)
572
573 -- | /O(m+min(n1,n2))/ For each index @i@ from the index vector and the
574 -- corresponding value @a@ from the value vector, replace the element of the
575 -- initial vector at position @i@ by @a@.
576 --
577 -- > update_ <5,9,2,7> <2,0,2> <1,3,8> = <3,9,8,7>
578 --
579 update_ :: Prim a
580 => Vector a -- ^ initial vector (of length @m@)
581 -> Vector Int -- ^ index vector (of length @n1@)
582 -> Vector a -- ^ value vector (of length @n2@)
583 -> Vector a
584 {-# INLINE update_ #-}
585 update_ = G.update_
586
587 -- | Same as ('//') but without bounds checking.
588 unsafeUpd :: Prim a => Vector a -> [(Int, a)] -> Vector a
589 {-# INLINE unsafeUpd #-}
590 unsafeUpd = G.unsafeUpd
591
592 -- | Same as 'update_' but without bounds checking.
593 unsafeUpdate_ :: Prim a => Vector a -> Vector Int -> Vector a -> Vector a
594 {-# INLINE unsafeUpdate_ #-}
595 unsafeUpdate_ = G.unsafeUpdate_
596
597 -- Accumulations
598 -- -------------
599
600 -- | /O(m+n)/ For each pair @(i,b)@ from the list, replace the vector element
601 -- @a@ at position @i@ by @f a b@.
602 --
603 -- > accum (+) <5,9,2> [(2,4),(1,6),(0,3),(1,7)] = <5+3, 9+6+7, 2+4>
604 accum :: Prim a
605 => (a -> b -> a) -- ^ accumulating function @f@
606 -> Vector a -- ^ initial vector (of length @m@)
607 -> [(Int,b)] -- ^ list of index/value pairs (of length @n@)
608 -> Vector a
609 {-# INLINE accum #-}
610 accum = G.accum
611
612 -- | /O(m+min(n1,n2))/ For each index @i@ from the index vector and the
613 -- corresponding value @b@ from the the value vector,
614 -- replace the element of the initial vector at
615 -- position @i@ by @f a b@.
616 --
617 -- > accumulate_ (+) <5,9,2> <2,1,0,1> <4,6,3,7> = <5+3, 9+6+7, 2+4>
618 --
619 accumulate_ :: (Prim a, Prim b)
620 => (a -> b -> a) -- ^ accumulating function @f@
621 -> Vector a -- ^ initial vector (of length @m@)
622 -> Vector Int -- ^ index vector (of length @n1@)
623 -> Vector b -- ^ value vector (of length @n2@)
624 -> Vector a
625 {-# INLINE accumulate_ #-}
626 accumulate_ = G.accumulate_
627
628 -- | Same as 'accum' but without bounds checking.
629 unsafeAccum :: Prim a => (a -> b -> a) -> Vector a -> [(Int,b)] -> Vector a
630 {-# INLINE unsafeAccum #-}
631 unsafeAccum = G.unsafeAccum
632
633 -- | Same as 'accumulate_' but without bounds checking.
634 unsafeAccumulate_ :: (Prim a, Prim b) =>
635 (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a
636 {-# INLINE unsafeAccumulate_ #-}
637 unsafeAccumulate_ = G.unsafeAccumulate_
638
639 -- Permutations
640 -- ------------
641
642 -- | /O(n)/ Reverse a vector
643 reverse :: Prim a => Vector a -> Vector a
644 {-# INLINE reverse #-}
645 reverse = G.reverse
646
647 -- | /O(n)/ Yield the vector obtained by replacing each element @i@ of the
648 -- index vector by @xs'!'i@. This is equivalent to @'map' (xs'!') is@ but is
649 -- often much more efficient.
650 --
651 -- > backpermute <a,b,c,d> <0,3,2,3,1,0> = <a,d,c,d,b,a>
652 backpermute :: Prim a => Vector a -> Vector Int -> Vector a
653 {-# INLINE backpermute #-}
654 backpermute = G.backpermute
655
656 -- | Same as 'backpermute' but without bounds checking.
657 unsafeBackpermute :: Prim a => Vector a -> Vector Int -> Vector a
658 {-# INLINE unsafeBackpermute #-}
659 unsafeBackpermute = G.unsafeBackpermute
660
661 -- Safe destructive updates
662 -- ------------------------
663
664 -- | Apply a destructive operation to a vector. The operation will be
665 -- performed in place if it is safe to do so and will modify a copy of the
666 -- vector otherwise.
667 --
668 -- @
669 -- modify (\\v -> write v 0 \'x\') ('replicate' 3 \'a\') = \<\'x\',\'a\',\'a\'\>
670 -- @
671 modify :: Prim a => (forall s. MVector s a -> ST s ()) -> Vector a -> Vector a
672 {-# INLINE modify #-}
673 modify = G.modify
674
675 -- Mapping
676 -- -------
677
678 -- | /O(n)/ Map a function over a vector
679 map :: (Prim a, Prim b) => (a -> b) -> Vector a -> Vector b
680 {-# INLINE map #-}
681 map = G.map
682
683 -- | /O(n)/ Apply a function to every element of a vector and its index
684 imap :: (Prim a, Prim b) => (Int -> a -> b) -> Vector a -> Vector b
685 {-# INLINE imap #-}
686 imap = G.imap
687
688 -- | Map a function over a vector and concatenate the results.
689 concatMap :: (Prim a, Prim b) => (a -> Vector b) -> Vector a -> Vector b
690 {-# INLINE concatMap #-}
691 concatMap = G.concatMap
692
693 -- Monadic mapping
694 -- ---------------
695
696 -- | /O(n)/ Apply the monadic action to all elements of the vector, yielding a
697 -- vector of results
698 mapM :: (Monad m, Prim a, Prim b) => (a -> m b) -> Vector a -> m (Vector b)
699 {-# INLINE mapM #-}
700 mapM = G.mapM
701
702 -- | /O(n)/ Apply the monadic action to all elements of a vector and ignore the
703 -- results
704 mapM_ :: (Monad m, Prim a) => (a -> m b) -> Vector a -> m ()
705 {-# INLINE mapM_ #-}
706 mapM_ = G.mapM_
707
708 -- | /O(n)/ Apply the monadic action to all elements of the vector, yielding a
709 -- vector of results. Equvalent to @flip 'mapM'@.
710 forM :: (Monad m, Prim a, Prim b) => Vector a -> (a -> m b) -> m (Vector b)
711 {-# INLINE forM #-}
712 forM = G.forM
713
714 -- | /O(n)/ Apply the monadic action to all elements of a vector and ignore the
715 -- results. Equivalent to @flip 'mapM_'@.
716 forM_ :: (Monad m, Prim a) => Vector a -> (a -> m b) -> m ()
717 {-# INLINE forM_ #-}
718 forM_ = G.forM_
719
720 -- Zipping
721 -- -------
722
723 -- | /O(min(m,n))/ Zip two vectors with the given function.
724 zipWith :: (Prim a, Prim b, Prim c)
725 => (a -> b -> c) -> Vector a -> Vector b -> Vector c
726 {-# INLINE zipWith #-}
727 zipWith = G.zipWith
728
729 -- | Zip three vectors with the given function.
730 zipWith3 :: (Prim a, Prim b, Prim c, Prim d)
731 => (a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d
732 {-# INLINE zipWith3 #-}
733 zipWith3 = G.zipWith3
734
735 zipWith4 :: (Prim a, Prim b, Prim c, Prim d, Prim e)
736 => (a -> b -> c -> d -> e)
737 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
738 {-# INLINE zipWith4 #-}
739 zipWith4 = G.zipWith4
740
741 zipWith5 :: (Prim a, Prim b, Prim c, Prim d, Prim e,
742 Prim f)
743 => (a -> b -> c -> d -> e -> f)
744 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
745 -> Vector f
746 {-# INLINE zipWith5 #-}
747 zipWith5 = G.zipWith5
748
749 zipWith6 :: (Prim a, Prim b, Prim c, Prim d, Prim e,
750 Prim f, Prim g)
751 => (a -> b -> c -> d -> e -> f -> g)
752 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
753 -> Vector f -> Vector g
754 {-# INLINE zipWith6 #-}
755 zipWith6 = G.zipWith6
756
757 -- | /O(min(m,n))/ Zip two vectors with a function that also takes the
758 -- elements' indices.
759 izipWith :: (Prim a, Prim b, Prim c)
760 => (Int -> a -> b -> c) -> Vector a -> Vector b -> Vector c
761 {-# INLINE izipWith #-}
762 izipWith = G.izipWith
763
764 -- | Zip three vectors and their indices with the given function.
765 izipWith3 :: (Prim a, Prim b, Prim c, Prim d)
766 => (Int -> a -> b -> c -> d)
767 -> Vector a -> Vector b -> Vector c -> Vector d
768 {-# INLINE izipWith3 #-}
769 izipWith3 = G.izipWith3
770
771 izipWith4 :: (Prim a, Prim b, Prim c, Prim d, Prim e)
772 => (Int -> a -> b -> c -> d -> e)
773 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
774 {-# INLINE izipWith4 #-}
775 izipWith4 = G.izipWith4
776
777 izipWith5 :: (Prim a, Prim b, Prim c, Prim d, Prim e,
778 Prim f)
779 => (Int -> a -> b -> c -> d -> e -> f)
780 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
781 -> Vector f
782 {-# INLINE izipWith5 #-}
783 izipWith5 = G.izipWith5
784
785 izipWith6 :: (Prim a, Prim b, Prim c, Prim d, Prim e,
786 Prim f, Prim g)
787 => (Int -> a -> b -> c -> d -> e -> f -> g)
788 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
789 -> Vector f -> Vector g
790 {-# INLINE izipWith6 #-}
791 izipWith6 = G.izipWith6
792
793 -- Monadic zipping
794 -- ---------------
795
796 -- | /O(min(m,n))/ Zip the two vectors with the monadic action and yield a
797 -- vector of results
798 zipWithM :: (Monad m, Prim a, Prim b, Prim c)
799 => (a -> b -> m c) -> Vector a -> Vector b -> m (Vector c)
800 {-# INLINE zipWithM #-}
801 zipWithM = G.zipWithM
802
803 -- | /O(min(m,n))/ Zip the two vectors with the monadic action and ignore the
804 -- results
805 zipWithM_ :: (Monad m, Prim a, Prim b)
806 => (a -> b -> m c) -> Vector a -> Vector b -> m ()
807 {-# INLINE zipWithM_ #-}
808 zipWithM_ = G.zipWithM_
809
810 -- Filtering
811 -- ---------
812
813 -- | /O(n)/ Drop elements that do not satisfy the predicate
814 filter :: Prim a => (a -> Bool) -> Vector a -> Vector a
815 {-# INLINE filter #-}
816 filter = G.filter
817
818 -- | /O(n)/ Drop elements that do not satisfy the predicate which is applied to
819 -- values and their indices
820 ifilter :: Prim a => (Int -> a -> Bool) -> Vector a -> Vector a
821 {-# INLINE ifilter #-}
822 ifilter = G.ifilter
823
824 -- | /O(n)/ Drop elements that do not satisfy the monadic predicate
825 filterM :: (Monad m, Prim a) => (a -> m Bool) -> Vector a -> m (Vector a)
826 {-# INLINE filterM #-}
827 filterM = G.filterM
828
829 -- | /O(n)/ Yield the longest prefix of elements satisfying the predicate
830 -- without copying.
831 takeWhile :: Prim a => (a -> Bool) -> Vector a -> Vector a
832 {-# INLINE takeWhile #-}
833 takeWhile = G.takeWhile
834
835 -- | /O(n)/ Drop the longest prefix of elements that satisfy the predicate
836 -- without copying.
837 dropWhile :: Prim a => (a -> Bool) -> Vector a -> Vector a
838 {-# INLINE dropWhile #-}
839 dropWhile = G.dropWhile
840
841 -- Parititioning
842 -- -------------
843
844 -- | /O(n)/ Split the vector in two parts, the first one containing those
845 -- elements that satisfy the predicate and the second one those that don't. The
846 -- relative order of the elements is preserved at the cost of a sometimes
847 -- reduced performance compared to 'unstablePartition'.
848 partition :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
849 {-# INLINE partition #-}
850 partition = G.partition
851
852 -- | /O(n)/ Split the vector in two parts, the first one containing those
853 -- elements that satisfy the predicate and the second one those that don't.
854 -- The order of the elements is not preserved but the operation is often
855 -- faster than 'partition'.
856 unstablePartition :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
857 {-# INLINE unstablePartition #-}
858 unstablePartition = G.unstablePartition
859
860 -- | /O(n)/ Split the vector into the longest prefix of elements that satisfy
861 -- the predicate and the rest without copying.
862 span :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
863 {-# INLINE span #-}
864 span = G.span
865
866 -- | /O(n)/ Split the vector into the longest prefix of elements that do not
867 -- satisfy the predicate and the rest without copying.
868 break :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
869 {-# INLINE break #-}
870 break = G.break
871
872 -- Searching
873 -- ---------
874
875 infix 4 `elem`
876 -- | /O(n)/ Check if the vector contains an element
877 elem :: (Prim a, Eq a) => a -> Vector a -> Bool
878 {-# INLINE elem #-}
879 elem = G.elem
880
881 infix 4 `notElem`
882 -- | /O(n)/ Check if the vector does not contain an element (inverse of 'elem')
883 notElem :: (Prim a, Eq a) => a -> Vector a -> Bool
884 {-# INLINE notElem #-}
885 notElem = G.notElem
886
887 -- | /O(n)/ Yield 'Just' the first element matching the predicate or 'Nothing'
888 -- if no such element exists.
889 find :: Prim a => (a -> Bool) -> Vector a -> Maybe a
890 {-# INLINE find #-}
891 find = G.find
892
893 -- | /O(n)/ Yield 'Just' the index of the first element matching the predicate
894 -- or 'Nothing' if no such element exists.
895 findIndex :: Prim a => (a -> Bool) -> Vector a -> Maybe Int
896 {-# INLINE findIndex #-}
897 findIndex = G.findIndex
898
899 -- | /O(n)/ Yield the indices of elements satisfying the predicate in ascending
900 -- order.
901 findIndices :: Prim a => (a -> Bool) -> Vector a -> Vector Int
902 {-# INLINE findIndices #-}
903 findIndices = G.findIndices
904
905 -- | /O(n)/ Yield 'Just' the index of the first occurence of the given element or
906 -- 'Nothing' if the vector does not contain the element. This is a specialised
907 -- version of 'findIndex'.
908 elemIndex :: (Prim a, Eq a) => a -> Vector a -> Maybe Int
909 {-# INLINE elemIndex #-}
910 elemIndex = G.elemIndex
911
912 -- | /O(n)/ Yield the indices of all occurences of the given element in
913 -- ascending order. This is a specialised version of 'findIndices'.
914 elemIndices :: (Prim a, Eq a) => a -> Vector a -> Vector Int
915 {-# INLINE elemIndices #-}
916 elemIndices = G.elemIndices
917
918 -- Folding
919 -- -------
920
921 -- | /O(n)/ Left fold
922 foldl :: Prim b => (a -> b -> a) -> a -> Vector b -> a
923 {-# INLINE foldl #-}
924 foldl = G.foldl
925
926 -- | /O(n)/ Left fold on non-empty vectors
927 foldl1 :: Prim a => (a -> a -> a) -> Vector a -> a
928 {-# INLINE foldl1 #-}
929 foldl1 = G.foldl1
930
931 -- | /O(n)/ Left fold with strict accumulator
932 foldl' :: Prim b => (a -> b -> a) -> a -> Vector b -> a
933 {-# INLINE foldl' #-}
934 foldl' = G.foldl'
935
936 -- | /O(n)/ Left fold on non-empty vectors with strict accumulator
937 foldl1' :: Prim a => (a -> a -> a) -> Vector a -> a
938 {-# INLINE foldl1' #-}
939 foldl1' = G.foldl1'
940
941 -- | /O(n)/ Right fold
942 foldr :: Prim a => (a -> b -> b) -> b -> Vector a -> b
943 {-# INLINE foldr #-}
944 foldr = G.foldr
945
946 -- | /O(n)/ Right fold on non-empty vectors
947 foldr1 :: Prim a => (a -> a -> a) -> Vector a -> a
948 {-# INLINE foldr1 #-}
949 foldr1 = G.foldr1
950
951 -- | /O(n)/ Right fold with a strict accumulator
952 foldr' :: Prim a => (a -> b -> b) -> b -> Vector a -> b
953 {-# INLINE foldr' #-}
954 foldr' = G.foldr'
955
956 -- | /O(n)/ Right fold on non-empty vectors with strict accumulator
957 foldr1' :: Prim a => (a -> a -> a) -> Vector a -> a
958 {-# INLINE foldr1' #-}
959 foldr1' = G.foldr1'
960
961 -- | /O(n)/ Left fold (function applied to each element and its index)
962 ifoldl :: Prim b => (a -> Int -> b -> a) -> a -> Vector b -> a
963 {-# INLINE ifoldl #-}
964 ifoldl = G.ifoldl
965
966 -- | /O(n)/ Left fold with strict accumulator (function applied to each element
967 -- and its index)
968 ifoldl' :: Prim b => (a -> Int -> b -> a) -> a -> Vector b -> a
969 {-# INLINE ifoldl' #-}
970 ifoldl' = G.ifoldl'
971
972 -- | /O(n)/ Right fold (function applied to each element and its index)
973 ifoldr :: Prim a => (Int -> a -> b -> b) -> b -> Vector a -> b
974 {-# INLINE ifoldr #-}
975 ifoldr = G.ifoldr
976
977 -- | /O(n)/ Right fold with strict accumulator (function applied to each
978 -- element and its index)
979 ifoldr' :: Prim a => (Int -> a -> b -> b) -> b -> Vector a -> b
980 {-# INLINE ifoldr' #-}
981 ifoldr' = G.ifoldr'
982
983 -- Specialised folds
984 -- -----------------
985
986 -- | /O(n)/ Check if all elements satisfy the predicate.
987 all :: Prim a => (a -> Bool) -> Vector a -> Bool
988 {-# INLINE all #-}
989 all = G.all
990
991 -- | /O(n)/ Check if any element satisfies the predicate.
992 any :: Prim a => (a -> Bool) -> Vector a -> Bool
993 {-# INLINE any #-}
994 any = G.any
995
996 -- | /O(n)/ Compute the sum of the elements
997 sum :: (Prim a, Num a) => Vector a -> a
998 {-# INLINE sum #-}
999 sum = G.sum
1000
1001 -- | /O(n)/ Compute the produce of the elements
1002 product :: (Prim a, Num a) => Vector a -> a
1003 {-# INLINE product #-}
1004 product = G.product
1005
1006 -- | /O(n)/ Yield the maximum element of the vector. The vector may not be
1007 -- empty.
1008 maximum :: (Prim a, Ord a) => Vector a -> a
1009 {-# INLINE maximum #-}
1010 maximum = G.maximum
1011
1012 -- | /O(n)/ Yield the maximum element of the vector according to the given
1013 -- comparison function. The vector may not be empty.
1014 maximumBy :: Prim a => (a -> a -> Ordering) -> Vector a -> a
1015 {-# INLINE maximumBy #-}
1016 maximumBy = G.maximumBy
1017
1018 -- | /O(n)/ Yield the minimum element of the vector. The vector may not be
1019 -- empty.
1020 minimum :: (Prim a, Ord a) => Vector a -> a
1021 {-# INLINE minimum #-}
1022 minimum = G.minimum
1023
1024 -- | /O(n)/ Yield the minimum element of the vector according to the given
1025 -- comparison function. The vector may not be empty.
1026 minimumBy :: Prim a => (a -> a -> Ordering) -> Vector a -> a
1027 {-# INLINE minimumBy #-}
1028 minimumBy = G.minimumBy
1029
1030 -- | /O(n)/ Yield the index of the maximum element of the vector. The vector
1031 -- may not be empty.
1032 maxIndex :: (Prim a, Ord a) => Vector a -> Int
1033 {-# INLINE maxIndex #-}
1034 maxIndex = G.maxIndex
1035
1036 -- | /O(n)/ Yield the index of the maximum element of the vector according to
1037 -- the given comparison function. The vector may not be empty.
1038 maxIndexBy :: Prim a => (a -> a -> Ordering) -> Vector a -> Int
1039 {-# INLINE maxIndexBy #-}
1040 maxIndexBy = G.maxIndexBy
1041
1042 -- | /O(n)/ Yield the index of the minimum element of the vector. The vector
1043 -- may not be empty.
1044 minIndex :: (Prim a, Ord a) => Vector a -> Int
1045 {-# INLINE minIndex #-}
1046 minIndex = G.minIndex
1047
1048 -- | /O(n)/ Yield the index of the minimum element of the vector according to
1049 -- the given comparison function. The vector may not be empty.
1050 minIndexBy :: Prim a => (a -> a -> Ordering) -> Vector a -> Int
1051 {-# INLINE minIndexBy #-}
1052 minIndexBy = G.minIndexBy
1053
1054 -- Monadic folds
1055 -- -------------
1056
1057 -- | /O(n)/ Monadic fold
1058 foldM :: (Monad m, Prim b) => (a -> b -> m a) -> a -> Vector b -> m a
1059 {-# INLINE foldM #-}
1060 foldM = G.foldM
1061
1062 -- | /O(n)/ Monadic fold over non-empty vectors
1063 fold1M :: (Monad m, Prim a) => (a -> a -> m a) -> Vector a -> m a
1064 {-# INLINE fold1M #-}
1065 fold1M = G.fold1M
1066
1067 -- | /O(n)/ Monadic fold with strict accumulator
1068 foldM' :: (Monad m, Prim b) => (a -> b -> m a) -> a -> Vector b -> m a
1069 {-# INLINE foldM' #-}
1070 foldM' = G.foldM'
1071
1072 -- | /O(n)/ Monad fold over non-empty vectors with strict accumulator
1073 fold1M' :: (Monad m, Prim a) => (a -> a -> m a) -> Vector a -> m a
1074 {-# INLINE fold1M' #-}
1075 fold1M' = G.fold1M'
1076
1077 -- Prefix sums (scans)
1078 -- -------------------
1079
1080 -- | /O(n)/ Prescan
1081 --
1082 -- @
1083 -- prescanl f z = 'init' . 'scanl' f z
1084 -- @
1085 --
1086 -- Example: @prescanl (+) 0 \<1,2,3,4\> = \<0,1,3,6\>@
1087 --
1088 prescanl :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
1089 {-# INLINE prescanl #-}
1090 prescanl = G.prescanl
1091
1092 -- | /O(n)/ Prescan with strict accumulator
1093 prescanl' :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
1094 {-# INLINE prescanl' #-}
1095 prescanl' = G.prescanl'
1096
1097 -- | /O(n)/ Scan
1098 --
1099 -- @
1100 -- postscanl f z = 'tail' . 'scanl' f z
1101 -- @
1102 --
1103 -- Example: @postscanl (+) 0 \<1,2,3,4\> = \<1,3,6,10\>@
1104 --
1105 postscanl :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
1106 {-# INLINE postscanl #-}
1107 postscanl = G.postscanl
1108
1109 -- | /O(n)/ Scan with strict accumulator
1110 postscanl' :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
1111 {-# INLINE postscanl' #-}
1112 postscanl' = G.postscanl'
1113
1114 -- | /O(n)/ Haskell-style scan
1115 --
1116 -- > scanl f z <x1,...,xn> = <y1,...,y(n+1)>
1117 -- > where y1 = z
1118 -- > yi = f y(i-1) x(i-1)
1119 --
1120 -- Example: @scanl (+) 0 \<1,2,3,4\> = \<0,1,3,6,10\>@
1121 --
1122 scanl :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
1123 {-# INLINE scanl #-}
1124 scanl = G.scanl
1125
1126 -- | /O(n)/ Haskell-style scan with strict accumulator
1127 scanl' :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
1128 {-# INLINE scanl' #-}
1129 scanl' = G.scanl'
1130
1131 -- | /O(n)/ Scan over a non-empty vector
1132 --
1133 -- > scanl f <x1,...,xn> = <y1,...,yn>
1134 -- > where y1 = x1
1135 -- > yi = f y(i-1) xi
1136 --
1137 scanl1 :: Prim a => (a -> a -> a) -> Vector a -> Vector a
1138 {-# INLINE scanl1 #-}
1139 scanl1 = G.scanl1
1140
1141 -- | /O(n)/ Scan over a non-empty vector with a strict accumulator
1142 scanl1' :: Prim a => (a -> a -> a) -> Vector a -> Vector a
1143 {-# INLINE scanl1' #-}
1144 scanl1' = G.scanl1'
1145
1146 -- | /O(n)/ Right-to-left prescan
1147 --
1148 -- @
1149 -- prescanr f z = 'reverse' . 'prescanl' (flip f) z . 'reverse'
1150 -- @
1151 --
1152 prescanr :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b
1153 {-# INLINE prescanr #-}
1154 prescanr = G.prescanr
1155
1156 -- | /O(n)/ Right-to-left prescan with strict accumulator
1157 prescanr' :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b
1158 {-# INLINE prescanr' #-}
1159 prescanr' = G.prescanr'
1160
1161 -- | /O(n)/ Right-to-left scan
1162 postscanr :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b
1163 {-# INLINE postscanr #-}
1164 postscanr = G.postscanr
1165
1166 -- | /O(n)/ Right-to-left scan with strict accumulator
1167 postscanr' :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b
1168 {-# INLINE postscanr' #-}
1169 postscanr' = G.postscanr'
1170
1171 -- | /O(n)/ Right-to-left Haskell-style scan
1172 scanr :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b
1173 {-# INLINE scanr #-}
1174 scanr = G.scanr
1175
1176 -- | /O(n)/ Right-to-left Haskell-style scan with strict accumulator
1177 scanr' :: (Prim a, Prim b) => (a -> b -> b) -> b -> Vector a -> Vector b
1178 {-# INLINE scanr' #-}
1179 scanr' = G.scanr'
1180
1181 -- | /O(n)/ Right-to-left scan over a non-empty vector
1182 scanr1 :: Prim a => (a -> a -> a) -> Vector a -> Vector a
1183 {-# INLINE scanr1 #-}
1184 scanr1 = G.scanr1
1185
1186 -- | /O(n)/ Right-to-left scan over a non-empty vector with a strict
1187 -- accumulator
1188 scanr1' :: Prim a => (a -> a -> a) -> Vector a -> Vector a
1189 {-# INLINE scanr1' #-}
1190 scanr1' = G.scanr1'
1191
1192 -- Conversions - Lists
1193 -- ------------------------
1194
1195 -- | /O(n)/ Convert a vector to a list
1196 toList :: Prim a => Vector a -> [a]
1197 {-# INLINE toList #-}
1198 toList = G.toList
1199
1200 -- | /O(n)/ Convert a list to a vector
1201 fromList :: Prim a => [a] -> Vector a
1202 {-# INLINE fromList #-}
1203 fromList = G.fromList
1204
1205 -- | /O(n)/ Convert the first @n@ elements of a list to a vector
1206 --
1207 -- @
1208 -- fromListN n xs = 'fromList' ('take' n xs)
1209 -- @
1210 fromListN :: Prim a => Int -> [a] -> Vector a
1211 {-# INLINE fromListN #-}
1212 fromListN = G.fromListN
1213
1214 -- Conversions - Mutable vectors
1215 -- -----------------------------
1216
1217 -- | /O(1)/ Unsafe convert a mutable vector to an immutable one without
1218 -- copying. The mutable vector may not be used after this operation.
1219 unsafeFreeze :: (Prim a, PrimMonad m) => MVector (PrimState m) a -> m (Vector a)
1220 {-# INLINE unsafeFreeze #-}
1221 unsafeFreeze = G.unsafeFreeze
1222
1223 -- | /O(n)/ Yield a mutable copy of the immutable vector.
1224 thaw :: (Prim a, PrimMonad m) => Vector a -> m (MVector (PrimState m) a)
1225 {-# INLINE thaw #-}
1226 thaw = G.thaw
1227
1228 -- | /O(n)/ Copy an immutable vector into a mutable one. The two vectors must
1229 -- have the same length.
1230 unsafeCopy
1231 :: (Prim a, PrimMonad m) => MVector (PrimState m) a -> Vector a -> m ()
1232 {-# INLINE unsafeCopy #-}
1233 unsafeCopy = G.unsafeCopy
1234
1235 -- | /O(n)/ Copy an immutable vector into a mutable one. The two vectors must
1236 -- have the same length. This is not checked.
1237 copy :: (Prim a, PrimMonad m) => MVector (PrimState m) a -> Vector a -> m ()
1238 {-# INLINE copy #-}
1239 copy = G.copy
1240
1241