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