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