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