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