Export and & or 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 and, or, 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 and, or, 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 and :: Vector Bool -> Bool
363 {-# INLINE and #-}
364 and = G.and
365
366 or :: Vector Bool -> Bool
367 {-# INLINE or #-}
368 or = G.or
369
370 sum :: (Unbox a, Num a) => Vector a -> a
371 {-# INLINE sum #-}
372 sum = G.sum
373
374 product :: (Unbox a, Num a) => Vector a -> a
375 {-# INLINE product #-}
376 product = G.product
377
378 maximum :: (Unbox a, Ord a) => Vector a -> a
379 {-# INLINE maximum #-}
380 maximum = G.maximum
381
382 minimum :: (Unbox a, Ord a) => Vector a -> a
383 {-# INLINE minimum #-}
384 minimum = G.minimum
385
386 -- Unfolding
387 -- ---------
388
389 unfoldr :: Unbox a => (b -> Maybe (a, b)) -> b -> Vector a
390 {-# INLINE unfoldr #-}
391 unfoldr = G.unfoldr
392
393 -- Scans
394 -- -----
395
396 -- | Prefix scan
397 prescanl :: (Unbox a, Unbox b) => (a -> b -> a) -> a -> Vector b -> Vector a
398 {-# INLINE prescanl #-}
399 prescanl = G.prescanl
400
401 -- | Prefix scan with strict accumulator
402 prescanl' :: (Unbox a, Unbox b) => (a -> b -> a) -> a -> Vector b -> Vector a
403 {-# INLINE prescanl' #-}
404 prescanl' = G.prescanl'
405
406 -- | Suffix scan
407 postscanl :: (Unbox a, Unbox b) => (a -> b -> a) -> a -> Vector b -> Vector a
408 {-# INLINE postscanl #-}
409 postscanl = G.postscanl
410
411 -- | Suffix scan with strict accumulator
412 postscanl' :: (Unbox a, Unbox b) => (a -> b -> a) -> a -> Vector b -> Vector a
413 {-# INLINE postscanl' #-}
414 postscanl' = G.postscanl'
415
416 -- | Haskell-style scan
417 scanl :: (Unbox a, Unbox b) => (a -> b -> a) -> a -> Vector b -> Vector a
418 {-# INLINE scanl #-}
419 scanl = G.scanl
420
421 -- | Haskell-style scan with strict accumulator
422 scanl' :: (Unbox a, Unbox b) => (a -> b -> a) -> a -> Vector b -> Vector a
423 {-# INLINE scanl' #-}
424 scanl' = G.scanl'
425
426 -- | Scan over a non-empty 'Vector'
427 scanl1 :: Unbox a => (a -> a -> a) -> Vector a -> Vector a
428 {-# INLINE scanl1 #-}
429 scanl1 = G.scanl1
430
431 -- | Scan over a non-empty 'Vector' with a strict accumulator
432 scanl1' :: Unbox a => (a -> a -> a) -> Vector a -> Vector a
433 {-# INLINE scanl1' #-}
434 scanl1' = G.scanl1'
435
436 -- Enumeration
437 -- -----------
438
439 enumFromTo :: (Unbox a, Enum a) => a -> a -> Vector a
440 {-# INLINE enumFromTo #-}
441 enumFromTo = G.enumFromTo
442
443 enumFromThenTo :: (Unbox a, Enum a) => a -> a -> a -> Vector a
444 {-# INLINE enumFromThenTo #-}
445 enumFromThenTo = G.enumFromThenTo
446
447 -- Conversion to/from lists
448 -- ------------------------
449
450 -- | Convert a vector to a list
451 toList :: Unbox a => Vector a -> [a]
452 {-# INLINE toList #-}
453 toList = G.toList
454
455 -- | Convert a list to a vector
456 fromList :: Unbox a => [a] -> Vector a
457 {-# INLINE fromList #-}
458 fromList = G.fromList
459
460 #define DEFINE_IMMUTABLE
461 #include "unbox-tuple-instances"
462