vnew -> basicNew
[darcs-mirrors/vector.git] / Data / Vector / Storable.hs
1 {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
2
3 -- |
4 -- Module : Data.Vector.Storable
5 -- Copyright : (c) Roman Leshchinskiy 2009
6 -- License : BSD-style
7 --
8 -- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
9 -- Stability : experimental
10 -- Portability : non-portable
11 --
12 -- 'Storable'-based vectors.
13 --
14
15 module Data.Vector.Storable (
16 Vector, MVector(..), Storable,
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 postscanl, postscanl',
57 scanl, scanl', scanl1, scanl1',
58
59 -- * Enumeration
60 enumFromTo, enumFromThenTo,
61
62 -- * Conversion to/from lists
63 toList, fromList
64 ) where
65
66 import qualified Data.Vector.Generic as G
67 import Data.Vector.Storable.Mutable ( MVector(..) )
68 import Data.Vector.Storable.Internal
69
70 import Foreign.Storable
71 import Foreign.ForeignPtr
72
73 import System.IO.Unsafe ( unsafePerformIO )
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 scanl, scanl1,
86 enumFromTo, enumFromThenTo )
87
88 import qualified Prelude
89
90 -- | 'Storable'-based vectors
91 data Vector a = Vector {-# UNPACK #-} !Int
92 {-# UNPACK #-} !Int
93 {-# UNPACK #-} !(ForeignPtr a)
94
95 instance (Show a, Storable a) => Show (Vector a) where
96 show = (Prelude.++ " :: Data.Vector.Storable.Vector")
97 . ("fromList " Prelude.++)
98 . show
99 . toList
100
101 instance Storable a => G.Vector Vector a where
102 {-# INLINE basicNew #-}
103 basicNew init = unsafePerformIO (do
104 MVector i n p <- init
105 return (Vector i n p))
106
107 {-# INLINE vlength #-}
108 vlength (Vector _ n _) = n
109
110 {-# INLINE unsafeSlice #-}
111 unsafeSlice (Vector i _ p) j n = Vector (i+j) n p
112
113 {-# INLINE unsafeIndexM #-}
114 unsafeIndexM (Vector i _ p) j = return
115 . inlinePerformIO
116 $ withForeignPtr p (`peekElemOff` (i+j))
117
118 instance (Storable a, Eq a) => Eq (Vector a) where
119 {-# INLINE (==) #-}
120 (==) = G.eq
121
122 instance (Storable a, Ord a) => Ord (Vector a) where
123 {-# INLINE compare #-}
124 compare = G.cmp
125
126 -- Length
127 -- ------
128
129 length :: Storable a => Vector a -> Int
130 {-# INLINE length #-}
131 length = G.length
132
133 null :: Storable a => Vector a -> Bool
134 {-# INLINE null #-}
135 null = G.null
136
137 -- Construction
138 -- ------------
139
140 -- | Empty vector
141 empty :: Storable a => Vector a
142 {-# INLINE empty #-}
143 empty = G.empty
144
145 -- | Vector with exaclty one element
146 singleton :: Storable a => a -> Vector a
147 {-# INLINE singleton #-}
148 singleton = G.singleton
149
150 -- | Vector of the given length with the given value in each position
151 replicate :: Storable a => Int -> a -> Vector a
152 {-# INLINE replicate #-}
153 replicate = G.replicate
154
155 -- | Prepend an element
156 cons :: Storable a => a -> Vector a -> Vector a
157 {-# INLINE cons #-}
158 cons = G.cons
159
160 -- | Append an element
161 snoc :: Storable a => Vector a -> a -> Vector a
162 {-# INLINE snoc #-}
163 snoc = G.snoc
164
165 infixr 5 ++
166 -- | Concatenate two vectors
167 (++) :: Storable a => Vector a -> Vector a -> Vector a
168 {-# INLINE (++) #-}
169 (++) = (G.++)
170
171 -- | Create a copy of a vector. Useful when dealing with slices.
172 copy :: Storable a => Vector a -> Vector a
173 {-# INLINE copy #-}
174 copy = G.copy
175
176 -- Accessing individual elements
177 -- -----------------------------
178
179 -- | Indexing
180 (!) :: Storable a => Vector a -> Int -> a
181 {-# INLINE (!) #-}
182 (!) = (G.!)
183
184 -- | First element
185 head :: Storable a => Vector a -> a
186 {-# INLINE head #-}
187 head = G.head
188
189 -- | Last element
190 last :: Storable a => Vector a -> a
191 {-# INLINE last #-}
192 last = G.last
193
194 -- Subarrays
195 -- ---------
196
197 -- | Yield a part of the vector without copying it. Safer version of
198 -- 'unsafeSlice'.
199 slice :: Storable a => Vector a -> Int -- ^ starting index
200 -> Int -- ^ length
201 -> Vector a
202 {-# INLINE slice #-}
203 slice = G.slice
204
205 -- | Yield all but the last element without copying.
206 init :: Storable a => Vector a -> Vector a
207 {-# INLINE init #-}
208 init = G.init
209
210 -- | All but the first element (without copying).
211 tail :: Storable a => Vector a -> Vector a
212 {-# INLINE tail #-}
213 tail = G.tail
214
215 -- | Yield the first @n@ elements without copying.
216 take :: Storable a => Int -> Vector a -> Vector a
217 {-# INLINE take #-}
218 take = G.take
219
220 -- | Yield all but the first @n@ elements without copying.
221 drop :: Storable a => Int -> Vector a -> Vector a
222 {-# INLINE drop #-}
223 drop = G.drop
224
225 -- Permutations
226 -- ------------
227
228 accum :: Storable a => (a -> b -> a) -> Vector a -> [(Int,b)] -> Vector a
229 {-# INLINE accum #-}
230 accum = G.accum
231
232 (//) :: Storable a => Vector a -> [(Int, a)] -> Vector a
233 {-# INLINE (//) #-}
234 (//) = (G.//)
235
236 backpermute :: Storable a => Vector a -> Vector Int -> Vector a
237 {-# INLINE backpermute #-}
238 backpermute = G.backpermute
239
240 reverse :: Storable a => Vector a -> Vector a
241 {-# INLINE reverse #-}
242 reverse = G.reverse
243
244 -- Mapping
245 -- -------
246
247 -- | Map a function over a vector
248 map :: (Storable a, Storable b) => (a -> b) -> Vector a -> Vector b
249 {-# INLINE map #-}
250 map = G.map
251
252 concatMap :: (Storable a, Storable b) => (a -> Vector b) -> Vector a -> Vector b
253 {-# INLINE concatMap #-}
254 concatMap = G.concatMap
255
256 -- Zipping/unzipping
257 -- -----------------
258
259 -- | Zip two vectors with the given function.
260 zipWith :: (Storable a, Storable b, Storable c)
261 => (a -> b -> c) -> Vector a -> Vector b -> Vector c
262 {-# INLINE zipWith #-}
263 zipWith = G.zipWith
264
265 -- | Zip three vectors with the given function.
266 zipWith3 :: (Storable a, Storable b, Storable c, Storable d)
267 => (a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d
268 {-# INLINE zipWith3 #-}
269 zipWith3 = G.zipWith3
270
271 -- Filtering
272 -- ---------
273
274 -- | Drop elements which do not satisfy the predicate
275 filter :: Storable a => (a -> Bool) -> Vector a -> Vector a
276 {-# INLINE filter #-}
277 filter = G.filter
278
279 -- | Yield the longest prefix of elements satisfying the predicate.
280 takeWhile :: Storable a => (a -> Bool) -> Vector a -> Vector a
281 {-# INLINE takeWhile #-}
282 takeWhile = G.takeWhile
283
284 -- | Drop the longest prefix of elements that satisfy the predicate.
285 dropWhile :: Storable a => (a -> Bool) -> Vector a -> Vector a
286 {-# INLINE dropWhile #-}
287 dropWhile = G.dropWhile
288
289 -- Searching
290 -- ---------
291
292 infix 4 `elem`
293 -- | Check whether the vector contains an element
294 elem :: (Storable a, Eq a) => a -> Vector a -> Bool
295 {-# INLINE elem #-}
296 elem = G.elem
297
298 infix 4 `notElem`
299 -- | Inverse of `elem`
300 notElem :: (Storable a, Eq a) => a -> Vector a -> Bool
301 {-# INLINE notElem #-}
302 notElem = G.notElem
303
304 -- | Yield 'Just' the first element matching the predicate or 'Nothing' if no
305 -- such element exists.
306 find :: Storable a => (a -> Bool) -> Vector a -> Maybe a
307 {-# INLINE find #-}
308 find = G.find
309
310 -- | Yield 'Just' the index of the first element matching the predicate or
311 -- 'Nothing' if no such element exists.
312 findIndex :: Storable a => (a -> Bool) -> Vector a -> Maybe Int
313 {-# INLINE findIndex #-}
314 findIndex = G.findIndex
315
316 -- Folding
317 -- -------
318
319 -- | Left fold
320 foldl :: Storable b => (a -> b -> a) -> a -> Vector b -> a
321 {-# INLINE foldl #-}
322 foldl = G.foldl
323
324 -- | Lefgt fold on non-empty vectors
325 foldl1 :: Storable a => (a -> a -> a) -> Vector a -> a
326 {-# INLINE foldl1 #-}
327 foldl1 = G.foldl1
328
329 -- | Left fold with strict accumulator
330 foldl' :: Storable b => (a -> b -> a) -> a -> Vector b -> a
331 {-# INLINE foldl' #-}
332 foldl' = G.foldl'
333
334 -- | Left fold on non-empty vectors with strict accumulator
335 foldl1' :: Storable a => (a -> a -> a) -> Vector a -> a
336 {-# INLINE foldl1' #-}
337 foldl1' = G.foldl1'
338
339 -- | Right fold
340 foldr :: Storable a => (a -> b -> b) -> b -> Vector a -> b
341 {-# INLINE foldr #-}
342 foldr = G.foldr
343
344 -- | Right fold on non-empty vectors
345 foldr1 :: Storable a => (a -> a -> a) -> Vector a -> a
346 {-# INLINE foldr1 #-}
347 foldr1 = G.foldr1
348
349 -- Specialised folds
350 -- -----------------
351
352 and :: Vector Bool -> Bool
353 {-# INLINE and #-}
354 and = G.and
355
356 or :: Vector Bool -> Bool
357 {-# INLINE or #-}
358 or = G.or
359
360 sum :: (Storable a, Num a) => Vector a -> a
361 {-# INLINE sum #-}
362 sum = G.sum
363
364 product :: (Storable a, Num a) => Vector a -> a
365 {-# INLINE product #-}
366 product = G.product
367
368 maximum :: (Storable a, Ord a) => Vector a -> a
369 {-# INLINE maximum #-}
370 maximum = G.maximum
371
372 minimum :: (Storable a, Ord a) => Vector a -> a
373 {-# INLINE minimum #-}
374 minimum = G.minimum
375
376 -- Unfolding
377 -- ---------
378
379 unfoldr :: Storable a => (b -> Maybe (a, b)) -> b -> Vector a
380 {-# INLINE unfoldr #-}
381 unfoldr = G.unfoldr
382
383 -- Scans
384 -- -----
385
386 -- | Prefix scan
387 prescanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
388 {-# INLINE prescanl #-}
389 prescanl = G.prescanl
390
391 -- | Prefix scan with strict accumulator
392 prescanl' :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
393 {-# INLINE prescanl' #-}
394 prescanl' = G.prescanl'
395
396 -- | Suffix scan
397 postscanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
398 {-# INLINE postscanl #-}
399 postscanl = G.postscanl
400
401 -- | Suffix scan with strict accumulator
402 postscanl' :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
403 {-# INLINE postscanl' #-}
404 postscanl' = G.postscanl'
405
406 -- | Haskell-style scan
407 scanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
408 {-# INLINE scanl #-}
409 scanl = G.scanl
410
411 -- | Haskell-style scan with strict accumulator
412 scanl' :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
413 {-# INLINE scanl' #-}
414 scanl' = G.scanl'
415
416 -- | Scan over a non-empty 'Vector'
417 scanl1 :: Storable a => (a -> a -> a) -> Vector a -> Vector a
418 {-# INLINE scanl1 #-}
419 scanl1 = G.scanl1
420
421 -- | Scan over a non-empty 'Vector' with a strict accumulator
422 scanl1' :: Storable a => (a -> a -> a) -> Vector a -> Vector a
423 {-# INLINE scanl1' #-}
424 scanl1' = G.scanl1'
425
426 -- Enumeration
427 -- -----------
428
429 enumFromTo :: (Storable a, Enum a) => a -> a -> Vector a
430 {-# INLINE enumFromTo #-}
431 enumFromTo = G.enumFromTo
432
433 enumFromThenTo :: (Storable a, Enum a) => a -> a -> a -> Vector a
434 {-# INLINE enumFromThenTo #-}
435 enumFromThenTo = G.enumFromThenTo
436
437 -- Conversion to/from lists
438 -- ------------------------
439
440 -- | Convert a vector to a list
441 toList :: Storable a => Vector a -> [a]
442 {-# INLINE toList #-}
443 toList = G.toList
444
445 -- | Convert a list to a vector
446 fromList :: Storable a => [a] -> Vector a
447 {-# INLINE fromList #-}
448 fromList = G.fromList
449