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