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