5637d6f5af642033e7ceee1fd7eb239124a2536e
[darcs-mirrors/vector.git] / Data / Vector / Base.hs
1 {-# LANGUAGE Rank2Types, MultiParamTypeClasses, BangPatterns, CPP #-}
2
3 #include "phases.h"
4
5 module Data.Vector.Base
6 where
7
8 import qualified Data.Vector.Base.Mutable as Mut
9
10 import qualified Data.Vector.Stream as Stream
11 import Data.Vector.Stream ( Stream )
12 import Data.Vector.Stream.Size
13
14 import Prelude hiding ( length,
15 replicate, (++),
16 head, last,
17 init, tail, take, drop,
18 map, zipWith,
19 filter, takeWhile, dropWhile,
20 foldl, foldl1, foldr, foldr1 )
21
22 class Base v a where
23 create :: (forall mv m. Mut.Base mv m a => m (mv m a)) -> v a
24
25 length :: v a -> Int
26 unsafeSlice :: v a -> Int -> Int -> v a
27
28 unsafeIndex :: v a -> Int -> (a -> b) -> b
29
30 stream :: Base v a => v a -> Stream a
31 {-# INLINE_STREAM stream #-}
32 stream !v = Stream.unfold get 0 `Stream.sized` Exact n
33 where
34 n = length v
35
36 {-# INLINE get #-}
37 get i | i < n = unsafeIndex v i $ \x -> Just (x, i+1)
38 | otherwise = Nothing
39
40 unstream :: Base v a => Stream a -> v a
41 {-# INLINE_STREAM unstream #-}
42 unstream s = create (Mut.unstream s)
43
44 {-# RULES
45
46 "stream/unstream [Vector.Base]" forall s.
47 stream (unstream s) = s
48
49 #-}
50
51 -- Construction
52 -- ------------
53
54 empty :: Base v a => v a
55 {-# INLINE empty #-}
56 empty = unstream Stream.empty
57
58 singleton :: Base v a => a -> v a
59 {-# INLINE singleton #-}
60 singleton x = unstream (Stream.singleton x)
61
62 replicate :: Base v a => Int -> a -> v a
63 {-# INLINE replicate #-}
64 replicate n = unstream . Stream.replicate n
65
66 cons :: Base v a => a -> v a -> v a
67 {-# INLINE cons #-}
68 cons x = unstream . Stream.cons x . stream
69
70 snoc :: Base v a => v a -> a -> v a
71 {-# INLINE snoc #-}
72 snoc v = unstream . Stream.snoc (stream v)
73
74 infixr 5 ++
75 (++) :: Base v a => v a -> v a -> v a
76 {-# INLINE (++) #-}
77 v ++ w = unstream (stream v Stream.++ stream w)
78
79 -- Subarrays
80 -- ---------
81
82 take :: Base v a => Int -> v a -> v a
83 {-# INLINE take #-}
84 take n = unstream . Stream.take n . stream
85
86 drop :: Base v a => Int -> v a -> v a
87 {-# INLINE drop #-}
88 drop n = unstream . Stream.drop n . stream
89
90 -- Mapping/zipping
91 -- ---------------
92
93 map :: (Base v a, Base v b) => (a -> b) -> v a -> v b
94 {-# INLINE map #-}
95 map f = unstream . Stream.map f . stream
96
97 zipWith :: (Base v a, Base v b, Base v c) => (a -> b -> c) -> v a -> v b -> v c
98 {-# INLINE zipWith #-}
99 zipWith f xs ys = unstream (Stream.zipWith f (stream xs) (stream ys))
100
101 -- Filtering
102 -- ---------
103
104 filter :: Base v a => (a -> Bool) -> v a -> v a
105 {-# INLINE filter #-}
106 filter f = unstream . Stream.filter f . stream
107
108 takeWhile :: Base v a => (a -> Bool) -> v a -> v a
109 {-# INLINE takeWhile #-}
110 takeWhile f = unstream . Stream.takeWhile f . stream
111
112 dropWhile :: Base v a => (a -> Bool) -> v a -> v a
113 {-# INLINE dropWhile #-}
114 dropWhile f = unstream . Stream.dropWhile f . stream
115
116 -- Folding
117 -- -------
118
119 foldl :: Base v b => (a -> b -> a) -> a -> v b -> a
120 {-# INLINE foldl #-}
121 foldl f z = Stream.foldl f z . stream
122
123 foldl1 :: Base v a => (a -> a -> a) -> v a -> a
124 {-# INLINE foldl1 #-}
125 foldl1 f = Stream.foldl1 f . stream
126
127 foldl' :: Base v b => (a -> b -> a) -> a -> v b -> a
128 {-# INLINE foldl' #-}
129 foldl' f z = Stream.foldl' f z . stream
130
131 foldl1' :: Base v a => (a -> a -> a) -> v a -> a
132 {-# INLINE foldl1' #-}
133 foldl1' f = Stream.foldl1' f . stream
134
135 foldr :: Base v a => (a -> b -> b) -> b -> v a -> b
136 {-# INLINE foldr #-}
137 foldr f z = Stream.foldr f z . stream
138
139 foldr1 :: Base v a => (a -> a -> a) -> v a -> a
140 {-# INLINE foldr1 #-}
141 foldr1 f = Stream.foldr1 f . stream
142
143 toList :: Base v a => v a -> [a]
144 {-# INLINE toList #-}
145 toList = Stream.toList . stream
146
147 fromList :: Base v a => [a] -> v a
148 {-# INLINE fromList #-}
149 fromList = unstream . Stream.fromList
150