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