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