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