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