{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-} -- | -- Module : Data.Vector.Storable -- Copyright : (c) Roman Leshchinskiy 2009 -- License : BSD-style -- -- Maintainer : Roman Leshchinskiy -- Stability : experimental -- Portability : non-portable -- -- 'Storable'-based vectors. -- module Data.Vector.Storable ( Vector, MVector(..), Storable, -- * Length information length, null, -- * Construction empty, singleton, cons, snoc, replicate, (++), copy, -- * Accessing individual elements (!), head, last, -- * Subvectors slice, init, tail, take, drop, -- * Permutations accum, (//), backpermute, reverse, -- * Mapping map, concatMap, -- * Zipping and unzipping zipWith, zipWith3, -- * Filtering filter, takeWhile, dropWhile, -- * Searching elem, notElem, find, findIndex, -- * Folding foldl, foldl1, foldl', foldl1', foldr, foldr1, -- * Specialised folds and, or, sum, product, maximum, minimum, -- * Unfolding unfoldr, -- * Scans prescanl, prescanl', postscanl, postscanl', scanl, scanl', scanl1, scanl1', -- * Enumeration enumFromTo, enumFromThenTo, -- * Conversion to/from lists toList, fromList ) where import qualified Data.Vector.Generic as G import Data.Vector.Storable.Mutable ( MVector(..) ) import Data.Vector.Storable.Internal import Foreign.Storable import Foreign.ForeignPtr import System.IO.Unsafe ( unsafePerformIO ) import Prelude hiding ( length, null, replicate, (++), head, last, init, tail, take, drop, reverse, map, concatMap, zipWith, zipWith3, zip, zip3, unzip, unzip3, filter, takeWhile, dropWhile, elem, notElem, foldl, foldl1, foldr, foldr1, and, or, sum, product, minimum, maximum, scanl, scanl1, enumFromTo, enumFromThenTo ) import qualified Prelude -- | 'Storable'-based vectors data Vector a = Vector {-# UNPACK #-} !Int {-# UNPACK #-} !Int {-# UNPACK #-} !(ForeignPtr a) instance (Show a, Storable a) => Show (Vector a) where show = (Prelude.++ " :: Data.Vector.Storable.Vector") . ("fromList " Prelude.++) . show . toList instance Storable a => G.Vector Vector a where {-# INLINE vnew #-} vnew init = unsafePerformIO (do MVector i n p <- init return (Vector i n p)) {-# INLINE vlength #-} vlength (Vector _ n _) = n {-# INLINE unsafeSlice #-} unsafeSlice (Vector i _ p) j n = Vector (i+j) n p {-# INLINE unsafeIndexM #-} unsafeIndexM (Vector i _ p) j = return . inlinePerformIO $ withForeignPtr p (`peekElemOff` (i+j)) instance (Storable a, Eq a) => Eq (Vector a) where {-# INLINE (==) #-} (==) = G.eq instance (Storable a, Ord a) => Ord (Vector a) where {-# INLINE compare #-} compare = G.cmp -- Length -- ------ length :: Storable a => Vector a -> Int {-# INLINE length #-} length = G.length null :: Storable a => Vector a -> Bool {-# INLINE null #-} null = G.null -- Construction -- ------------ -- | Empty vector empty :: Storable a => Vector a {-# INLINE empty #-} empty = G.empty -- | Vector with exaclty one element singleton :: Storable a => a -> Vector a {-# INLINE singleton #-} singleton = G.singleton -- | Vector of the given length with the given value in each position replicate :: Storable a => Int -> a -> Vector a {-# INLINE replicate #-} replicate = G.replicate -- | Prepend an element cons :: Storable a => a -> Vector a -> Vector a {-# INLINE cons #-} cons = G.cons -- | Append an element snoc :: Storable a => Vector a -> a -> Vector a {-# INLINE snoc #-} snoc = G.snoc infixr 5 ++ -- | Concatenate two vectors (++) :: Storable a => Vector a -> Vector a -> Vector a {-# INLINE (++) #-} (++) = (G.++) -- | Create a copy of a vector. Useful when dealing with slices. copy :: Storable a => Vector a -> Vector a {-# INLINE copy #-} copy = G.copy -- Accessing individual elements -- ----------------------------- -- | Indexing (!) :: Storable a => Vector a -> Int -> a {-# INLINE (!) #-} (!) = (G.!) -- | First element head :: Storable a => Vector a -> a {-# INLINE head #-} head = G.head -- | Last element last :: Storable a => Vector a -> a {-# INLINE last #-} last = G.last -- Subarrays -- --------- -- | Yield a part of the vector without copying it. Safer version of -- 'unsafeSlice'. slice :: Storable a => Vector a -> Int -- ^ starting index -> Int -- ^ length -> Vector a {-# INLINE slice #-} slice = G.slice -- | Yield all but the last element without copying. init :: Storable a => Vector a -> Vector a {-# INLINE init #-} init = G.init -- | All but the first element (without copying). tail :: Storable a => Vector a -> Vector a {-# INLINE tail #-} tail = G.tail -- | Yield the first @n@ elements without copying. take :: Storable a => Int -> Vector a -> Vector a {-# INLINE take #-} take = G.take -- | Yield all but the first @n@ elements without copying. drop :: Storable a => Int -> Vector a -> Vector a {-# INLINE drop #-} drop = G.drop -- Permutations -- ------------ accum :: Storable a => (a -> b -> a) -> Vector a -> [(Int,b)] -> Vector a {-# INLINE accum #-} accum = G.accum (//) :: Storable a => Vector a -> [(Int, a)] -> Vector a {-# INLINE (//) #-} (//) = (G.//) backpermute :: Storable a => Vector a -> Vector Int -> Vector a {-# INLINE backpermute #-} backpermute = G.backpermute reverse :: Storable a => Vector a -> Vector a {-# INLINE reverse #-} reverse = G.reverse -- Mapping -- ------- -- | Map a function over a vector map :: (Storable a, Storable b) => (a -> b) -> Vector a -> Vector b {-# INLINE map #-} map = G.map concatMap :: (Storable a, Storable b) => (a -> Vector b) -> Vector a -> Vector b {-# INLINE concatMap #-} concatMap = G.concatMap -- Zipping/unzipping -- ----------------- -- | Zip two vectors with the given function. zipWith :: (Storable a, Storable b, Storable c) => (a -> b -> c) -> Vector a -> Vector b -> Vector c {-# INLINE zipWith #-} zipWith = G.zipWith -- | Zip three vectors with the given function. zipWith3 :: (Storable a, Storable b, Storable c, Storable d) => (a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d {-# INLINE zipWith3 #-} zipWith3 = G.zipWith3 -- Filtering -- --------- -- | Drop elements which do not satisfy the predicate filter :: Storable a => (a -> Bool) -> Vector a -> Vector a {-# INLINE filter #-} filter = G.filter -- | Yield the longest prefix of elements satisfying the predicate. takeWhile :: Storable a => (a -> Bool) -> Vector a -> Vector a {-# INLINE takeWhile #-} takeWhile = G.takeWhile -- | Drop the longest prefix of elements that satisfy the predicate. dropWhile :: Storable a => (a -> Bool) -> Vector a -> Vector a {-# INLINE dropWhile #-} dropWhile = G.dropWhile -- Searching -- --------- infix 4 `elem` -- | Check whether the vector contains an element elem :: (Storable a, Eq a) => a -> Vector a -> Bool {-# INLINE elem #-} elem = G.elem infix 4 `notElem` -- | Inverse of `elem` notElem :: (Storable a, Eq a) => a -> Vector a -> Bool {-# INLINE notElem #-} notElem = G.notElem -- | Yield 'Just' the first element matching the predicate or 'Nothing' if no -- such element exists. find :: Storable a => (a -> Bool) -> Vector a -> Maybe a {-# INLINE find #-} find = G.find -- | Yield 'Just' the index of the first element matching the predicate or -- 'Nothing' if no such element exists. findIndex :: Storable a => (a -> Bool) -> Vector a -> Maybe Int {-# INLINE findIndex #-} findIndex = G.findIndex -- Folding -- ------- -- | Left fold foldl :: Storable b => (a -> b -> a) -> a -> Vector b -> a {-# INLINE foldl #-} foldl = G.foldl -- | Lefgt fold on non-empty vectors foldl1 :: Storable a => (a -> a -> a) -> Vector a -> a {-# INLINE foldl1 #-} foldl1 = G.foldl1 -- | Left fold with strict accumulator foldl' :: Storable b => (a -> b -> a) -> a -> Vector b -> a {-# INLINE foldl' #-} foldl' = G.foldl' -- | Left fold on non-empty vectors with strict accumulator foldl1' :: Storable a => (a -> a -> a) -> Vector a -> a {-# INLINE foldl1' #-} foldl1' = G.foldl1' -- | Right fold foldr :: Storable a => (a -> b -> b) -> b -> Vector a -> b {-# INLINE foldr #-} foldr = G.foldr -- | Right fold on non-empty vectors foldr1 :: Storable a => (a -> a -> a) -> Vector a -> a {-# INLINE foldr1 #-} foldr1 = G.foldr1 -- Specialised folds -- ----------------- and :: Vector Bool -> Bool {-# INLINE and #-} and = G.and or :: Vector Bool -> Bool {-# INLINE or #-} or = G.or sum :: (Storable a, Num a) => Vector a -> a {-# INLINE sum #-} sum = G.sum product :: (Storable a, Num a) => Vector a -> a {-# INLINE product #-} product = G.product maximum :: (Storable a, Ord a) => Vector a -> a {-# INLINE maximum #-} maximum = G.maximum minimum :: (Storable a, Ord a) => Vector a -> a {-# INLINE minimum #-} minimum = G.minimum -- Unfolding -- --------- unfoldr :: Storable a => (b -> Maybe (a, b)) -> b -> Vector a {-# INLINE unfoldr #-} unfoldr = G.unfoldr -- Scans -- ----- -- | Prefix scan prescanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a {-# INLINE prescanl #-} prescanl = G.prescanl -- | Prefix scan with strict accumulator prescanl' :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a {-# INLINE prescanl' #-} prescanl' = G.prescanl' -- | Suffix scan postscanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a {-# INLINE postscanl #-} postscanl = G.postscanl -- | Suffix scan with strict accumulator postscanl' :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a {-# INLINE postscanl' #-} postscanl' = G.postscanl' -- | Haskell-style scan scanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a {-# INLINE scanl #-} scanl = G.scanl -- | Haskell-style scan with strict accumulator scanl' :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a {-# INLINE scanl' #-} scanl' = G.scanl' -- | Scan over a non-empty 'Vector' scanl1 :: Storable a => (a -> a -> a) -> Vector a -> Vector a {-# INLINE scanl1 #-} scanl1 = G.scanl1 -- | Scan over a non-empty 'Vector' with a strict accumulator scanl1' :: Storable a => (a -> a -> a) -> Vector a -> Vector a {-# INLINE scanl1' #-} scanl1' = G.scanl1' -- Enumeration -- ----------- enumFromTo :: (Storable a, Enum a) => a -> a -> Vector a {-# INLINE enumFromTo #-} enumFromTo = G.enumFromTo enumFromThenTo :: (Storable a, Enum a) => a -> a -> a -> Vector a {-# INLINE enumFromThenTo #-} enumFromThenTo = G.enumFromThenTo -- Conversion to/from lists -- ------------------------ -- | Convert a vector to a list toList :: Storable a => Vector a -> [a] {-# INLINE toList #-} toList = G.toList -- | Convert a list to a vector fromList :: Storable a => [a] -> Vector a {-# INLINE fromList #-} fromList = G.fromList