@@ -158,150 +158,17 @@ module Data.Vector.Primitive (
158158) where
159159
160160import qualified Data.Vector.Generic as G
161- import Data.Vector.Primitive.Mutable ( MVector (.. ) )
161+ import Data.Vector.Primitive.Unsafe ( Vector ( .. ), MVector (.. ), unsafeCoerceVector )
162162import Data.Vector.Internal.Check
163- import qualified Data.Vector.Fusion.Bundle as Bundle
164- import Data.Primitive.ByteArray
165163import Data.Primitive ( Prim , sizeOf )
166164
167- import Control.DeepSeq ( NFData (rnf )
168- #if MIN_VERSION_deepseq(1,4,3)
169- , NFData1 (liftRnf )
170- #endif
171- )
172-
173- import Control.Monad ( liftM )
174165import Control.Monad.ST ( ST )
175166import Control.Monad.Primitive
176167
177168import Prelude
178- ( Eq , Ord , Num , Enum , Monoid , Traversable , Monad , Read , Show , Bool , Ordering (.. ), Int , Maybe , Either
179- , compare , mempty , mappend , mconcat , showsPrec , return , otherwise , seq , error , undefined
180- , (+) , (*) , (<) , (<=) , (>) , (>=) , (==) , (/=) , ($!) )
181-
182- import Data.Data ( Data (.. ) )
183- import Text.Read ( Read (.. ), readListPrecDefault )
184- import Data.Semigroup ( Semigroup (.. ) )
185-
186- import Data.Coerce
187- import Unsafe.Coerce
188- import qualified GHC.Exts as Exts
189-
190- type role Vector nominal
191-
192- -- | /O(1)/ Unsafely coerce an immutable vector from one element type to another,
193- -- representationally equal type. The operation just changes the type of the
194- -- underlying pointer and does not modify the elements.
195- --
196- -- This is marginally safer than 'unsafeCast', since this function imposes an
197- -- extra 'Coercible' constraint. The constraint guarantees that the element types
198- -- are representationally equal. It however cannot guarantee
199- -- that their respective 'Prim' instances are compatible.
200- unsafeCoerceVector :: Coercible a b => Vector a -> Vector b
201- unsafeCoerceVector = unsafeCoerce
202-
203- -- | Unboxed vectors of primitive types.
204- data Vector a = Vector {- # UNPACK #-} !Int -- ^ offset
205- {- # UNPACK #-} !Int -- ^ length
206- {- # UNPACK #-} !ByteArray -- ^ underlying byte array
207-
208- instance NFData (Vector a ) where
209- rnf (Vector _ _ _) = ()
210-
211- #if MIN_VERSION_deepseq(1,4,3)
212- -- | @since 0.12.1.0
213- instance NFData1 Vector where
214- liftRnf _ (Vector _ _ _) = ()
215- #endif
216-
217- instance (Show a , Prim a ) => Show (Vector a ) where
218- showsPrec = G. showsPrec
219-
220- instance (Read a , Prim a ) => Read (Vector a ) where
221- readPrec = G. readPrec
222- readListPrec = readListPrecDefault
223-
224- instance (Data a , Prim a ) => Data (Vector a ) where
225- gfoldl = G. gfoldl
226- toConstr _ = G. mkVecConstr " Data.Vector.Primitive.Vector"
227- gunfold = G. gunfold
228- dataTypeOf _ = G. mkVecType " Data.Vector.Primitive.Vector"
229- dataCast1 = G. dataCast
230-
231-
232- type instance G. Mutable Vector = MVector
233-
234- instance Prim a => G. Vector Vector a where
235- {-# INLINE basicUnsafeFreeze #-}
236- basicUnsafeFreeze (MVector i n marr)
237- = Vector i n `liftM` unsafeFreezeByteArray marr
238-
239- {-# INLINE basicUnsafeThaw #-}
240- basicUnsafeThaw (Vector i n arr)
241- = MVector i n `liftM` unsafeThawByteArray arr
242-
243- {-# INLINE basicLength #-}
244- basicLength (Vector _ n _) = n
245-
246- {-# INLINE basicUnsafeSlice #-}
247- basicUnsafeSlice j n (Vector i _ arr) = Vector (i+ j) n arr
248-
249- {-# INLINE basicUnsafeIndexM #-}
250- basicUnsafeIndexM (Vector i _ arr) j = return $! indexByteArray arr (i+ j)
251-
252- {-# INLINE basicUnsafeCopy #-}
253- basicUnsafeCopy (MVector i n dst) (Vector j _ src)
254- = copyByteArray dst (i* sz) src (j* sz) (n* sz)
255- where
256- sz = sizeOf (undefined :: a )
257-
258- {-# INLINE elemseq #-}
259- elemseq _ = seq
260-
261- -- See http://trac.haskell.org/vector/ticket/12
262- instance (Prim a , Eq a ) => Eq (Vector a ) where
263- {-# INLINE (==) #-}
264- xs == ys = Bundle. eq (G. stream xs) (G. stream ys)
265-
266- -- See http://trac.haskell.org/vector/ticket/12
267- instance (Prim a , Ord a ) => Ord (Vector a ) where
268- {-# INLINE compare #-}
269- compare xs ys = Bundle. cmp (G. stream xs) (G. stream ys)
270-
271- {-# INLINE (<) #-}
272- xs < ys = Bundle. cmp (G. stream xs) (G. stream ys) == LT
273-
274- {-# INLINE (<=) #-}
275- xs <= ys = Bundle. cmp (G. stream xs) (G. stream ys) /= GT
276-
277- {-# INLINE (>) #-}
278- xs > ys = Bundle. cmp (G. stream xs) (G. stream ys) == GT
279-
280- {-# INLINE (>=) #-}
281- xs >= ys = Bundle. cmp (G. stream xs) (G. stream ys) /= LT
282-
283- instance Prim a => Semigroup (Vector a ) where
284- {-# INLINE (<>) #-}
285- (<>) = (++)
286-
287- {-# INLINE sconcat #-}
288- sconcat = G. concatNE
289-
290- instance Prim a => Monoid (Vector a ) where
291- {-# INLINE mempty #-}
292- mempty = empty
293-
294- {-# INLINE mappend #-}
295- mappend = (<>)
296-
297- {-# INLINE mconcat #-}
298- mconcat = concat
299-
300- instance Prim a => Exts. IsList (Vector a ) where
301- type Item (Vector a ) = a
302- fromList = fromList
303- fromListN = fromListN
304- toList = toList
169+ ( Eq , Ord , Num , Enum , Monoid , Traversable , Monad , Bool , Ordering (.. ), Int , Maybe , Either
170+ , otherwise , error , undefined
171+ , (==) )
305172
306173
307174-- Length
@@ -1952,4 +1819,4 @@ copy :: (Prim a, PrimMonad m) => MVector (PrimState m) a -> Vector a -> m ()
19521819copy = G. copy
19531820
19541821-- $setup
1955- -- >>> import Prelude (($), min, even, max, succ, id, Ord(..))
1822+ -- >>> import Prelude (($), min, even, max, succ, id, Ord(..), Num(..) )
0 commit comments