Rename copy -> force
[darcs-mirrors/vector.git] / Data / Vector / Storable.hs
1 {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, TypeFamilies, ScopedTypeVariables #-}
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 Vector, MVector(..), Storable,
17
18 -- * Length information
19 length, null,
20
21 -- * Construction
22 empty, singleton, cons, snoc, replicate, generate, (++), force,
23
24 -- * Accessing individual elements
25 (!), head, last, indexM, headM, lastM,
26 unsafeIndex, unsafeHead, unsafeLast,
27 unsafeIndexM, unsafeHeadM, unsafeLastM,
28
29 -- * Subvectors
30 slice, init, tail, take, drop,
31 unsafeSlice, unsafeInit, unsafeTail, unsafeTake, unsafeDrop,
32
33 -- * Permutations
34 accum, accumulate_, (//), update_, backpermute, reverse,
35 unsafeAccum, unsafeAccumulate_,
36 unsafeUpd, unsafeUpdate_,
37 unsafeBackpermute,
38
39 -- * Mapping
40 map, imap, concatMap,
41
42 -- * Zipping and unzipping
43 zipWith, zipWith3, zipWith4, zipWith5, zipWith6,
44 izipWith, izipWith3, izipWith4, izipWith5, izipWith6,
45
46 -- * Filtering
47 filter, ifilter, takeWhile, dropWhile,
48 partition, unstablePartition, span, break,
49
50 -- * Searching
51 elem, notElem, find, findIndex, findIndices, elemIndex, elemIndices,
52
53 -- * Folding
54 foldl, foldl1, foldl', foldl1', foldr, foldr1, foldr', foldr1',
55 ifoldl, ifoldl', ifoldr, ifoldr',
56
57 -- * Specialised folds
58 all, any, and, or,
59 sum, product,
60 maximum, maximumBy, minimum, minimumBy,
61 minIndex, minIndexBy, maxIndex, maxIndexBy,
62
63 -- * Unfolding
64 unfoldr, unfoldrN,
65
66 -- * Scans
67 prescanl, prescanl',
68 postscanl, postscanl',
69 scanl, scanl', scanl1, scanl1',
70 prescanr, prescanr',
71 postscanr, postscanr',
72 scanr, scanr', scanr1, scanr1',
73
74 -- * Enumeration
75 enumFromN, enumFromStepN, enumFromTo, enumFromThenTo,
76
77 -- * Conversion to/from lists
78 toList, fromList, fromListN,
79
80 -- * Accessing the underlying memory
81 unsafeFromForeignPtr, unsafeToForeignPtr, unsafeWith
82 ) where
83
84 import qualified Data.Vector.Generic as G
85 import Data.Vector.Storable.Mutable ( MVector(..) )
86 import Data.Vector.Storable.Internal
87 import qualified Data.Vector.Fusion.Stream as Stream
88
89 import Foreign.Storable
90 import Foreign.ForeignPtr
91 import Foreign.Ptr
92 import Foreign.Marshal.Array ( advancePtr )
93 import Foreign.Marshal.Utils ( copyBytes )
94
95 import Control.Monad.Primitive ( unsafePrimToPrim )
96
97 import Prelude hiding ( length, null,
98 replicate, (++),
99 head, last,
100 init, tail, take, drop, reverse,
101 map, concatMap,
102 zipWith, zipWith3, zip, zip3, unzip, unzip3,
103 filter, takeWhile, dropWhile, span, break,
104 elem, notElem,
105 foldl, foldl1, foldr, foldr1,
106 all, any, and, or, sum, product, minimum, maximum,
107 scanl, scanl1, scanr, scanr1,
108 enumFromTo, enumFromThenTo )
109
110 import qualified Prelude
111
112 import Data.Typeable ( Typeable )
113 import Data.Data ( Data(..) )
114
115 #include "vector.h"
116
117 -- | 'Storable'-based vectors
118 data Vector a = Vector {-# UNPACK #-} !(Ptr a)
119 {-# UNPACK #-} !Int
120 {-# UNPACK #-} !(ForeignPtr a)
121 deriving ( Typeable )
122
123 instance (Show a, Storable a) => Show (Vector a) where
124 show = (Prelude.++ " :: Data.Vector.Storable.Vector")
125 . ("fromList " Prelude.++)
126 . show
127 . toList
128
129 instance (Data a, Storable a) => Data (Vector a) where
130 gfoldl = G.gfoldl
131 toConstr _ = error "toConstr"
132 gunfold _ _ = error "gunfold"
133 dataTypeOf _ = G.mkType "Data.Vector.Storable.Vector"
134 dataCast1 = G.dataCast
135
136 type instance G.Mutable Vector = MVector
137
138 instance Storable a => G.Vector Vector a where
139 {-# INLINE unsafeFreeze #-}
140 unsafeFreeze (MVector p n fp) = return $ Vector p n fp
141
142 {-# INLINE basicLength #-}
143 basicLength (Vector _ n _) = n
144
145 {-# INLINE basicUnsafeSlice #-}
146 basicUnsafeSlice i n (Vector p _ fp) = Vector (p `advancePtr` i) n fp
147
148 {-# INLINE basicUnsafeIndexM #-}
149 basicUnsafeIndexM (Vector p _ fp) i = return
150 . inlinePerformIO
151 $ withForeignPtr fp $ \_ ->
152 peekElemOff p i
153
154 {-# INLINE basicUnsafeCopy #-}
155 basicUnsafeCopy (MVector p n fp) (Vector q _ fq)
156 = unsafePrimToPrim
157 $ withForeignPtr fp $ \_ ->
158 withForeignPtr fq $ \_ ->
159 do
160 copyBytes p q (fromIntegral (n * sizeOf (undefined :: a)))
161 return ()
162
163 {-# INLINE elemseq #-}
164 elemseq _ = seq
165
166 -- See [HACKS:Eq and Ord instances]
167 instance (Storable a, Eq a) => Eq (Vector a) where
168 {-# INLINE (==) #-}
169 xs == ys = Stream.eq (G.stream xs) (G.stream ys)
170
171 {-# INLINE (/=) #-}
172 xs /= ys = not (Stream.eq (G.stream xs) (G.stream ys))
173
174 -- See [HACKS:Eq and Ord instances]
175 instance (Storable a, Ord a) => Ord (Vector a) where
176 {-# INLINE compare #-}
177 compare xs ys = Stream.cmp (G.stream xs) (G.stream ys)
178
179 {-# INLINE (<) #-}
180 xs < ys = Stream.cmp (G.stream xs) (G.stream ys) == LT
181
182 {-# INLINE (<=) #-}
183 xs <= ys = Stream.cmp (G.stream xs) (G.stream ys) /= GT
184
185 {-# INLINE (>) #-}
186 xs > ys = Stream.cmp (G.stream xs) (G.stream ys) == GT
187
188 {-# INLINE (>=) #-}
189 xs >= ys = Stream.cmp (G.stream xs) (G.stream ys) /= LT
190
191 {-
192 eq_memcmp :: forall a. Storable a => Vector a -> Vector a -> Bool
193 {-# INLINE_STREAM eq_memcmp #-}
194 eq_memcmp (Vector i m p) (Vector j n q)
195 = m == n && inlinePerformIO
196 (withForeignPtr p $ \p' ->
197 withForeignPtr q $ \q' ->
198 return $
199 memcmp (p' `plusPtr` i) (q' `plusPtr` j)
200 (fromIntegral $ sizeOf (undefined :: a) * m) == 0)
201
202 foreign import ccall unsafe "string.h memcmp" memcmp
203 :: Ptr a -> Ptr a -> CSize -> CInt
204
205 {-# RULES
206
207 "(==) [Vector.Storable Int]"
208 G.eq = eq_memcmp :: Vector Int -> Vector Int -> Bool
209 #-}
210 -}
211
212
213 -- Length
214 -- ------
215
216 length :: Storable a => Vector a -> Int
217 {-# INLINE length #-}
218 length = G.length
219
220 null :: Storable a => Vector a -> Bool
221 {-# INLINE null #-}
222 null = G.null
223
224 -- Construction
225 -- ------------
226
227 -- | Empty vector
228 empty :: Storable a => Vector a
229 {-# INLINE empty #-}
230 empty = G.empty
231
232 -- | Vector with exaclty one element
233 singleton :: Storable a => a -> Vector a
234 {-# INLINE singleton #-}
235 singleton = G.singleton
236
237 -- | Vector of the given length with the given value in each position
238 replicate :: Storable a => Int -> a -> Vector a
239 {-# INLINE replicate #-}
240 replicate = G.replicate
241
242 -- | Generate a vector of the given length by applying the function to each
243 -- index
244 generate :: Storable a => Int -> (Int -> a) -> Vector a
245 {-# INLINE generate #-}
246 generate = G.generate
247
248 -- | Prepend an element
249 cons :: Storable a => a -> Vector a -> Vector a
250 {-# INLINE cons #-}
251 cons = G.cons
252
253 -- | Append an element
254 snoc :: Storable a => Vector a -> a -> Vector a
255 {-# INLINE snoc #-}
256 snoc = G.snoc
257
258 infixr 5 ++
259 -- | Concatenate two vectors
260 (++) :: Storable a => Vector a -> Vector a -> Vector a
261 {-# INLINE (++) #-}
262 (++) = (G.++)
263
264 -- | Create a copy of a vector. Useful when dealing with slices.
265 force :: Storable a => Vector a -> Vector a
266 {-# INLINE force #-}
267 force = G.force
268
269 -- Accessing individual elements
270 -- -----------------------------
271
272 -- | Indexing
273 (!) :: Storable a => Vector a -> Int -> a
274 {-# INLINE (!) #-}
275 (!) = (G.!)
276
277 -- | First element
278 head :: Storable a => Vector a -> a
279 {-# INLINE head #-}
280 head = G.head
281
282 -- | Last element
283 last :: Storable a => Vector a -> a
284 {-# INLINE last #-}
285 last = G.last
286
287 -- | Unsafe indexing without bounds checking
288 unsafeIndex :: Storable a => Vector a -> Int -> a
289 {-# INLINE unsafeIndex #-}
290 unsafeIndex = G.unsafeIndex
291
292 -- | Yield the first element of a vector without checking if the vector is
293 -- empty
294 unsafeHead :: Storable a => Vector a -> a
295 {-# INLINE unsafeHead #-}
296 unsafeHead = G.unsafeHead
297
298 -- | Yield the last element of a vector without checking if the vector is
299 -- empty
300 unsafeLast :: Storable a => Vector a -> a
301 {-# INLINE unsafeLast #-}
302 unsafeLast = G.unsafeLast
303
304 -- | Monadic indexing which can be strict in the vector while remaining lazy in
305 -- the element
306 indexM :: (Storable a, Monad m) => Vector a -> Int -> m a
307 {-# INLINE indexM #-}
308 indexM = G.indexM
309
310 headM :: (Storable a, Monad m) => Vector a -> m a
311 {-# INLINE headM #-}
312 headM = G.headM
313
314 lastM :: (Storable a, Monad m) => Vector a -> m a
315 {-# INLINE lastM #-}
316 lastM = G.lastM
317
318 -- | Unsafe monadic indexing without bounds checks
319 unsafeIndexM :: (Storable a, Monad m) => Vector a -> Int -> m a
320 {-# INLINE unsafeIndexM #-}
321 unsafeIndexM = G.unsafeIndexM
322
323 unsafeHeadM :: (Storable a, Monad m) => Vector a -> m a
324 {-# INLINE unsafeHeadM #-}
325 unsafeHeadM = G.unsafeHeadM
326
327 unsafeLastM :: (Storable a, Monad m) => Vector a -> m a
328 {-# INLINE unsafeLastM #-}
329 unsafeLastM = G.unsafeLastM
330
331 -- Subarrays
332 -- ---------
333
334 -- | Yield a part of the vector without copying it. Safer version of
335 -- 'basicUnsafeSlice'.
336 slice :: Storable a => Int -- ^ starting index
337 -> Int -- ^ length
338 -> Vector a
339 -> Vector a
340 {-# INLINE slice #-}
341 slice = G.slice
342
343 -- | Yield all but the last element without copying.
344 init :: Storable a => Vector a -> Vector a
345 {-# INLINE init #-}
346 init = G.init
347
348 -- | All but the first element (without copying).
349 tail :: Storable a => Vector a -> Vector a
350 {-# INLINE tail #-}
351 tail = G.tail
352
353 -- | Yield the first @n@ elements without copying.
354 take :: Storable a => Int -> Vector a -> Vector a
355 {-# INLINE take #-}
356 take = G.take
357
358 -- | Yield all but the first @n@ elements without copying.
359 drop :: Storable a => Int -> Vector a -> Vector a
360 {-# INLINE drop #-}
361 drop = G.drop
362
363 -- | Unsafely yield a part of the vector without copying it and without
364 -- performing bounds checks.
365 unsafeSlice :: Storable a => Int -- ^ starting index
366 -> Int -- ^ length
367 -> Vector a
368 -> Vector a
369 {-# INLINE unsafeSlice #-}
370 unsafeSlice = G.unsafeSlice
371
372 unsafeInit :: Storable a => Vector a -> Vector a
373 {-# INLINE unsafeInit #-}
374 unsafeInit = G.unsafeInit
375
376 unsafeTail :: Storable a => Vector a -> Vector a
377 {-# INLINE unsafeTail #-}
378 unsafeTail = G.unsafeTail
379
380 unsafeTake :: Storable a => Int -> Vector a -> Vector a
381 {-# INLINE unsafeTake #-}
382 unsafeTake = G.unsafeTake
383
384 unsafeDrop :: Storable a => Int -> Vector a -> Vector a
385 {-# INLINE unsafeDrop #-}
386 unsafeDrop = G.unsafeDrop
387
388 -- Permutations
389 -- ------------
390
391 unsafeAccum :: Storable a => (a -> b -> a) -> Vector a -> [(Int,b)] -> Vector a
392 {-# INLINE unsafeAccum #-}
393 unsafeAccum = G.unsafeAccum
394
395 unsafeAccumulate_ :: (Storable a, Storable b) =>
396 (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a
397 {-# INLINE unsafeAccumulate_ #-}
398 unsafeAccumulate_ = G.unsafeAccumulate_
399
400 accum :: Storable a => (a -> b -> a) -> Vector a -> [(Int,b)] -> Vector a
401 {-# INLINE accum #-}
402 accum = G.accum
403
404 accumulate_ :: (Storable a, Storable b) =>
405 (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a
406 {-# INLINE accumulate_ #-}
407 accumulate_ = G.accumulate_
408
409 unsafeUpd :: Storable a => Vector a -> [(Int, a)] -> Vector a
410 {-# INLINE unsafeUpd #-}
411 unsafeUpd = G.unsafeUpd
412
413 unsafeUpdate_ :: Storable a => Vector a -> Vector Int -> Vector a -> Vector a
414 {-# INLINE unsafeUpdate_ #-}
415 unsafeUpdate_ = G.unsafeUpdate_
416
417 (//) :: Storable a => Vector a -> [(Int, a)] -> Vector a
418 {-# INLINE (//) #-}
419 (//) = (G.//)
420
421 update_ :: Storable a => Vector a -> Vector Int -> Vector a -> Vector a
422 {-# INLINE update_ #-}
423 update_ = G.update_
424
425 backpermute :: Storable a => Vector a -> Vector Int -> Vector a
426 {-# INLINE backpermute #-}
427 backpermute = G.backpermute
428
429 unsafeBackpermute :: Storable a => Vector a -> Vector Int -> Vector a
430 {-# INLINE unsafeBackpermute #-}
431 unsafeBackpermute = G.unsafeBackpermute
432
433 reverse :: Storable a => Vector a -> Vector a
434 {-# INLINE reverse #-}
435 reverse = G.reverse
436
437 -- Mapping
438 -- -------
439
440 -- | Map a function over a vector
441 map :: (Storable a, Storable b) => (a -> b) -> Vector a -> Vector b
442 {-# INLINE map #-}
443 map = G.map
444
445 -- | Apply a function to every index/value pair
446 imap :: (Storable a, Storable b) => (Int -> a -> b) -> Vector a -> Vector b
447 {-# INLINE imap #-}
448 imap = G.imap
449
450 concatMap :: (Storable a, Storable b) => (a -> Vector b) -> Vector a -> Vector b
451 {-# INLINE concatMap #-}
452 concatMap = G.concatMap
453
454 -- Zipping/unzipping
455 -- -----------------
456
457 -- | Zip two vectors with the given function.
458 zipWith :: (Storable a, Storable b, Storable c)
459 => (a -> b -> c) -> Vector a -> Vector b -> Vector c
460 {-# INLINE zipWith #-}
461 zipWith = G.zipWith
462
463 -- | Zip three vectors with the given function.
464 zipWith3 :: (Storable a, Storable b, Storable c, Storable d)
465 => (a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d
466 {-# INLINE zipWith3 #-}
467 zipWith3 = G.zipWith3
468
469 zipWith4 :: (Storable a, Storable b, Storable c, Storable d, Storable e)
470 => (a -> b -> c -> d -> e)
471 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
472 {-# INLINE zipWith4 #-}
473 zipWith4 = G.zipWith4
474
475 zipWith5 :: (Storable a, Storable b, Storable c, Storable d, Storable e,
476 Storable f)
477 => (a -> b -> c -> d -> e -> f)
478 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
479 -> Vector f
480 {-# INLINE zipWith5 #-}
481 zipWith5 = G.zipWith5
482
483 zipWith6 :: (Storable a, Storable b, Storable c, Storable d, Storable e,
484 Storable f, Storable g)
485 => (a -> b -> c -> d -> e -> f -> g)
486 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
487 -> Vector f -> Vector g
488 {-# INLINE zipWith6 #-}
489 zipWith6 = G.zipWith6
490
491 -- | Zip two vectors and their indices with the given function.
492 izipWith :: (Storable a, Storable b, Storable c)
493 => (Int -> a -> b -> c) -> Vector a -> Vector b -> Vector c
494 {-# INLINE izipWith #-}
495 izipWith = G.izipWith
496
497 -- | Zip three vectors and their indices with the given function.
498 izipWith3 :: (Storable a, Storable b, Storable c, Storable d)
499 => (Int -> a -> b -> c -> d)
500 -> Vector a -> Vector b -> Vector c -> Vector d
501 {-# INLINE izipWith3 #-}
502 izipWith3 = G.izipWith3
503
504 izipWith4 :: (Storable a, Storable b, Storable c, Storable d, Storable e)
505 => (Int -> a -> b -> c -> d -> e)
506 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
507 {-# INLINE izipWith4 #-}
508 izipWith4 = G.izipWith4
509
510 izipWith5 :: (Storable a, Storable b, Storable c, Storable d, Storable e,
511 Storable f)
512 => (Int -> a -> b -> c -> d -> e -> f)
513 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
514 -> Vector f
515 {-# INLINE izipWith5 #-}
516 izipWith5 = G.izipWith5
517
518 izipWith6 :: (Storable a, Storable b, Storable c, Storable d, Storable e,
519 Storable f, Storable g)
520 => (Int -> a -> b -> c -> d -> e -> f -> g)
521 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
522 -> Vector f -> Vector g
523 {-# INLINE izipWith6 #-}
524 izipWith6 = G.izipWith6
525
526 -- Filtering
527 -- ---------
528
529 -- | Drop elements which do not satisfy the predicate
530 filter :: Storable a => (a -> Bool) -> Vector a -> Vector a
531 {-# INLINE filter #-}
532 filter = G.filter
533
534 -- | Drop elements that do not satisfy the predicate (applied to values and
535 -- their indices)
536 ifilter :: Storable a => (Int -> a -> Bool) -> Vector a -> Vector a
537 {-# INLINE ifilter #-}
538 ifilter = G.ifilter
539
540 -- | Yield the longest prefix of elements satisfying the predicate.
541 takeWhile :: Storable a => (a -> Bool) -> Vector a -> Vector a
542 {-# INLINE takeWhile #-}
543 takeWhile = G.takeWhile
544
545 -- | Drop the longest prefix of elements that satisfy the predicate.
546 dropWhile :: Storable a => (a -> Bool) -> Vector a -> Vector a
547 {-# INLINE dropWhile #-}
548 dropWhile = G.dropWhile
549
550 -- | Split the vector in two parts, the first one containing those elements
551 -- that satisfy the predicate and the second one those that don't. The
552 -- relative order of the elements is preserved at the cost of a (sometimes)
553 -- reduced performance compared to 'unstablePartition'.
554 partition :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
555 {-# INLINE partition #-}
556 partition = G.partition
557
558 -- | Split the vector in two parts, the first one containing those elements
559 -- that satisfy the predicate and the second one those that don't. The order
560 -- of the elements is not preserved.
561 unstablePartition
562 :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
563 {-# INLINE unstablePartition #-}
564 unstablePartition = G.unstablePartition
565
566 -- | Split the vector into the longest prefix of elements that satisfy the
567 -- predicate and the rest.
568 span :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
569 {-# INLINE span #-}
570 span = G.span
571
572 -- | Split the vector into the longest prefix of elements that do not satisfy
573 -- the predicate and the rest.
574 break :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
575 {-# INLINE break #-}
576 break = G.break
577
578 -- Searching
579 -- ---------
580
581 infix 4 `elem`
582 -- | Check whether the vector contains an element
583 elem :: (Storable a, Eq a) => a -> Vector a -> Bool
584 {-# INLINE elem #-}
585 elem = G.elem
586
587 infix 4 `notElem`
588 -- | Inverse of `elem`
589 notElem :: (Storable a, Eq a) => a -> Vector a -> Bool
590 {-# INLINE notElem #-}
591 notElem = G.notElem
592
593 -- | Yield 'Just' the first element matching the predicate or 'Nothing' if no
594 -- such element exists.
595 find :: Storable a => (a -> Bool) -> Vector a -> Maybe a
596 {-# INLINE find #-}
597 find = G.find
598
599 -- | Yield 'Just' the index of the first element matching the predicate or
600 -- 'Nothing' if no such element exists.
601 findIndex :: Storable a => (a -> Bool) -> Vector a -> Maybe Int
602 {-# INLINE findIndex #-}
603 findIndex = G.findIndex
604
605 -- | Yield the indices of elements satisfying the predicate
606 findIndices :: Storable a => (a -> Bool) -> Vector a -> Vector Int
607 {-# INLINE findIndices #-}
608 findIndices = G.findIndices
609
610 -- | Yield 'Just' the index of the first occurence of the given element or
611 -- 'Nothing' if the vector does not contain the element
612 elemIndex :: (Storable a, Eq a) => a -> Vector a -> Maybe Int
613 {-# INLINE elemIndex #-}
614 elemIndex = G.elemIndex
615
616 -- | Yield the indices of all occurences of the given element
617 elemIndices :: (Storable a, Eq a) => a -> Vector a -> Vector Int
618 {-# INLINE elemIndices #-}
619 elemIndices = G.elemIndices
620
621 -- Folding
622 -- -------
623
624 -- | Left fold
625 foldl :: Storable b => (a -> b -> a) -> a -> Vector b -> a
626 {-# INLINE foldl #-}
627 foldl = G.foldl
628
629 -- | Lefgt fold on non-empty vectors
630 foldl1 :: Storable a => (a -> a -> a) -> Vector a -> a
631 {-# INLINE foldl1 #-}
632 foldl1 = G.foldl1
633
634 -- | Left fold with strict accumulator
635 foldl' :: Storable b => (a -> b -> a) -> a -> Vector b -> a
636 {-# INLINE foldl' #-}
637 foldl' = G.foldl'
638
639 -- | Left fold on non-empty vectors with strict accumulator
640 foldl1' :: Storable a => (a -> a -> a) -> Vector a -> a
641 {-# INLINE foldl1' #-}
642 foldl1' = G.foldl1'
643
644 -- | Right fold
645 foldr :: Storable a => (a -> b -> b) -> b -> Vector a -> b
646 {-# INLINE foldr #-}
647 foldr = G.foldr
648
649 -- | Right fold on non-empty vectors
650 foldr1 :: Storable a => (a -> a -> a) -> Vector a -> a
651 {-# INLINE foldr1 #-}
652 foldr1 = G.foldr1
653
654 -- | Right fold with a strict accumulator
655 foldr' :: Storable a => (a -> b -> b) -> b -> Vector a -> b
656 {-# INLINE foldr' #-}
657 foldr' = G.foldr'
658
659 -- | Right fold on non-empty vectors with strict accumulator
660 foldr1' :: Storable a => (a -> a -> a) -> Vector a -> a
661 {-# INLINE foldr1' #-}
662 foldr1' = G.foldr1'
663
664 -- | Left fold (function applied to each element and its index)
665 ifoldl :: Storable b => (a -> Int -> b -> a) -> a -> Vector b -> a
666 {-# INLINE ifoldl #-}
667 ifoldl = G.ifoldl
668
669 -- | Left fold with strict accumulator (function applied to each element and
670 -- its index)
671 ifoldl' :: Storable b => (a -> Int -> b -> a) -> a -> Vector b -> a
672 {-# INLINE ifoldl' #-}
673 ifoldl' = G.ifoldl'
674
675 -- | Right fold (function applied to each element and its index)
676 ifoldr :: Storable a => (Int -> a -> b -> b) -> b -> Vector a -> b
677 {-# INLINE ifoldr #-}
678 ifoldr = G.ifoldr
679
680 -- | Right fold with strict accumulator (function applied to each element and
681 -- its index)
682 ifoldr' :: Storable a => (Int -> a -> b -> b) -> b -> Vector a -> b
683 {-# INLINE ifoldr' #-}
684 ifoldr' = G.ifoldr'
685
686 -- Specialised folds
687 -- -----------------
688
689 all :: Storable a => (a -> Bool) -> Vector a -> Bool
690 {-# INLINE all #-}
691 all = G.all
692
693 any :: Storable a => (a -> Bool) -> Vector a -> Bool
694 {-# INLINE any #-}
695 any = G.any
696
697 and :: Vector Bool -> Bool
698 {-# INLINE and #-}
699 and = G.and
700
701 or :: Vector Bool -> Bool
702 {-# INLINE or #-}
703 or = G.or
704
705 sum :: (Storable a, Num a) => Vector a -> a
706 {-# INLINE sum #-}
707 sum = G.sum
708
709 product :: (Storable a, Num a) => Vector a -> a
710 {-# INLINE product #-}
711 product = G.product
712
713 maximum :: (Storable a, Ord a) => Vector a -> a
714 {-# INLINE maximum #-}
715 maximum = G.maximum
716
717 maximumBy :: Storable a => (a -> a -> Ordering) -> Vector a -> a
718 {-# INLINE maximumBy #-}
719 maximumBy = G.maximumBy
720
721 minimum :: (Storable a, Ord a) => Vector a -> a
722 {-# INLINE minimum #-}
723 minimum = G.minimum
724
725 minimumBy :: Storable a => (a -> a -> Ordering) -> Vector a -> a
726 {-# INLINE minimumBy #-}
727 minimumBy = G.minimumBy
728
729 maxIndex :: (Storable a, Ord a) => Vector a -> Int
730 {-# INLINE maxIndex #-}
731 maxIndex = G.maxIndex
732
733 maxIndexBy :: Storable a => (a -> a -> Ordering) -> Vector a -> Int
734 {-# INLINE maxIndexBy #-}
735 maxIndexBy = G.maxIndexBy
736
737 minIndex :: (Storable a, Ord a) => Vector a -> Int
738 {-# INLINE minIndex #-}
739 minIndex = G.minIndex
740
741 minIndexBy :: Storable a => (a -> a -> Ordering) -> Vector a -> Int
742 {-# INLINE minIndexBy #-}
743 minIndexBy = G.minIndexBy
744
745 -- Unfolding
746 -- ---------
747
748 -- | The 'unfoldr' function is a \`dual\' to 'foldr': while 'foldr'
749 -- reduces a vector to a summary value, 'unfoldr' builds a list from
750 -- a seed value. The function takes the element and returns 'Nothing'
751 -- if it is done generating the vector or returns 'Just' @(a,b)@, in which
752 -- case, @a@ is a prepended to the vector and @b@ is used as the next
753 -- element in a recursive call.
754 --
755 -- A simple use of unfoldr:
756 --
757 -- > unfoldr (\b -> if b == 0 then Nothing else Just (b, b-1)) 10
758 -- > [10,9,8,7,6,5,4,3,2,1]
759 --
760 unfoldr :: Storable a => (b -> Maybe (a, b)) -> b -> Vector a
761 {-# INLINE unfoldr #-}
762 unfoldr = G.unfoldr
763
764 -- | Unfold at most @n@ elements
765 unfoldrN :: Storable a => Int -> (b -> Maybe (a, b)) -> b -> Vector a
766 {-# INLINE unfoldrN #-}
767 unfoldrN = G.unfoldrN
768
769 -- Scans
770 -- -----
771
772 -- | Prefix scan
773 prescanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
774 {-# INLINE prescanl #-}
775 prescanl = G.prescanl
776
777 -- | Prefix scan with strict accumulator
778 prescanl' :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
779 {-# INLINE prescanl' #-}
780 prescanl' = G.prescanl'
781
782 -- | Suffix scan
783 postscanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
784 {-# INLINE postscanl #-}
785 postscanl = G.postscanl
786
787 -- | Suffix scan with strict accumulator
788 postscanl' :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
789 {-# INLINE postscanl' #-}
790 postscanl' = G.postscanl'
791
792 -- | Haskell-style scan
793 scanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
794 {-# INLINE scanl #-}
795 scanl = G.scanl
796
797 -- | Haskell-style scan with strict accumulator
798 scanl' :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
799 {-# INLINE scanl' #-}
800 scanl' = G.scanl'
801
802 -- | Scan over a non-empty 'Vector'
803 scanl1 :: Storable a => (a -> a -> a) -> Vector a -> Vector a
804 {-# INLINE scanl1 #-}
805 scanl1 = G.scanl1
806
807 -- | Scan over a non-empty 'Vector' with a strict accumulator
808 scanl1' :: Storable a => (a -> a -> a) -> Vector a -> Vector a
809 {-# INLINE scanl1' #-}
810 scanl1' = G.scanl1'
811
812
813 -- | Prefix right-to-left scan
814 prescanr
815 :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b
816 {-# INLINE prescanr #-}
817 prescanr = G.prescanr
818
819 -- | Prefix right-to-left scan with strict accumulator
820 prescanr'
821 :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b
822 {-# INLINE prescanr' #-}
823 prescanr' = G.prescanr'
824
825 -- | Suffix right-to-left scan
826 postscanr
827 :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b
828 {-# INLINE postscanr #-}
829 postscanr = G.postscanr
830
831 -- | Suffix right-to-left scan with strict accumulator
832 postscanr'
833 :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b
834 {-# INLINE postscanr' #-}
835 postscanr' = G.postscanr'
836
837 -- | Haskell-style right-to-left scan
838 scanr :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b
839 {-# INLINE scanr #-}
840 scanr = G.scanr
841
842 -- | Haskell-style right-to-left scan with strict accumulator
843 scanr' :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b
844 {-# INLINE scanr' #-}
845 scanr' = G.scanr'
846
847 -- | Right-to-left scan over a non-empty vector
848 scanr1 :: Storable a => (a -> a -> a) -> Vector a -> Vector a
849 {-# INLINE scanr1 #-}
850 scanr1 = G.scanr1
851
852 -- | Right-to-left scan over a non-empty vector with a strict accumulator
853 scanr1' :: Storable a => (a -> a -> a) -> Vector a -> Vector a
854 {-# INLINE scanr1' #-}
855 scanr1' = G.scanr1'
856
857 -- Enumeration
858 -- -----------
859
860 -- | Yield a vector of the given length containing the values @x@, @x+1@ etc.
861 -- This operation is usually more efficient than 'enumFromTo'.
862 enumFromN :: (Storable a, Num a) => a -> Int -> Vector a
863 {-# INLINE enumFromN #-}
864 enumFromN = G.enumFromN
865
866 -- | Yield a vector of the given length containing the values @x@, @x+y@,
867 -- @x+y+y@ etc. This operations is usually more efficient than
868 -- 'enumFromThenTo'.
869 enumFromStepN :: (Storable a, Num a) => a -> a -> Int -> Vector a
870 {-# INLINE enumFromStepN #-}
871 enumFromStepN = G.enumFromStepN
872
873 -- | Enumerate values from @x@ to @y@.
874 --
875 -- /WARNING:/ This operation can be very inefficient. If at all possible, use
876 -- 'enumFromN' instead.
877 enumFromTo :: (Storable a, Enum a) => a -> a -> Vector a
878 {-# INLINE enumFromTo #-}
879 enumFromTo = G.enumFromTo
880
881 -- | Enumerate values from @x@ to @y@ with a specific step @z@.
882 --
883 -- /WARNING:/ This operation can be very inefficient. If at all possible, use
884 -- 'enumFromStepN' instead.
885 enumFromThenTo :: (Storable a, Enum a) => a -> a -> a -> Vector a
886 {-# INLINE enumFromThenTo #-}
887 enumFromThenTo = G.enumFromThenTo
888
889 -- Conversion to/from lists
890 -- ------------------------
891
892 -- | Convert a vector to a list
893 toList :: Storable a => Vector a -> [a]
894 {-# INLINE toList #-}
895 toList = G.toList
896
897 -- | Convert a list to a vector
898 fromList :: Storable a => [a] -> Vector a
899 {-# INLINE fromList #-}
900 fromList = G.fromList
901
902 -- | Convert the first @n@ elements of a list to a vector
903 --
904 -- > fromListN n xs = fromList (take n xs)
905 fromListN :: Storable a => Int -> [a] -> Vector a
906 {-# INLINE fromListN #-}
907 fromListN = G.fromListN
908
909 -- Accessing the underlying memory
910 -- -------------------------------
911
912 -- | Create a vector from a 'ForeignPtr' with an offset and a length. The data
913 -- may not be modified through the 'ForeignPtr' afterwards.
914 unsafeFromForeignPtr :: Storable a
915 => ForeignPtr a -- ^ pointer
916 -> Int -- ^ offset
917 -> Int -- ^ length
918 -> Vector a
919 {-# INLINE unsafeFromForeignPtr #-}
920 unsafeFromForeignPtr fp i n = Vector (offsetToPtr fp i) n fp
921
922 -- | Yield the underlying 'ForeignPtr' together with the offset to the data
923 -- and its length. The data may not be modified through the 'ForeignPtr'.
924 unsafeToForeignPtr :: Storable a => Vector a -> (ForeignPtr a, Int, Int)
925 {-# INLINE unsafeToForeignPtr #-}
926 unsafeToForeignPtr (Vector p n fp) = (fp, ptrToOffset fp p, n)
927
928 -- | Pass a pointer to the vector's data to the IO action. The data may not be
929 -- modified through the 'Ptr.
930 unsafeWith :: Storable a => Vector a -> (Ptr a -> IO b) -> IO b
931 {-# INLINE unsafeWith #-}
932 unsafeWith (Vector p n fp) m = withForeignPtr fp $ \_ -> m p
933