Add all, any
[darcs-mirrors/vector.git] / Data / Vector / Primitive.hs
1 {-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, TypeFamilies #-}
2
3 -- |
4 -- Module : Data.Vector.Primitive
5 -- Copyright : (c) Roman Leshchinskiy 2008-2009
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, (++), copy,
23
24 -- * Accessing individual elements
25 (!), head, last,
26
27 -- * Subvectors
28 slice, init, tail, take, drop,
29 unsafeSlice,
30
31 -- * Permutations
32 accum, accumulate_, (//), update_, backpermute, reverse,
33
34 -- * Mapping
35 map, imap, concatMap,
36
37 -- * Zipping and unzipping
38 zipWith, zipWith3, zipWith4, zipWith5, zipWith6,
39 izipWith, izipWith3, izipWith4, izipWith5, izipWith6,
40
41 -- * Filtering
42 filter, ifilter, takeWhile, dropWhile,
43
44 -- * Searching
45 elem, notElem, find, findIndex, findIndices, elemIndex, elemIndices,
46
47 -- * Folding
48 foldl, foldl1, foldl', foldl1', foldr, foldr1,
49 ifoldl, ifoldl', ifoldr,
50
51 -- * Specialised folds
52 all, any,
53 sum, product,
54 maximum, maximumBy, minimum, minimumBy,
55 minIndex, minIndexBy, maxIndex, maxIndexBy,
56
57 -- * Unfolding
58 unfoldr,
59
60 -- * Scans
61 prescanl, prescanl',
62 postscanl, postscanl',
63 scanl, scanl', scanl1, scanl1',
64
65 -- * Enumeration
66 enumFromTo, enumFromThenTo,
67
68 -- * Conversion to/from lists
69 toList, fromList,
70
71 -- * Unsafe operations
72 unsafeIndex,
73 unsafeAccum, unsafeAccumulate_,
74 unsafeUpd, unsafeUpdate_
75 ) where
76
77 import qualified Data.Vector.Generic as G
78 import Data.Vector.Primitive.Mutable ( MVector(..) )
79 import Data.Primitive.ByteArray
80 import Data.Primitive ( Prim )
81
82 import Control.Monad ( liftM )
83
84 import Prelude hiding ( length, null,
85 replicate, (++),
86 head, last,
87 init, tail, take, drop, reverse,
88 map, concatMap,
89 zipWith, zipWith3, zip, zip3, unzip, unzip3,
90 filter, takeWhile, dropWhile,
91 elem, notElem,
92 foldl, foldl1, foldr, foldr1,
93 all, any, sum, product, minimum, maximum,
94 scanl, scanl1,
95 enumFromTo, enumFromThenTo )
96
97 import qualified Prelude
98
99 -- | Unboxed vectors of primitive types
100 data Vector a = Vector {-# UNPACK #-} !Int
101 {-# UNPACK #-} !Int
102 {-# UNPACK #-} !ByteArray
103
104 instance (Show a, Prim a) => Show (Vector a) where
105 show = (Prelude.++ " :: Data.Vector.Primitive.Vector") . ("fromList " Prelude.++) . show . toList
106
107 type instance G.Mutable Vector = MVector
108
109 instance Prim a => G.Vector Vector a where
110 {-# INLINE unsafeFreeze #-}
111 unsafeFreeze (MVector i n marr)
112 = Vector i n `liftM` unsafeFreezeByteArray marr
113
114 {-# INLINE basicLength #-}
115 basicLength (Vector _ n _) = n
116
117 {-# INLINE basicUnsafeSlice #-}
118 basicUnsafeSlice (Vector i _ arr) j n = Vector (i+j) n arr
119
120 {-# INLINE basicUnsafeIndexM #-}
121 basicUnsafeIndexM (Vector i _ arr) j = return (indexByteArray arr (i+j))
122
123 {-# INLINE elemseq #-}
124 elemseq _ = seq
125
126 instance (Prim a, Eq a) => Eq (Vector a) where
127 {-# INLINE (==) #-}
128 (==) = G.eq
129
130 instance (Prim a, Ord a) => Ord (Vector a) where
131 {-# INLINE compare #-}
132 compare = G.cmp
133
134 -- Length
135 -- ------
136
137 length :: Prim a => Vector a -> Int
138 {-# INLINE length #-}
139 length = G.length
140
141 null :: Prim a => Vector a -> Bool
142 {-# INLINE null #-}
143 null = G.null
144
145 -- Construction
146 -- ------------
147
148 -- | Empty vector
149 empty :: Prim a => Vector a
150 {-# INLINE empty #-}
151 empty = G.empty
152
153 -- | Vector with exaclty one element
154 singleton :: Prim a => a -> Vector a
155 {-# INLINE singleton #-}
156 singleton = G.singleton
157
158 -- | Vector of the given length with the given value in each position
159 replicate :: Prim a => Int -> a -> Vector a
160 {-# INLINE replicate #-}
161 replicate = G.replicate
162
163 -- | Prepend an element
164 cons :: Prim a => a -> Vector a -> Vector a
165 {-# INLINE cons #-}
166 cons = G.cons
167
168 -- | Append an element
169 snoc :: Prim a => Vector a -> a -> Vector a
170 {-# INLINE snoc #-}
171 snoc = G.snoc
172
173 infixr 5 ++
174 -- | Concatenate two vectors
175 (++) :: Prim a => Vector a -> Vector a -> Vector a
176 {-# INLINE (++) #-}
177 (++) = (G.++)
178
179 -- | Create a copy of a vector. Useful when dealing with slices.
180 copy :: Prim a => Vector a -> Vector a
181 {-# INLINE copy #-}
182 copy = G.copy
183
184 -- Accessing individual elements
185 -- -----------------------------
186
187 -- | Indexing
188 (!) :: Prim a => Vector a -> Int -> a
189 {-# INLINE (!) #-}
190 (!) = (G.!)
191
192 -- | Unsafe indexing without bounds checks
193 unsafeIndex :: Prim a => Vector a -> Int -> a
194 {-# INLINE unsafeIndex #-}
195 unsafeIndex = G.unsafeIndex
196
197 -- | First element
198 head :: Prim a => Vector a -> a
199 {-# INLINE head #-}
200 head = G.head
201
202 -- | Last element
203 last :: Prim a => Vector a -> a
204 {-# INLINE last #-}
205 last = G.last
206
207 -- Subarrays
208 -- ---------
209
210 -- | Yield a part of the vector without copying it. Safer version of
211 -- 'basicUnsafeSlice'.
212 slice :: Prim a => Vector a -> Int -- ^ starting index
213 -> Int -- ^ length
214 -> Vector a
215 {-# INLINE slice #-}
216 slice = G.slice
217
218 -- | Unsafely yield a part of the vector without copying it and without
219 -- performing bounds checks.
220 unsafeSlice :: Prim a => Vector a -> Int -- ^ starting index
221 -> Int -- ^ length
222 -> Vector a
223 {-# INLINE unsafeSlice #-}
224 unsafeSlice = G.unsafeSlice
225
226 -- | Yield all but the last element without copying.
227 init :: Prim a => Vector a -> Vector a
228 {-# INLINE init #-}
229 init = G.init
230
231 -- | All but the first element (without copying).
232 tail :: Prim a => Vector a -> Vector a
233 {-# INLINE tail #-}
234 tail = G.tail
235
236 -- | Yield the first @n@ elements without copying.
237 take :: Prim a => Int -> Vector a -> Vector a
238 {-# INLINE take #-}
239 take = G.take
240
241 -- | Yield all but the first @n@ elements without copying.
242 drop :: Prim a => Int -> Vector a -> Vector a
243 {-# INLINE drop #-}
244 drop = G.drop
245
246 -- Permutations
247 -- ------------
248
249 unsafeAccum :: Prim a => (a -> b -> a) -> Vector a -> [(Int,b)] -> Vector a
250 {-# INLINE unsafeAccum #-}
251 unsafeAccum = G.unsafeAccum
252
253 unsafeAccumulate_ :: (Prim a, Prim b) =>
254 (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a
255 {-# INLINE unsafeAccumulate_ #-}
256 unsafeAccumulate_ = G.unsafeAccumulate_
257
258 accum :: Prim a => (a -> b -> a) -> Vector a -> [(Int,b)] -> Vector a
259 {-# INLINE accum #-}
260 accum = G.accum
261
262 accumulate_ :: (Prim a, Prim b) =>
263 (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a
264 {-# INLINE accumulate_ #-}
265 accumulate_ = G.accumulate_
266
267 unsafeUpd :: Prim a => Vector a -> [(Int, a)] -> Vector a
268 {-# INLINE unsafeUpd #-}
269 unsafeUpd = G.unsafeUpd
270
271 unsafeUpdate_ :: Prim a => Vector a -> Vector Int -> Vector a -> Vector a
272 {-# INLINE unsafeUpdate_ #-}
273 unsafeUpdate_ = G.unsafeUpdate_
274
275 (//) :: Prim a => Vector a -> [(Int, a)] -> Vector a
276 {-# INLINE (//) #-}
277 (//) = (G.//)
278
279 update_ :: Prim a => Vector a -> Vector Int -> Vector a -> Vector a
280 {-# INLINE update_ #-}
281 update_ = G.update_
282
283 backpermute :: Prim a => Vector a -> Vector Int -> Vector a
284 {-# INLINE backpermute #-}
285 backpermute = G.backpermute
286
287 reverse :: Prim a => Vector a -> Vector a
288 {-# INLINE reverse #-}
289 reverse = G.reverse
290
291 -- Mapping
292 -- -------
293
294 -- | Map a function over a vector
295 map :: (Prim a, Prim b) => (a -> b) -> Vector a -> Vector b
296 {-# INLINE map #-}
297 map = G.map
298
299 -- | Apply a function to every index/value pair
300 imap :: (Prim a, Prim b) => (Int -> a -> b) -> Vector a -> Vector b
301 {-# INLINE imap #-}
302 imap = G.imap
303
304 concatMap :: (Prim a, Prim b) => (a -> Vector b) -> Vector a -> Vector b
305 {-# INLINE concatMap #-}
306 concatMap = G.concatMap
307
308 -- Zipping/unzipping
309 -- -----------------
310
311 -- | Zip two vectors with the given function.
312 zipWith :: (Prim a, Prim b, Prim c)
313 => (a -> b -> c) -> Vector a -> Vector b -> Vector c
314 {-# INLINE zipWith #-}
315 zipWith = G.zipWith
316
317 -- | Zip three vectors with the given function.
318 zipWith3 :: (Prim a, Prim b, Prim c, Prim d)
319 => (a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d
320 {-# INLINE zipWith3 #-}
321 zipWith3 = G.zipWith3
322
323 zipWith4 :: (Prim a, Prim b, Prim c, Prim d, Prim e)
324 => (a -> b -> c -> d -> e)
325 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
326 {-# INLINE zipWith4 #-}
327 zipWith4 = G.zipWith4
328
329 zipWith5 :: (Prim a, Prim b, Prim c, Prim d, Prim e, Prim f)
330 => (a -> b -> c -> d -> e -> f)
331 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
332 -> Vector f
333 {-# INLINE zipWith5 #-}
334 zipWith5 = G.zipWith5
335
336 zipWith6 :: (Prim a, Prim b, Prim c, Prim d, Prim e, Prim f, Prim g)
337 => (a -> b -> c -> d -> e -> f -> g)
338 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
339 -> Vector f -> Vector g
340 {-# INLINE zipWith6 #-}
341 zipWith6 = G.zipWith6
342
343 -- | Zip two vectors and their indices with the given function.
344 izipWith :: (Prim a, Prim b, Prim c)
345 => (Int -> a -> b -> c) -> Vector a -> Vector b -> Vector c
346 {-# INLINE izipWith #-}
347 izipWith = G.izipWith
348
349 -- | Zip three vectors and their indices with the given function.
350 izipWith3 :: (Prim a, Prim b, Prim c, Prim d)
351 => (Int -> a -> b -> c -> d)
352 -> Vector a -> Vector b -> Vector c -> Vector d
353 {-# INLINE izipWith3 #-}
354 izipWith3 = G.izipWith3
355
356 izipWith4 :: (Prim a, Prim b, Prim c, Prim d, Prim e)
357 => (Int -> a -> b -> c -> d -> e)
358 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
359 {-# INLINE izipWith4 #-}
360 izipWith4 = G.izipWith4
361
362 izipWith5 :: (Prim a, Prim b, Prim c, Prim d, Prim e, Prim f)
363 => (Int -> a -> b -> c -> d -> e -> f)
364 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
365 -> Vector f
366 {-# INLINE izipWith5 #-}
367 izipWith5 = G.izipWith5
368
369 izipWith6 :: (Prim a, Prim b, Prim c, Prim d, Prim e, Prim f, Prim g)
370 => (Int -> a -> b -> c -> d -> e -> f -> g)
371 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
372 -> Vector f -> Vector g
373 {-# INLINE izipWith6 #-}
374 izipWith6 = G.izipWith6
375
376 -- Filtering
377 -- ---------
378
379 -- | Drop elements which do not satisfy the predicate
380 filter :: Prim a => (a -> Bool) -> Vector a -> Vector a
381 {-# INLINE filter #-}
382 filter = G.filter
383
384 -- | Drop elements that do not satisfy the predicate (applied to values and
385 -- their indices)
386 ifilter :: Prim a => (Int -> a -> Bool) -> Vector a -> Vector a
387 {-# INLINE ifilter #-}
388 ifilter = G.ifilter
389
390 -- | Yield the longest prefix of elements satisfying the predicate.
391 takeWhile :: Prim a => (a -> Bool) -> Vector a -> Vector a
392 {-# INLINE takeWhile #-}
393 takeWhile = G.takeWhile
394
395 -- | Drop the longest prefix of elements that satisfy the predicate.
396 dropWhile :: Prim a => (a -> Bool) -> Vector a -> Vector a
397 {-# INLINE dropWhile #-}
398 dropWhile = G.dropWhile
399
400 -- Searching
401 -- ---------
402
403 infix 4 `elem`
404 -- | Check whether the vector contains an element
405 elem :: (Prim a, Eq a) => a -> Vector a -> Bool
406 {-# INLINE elem #-}
407 elem = G.elem
408
409 infix 4 `notElem`
410 -- | Inverse of `elem`
411 notElem :: (Prim a, Eq a) => a -> Vector a -> Bool
412 {-# INLINE notElem #-}
413 notElem = G.notElem
414
415 -- | Yield 'Just' the first element matching the predicate or 'Nothing' if no
416 -- such element exists.
417 find :: Prim a => (a -> Bool) -> Vector a -> Maybe a
418 {-# INLINE find #-}
419 find = G.find
420
421 -- | Yield 'Just' the index of the first element matching the predicate or
422 -- 'Nothing' if no such element exists.
423 findIndex :: Prim a => (a -> Bool) -> Vector a -> Maybe Int
424 {-# INLINE findIndex #-}
425 findIndex = G.findIndex
426
427 -- | Yield the indices of elements satisfying the predicate
428 findIndices :: Prim a => (a -> Bool) -> Vector a -> Vector Int
429 {-# INLINE findIndices #-}
430 findIndices = G.findIndices
431
432 -- | Yield 'Just' the index of the first occurence of the given element or
433 -- 'Nothing' if the vector does not contain the element
434 elemIndex :: (Prim a, Eq a) => a -> Vector a -> Maybe Int
435 {-# INLINE elemIndex #-}
436 elemIndex = G.elemIndex
437
438 -- | Yield the indices of all occurences of the given element
439 elemIndices :: (Prim a, Eq a) => a -> Vector a -> Vector Int
440 {-# INLINE elemIndices #-}
441 elemIndices = G.elemIndices
442
443 -- Folding
444 -- -------
445
446 -- | Left fold
447 foldl :: Prim b => (a -> b -> a) -> a -> Vector b -> a
448 {-# INLINE foldl #-}
449 foldl = G.foldl
450
451 -- | Lefgt fold on non-empty vectors
452 foldl1 :: Prim a => (a -> a -> a) -> Vector a -> a
453 {-# INLINE foldl1 #-}
454 foldl1 = G.foldl1
455
456 -- | Left fold with strict accumulator
457 foldl' :: Prim b => (a -> b -> a) -> a -> Vector b -> a
458 {-# INLINE foldl' #-}
459 foldl' = G.foldl'
460
461 -- | Left fold on non-empty vectors with strict accumulator
462 foldl1' :: Prim a => (a -> a -> a) -> Vector a -> a
463 {-# INLINE foldl1' #-}
464 foldl1' = G.foldl1'
465
466 -- | Right fold
467 foldr :: Prim a => (a -> b -> b) -> b -> Vector a -> b
468 {-# INLINE foldr #-}
469 foldr = G.foldr
470
471 -- | Right fold on non-empty vectors
472 foldr1 :: Prim a => (a -> a -> a) -> Vector a -> a
473 {-# INLINE foldr1 #-}
474 foldr1 = G.foldr1
475
476 -- | Left fold (function applied to each element and its index)
477 ifoldl :: Prim b => (a -> Int -> b -> a) -> a -> Vector b -> a
478 {-# INLINE ifoldl #-}
479 ifoldl = G.ifoldl
480
481 -- | Left fold with strict accumulator (function applied to each element and
482 -- its index)
483 ifoldl' :: Prim b => (a -> Int -> b -> a) -> a -> Vector b -> a
484 {-# INLINE ifoldl' #-}
485 ifoldl' = G.ifoldl'
486
487 -- | Right fold (function applied to each element and its index)
488 ifoldr :: Prim a => (Int -> a -> b -> b) -> b -> Vector a -> b
489 {-# INLINE ifoldr #-}
490 ifoldr = G.ifoldr
491
492 -- Specialised folds
493 -- -----------------
494
495 all :: Prim a => (a -> Bool) -> Vector a -> Bool
496 {-# INLINE all #-}
497 all = G.all
498
499 any :: Prim a => (a -> Bool) -> Vector a -> Bool
500 {-# INLINE any #-}
501 any = G.any
502
503 sum :: (Prim a, Num a) => Vector a -> a
504 {-# INLINE sum #-}
505 sum = G.sum
506
507 product :: (Prim a, Num a) => Vector a -> a
508 {-# INLINE product #-}
509 product = G.product
510
511 maximum :: (Prim a, Ord a) => Vector a -> a
512 {-# INLINE maximum #-}
513 maximum = G.maximum
514
515 maximumBy :: Prim a => (a -> a -> Ordering) -> Vector a -> a
516 {-# INLINE maximumBy #-}
517 maximumBy = G.maximumBy
518
519 minimum :: (Prim a, Ord a) => Vector a -> a
520 {-# INLINE minimum #-}
521 minimum = G.minimum
522
523 minimumBy :: Prim a => (a -> a -> Ordering) -> Vector a -> a
524 {-# INLINE minimumBy #-}
525 minimumBy = G.minimumBy
526
527 maxIndex :: (Prim a, Ord a) => Vector a -> Int
528 {-# INLINE maxIndex #-}
529 maxIndex = G.maxIndex
530
531 maxIndexBy :: Prim a => (a -> a -> Ordering) -> Vector a -> Int
532 {-# INLINE maxIndexBy #-}
533 maxIndexBy = G.maxIndexBy
534
535 minIndex :: (Prim a, Ord a) => Vector a -> Int
536 {-# INLINE minIndex #-}
537 minIndex = G.minIndex
538
539 minIndexBy :: Prim a => (a -> a -> Ordering) -> Vector a -> Int
540 {-# INLINE minIndexBy #-}
541 minIndexBy = G.minIndexBy
542
543 -- Unfolding
544 -- ---------
545
546 unfoldr :: Prim a => (b -> Maybe (a, b)) -> b -> Vector a
547 {-# INLINE unfoldr #-}
548 unfoldr = G.unfoldr
549
550 -- Scans
551 -- -----
552
553 -- | Prefix scan
554 prescanl :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
555 {-# INLINE prescanl #-}
556 prescanl = G.prescanl
557
558 -- | Prefix scan with strict accumulator
559 prescanl' :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
560 {-# INLINE prescanl' #-}
561 prescanl' = G.prescanl'
562
563 -- | Suffix scan
564 postscanl :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
565 {-# INLINE postscanl #-}
566 postscanl = G.postscanl
567
568 -- | Suffix scan with strict accumulator
569 postscanl' :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
570 {-# INLINE postscanl' #-}
571 postscanl' = G.postscanl'
572
573 -- | Haskell-style scan
574 scanl :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
575 {-# INLINE scanl #-}
576 scanl = G.scanl
577
578 -- | Haskell-style scan with strict accumulator
579 scanl' :: (Prim a, Prim b) => (a -> b -> a) -> a -> Vector b -> Vector a
580 {-# INLINE scanl' #-}
581 scanl' = G.scanl'
582
583 -- | Scan over a non-empty 'Vector'
584 scanl1 :: Prim a => (a -> a -> a) -> Vector a -> Vector a
585 {-# INLINE scanl1 #-}
586 scanl1 = G.scanl1
587
588 -- | Scan over a non-empty 'Vector' with a strict accumulator
589 scanl1' :: Prim a => (a -> a -> a) -> Vector a -> Vector a
590 {-# INLINE scanl1' #-}
591 scanl1' = G.scanl1'
592
593 -- Enumeration
594 -- -----------
595
596 enumFromTo :: (Prim a, Enum a) => a -> a -> Vector a
597 {-# INLINE enumFromTo #-}
598 enumFromTo = G.enumFromTo
599
600 enumFromThenTo :: (Prim a, Enum a) => a -> a -> a -> Vector a
601 {-# INLINE enumFromThenTo #-}
602 enumFromThenTo = G.enumFromThenTo
603
604 -- Conversion to/from lists
605 -- ------------------------
606
607 -- | Convert a vector to a list
608 toList :: Prim a => Vector a -> [a]
609 {-# INLINE toList #-}
610 toList = G.toList
611
612 -- | Convert a list to a vector
613 fromList :: Prim a => [a] -> Vector a
614 {-# INLINE fromList #-}
615 fromList = G.fromList
616