Export accumulate and update from D.V.Unboxed
[darcs-mirrors/vector.git] / Data / Vector / Unboxed.hs
1 module Data.Vector.Unboxed (
2 Vector, MVector(..), Unbox,
3
4 -- * Length information
5 length, null,
6
7 -- * Construction
8 empty, singleton, cons, snoc, replicate, (++), copy,
9
10 -- * Accessing individual elements
11 (!), head, last,
12 unsafeIndex,
13
14 -- * Subvectors
15 slice, init, tail, take, drop,
16 unsafeSlice,
17
18 -- * Permutations
19 accum, accumulate, accumulate_,
20 (//), update, update_,
21 backpermute, reverse,
22
23 -- * Mapping
24 map, concatMap,
25
26 -- * Zipping and unzipping
27 zipWith, zipWith3, zipWith4, zipWith5, zipWith6,
28 zip, zip3, zip4, zip5, zip6,
29 unzip, unzip3, unzip4, unzip5, unzip6,
30
31 -- * Filtering
32 filter, takeWhile, dropWhile,
33
34 -- * Searching
35 elem, notElem, find, findIndex,
36
37 -- * Folding
38 foldl, foldl1, foldl', foldl1', foldr, foldr1,
39
40 -- * Specialised folds
41 sum, product, maximum, minimum,
42
43 -- * Unfolding
44 unfoldr,
45
46 -- * Scans
47 prescanl, prescanl',
48 postscanl, postscanl',
49 scanl, scanl', scanl1, scanl1',
50
51 -- * Enumeration
52 enumFromTo, enumFromThenTo,
53
54 -- * Conversion to/from lists
55 toList, fromList
56 ) where
57
58 import Data.Vector.Unboxed.Base
59 import qualified Data.Vector.Generic as G
60 import qualified Data.Vector.Fusion.Stream as Stream
61
62 import Prelude hiding ( length, null,
63 replicate, (++),
64 head, last,
65 init, tail, take, drop, reverse,
66 map, concatMap,
67 zipWith, zipWith3, zip, zip3, unzip, unzip3,
68 filter, takeWhile, dropWhile,
69 elem, notElem,
70 foldl, foldl1, foldr, foldr1,
71 sum, product, minimum, maximum,
72 scanl, scanl1,
73 enumFromTo, enumFromThenTo )
74
75 #include "vector.h"
76
77 instance (Unbox a, Eq a) => Eq (Vector a) where
78 {-# INLINE (==) #-}
79 (==) = G.eq
80
81 instance (Unbox a, Ord a) => Ord (Vector a) where
82 {-# INLINE compare #-}
83 compare = G.cmp
84
85 -- Length
86 -- ------
87
88 length :: Unbox a => Vector a -> Int
89 {-# INLINE length #-}
90 length = G.length
91
92 null :: Unbox a => Vector a -> Bool
93 {-# INLINE null #-}
94 null = G.null
95
96 -- Construction
97 -- ------------
98
99 -- | Empty vector
100 empty :: Unbox a => Vector a
101 {-# INLINE empty #-}
102 empty = G.empty
103
104 -- | Vector with exaclty one element
105 singleton :: Unbox a => a -> Vector a
106 {-# INLINE singleton #-}
107 singleton = G.singleton
108
109 -- | Vector of the given length with the given value in each position
110 replicate :: Unbox a => Int -> a -> Vector a
111 {-# INLINE replicate #-}
112 replicate = G.replicate
113
114 -- | Prepend an element
115 cons :: Unbox a => a -> Vector a -> Vector a
116 {-# INLINE cons #-}
117 cons = G.cons
118
119 -- | Append an element
120 snoc :: Unbox a => Vector a -> a -> Vector a
121 {-# INLINE snoc #-}
122 snoc = G.snoc
123
124 infixr 5 ++
125 -- | Concatenate two vectors
126 (++) :: Unbox a => Vector a -> Vector a -> Vector a
127 {-# INLINE (++) #-}
128 (++) = (G.++)
129
130 -- | Create a copy of a vector. Useful when dealing with slices.
131 copy :: Unbox a => Vector a -> Vector a
132 {-# INLINE copy #-}
133 copy = G.copy
134
135 -- Accessing individual elements
136 -- -----------------------------
137
138 -- | Indexing
139 (!) :: Unbox a => Vector a -> Int -> a
140 {-# INLINE (!) #-}
141 (!) = (G.!)
142
143 -- | Unsafe indexing without bounds checks
144 unsafeIndex :: Unbox a => Vector a -> Int -> a
145 {-# INLINE unsafeIndex #-}
146 unsafeIndex = G.unsafeIndex
147
148 -- | First element
149 head :: Unbox a => Vector a -> a
150 {-# INLINE head #-}
151 head = G.head
152
153 -- | Last element
154 last :: Unbox a => Vector a -> a
155 {-# INLINE last #-}
156 last = G.last
157
158 -- Subarrays
159 -- ---------
160
161 -- | Yield a part of the vector without copying it. Safer version of
162 -- 'basicUnsafeSlice'.
163 slice :: Unbox a => Vector a -> Int -- ^ starting index
164 -> Int -- ^ length
165 -> Vector a
166 {-# INLINE slice #-}
167 slice = G.slice
168
169 -- | Unsafely yield a part of the vector without copying it and without
170 -- performing bounds checks.
171 unsafeSlice :: Unbox a => Vector a -> Int -- ^ starting index
172 -> Int -- ^ length
173 -> Vector a
174 {-# INLINE unsafeSlice #-}
175 unsafeSlice = G.unsafeSlice
176
177 -- | Yield all but the last element without copying.
178 init :: Unbox a => Vector a -> Vector a
179 {-# INLINE init #-}
180 init = G.init
181
182 -- | All but the first element (without copying).
183 tail :: Unbox a => Vector a -> Vector a
184 {-# INLINE tail #-}
185 tail = G.tail
186
187 -- | Yield the first @n@ elements without copying.
188 take :: Unbox a => Int -> Vector a -> Vector a
189 {-# INLINE take #-}
190 take = G.take
191
192 -- | Yield all but the first @n@ elements without copying.
193 drop :: Unbox a => Int -> Vector a -> Vector a
194 {-# INLINE drop #-}
195 drop = G.drop
196
197 -- Permutations
198 -- ------------
199
200 accum :: Unbox a => (a -> b -> a) -> Vector a -> [(Int,b)] -> Vector a
201 {-# INLINE accum #-}
202 accum = G.accum
203
204 accumulate :: (Unbox a, Unbox b)
205 => (a -> b -> a) -> Vector a -> Vector (Int,b) -> Vector a
206 {-# INLINE accumulate #-}
207 accumulate = G.accumulate
208
209 accumulate_ :: (Unbox a, Unbox b) =>
210 (a -> b -> a) -> Vector a -> Vector Int -> Vector b -> Vector a
211 {-# INLINE accumulate_ #-}
212 accumulate_ = G.accumulate_
213
214 (//) :: Unbox a => Vector a -> [(Int, a)] -> Vector a
215 {-# INLINE (//) #-}
216 (//) = (G.//)
217
218 update :: Unbox a => Vector a -> Vector (Int, a) -> Vector a
219 {-# INLINE update #-}
220 update = G.update
221
222 update_ :: Unbox a => Vector a -> Vector Int -> Vector a -> Vector a
223 {-# INLINE update_ #-}
224 update_ = G.update_
225
226 backpermute :: Unbox a => Vector a -> Vector Int -> Vector a
227 {-# INLINE backpermute #-}
228 backpermute = G.backpermute
229
230 reverse :: Unbox a => Vector a -> Vector a
231 {-# INLINE reverse #-}
232 reverse = G.reverse
233
234 -- Mapping
235 -- -------
236
237 -- | Map a function over a vector
238 map :: (Unbox a, Unbox b) => (a -> b) -> Vector a -> Vector b
239 {-# INLINE map #-}
240 map = G.map
241
242 concatMap :: (Unbox a, Unbox b) => (a -> Vector b) -> Vector a -> Vector b
243 {-# INLINE concatMap #-}
244 concatMap = G.concatMap
245
246 -- Zipping/unzipping
247 -- -----------------
248
249 -- | Zip two vectors with the given function.
250 zipWith :: (Unbox a, Unbox b, Unbox c)
251 => (a -> b -> c) -> Vector a -> Vector b -> Vector c
252 {-# INLINE zipWith #-}
253 zipWith = G.zipWith
254
255 -- | Zip three vectors with the given function.
256 zipWith3 :: (Unbox a, Unbox b, Unbox c, Unbox d)
257 => (a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d
258 {-# INLINE zipWith3 #-}
259 zipWith3 = G.zipWith3
260
261 zipWith4 :: (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e)
262 => (a -> b -> c -> d -> e)
263 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
264 {-# INLINE zipWith4 #-}
265 zipWith4 = G.zipWith4
266
267 zipWith5 :: (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e, Unbox f)
268 => (a -> b -> c -> d -> e -> f)
269 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
270 -> Vector f
271 {-# INLINE zipWith5 #-}
272 zipWith5 = G.zipWith5
273
274 zipWith6 :: (Unbox a, Unbox b, Unbox c, Unbox d, Unbox e, Unbox f, Unbox g)
275 => (a -> b -> c -> d -> e -> f -> g)
276 -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
277 -> Vector f -> Vector g
278 {-# INLINE zipWith6 #-}
279 zipWith6 = G.zipWith6
280
281 -- Filtering
282 -- ---------
283
284 -- | Drop elements which do not satisfy the predicate
285 filter :: Unbox a => (a -> Bool) -> Vector a -> Vector a
286 {-# INLINE filter #-}
287 filter = G.filter
288
289 -- | Yield the longest prefix of elements satisfying the predicate.
290 takeWhile :: Unbox a => (a -> Bool) -> Vector a -> Vector a
291 {-# INLINE takeWhile #-}
292 takeWhile = G.takeWhile
293
294 -- | Drop the longest prefix of elements that satisfy the predicate.
295 dropWhile :: Unbox a => (a -> Bool) -> Vector a -> Vector a
296 {-# INLINE dropWhile #-}
297 dropWhile = G.dropWhile
298
299 -- Searching
300 -- ---------
301
302 infix 4 `elem`
303 -- | Check whether the vector contains an element
304 elem :: (Unbox a, Eq a) => a -> Vector a -> Bool
305 {-# INLINE elem #-}
306 elem = G.elem
307
308 infix 4 `notElem`
309 -- | Inverse of `elem`
310 notElem :: (Unbox a, Eq a) => a -> Vector a -> Bool
311 {-# INLINE notElem #-}
312 notElem = G.notElem
313
314 -- | Yield 'Just' the first element matching the predicate or 'Nothing' if no
315 -- such element exists.
316 find :: Unbox a => (a -> Bool) -> Vector a -> Maybe a
317 {-# INLINE find #-}
318 find = G.find
319
320 -- | Yield 'Just' the index of the first element matching the predicate or
321 -- 'Nothing' if no such element exists.
322 findIndex :: Unbox a => (a -> Bool) -> Vector a -> Maybe Int
323 {-# INLINE findIndex #-}
324 findIndex = G.findIndex
325
326 -- Folding
327 -- -------
328
329 -- | Left fold
330 foldl :: Unbox b => (a -> b -> a) -> a -> Vector b -> a
331 {-# INLINE foldl #-}
332 foldl = G.foldl
333
334 -- | Lefgt fold on non-empty vectors
335 foldl1 :: Unbox a => (a -> a -> a) -> Vector a -> a
336 {-# INLINE foldl1 #-}
337 foldl1 = G.foldl1
338
339 -- | Left fold with strict accumulator
340 foldl' :: Unbox b => (a -> b -> a) -> a -> Vector b -> a
341 {-# INLINE foldl' #-}
342 foldl' = G.foldl'
343
344 -- | Left fold on non-empty vectors with strict accumulator
345 foldl1' :: Unbox a => (a -> a -> a) -> Vector a -> a
346 {-# INLINE foldl1' #-}
347 foldl1' = G.foldl1'
348
349 -- | Right fold
350 foldr :: Unbox a => (a -> b -> b) -> b -> Vector a -> b
351 {-# INLINE foldr #-}
352 foldr = G.foldr
353
354 -- | Right fold on non-empty vectors
355 foldr1 :: Unbox a => (a -> a -> a) -> Vector a -> a
356 {-# INLINE foldr1 #-}
357 foldr1 = G.foldr1
358
359 -- Specialised folds
360 -- -----------------
361
362 sum :: (Unbox a, Num a) => Vector a -> a
363 {-# INLINE sum #-}
364 sum = G.sum
365
366 product :: (Unbox a, Num a) => Vector a -> a
367 {-# INLINE product #-}
368 product = G.product
369
370 maximum :: (Unbox a, Ord a) => Vector a -> a
371 {-# INLINE maximum #-}
372 maximum = G.maximum
373
374 minimum :: (Unbox a, Ord a) => Vector a -> a
375 {-# INLINE minimum #-}
376 minimum = G.minimum
377
378 -- Unfolding
379 -- ---------
380
381 unfoldr :: Unbox a => (b -> Maybe (a, b)) -> b -> Vector a
382 {-# INLINE unfoldr #-}
383 unfoldr = G.unfoldr
384
385 -- Scans
386 -- -----
387
388 -- | Prefix scan
389 prescanl :: (Unbox a, Unbox b) => (a -> b -> a) -> a -> Vector b -> Vector a
390 {-# INLINE prescanl #-}
391 prescanl = G.prescanl
392
393 -- | Prefix scan with strict accumulator
394 prescanl' :: (Unbox a, Unbox b) => (a -> b -> a) -> a -> Vector b -> Vector a
395 {-# INLINE prescanl' #-}
396 prescanl' = G.prescanl'
397
398 -- | Suffix scan
399 postscanl :: (Unbox a, Unbox b) => (a -> b -> a) -> a -> Vector b -> Vector a
400 {-# INLINE postscanl #-}
401 postscanl = G.postscanl
402
403 -- | Suffix scan with strict accumulator
404 postscanl' :: (Unbox a, Unbox b) => (a -> b -> a) -> a -> Vector b -> Vector a
405 {-# INLINE postscanl' #-}
406 postscanl' = G.postscanl'
407
408 -- | Haskell-style scan
409 scanl :: (Unbox a, Unbox b) => (a -> b -> a) -> a -> Vector b -> Vector a
410 {-# INLINE scanl #-}
411 scanl = G.scanl
412
413 -- | Haskell-style scan with strict accumulator
414 scanl' :: (Unbox a, Unbox b) => (a -> b -> a) -> a -> Vector b -> Vector a
415 {-# INLINE scanl' #-}
416 scanl' = G.scanl'
417
418 -- | Scan over a non-empty 'Vector'
419 scanl1 :: Unbox a => (a -> a -> a) -> Vector a -> Vector a
420 {-# INLINE scanl1 #-}
421 scanl1 = G.scanl1
422
423 -- | Scan over a non-empty 'Vector' with a strict accumulator
424 scanl1' :: Unbox a => (a -> a -> a) -> Vector a -> Vector a
425 {-# INLINE scanl1' #-}
426 scanl1' = G.scanl1'
427
428 -- Enumeration
429 -- -----------
430
431 enumFromTo :: (Unbox a, Enum a) => a -> a -> Vector a
432 {-# INLINE enumFromTo #-}
433 enumFromTo = G.enumFromTo
434
435 enumFromThenTo :: (Unbox a, Enum a) => a -> a -> a -> Vector a
436 {-# INLINE enumFromThenTo #-}
437 enumFromThenTo = G.enumFromThenTo
438
439 -- Conversion to/from lists
440 -- ------------------------
441
442 -- | Convert a vector to a list
443 toList :: Unbox a => Vector a -> [a]
444 {-# INLINE toList #-}
445 toList = G.toList
446
447 -- | Convert a list to a vector
448 fromList :: Unbox a => [a] -> Vector a
449 {-# INLINE fromList #-}
450 fromList = G.fromList
451
452 #define DEFINE_IMMUTABLE
453 #include "unbox-tuple-instances"
454