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