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