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