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