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