feat(third_party/bazel): Check in rules_haskell from Tweag
This commit is contained in:
parent
2eb1dc26e4
commit
f723b8b878
479 changed files with 51484 additions and 0 deletions
1719
third_party/bazel/rules_haskell/examples/vector/Data/Vector.hs
vendored
Normal file
1719
third_party/bazel/rules_haskell/examples/vector/Data/Vector.hs
vendored
Normal file
File diff suppressed because it is too large
Load diff
655
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Fusion/Bundle.hs
vendored
Normal file
655
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Fusion/Bundle.hs
vendored
Normal file
|
@ -0,0 +1,655 @@
|
|||
{-# LANGUAGE CPP, FlexibleInstances, Rank2Types, BangPatterns #-}
|
||||
|
||||
-- |
|
||||
-- Module : Data.Vector.Fusion.Bundle
|
||||
-- Copyright : (c) Roman Leshchinskiy 2008-2010
|
||||
-- License : BSD-style
|
||||
--
|
||||
-- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
|
||||
-- Stability : experimental
|
||||
-- Portability : non-portable
|
||||
--
|
||||
-- Bundles for stream fusion
|
||||
--
|
||||
|
||||
module Data.Vector.Fusion.Bundle (
|
||||
-- * Types
|
||||
Step(..), Chunk(..), Bundle, MBundle,
|
||||
|
||||
-- * In-place markers
|
||||
inplace,
|
||||
|
||||
-- * Size hints
|
||||
size, sized,
|
||||
|
||||
-- * Length information
|
||||
length, null,
|
||||
|
||||
-- * Construction
|
||||
empty, singleton, cons, snoc, replicate, generate, (++),
|
||||
|
||||
-- * Accessing individual elements
|
||||
head, last, (!!), (!?),
|
||||
|
||||
-- * Substreams
|
||||
slice, init, tail, take, drop,
|
||||
|
||||
-- * Mapping
|
||||
map, concatMap, flatten, unbox,
|
||||
|
||||
-- * Zipping
|
||||
indexed, indexedR,
|
||||
zipWith, zipWith3, zipWith4, zipWith5, zipWith6,
|
||||
zip, zip3, zip4, zip5, zip6,
|
||||
|
||||
-- * Filtering
|
||||
filter, takeWhile, dropWhile,
|
||||
|
||||
-- * Searching
|
||||
elem, notElem, find, findIndex,
|
||||
|
||||
-- * Folding
|
||||
foldl, foldl1, foldl', foldl1', foldr, foldr1,
|
||||
|
||||
-- * Specialised folds
|
||||
and, or,
|
||||
|
||||
-- * Unfolding
|
||||
unfoldr, unfoldrN, iterateN,
|
||||
|
||||
-- * Scans
|
||||
prescanl, prescanl',
|
||||
postscanl, postscanl',
|
||||
scanl, scanl',
|
||||
scanl1, scanl1',
|
||||
|
||||
-- * Enumerations
|
||||
enumFromStepN, enumFromTo, enumFromThenTo,
|
||||
|
||||
-- * Conversions
|
||||
toList, fromList, fromListN, unsafeFromList, lift,
|
||||
fromVector, reVector, fromVectors, concatVectors,
|
||||
|
||||
-- * Monadic combinators
|
||||
mapM, mapM_, zipWithM, zipWithM_, filterM, foldM, fold1M, foldM', fold1M',
|
||||
|
||||
eq, cmp, eqBy, cmpBy
|
||||
) where
|
||||
|
||||
import Data.Vector.Generic.Base ( Vector )
|
||||
import Data.Vector.Fusion.Bundle.Size
|
||||
import Data.Vector.Fusion.Util
|
||||
import Data.Vector.Fusion.Stream.Monadic ( Stream(..), Step(..) )
|
||||
import Data.Vector.Fusion.Bundle.Monadic ( Chunk(..) )
|
||||
import qualified Data.Vector.Fusion.Bundle.Monadic as M
|
||||
import qualified Data.Vector.Fusion.Stream.Monadic as S
|
||||
|
||||
import Prelude hiding ( length, null,
|
||||
replicate, (++),
|
||||
head, last, (!!),
|
||||
init, tail, take, drop,
|
||||
map, concatMap,
|
||||
zipWith, zipWith3, zip, zip3,
|
||||
filter, takeWhile, dropWhile,
|
||||
elem, notElem,
|
||||
foldl, foldl1, foldr, foldr1,
|
||||
and, or,
|
||||
scanl, scanl1,
|
||||
enumFromTo, enumFromThenTo,
|
||||
mapM, mapM_ )
|
||||
|
||||
#if MIN_VERSION_base(4,9,0)
|
||||
import Data.Functor.Classes (Eq1 (..), Ord1 (..))
|
||||
#endif
|
||||
|
||||
import GHC.Base ( build )
|
||||
|
||||
-- Data.Vector.Internal.Check is unused
|
||||
#define NOT_VECTOR_MODULE
|
||||
#include "vector.h"
|
||||
|
||||
-- | The type of pure streams
|
||||
type Bundle = M.Bundle Id
|
||||
|
||||
-- | Alternative name for monadic streams
|
||||
type MBundle = M.Bundle
|
||||
|
||||
inplace :: (forall m. Monad m => S.Stream m a -> S.Stream m b)
|
||||
-> (Size -> Size) -> Bundle v a -> Bundle v b
|
||||
{-# INLINE_FUSED inplace #-}
|
||||
inplace f g b = b `seq` M.fromStream (f (M.elements b)) (g (M.size b))
|
||||
|
||||
{-# RULES
|
||||
|
||||
"inplace/inplace [Vector]"
|
||||
forall (f1 :: forall m. Monad m => S.Stream m a -> S.Stream m a)
|
||||
(f2 :: forall m. Monad m => S.Stream m a -> S.Stream m a)
|
||||
g1 g2 s.
|
||||
inplace f1 g1 (inplace f2 g2 s) = inplace (f1 . f2) (g1 . g2) s #-}
|
||||
|
||||
|
||||
|
||||
-- | Convert a pure stream to a monadic stream
|
||||
lift :: Monad m => Bundle v a -> M.Bundle m v a
|
||||
{-# INLINE_FUSED lift #-}
|
||||
lift (M.Bundle (Stream step s) (Stream vstep t) v sz)
|
||||
= M.Bundle (Stream (return . unId . step) s)
|
||||
(Stream (return . unId . vstep) t) v sz
|
||||
|
||||
-- | 'Size' hint of a 'Bundle'
|
||||
size :: Bundle v a -> Size
|
||||
{-# INLINE size #-}
|
||||
size = M.size
|
||||
|
||||
-- | Attach a 'Size' hint to a 'Bundle'
|
||||
sized :: Bundle v a -> Size -> Bundle v a
|
||||
{-# INLINE sized #-}
|
||||
sized = M.sized
|
||||
|
||||
-- Length
|
||||
-- ------
|
||||
|
||||
-- | Length of a 'Bundle'
|
||||
length :: Bundle v a -> Int
|
||||
{-# INLINE length #-}
|
||||
length = unId . M.length
|
||||
|
||||
-- | Check if a 'Bundle' is empty
|
||||
null :: Bundle v a -> Bool
|
||||
{-# INLINE null #-}
|
||||
null = unId . M.null
|
||||
|
||||
-- Construction
|
||||
-- ------------
|
||||
|
||||
-- | Empty 'Bundle'
|
||||
empty :: Bundle v a
|
||||
{-# INLINE empty #-}
|
||||
empty = M.empty
|
||||
|
||||
-- | Singleton 'Bundle'
|
||||
singleton :: a -> Bundle v a
|
||||
{-# INLINE singleton #-}
|
||||
singleton = M.singleton
|
||||
|
||||
-- | Replicate a value to a given length
|
||||
replicate :: Int -> a -> Bundle v a
|
||||
{-# INLINE replicate #-}
|
||||
replicate = M.replicate
|
||||
|
||||
-- | Generate a stream from its indices
|
||||
generate :: Int -> (Int -> a) -> Bundle v a
|
||||
{-# INLINE generate #-}
|
||||
generate = M.generate
|
||||
|
||||
-- | Prepend an element
|
||||
cons :: a -> Bundle v a -> Bundle v a
|
||||
{-# INLINE cons #-}
|
||||
cons = M.cons
|
||||
|
||||
-- | Append an element
|
||||
snoc :: Bundle v a -> a -> Bundle v a
|
||||
{-# INLINE snoc #-}
|
||||
snoc = M.snoc
|
||||
|
||||
infixr 5 ++
|
||||
-- | Concatenate two 'Bundle's
|
||||
(++) :: Bundle v a -> Bundle v a -> Bundle v a
|
||||
{-# INLINE (++) #-}
|
||||
(++) = (M.++)
|
||||
|
||||
-- Accessing elements
|
||||
-- ------------------
|
||||
|
||||
-- | First element of the 'Bundle' or error if empty
|
||||
head :: Bundle v a -> a
|
||||
{-# INLINE head #-}
|
||||
head = unId . M.head
|
||||
|
||||
-- | Last element of the 'Bundle' or error if empty
|
||||
last :: Bundle v a -> a
|
||||
{-# INLINE last #-}
|
||||
last = unId . M.last
|
||||
|
||||
infixl 9 !!
|
||||
-- | Element at the given position
|
||||
(!!) :: Bundle v a -> Int -> a
|
||||
{-# INLINE (!!) #-}
|
||||
s !! i = unId (s M.!! i)
|
||||
|
||||
infixl 9 !?
|
||||
-- | Element at the given position or 'Nothing' if out of bounds
|
||||
(!?) :: Bundle v a -> Int -> Maybe a
|
||||
{-# INLINE (!?) #-}
|
||||
s !? i = unId (s M.!? i)
|
||||
|
||||
-- Substreams
|
||||
-- ----------
|
||||
|
||||
-- | Extract a substream of the given length starting at the given position.
|
||||
slice :: Int -- ^ starting index
|
||||
-> Int -- ^ length
|
||||
-> Bundle v a
|
||||
-> Bundle v a
|
||||
{-# INLINE slice #-}
|
||||
slice = M.slice
|
||||
|
||||
-- | All but the last element
|
||||
init :: Bundle v a -> Bundle v a
|
||||
{-# INLINE init #-}
|
||||
init = M.init
|
||||
|
||||
-- | All but the first element
|
||||
tail :: Bundle v a -> Bundle v a
|
||||
{-# INLINE tail #-}
|
||||
tail = M.tail
|
||||
|
||||
-- | The first @n@ elements
|
||||
take :: Int -> Bundle v a -> Bundle v a
|
||||
{-# INLINE take #-}
|
||||
take = M.take
|
||||
|
||||
-- | All but the first @n@ elements
|
||||
drop :: Int -> Bundle v a -> Bundle v a
|
||||
{-# INLINE drop #-}
|
||||
drop = M.drop
|
||||
|
||||
-- Mapping
|
||||
-- ---------------
|
||||
|
||||
-- | Map a function over a 'Bundle'
|
||||
map :: (a -> b) -> Bundle v a -> Bundle v b
|
||||
{-# INLINE map #-}
|
||||
map = M.map
|
||||
|
||||
unbox :: Bundle v (Box a) -> Bundle v a
|
||||
{-# INLINE unbox #-}
|
||||
unbox = M.unbox
|
||||
|
||||
concatMap :: (a -> Bundle v b) -> Bundle v a -> Bundle v b
|
||||
{-# INLINE concatMap #-}
|
||||
concatMap = M.concatMap
|
||||
|
||||
-- Zipping
|
||||
-- -------
|
||||
|
||||
-- | Pair each element in a 'Bundle' with its index
|
||||
indexed :: Bundle v a -> Bundle v (Int,a)
|
||||
{-# INLINE indexed #-}
|
||||
indexed = M.indexed
|
||||
|
||||
-- | Pair each element in a 'Bundle' with its index, starting from the right
|
||||
-- and counting down
|
||||
indexedR :: Int -> Bundle v a -> Bundle v (Int,a)
|
||||
{-# INLINE_FUSED indexedR #-}
|
||||
indexedR = M.indexedR
|
||||
|
||||
-- | Zip two 'Bundle's with the given function
|
||||
zipWith :: (a -> b -> c) -> Bundle v a -> Bundle v b -> Bundle v c
|
||||
{-# INLINE zipWith #-}
|
||||
zipWith = M.zipWith
|
||||
|
||||
-- | Zip three 'Bundle's with the given function
|
||||
zipWith3 :: (a -> b -> c -> d) -> Bundle v a -> Bundle v b -> Bundle v c -> Bundle v d
|
||||
{-# INLINE zipWith3 #-}
|
||||
zipWith3 = M.zipWith3
|
||||
|
||||
zipWith4 :: (a -> b -> c -> d -> e)
|
||||
-> Bundle v a -> Bundle v b -> Bundle v c -> Bundle v d
|
||||
-> Bundle v e
|
||||
{-# INLINE zipWith4 #-}
|
||||
zipWith4 = M.zipWith4
|
||||
|
||||
zipWith5 :: (a -> b -> c -> d -> e -> f)
|
||||
-> Bundle v a -> Bundle v b -> Bundle v c -> Bundle v d
|
||||
-> Bundle v e -> Bundle v f
|
||||
{-# INLINE zipWith5 #-}
|
||||
zipWith5 = M.zipWith5
|
||||
|
||||
zipWith6 :: (a -> b -> c -> d -> e -> f -> g)
|
||||
-> Bundle v a -> Bundle v b -> Bundle v c -> Bundle v d
|
||||
-> Bundle v e -> Bundle v f -> Bundle v g
|
||||
{-# INLINE zipWith6 #-}
|
||||
zipWith6 = M.zipWith6
|
||||
|
||||
zip :: Bundle v a -> Bundle v b -> Bundle v (a,b)
|
||||
{-# INLINE zip #-}
|
||||
zip = M.zip
|
||||
|
||||
zip3 :: Bundle v a -> Bundle v b -> Bundle v c -> Bundle v (a,b,c)
|
||||
{-# INLINE zip3 #-}
|
||||
zip3 = M.zip3
|
||||
|
||||
zip4 :: Bundle v a -> Bundle v b -> Bundle v c -> Bundle v d
|
||||
-> Bundle v (a,b,c,d)
|
||||
{-# INLINE zip4 #-}
|
||||
zip4 = M.zip4
|
||||
|
||||
zip5 :: Bundle v a -> Bundle v b -> Bundle v c -> Bundle v d
|
||||
-> Bundle v e -> Bundle v (a,b,c,d,e)
|
||||
{-# INLINE zip5 #-}
|
||||
zip5 = M.zip5
|
||||
|
||||
zip6 :: Bundle v a -> Bundle v b -> Bundle v c -> Bundle v d
|
||||
-> Bundle v e -> Bundle v f -> Bundle v (a,b,c,d,e,f)
|
||||
{-# INLINE zip6 #-}
|
||||
zip6 = M.zip6
|
||||
|
||||
-- Filtering
|
||||
-- ---------
|
||||
|
||||
-- | Drop elements which do not satisfy the predicate
|
||||
filter :: (a -> Bool) -> Bundle v a -> Bundle v a
|
||||
{-# INLINE filter #-}
|
||||
filter = M.filter
|
||||
|
||||
-- | Longest prefix of elements that satisfy the predicate
|
||||
takeWhile :: (a -> Bool) -> Bundle v a -> Bundle v a
|
||||
{-# INLINE takeWhile #-}
|
||||
takeWhile = M.takeWhile
|
||||
|
||||
-- | Drop the longest prefix of elements that satisfy the predicate
|
||||
dropWhile :: (a -> Bool) -> Bundle v a -> Bundle v a
|
||||
{-# INLINE dropWhile #-}
|
||||
dropWhile = M.dropWhile
|
||||
|
||||
-- Searching
|
||||
-- ---------
|
||||
|
||||
infix 4 `elem`
|
||||
-- | Check whether the 'Bundle' contains an element
|
||||
elem :: Eq a => a -> Bundle v a -> Bool
|
||||
{-# INLINE elem #-}
|
||||
elem x = unId . M.elem x
|
||||
|
||||
infix 4 `notElem`
|
||||
-- | Inverse of `elem`
|
||||
notElem :: Eq a => a -> Bundle v a -> Bool
|
||||
{-# INLINE notElem #-}
|
||||
notElem x = unId . M.notElem x
|
||||
|
||||
-- | Yield 'Just' the first element matching the predicate or 'Nothing' if no
|
||||
-- such element exists.
|
||||
find :: (a -> Bool) -> Bundle v a -> Maybe a
|
||||
{-# INLINE find #-}
|
||||
find f = unId . M.find f
|
||||
|
||||
-- | Yield 'Just' the index of the first element matching the predicate or
|
||||
-- 'Nothing' if no such element exists.
|
||||
findIndex :: (a -> Bool) -> Bundle v a -> Maybe Int
|
||||
{-# INLINE findIndex #-}
|
||||
findIndex f = unId . M.findIndex f
|
||||
|
||||
-- Folding
|
||||
-- -------
|
||||
|
||||
-- | Left fold
|
||||
foldl :: (a -> b -> a) -> a -> Bundle v b -> a
|
||||
{-# INLINE foldl #-}
|
||||
foldl f z = unId . M.foldl f z
|
||||
|
||||
-- | Left fold on non-empty 'Bundle's
|
||||
foldl1 :: (a -> a -> a) -> Bundle v a -> a
|
||||
{-# INLINE foldl1 #-}
|
||||
foldl1 f = unId . M.foldl1 f
|
||||
|
||||
-- | Left fold with strict accumulator
|
||||
foldl' :: (a -> b -> a) -> a -> Bundle v b -> a
|
||||
{-# INLINE foldl' #-}
|
||||
foldl' f z = unId . M.foldl' f z
|
||||
|
||||
-- | Left fold on non-empty 'Bundle's with strict accumulator
|
||||
foldl1' :: (a -> a -> a) -> Bundle v a -> a
|
||||
{-# INLINE foldl1' #-}
|
||||
foldl1' f = unId . M.foldl1' f
|
||||
|
||||
-- | Right fold
|
||||
foldr :: (a -> b -> b) -> b -> Bundle v a -> b
|
||||
{-# INLINE foldr #-}
|
||||
foldr f z = unId . M.foldr f z
|
||||
|
||||
-- | Right fold on non-empty 'Bundle's
|
||||
foldr1 :: (a -> a -> a) -> Bundle v a -> a
|
||||
{-# INLINE foldr1 #-}
|
||||
foldr1 f = unId . M.foldr1 f
|
||||
|
||||
-- Specialised folds
|
||||
-- -----------------
|
||||
|
||||
and :: Bundle v Bool -> Bool
|
||||
{-# INLINE and #-}
|
||||
and = unId . M.and
|
||||
|
||||
or :: Bundle v Bool -> Bool
|
||||
{-# INLINE or #-}
|
||||
or = unId . M.or
|
||||
|
||||
-- Unfolding
|
||||
-- ---------
|
||||
|
||||
-- | Unfold
|
||||
unfoldr :: (s -> Maybe (a, s)) -> s -> Bundle v a
|
||||
{-# INLINE unfoldr #-}
|
||||
unfoldr = M.unfoldr
|
||||
|
||||
-- | Unfold at most @n@ elements
|
||||
unfoldrN :: Int -> (s -> Maybe (a, s)) -> s -> Bundle v a
|
||||
{-# INLINE unfoldrN #-}
|
||||
unfoldrN = M.unfoldrN
|
||||
|
||||
-- | Apply function n-1 times to value. Zeroth element is original value.
|
||||
iterateN :: Int -> (a -> a) -> a -> Bundle v a
|
||||
{-# INLINE iterateN #-}
|
||||
iterateN = M.iterateN
|
||||
|
||||
-- Scans
|
||||
-- -----
|
||||
|
||||
-- | Prefix scan
|
||||
prescanl :: (a -> b -> a) -> a -> Bundle v b -> Bundle v a
|
||||
{-# INLINE prescanl #-}
|
||||
prescanl = M.prescanl
|
||||
|
||||
-- | Prefix scan with strict accumulator
|
||||
prescanl' :: (a -> b -> a) -> a -> Bundle v b -> Bundle v a
|
||||
{-# INLINE prescanl' #-}
|
||||
prescanl' = M.prescanl'
|
||||
|
||||
-- | Suffix scan
|
||||
postscanl :: (a -> b -> a) -> a -> Bundle v b -> Bundle v a
|
||||
{-# INLINE postscanl #-}
|
||||
postscanl = M.postscanl
|
||||
|
||||
-- | Suffix scan with strict accumulator
|
||||
postscanl' :: (a -> b -> a) -> a -> Bundle v b -> Bundle v a
|
||||
{-# INLINE postscanl' #-}
|
||||
postscanl' = M.postscanl'
|
||||
|
||||
-- | Haskell-style scan
|
||||
scanl :: (a -> b -> a) -> a -> Bundle v b -> Bundle v a
|
||||
{-# INLINE scanl #-}
|
||||
scanl = M.scanl
|
||||
|
||||
-- | Haskell-style scan with strict accumulator
|
||||
scanl' :: (a -> b -> a) -> a -> Bundle v b -> Bundle v a
|
||||
{-# INLINE scanl' #-}
|
||||
scanl' = M.scanl'
|
||||
|
||||
-- | Scan over a non-empty 'Bundle'
|
||||
scanl1 :: (a -> a -> a) -> Bundle v a -> Bundle v a
|
||||
{-# INLINE scanl1 #-}
|
||||
scanl1 = M.scanl1
|
||||
|
||||
-- | Scan over a non-empty 'Bundle' with a strict accumulator
|
||||
scanl1' :: (a -> a -> a) -> Bundle v a -> Bundle v a
|
||||
{-# INLINE scanl1' #-}
|
||||
scanl1' = M.scanl1'
|
||||
|
||||
|
||||
-- Comparisons
|
||||
-- -----------
|
||||
|
||||
-- | Check if two 'Bundle's are equal
|
||||
eq :: (Eq a) => Bundle v a -> Bundle v a -> Bool
|
||||
{-# INLINE eq #-}
|
||||
eq = eqBy (==)
|
||||
|
||||
eqBy :: (a -> b -> Bool) -> Bundle v a -> Bundle v b -> Bool
|
||||
{-# INLINE eqBy #-}
|
||||
eqBy e x y = unId (M.eqBy e x y)
|
||||
|
||||
-- | Lexicographically compare two 'Bundle's
|
||||
cmp :: (Ord a) => Bundle v a -> Bundle v a -> Ordering
|
||||
{-# INLINE cmp #-}
|
||||
cmp = cmpBy compare
|
||||
|
||||
cmpBy :: (a -> b -> Ordering) -> Bundle v a -> Bundle v b -> Ordering
|
||||
{-# INLINE cmpBy #-}
|
||||
cmpBy c x y = unId (M.cmpBy c x y)
|
||||
|
||||
instance Eq a => Eq (M.Bundle Id v a) where
|
||||
{-# INLINE (==) #-}
|
||||
(==) = eq
|
||||
|
||||
instance Ord a => Ord (M.Bundle Id v a) where
|
||||
{-# INLINE compare #-}
|
||||
compare = cmp
|
||||
|
||||
#if MIN_VERSION_base(4,9,0)
|
||||
instance Eq1 (M.Bundle Id v) where
|
||||
{-# INLINE liftEq #-}
|
||||
liftEq = eqBy
|
||||
|
||||
instance Ord1 (M.Bundle Id v) where
|
||||
{-# INLINE liftCompare #-}
|
||||
liftCompare = cmpBy
|
||||
#endif
|
||||
|
||||
-- Monadic combinators
|
||||
-- -------------------
|
||||
|
||||
-- | Apply a monadic action to each element of the stream, producing a monadic
|
||||
-- stream of results
|
||||
mapM :: Monad m => (a -> m b) -> Bundle v a -> M.Bundle m v b
|
||||
{-# INLINE mapM #-}
|
||||
mapM f = M.mapM f . lift
|
||||
|
||||
-- | Apply a monadic action to each element of the stream
|
||||
mapM_ :: Monad m => (a -> m b) -> Bundle v a -> m ()
|
||||
{-# INLINE mapM_ #-}
|
||||
mapM_ f = M.mapM_ f . lift
|
||||
|
||||
zipWithM :: Monad m => (a -> b -> m c) -> Bundle v a -> Bundle v b -> M.Bundle m v c
|
||||
{-# INLINE zipWithM #-}
|
||||
zipWithM f as bs = M.zipWithM f (lift as) (lift bs)
|
||||
|
||||
zipWithM_ :: Monad m => (a -> b -> m c) -> Bundle v a -> Bundle v b -> m ()
|
||||
{-# INLINE zipWithM_ #-}
|
||||
zipWithM_ f as bs = M.zipWithM_ f (lift as) (lift bs)
|
||||
|
||||
-- | Yield a monadic stream of elements that satisfy the monadic predicate
|
||||
filterM :: Monad m => (a -> m Bool) -> Bundle v a -> M.Bundle m v a
|
||||
{-# INLINE filterM #-}
|
||||
filterM f = M.filterM f . lift
|
||||
|
||||
-- | Monadic fold
|
||||
foldM :: Monad m => (a -> b -> m a) -> a -> Bundle v b -> m a
|
||||
{-# INLINE foldM #-}
|
||||
foldM m z = M.foldM m z . lift
|
||||
|
||||
-- | Monadic fold over non-empty stream
|
||||
fold1M :: Monad m => (a -> a -> m a) -> Bundle v a -> m a
|
||||
{-# INLINE fold1M #-}
|
||||
fold1M m = M.fold1M m . lift
|
||||
|
||||
-- | Monadic fold with strict accumulator
|
||||
foldM' :: Monad m => (a -> b -> m a) -> a -> Bundle v b -> m a
|
||||
{-# INLINE foldM' #-}
|
||||
foldM' m z = M.foldM' m z . lift
|
||||
|
||||
-- | Monad fold over non-empty stream with strict accumulator
|
||||
fold1M' :: Monad m => (a -> a -> m a) -> Bundle v a -> m a
|
||||
{-# INLINE fold1M' #-}
|
||||
fold1M' m = M.fold1M' m . lift
|
||||
|
||||
-- Enumerations
|
||||
-- ------------
|
||||
|
||||
-- | Yield a 'Bundle' of the given length containing the values @x@, @x+y@,
|
||||
-- @x+y+y@ etc.
|
||||
enumFromStepN :: Num a => a -> a -> Int -> Bundle v a
|
||||
{-# INLINE enumFromStepN #-}
|
||||
enumFromStepN = M.enumFromStepN
|
||||
|
||||
-- | Enumerate values
|
||||
--
|
||||
-- /WARNING:/ This operations can be very inefficient. If at all possible, use
|
||||
-- 'enumFromStepN' instead.
|
||||
enumFromTo :: Enum a => a -> a -> Bundle v a
|
||||
{-# INLINE enumFromTo #-}
|
||||
enumFromTo = M.enumFromTo
|
||||
|
||||
-- | Enumerate values with a given step.
|
||||
--
|
||||
-- /WARNING:/ This operations is very inefficient. If at all possible, use
|
||||
-- 'enumFromStepN' instead.
|
||||
enumFromThenTo :: Enum a => a -> a -> a -> Bundle v a
|
||||
{-# INLINE enumFromThenTo #-}
|
||||
enumFromThenTo = M.enumFromThenTo
|
||||
|
||||
-- Conversions
|
||||
-- -----------
|
||||
|
||||
-- | Convert a 'Bundle' to a list
|
||||
toList :: Bundle v a -> [a]
|
||||
{-# INLINE toList #-}
|
||||
-- toList s = unId (M.toList s)
|
||||
toList s = build (\c n -> toListFB c n s)
|
||||
|
||||
-- This supports foldr/build list fusion that GHC implements
|
||||
toListFB :: (a -> b -> b) -> b -> Bundle v a -> b
|
||||
{-# INLINE [0] toListFB #-}
|
||||
toListFB c n M.Bundle{M.sElems = Stream step t} = go t
|
||||
where
|
||||
go s = case unId (step s) of
|
||||
Yield x s' -> x `c` go s'
|
||||
Skip s' -> go s'
|
||||
Done -> n
|
||||
|
||||
-- | Create a 'Bundle' from a list
|
||||
fromList :: [a] -> Bundle v a
|
||||
{-# INLINE fromList #-}
|
||||
fromList = M.fromList
|
||||
|
||||
-- | Create a 'Bundle' from the first @n@ elements of a list
|
||||
--
|
||||
-- > fromListN n xs = fromList (take n xs)
|
||||
fromListN :: Int -> [a] -> Bundle v a
|
||||
{-# INLINE fromListN #-}
|
||||
fromListN = M.fromListN
|
||||
|
||||
unsafeFromList :: Size -> [a] -> Bundle v a
|
||||
{-# INLINE unsafeFromList #-}
|
||||
unsafeFromList = M.unsafeFromList
|
||||
|
||||
fromVector :: Vector v a => v a -> Bundle v a
|
||||
{-# INLINE fromVector #-}
|
||||
fromVector = M.fromVector
|
||||
|
||||
reVector :: Bundle u a -> Bundle v a
|
||||
{-# INLINE reVector #-}
|
||||
reVector = M.reVector
|
||||
|
||||
fromVectors :: Vector v a => [v a] -> Bundle v a
|
||||
{-# INLINE fromVectors #-}
|
||||
fromVectors = M.fromVectors
|
||||
|
||||
concatVectors :: Vector v a => Bundle u (v a) -> Bundle v a
|
||||
{-# INLINE concatVectors #-}
|
||||
concatVectors = M.concatVectors
|
||||
|
||||
-- | Create a 'Bundle' of values from a 'Bundle' of streamable things
|
||||
flatten :: (a -> s) -> (s -> Step s b) -> Size -> Bundle v a -> Bundle v b
|
||||
{-# INLINE_FUSED flatten #-}
|
||||
flatten mk istep sz = M.flatten (return . mk) (return . istep) sz . lift
|
||||
|
1106
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Fusion/Bundle/Monadic.hs
vendored
Normal file
1106
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Fusion/Bundle/Monadic.hs
vendored
Normal file
File diff suppressed because it is too large
Load diff
121
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Fusion/Bundle/Size.hs
vendored
Normal file
121
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Fusion/Bundle/Size.hs
vendored
Normal file
|
@ -0,0 +1,121 @@
|
|||
-- |
|
||||
-- Module : Data.Vector.Fusion.Bundle.Size
|
||||
-- Copyright : (c) Roman Leshchinskiy 2008-2010
|
||||
-- License : BSD-style
|
||||
--
|
||||
-- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
|
||||
-- Stability : experimental
|
||||
-- Portability : portable
|
||||
--
|
||||
-- Size hints for streams.
|
||||
--
|
||||
|
||||
module Data.Vector.Fusion.Bundle.Size (
|
||||
Size(..), clampedSubtract, smaller, larger, toMax, upperBound, lowerBound
|
||||
) where
|
||||
|
||||
import Data.Vector.Fusion.Util ( delay_inline )
|
||||
|
||||
-- | Size hint
|
||||
data Size = Exact Int -- ^ Exact size
|
||||
| Max Int -- ^ Upper bound on the size
|
||||
| Unknown -- ^ Unknown size
|
||||
deriving( Eq, Show )
|
||||
|
||||
instance Num Size where
|
||||
Exact m + Exact n = checkedAdd Exact m n
|
||||
Exact m + Max n = checkedAdd Max m n
|
||||
|
||||
Max m + Exact n = checkedAdd Max m n
|
||||
Max m + Max n = checkedAdd Max m n
|
||||
|
||||
_ + _ = Unknown
|
||||
|
||||
|
||||
Exact m - Exact n = checkedSubtract Exact m n
|
||||
Exact m - Max _ = Max m
|
||||
|
||||
Max m - Exact n = checkedSubtract Max m n
|
||||
Max m - Max _ = Max m
|
||||
Max m - Unknown = Max m
|
||||
|
||||
_ - _ = Unknown
|
||||
|
||||
|
||||
fromInteger n = Exact (fromInteger n)
|
||||
|
||||
(*) = error "vector: internal error * for Bundle.size isn't defined"
|
||||
abs = error "vector: internal error abs for Bundle.size isn't defined"
|
||||
signum = error "vector: internal error signum for Bundle.size isn't defined"
|
||||
|
||||
{-# INLINE checkedAdd #-}
|
||||
checkedAdd :: (Int -> Size) -> Int -> Int -> Size
|
||||
checkedAdd con m n
|
||||
-- Note: we assume m and n are >= 0.
|
||||
| r < m || r < n =
|
||||
error $ "Data.Vector.Fusion.Bundle.Size.checkedAdd: overflow: " ++ show r
|
||||
| otherwise = con r
|
||||
where
|
||||
r = m + n
|
||||
|
||||
{-# INLINE checkedSubtract #-}
|
||||
checkedSubtract :: (Int -> Size) -> Int -> Int -> Size
|
||||
checkedSubtract con m n
|
||||
| r < 0 =
|
||||
error $ "Data.Vector.Fusion.Bundle.Size.checkedSubtract: underflow: " ++ show r
|
||||
| otherwise = con r
|
||||
where
|
||||
r = m - n
|
||||
|
||||
-- | Subtract two sizes with clamping to 0, for drop-like things
|
||||
{-# INLINE clampedSubtract #-}
|
||||
clampedSubtract :: Size -> Size -> Size
|
||||
clampedSubtract (Exact m) (Exact n) = Exact (max 0 (m - n))
|
||||
clampedSubtract (Max m) (Exact n)
|
||||
| m <= n = Exact 0
|
||||
| otherwise = Max (m - n)
|
||||
clampedSubtract (Exact m) (Max _) = Max m
|
||||
clampedSubtract (Max m) (Max _) = Max m
|
||||
clampedSubtract _ _ = Unknown
|
||||
|
||||
-- | Minimum of two size hints
|
||||
smaller :: Size -> Size -> Size
|
||||
{-# INLINE smaller #-}
|
||||
smaller (Exact m) (Exact n) = Exact (delay_inline min m n)
|
||||
smaller (Exact m) (Max n) = Max (delay_inline min m n)
|
||||
smaller (Exact m) Unknown = Max m
|
||||
smaller (Max m) (Exact n) = Max (delay_inline min m n)
|
||||
smaller (Max m) (Max n) = Max (delay_inline min m n)
|
||||
smaller (Max m) Unknown = Max m
|
||||
smaller Unknown (Exact n) = Max n
|
||||
smaller Unknown (Max n) = Max n
|
||||
smaller Unknown Unknown = Unknown
|
||||
|
||||
-- | Maximum of two size hints
|
||||
larger :: Size -> Size -> Size
|
||||
{-# INLINE larger #-}
|
||||
larger (Exact m) (Exact n) = Exact (delay_inline max m n)
|
||||
larger (Exact m) (Max n) | m >= n = Exact m
|
||||
| otherwise = Max n
|
||||
larger (Max m) (Exact n) | n >= m = Exact n
|
||||
| otherwise = Max m
|
||||
larger (Max m) (Max n) = Max (delay_inline max m n)
|
||||
larger _ _ = Unknown
|
||||
|
||||
-- | Convert a size hint to an upper bound
|
||||
toMax :: Size -> Size
|
||||
toMax (Exact n) = Max n
|
||||
toMax (Max n) = Max n
|
||||
toMax Unknown = Unknown
|
||||
|
||||
-- | Compute the minimum size from a size hint
|
||||
lowerBound :: Size -> Int
|
||||
lowerBound (Exact n) = n
|
||||
lowerBound _ = 0
|
||||
|
||||
-- | Compute the maximum size from a size hint if possible
|
||||
upperBound :: Size -> Maybe Int
|
||||
upperBound (Exact n) = Just n
|
||||
upperBound (Max n) = Just n
|
||||
upperBound Unknown = Nothing
|
||||
|
1639
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Fusion/Stream/Monadic.hs
vendored
Normal file
1639
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Fusion/Stream/Monadic.hs
vendored
Normal file
File diff suppressed because it is too large
Load diff
60
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Fusion/Util.hs
vendored
Normal file
60
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Fusion/Util.hs
vendored
Normal file
|
@ -0,0 +1,60 @@
|
|||
{-# LANGUAGE CPP #-}
|
||||
-- |
|
||||
-- Module : Data.Vector.Fusion.Util
|
||||
-- Copyright : (c) Roman Leshchinskiy 2009
|
||||
-- License : BSD-style
|
||||
--
|
||||
-- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
|
||||
-- Stability : experimental
|
||||
-- Portability : portable
|
||||
--
|
||||
-- Fusion-related utility types
|
||||
--
|
||||
|
||||
module Data.Vector.Fusion.Util (
|
||||
Id(..), Box(..),
|
||||
|
||||
delay_inline, delayed_min
|
||||
) where
|
||||
|
||||
#if !MIN_VERSION_base(4,8,0)
|
||||
import Control.Applicative (Applicative(..))
|
||||
#endif
|
||||
|
||||
-- | Identity monad
|
||||
newtype Id a = Id { unId :: a }
|
||||
|
||||
instance Functor Id where
|
||||
fmap f (Id x) = Id (f x)
|
||||
|
||||
instance Applicative Id where
|
||||
pure = Id
|
||||
Id f <*> Id x = Id (f x)
|
||||
|
||||
instance Monad Id where
|
||||
return = pure
|
||||
Id x >>= f = f x
|
||||
|
||||
-- | Box monad
|
||||
data Box a = Box { unBox :: a }
|
||||
|
||||
instance Functor Box where
|
||||
fmap f (Box x) = Box (f x)
|
||||
|
||||
instance Applicative Box where
|
||||
pure = Box
|
||||
Box f <*> Box x = Box (f x)
|
||||
|
||||
instance Monad Box where
|
||||
return = pure
|
||||
Box x >>= f = f x
|
||||
|
||||
-- | Delay inlining a function until late in the game (simplifier phase 0).
|
||||
delay_inline :: (a -> b) -> a -> b
|
||||
{-# INLINE [0] delay_inline #-}
|
||||
delay_inline f = f
|
||||
|
||||
-- | `min` inlined in phase 0
|
||||
delayed_min :: Int -> Int -> Int
|
||||
{-# INLINE [0] delayed_min #-}
|
||||
delayed_min m n = min m n
|
2206
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Generic.hs
vendored
Normal file
2206
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Generic.hs
vendored
Normal file
File diff suppressed because it is too large
Load diff
140
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Generic/Base.hs
vendored
Normal file
140
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Generic/Base.hs
vendored
Normal file
|
@ -0,0 +1,140 @@
|
|||
{-# LANGUAGE Rank2Types, MultiParamTypeClasses, FlexibleContexts,
|
||||
TypeFamilies, ScopedTypeVariables, BangPatterns #-}
|
||||
{-# OPTIONS_HADDOCK hide #-}
|
||||
|
||||
-- |
|
||||
-- Module : Data.Vector.Generic.Base
|
||||
-- Copyright : (c) Roman Leshchinskiy 2008-2010
|
||||
-- License : BSD-style
|
||||
--
|
||||
-- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
|
||||
-- Stability : experimental
|
||||
-- Portability : non-portable
|
||||
--
|
||||
-- Class of pure vectors
|
||||
--
|
||||
|
||||
module Data.Vector.Generic.Base (
|
||||
Vector(..), Mutable
|
||||
) where
|
||||
|
||||
import Data.Vector.Generic.Mutable.Base ( MVector )
|
||||
import qualified Data.Vector.Generic.Mutable.Base as M
|
||||
|
||||
import Control.Monad.Primitive
|
||||
|
||||
-- | @Mutable v s a@ is the mutable version of the pure vector type @v a@ with
|
||||
-- the state token @s@
|
||||
--
|
||||
type family Mutable (v :: * -> *) :: * -> * -> *
|
||||
|
||||
-- | Class of immutable vectors. Every immutable vector is associated with its
|
||||
-- mutable version through the 'Mutable' type family. Methods of this class
|
||||
-- should not be used directly. Instead, "Data.Vector.Generic" and other
|
||||
-- Data.Vector modules provide safe and fusible wrappers.
|
||||
--
|
||||
-- Minimum complete implementation:
|
||||
--
|
||||
-- * 'basicUnsafeFreeze'
|
||||
--
|
||||
-- * 'basicUnsafeThaw'
|
||||
--
|
||||
-- * 'basicLength'
|
||||
--
|
||||
-- * 'basicUnsafeSlice'
|
||||
--
|
||||
-- * 'basicUnsafeIndexM'
|
||||
--
|
||||
class MVector (Mutable v) a => Vector v a where
|
||||
-- | /Assumed complexity: O(1)/
|
||||
--
|
||||
-- Unsafely convert a mutable vector to its immutable version
|
||||
-- without copying. The mutable vector may not be used after
|
||||
-- this operation.
|
||||
basicUnsafeFreeze :: PrimMonad m => Mutable v (PrimState m) a -> m (v a)
|
||||
|
||||
-- | /Assumed complexity: O(1)/
|
||||
--
|
||||
-- Unsafely convert an immutable vector to its mutable version without
|
||||
-- copying. The immutable vector may not be used after this operation.
|
||||
basicUnsafeThaw :: PrimMonad m => v a -> m (Mutable v (PrimState m) a)
|
||||
|
||||
-- | /Assumed complexity: O(1)/
|
||||
--
|
||||
-- Yield the length of the vector.
|
||||
basicLength :: v a -> Int
|
||||
|
||||
-- | /Assumed complexity: O(1)/
|
||||
--
|
||||
-- Yield a slice of the vector without copying it. No range checks are
|
||||
-- performed.
|
||||
basicUnsafeSlice :: Int -- ^ starting index
|
||||
-> Int -- ^ length
|
||||
-> v a -> v a
|
||||
|
||||
-- | /Assumed complexity: O(1)/
|
||||
--
|
||||
-- Yield the element at the given position in a monad. No range checks are
|
||||
-- performed.
|
||||
--
|
||||
-- The monad allows us to be strict in the vector if we want. Suppose we had
|
||||
--
|
||||
-- > unsafeIndex :: v a -> Int -> a
|
||||
--
|
||||
-- instead. Now, if we wanted to copy a vector, we'd do something like
|
||||
--
|
||||
-- > copy mv v ... = ... unsafeWrite mv i (unsafeIndex v i) ...
|
||||
--
|
||||
-- For lazy vectors, the indexing would not be evaluated which means that we
|
||||
-- would retain a reference to the original vector in each element we write.
|
||||
-- This is not what we want!
|
||||
--
|
||||
-- With 'basicUnsafeIndexM', we can do
|
||||
--
|
||||
-- > copy mv v ... = ... case basicUnsafeIndexM v i of
|
||||
-- > Box x -> unsafeWrite mv i x ...
|
||||
--
|
||||
-- which does not have this problem because indexing (but not the returned
|
||||
-- element!) is evaluated immediately.
|
||||
--
|
||||
basicUnsafeIndexM :: Monad m => v a -> Int -> m a
|
||||
|
||||
-- | /Assumed complexity: O(n)/
|
||||
--
|
||||
-- Copy an immutable vector into a mutable one. The two vectors must have
|
||||
-- the same length but this is not checked.
|
||||
--
|
||||
-- Instances of 'Vector' should redefine this method if they wish to support
|
||||
-- an efficient block copy operation.
|
||||
--
|
||||
-- Default definition: copying basic on 'basicUnsafeIndexM' and
|
||||
-- 'basicUnsafeWrite'.
|
||||
basicUnsafeCopy :: PrimMonad m => Mutable v (PrimState m) a -> v a -> m ()
|
||||
|
||||
{-# INLINE basicUnsafeCopy #-}
|
||||
basicUnsafeCopy !dst !src = do_copy 0
|
||||
where
|
||||
!n = basicLength src
|
||||
|
||||
do_copy i | i < n = do
|
||||
x <- basicUnsafeIndexM src i
|
||||
M.basicUnsafeWrite dst i x
|
||||
do_copy (i+1)
|
||||
| otherwise = return ()
|
||||
|
||||
-- | Evaluate @a@ as far as storing it in a vector would and yield @b@.
|
||||
-- The @v a@ argument only fixes the type and is not touched. The method is
|
||||
-- only used for optimisation purposes. Thus, it is safe for instances of
|
||||
-- 'Vector' to evaluate @a@ less than it would be when stored in a vector
|
||||
-- although this might result in suboptimal code.
|
||||
--
|
||||
-- > elemseq v x y = (singleton x `asTypeOf` v) `seq` y
|
||||
--
|
||||
-- Default defintion: @a@ is not evaluated at all
|
||||
--
|
||||
elemseq :: v a -> a -> b -> b
|
||||
|
||||
{-# INLINE elemseq #-}
|
||||
elemseq _ = \_ x -> x
|
||||
|
||||
|
1034
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Generic/Mutable.hs
vendored
Normal file
1034
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Generic/Mutable.hs
vendored
Normal file
File diff suppressed because it is too large
Load diff
145
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Generic/Mutable/Base.hs
vendored
Normal file
145
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Generic/Mutable/Base.hs
vendored
Normal file
|
@ -0,0 +1,145 @@
|
|||
{-# LANGUAGE CPP, MultiParamTypeClasses, BangPatterns, TypeFamilies #-}
|
||||
-- |
|
||||
-- Module : Data.Vector.Generic.Mutable.Base
|
||||
-- Copyright : (c) Roman Leshchinskiy 2008-2011
|
||||
-- License : BSD-style
|
||||
--
|
||||
-- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
|
||||
-- Stability : experimental
|
||||
-- Portability : non-portable
|
||||
--
|
||||
-- Class of mutable vectors
|
||||
--
|
||||
|
||||
module Data.Vector.Generic.Mutable.Base (
|
||||
MVector(..)
|
||||
) where
|
||||
|
||||
import Control.Monad.Primitive ( PrimMonad, PrimState )
|
||||
|
||||
-- Data.Vector.Internal.Check is unused
|
||||
#define NOT_VECTOR_MODULE
|
||||
#include "vector.h"
|
||||
|
||||
-- | Class of mutable vectors parametrised with a primitive state token.
|
||||
--
|
||||
class MVector v a where
|
||||
-- | Length of the mutable vector. This method should not be
|
||||
-- called directly, use 'length' instead.
|
||||
basicLength :: v s a -> Int
|
||||
|
||||
-- | Yield a part of the mutable vector without copying it. This method
|
||||
-- should not be called directly, use 'unsafeSlice' instead.
|
||||
basicUnsafeSlice :: Int -- ^ starting index
|
||||
-> Int -- ^ length of the slice
|
||||
-> v s a
|
||||
-> v s a
|
||||
|
||||
-- | Check whether two vectors overlap. This method should not be
|
||||
-- called directly, use 'overlaps' instead.
|
||||
basicOverlaps :: v s a -> v s a -> Bool
|
||||
|
||||
-- | Create a mutable vector of the given length. This method should not be
|
||||
-- called directly, use 'unsafeNew' instead.
|
||||
basicUnsafeNew :: PrimMonad m => Int -> m (v (PrimState m) a)
|
||||
|
||||
-- | Initialize a vector to a standard value. This is intended to be called as
|
||||
-- part of the safe new operation (and similar operations), to properly blank
|
||||
-- the newly allocated memory if necessary.
|
||||
--
|
||||
-- Vectors that are necessarily initialized as part of creation may implement
|
||||
-- this as a no-op.
|
||||
basicInitialize :: PrimMonad m => v (PrimState m) a -> m ()
|
||||
|
||||
-- | Create a mutable vector of the given length and fill it with an
|
||||
-- initial value. This method should not be called directly, use
|
||||
-- 'replicate' instead.
|
||||
basicUnsafeReplicate :: PrimMonad m => Int -> a -> m (v (PrimState m) a)
|
||||
|
||||
-- | Yield the element at the given position. This method should not be
|
||||
-- called directly, use 'unsafeRead' instead.
|
||||
basicUnsafeRead :: PrimMonad m => v (PrimState m) a -> Int -> m a
|
||||
|
||||
-- | Replace the element at the given position. This method should not be
|
||||
-- called directly, use 'unsafeWrite' instead.
|
||||
basicUnsafeWrite :: PrimMonad m => v (PrimState m) a -> Int -> a -> m ()
|
||||
|
||||
-- | Reset all elements of the vector to some undefined value, clearing all
|
||||
-- references to external objects. This is usually a noop for unboxed
|
||||
-- vectors. This method should not be called directly, use 'clear' instead.
|
||||
basicClear :: PrimMonad m => v (PrimState m) a -> m ()
|
||||
|
||||
-- | Set all elements of the vector to the given value. This method should
|
||||
-- not be called directly, use 'set' instead.
|
||||
basicSet :: PrimMonad m => v (PrimState m) a -> a -> m ()
|
||||
|
||||
-- | Copy a vector. The two vectors may not overlap. This method should not
|
||||
-- be called directly, use 'unsafeCopy' instead.
|
||||
basicUnsafeCopy :: PrimMonad m => v (PrimState m) a -- ^ target
|
||||
-> v (PrimState m) a -- ^ source
|
||||
-> m ()
|
||||
|
||||
-- | Move the contents of a vector. The two vectors may overlap. This method
|
||||
-- should not be called directly, use 'unsafeMove' instead.
|
||||
basicUnsafeMove :: PrimMonad m => v (PrimState m) a -- ^ target
|
||||
-> v (PrimState m) a -- ^ source
|
||||
-> m ()
|
||||
|
||||
-- | Grow a vector by the given number of elements. This method should not be
|
||||
-- called directly, use 'unsafeGrow' instead.
|
||||
basicUnsafeGrow :: PrimMonad m => v (PrimState m) a -> Int
|
||||
-> m (v (PrimState m) a)
|
||||
|
||||
{-# INLINE basicUnsafeReplicate #-}
|
||||
basicUnsafeReplicate n x
|
||||
= do
|
||||
v <- basicUnsafeNew n
|
||||
basicSet v x
|
||||
return v
|
||||
|
||||
{-# INLINE basicClear #-}
|
||||
basicClear _ = return ()
|
||||
|
||||
{-# INLINE basicSet #-}
|
||||
basicSet !v x
|
||||
| n == 0 = return ()
|
||||
| otherwise = do
|
||||
basicUnsafeWrite v 0 x
|
||||
do_set 1
|
||||
where
|
||||
!n = basicLength v
|
||||
|
||||
do_set i | 2*i < n = do basicUnsafeCopy (basicUnsafeSlice i i v)
|
||||
(basicUnsafeSlice 0 i v)
|
||||
do_set (2*i)
|
||||
| otherwise = basicUnsafeCopy (basicUnsafeSlice i (n-i) v)
|
||||
(basicUnsafeSlice 0 (n-i) v)
|
||||
|
||||
{-# INLINE basicUnsafeCopy #-}
|
||||
basicUnsafeCopy !dst !src = do_copy 0
|
||||
where
|
||||
!n = basicLength src
|
||||
|
||||
do_copy i | i < n = do
|
||||
x <- basicUnsafeRead src i
|
||||
basicUnsafeWrite dst i x
|
||||
do_copy (i+1)
|
||||
| otherwise = return ()
|
||||
|
||||
{-# INLINE basicUnsafeMove #-}
|
||||
basicUnsafeMove !dst !src
|
||||
| basicOverlaps dst src = do
|
||||
srcCopy <- basicUnsafeNew (basicLength src)
|
||||
basicUnsafeCopy srcCopy src
|
||||
basicUnsafeCopy dst srcCopy
|
||||
| otherwise = basicUnsafeCopy dst src
|
||||
|
||||
{-# INLINE basicUnsafeGrow #-}
|
||||
basicUnsafeGrow v by
|
||||
= do
|
||||
v' <- basicUnsafeNew (n+by)
|
||||
basicUnsafeCopy (basicUnsafeSlice 0 n v') v
|
||||
return v'
|
||||
where
|
||||
n = basicLength v
|
||||
|
178
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Generic/New.hs
vendored
Normal file
178
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Generic/New.hs
vendored
Normal file
|
@ -0,0 +1,178 @@
|
|||
{-# LANGUAGE CPP, Rank2Types, FlexibleContexts, MultiParamTypeClasses #-}
|
||||
|
||||
-- |
|
||||
-- Module : Data.Vector.Generic.New
|
||||
-- Copyright : (c) Roman Leshchinskiy 2008-2010
|
||||
-- License : BSD-style
|
||||
--
|
||||
-- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
|
||||
-- Stability : experimental
|
||||
-- Portability : non-portable
|
||||
--
|
||||
-- Purely functional interface to initialisation of mutable vectors
|
||||
--
|
||||
|
||||
module Data.Vector.Generic.New (
|
||||
New(..), create, run, runPrim, apply, modify, modifyWithBundle,
|
||||
unstream, transform, unstreamR, transformR,
|
||||
slice, init, tail, take, drop,
|
||||
unsafeSlice, unsafeInit, unsafeTail
|
||||
) where
|
||||
|
||||
import qualified Data.Vector.Generic.Mutable as MVector
|
||||
|
||||
import Data.Vector.Generic.Base ( Vector, Mutable )
|
||||
|
||||
import Data.Vector.Fusion.Bundle ( Bundle )
|
||||
import qualified Data.Vector.Fusion.Bundle as Bundle
|
||||
import Data.Vector.Fusion.Stream.Monadic ( Stream )
|
||||
import Data.Vector.Fusion.Bundle.Size
|
||||
|
||||
import Control.Monad.Primitive
|
||||
import Control.Monad.ST ( ST )
|
||||
import Control.Monad ( liftM )
|
||||
import Prelude hiding ( init, tail, take, drop, reverse, map, filter )
|
||||
|
||||
-- Data.Vector.Internal.Check is unused
|
||||
#define NOT_VECTOR_MODULE
|
||||
#include "vector.h"
|
||||
|
||||
data New v a = New (forall s. ST s (Mutable v s a))
|
||||
|
||||
create :: (forall s. ST s (Mutable v s a)) -> New v a
|
||||
{-# INLINE create #-}
|
||||
create p = New p
|
||||
|
||||
run :: New v a -> ST s (Mutable v s a)
|
||||
{-# INLINE run #-}
|
||||
run (New p) = p
|
||||
|
||||
runPrim :: PrimMonad m => New v a -> m (Mutable v (PrimState m) a)
|
||||
{-# INLINE runPrim #-}
|
||||
runPrim (New p) = primToPrim p
|
||||
|
||||
apply :: (forall s. Mutable v s a -> Mutable v s a) -> New v a -> New v a
|
||||
{-# INLINE apply #-}
|
||||
apply f (New p) = New (liftM f p)
|
||||
|
||||
modify :: (forall s. Mutable v s a -> ST s ()) -> New v a -> New v a
|
||||
{-# INLINE modify #-}
|
||||
modify f (New p) = New (do { v <- p; f v; return v })
|
||||
|
||||
modifyWithBundle :: (forall s. Mutable v s a -> Bundle u b -> ST s ())
|
||||
-> New v a -> Bundle u b -> New v a
|
||||
{-# INLINE_FUSED modifyWithBundle #-}
|
||||
modifyWithBundle f (New p) s = s `seq` New (do { v <- p; f v s; return v })
|
||||
|
||||
unstream :: Vector v a => Bundle v a -> New v a
|
||||
{-# INLINE_FUSED unstream #-}
|
||||
unstream s = s `seq` New (MVector.vunstream s)
|
||||
|
||||
transform
|
||||
:: Vector v a => (forall m. Monad m => Stream m a -> Stream m a)
|
||||
-> (Size -> Size) -> New v a -> New v a
|
||||
{-# INLINE_FUSED transform #-}
|
||||
transform f _ (New p) = New (MVector.transform f =<< p)
|
||||
|
||||
{-# RULES
|
||||
|
||||
"transform/transform [New]"
|
||||
forall (f1 :: forall m. Monad m => Stream m a -> Stream m a)
|
||||
(f2 :: forall m. Monad m => Stream m a -> Stream m a)
|
||||
g1 g2 p .
|
||||
transform f1 g1 (transform f2 g2 p) = transform (f1 . f2) (g1 . g2) p
|
||||
|
||||
"transform/unstream [New]"
|
||||
forall (f :: forall m. Monad m => Stream m a -> Stream m a)
|
||||
g s.
|
||||
transform f g (unstream s) = unstream (Bundle.inplace f g s) #-}
|
||||
|
||||
|
||||
|
||||
|
||||
unstreamR :: Vector v a => Bundle v a -> New v a
|
||||
{-# INLINE_FUSED unstreamR #-}
|
||||
unstreamR s = s `seq` New (MVector.unstreamR s)
|
||||
|
||||
transformR
|
||||
:: Vector v a => (forall m. Monad m => Stream m a -> Stream m a)
|
||||
-> (Size -> Size) -> New v a -> New v a
|
||||
{-# INLINE_FUSED transformR #-}
|
||||
transformR f _ (New p) = New (MVector.transformR f =<< p)
|
||||
|
||||
{-# RULES
|
||||
|
||||
"transformR/transformR [New]"
|
||||
forall (f1 :: forall m. Monad m => Stream m a -> Stream m a)
|
||||
(f2 :: forall m. Monad m => Stream m a -> Stream m a)
|
||||
g1 g2
|
||||
p .
|
||||
transformR f1 g1 (transformR f2 g2 p) = transformR (f1 . f2) (g1 . g2) p
|
||||
|
||||
"transformR/unstreamR [New]"
|
||||
forall (f :: forall m. Monad m => Stream m a -> Stream m a)
|
||||
g s.
|
||||
transformR f g (unstreamR s) = unstreamR (Bundle.inplace f g s) #-}
|
||||
|
||||
|
||||
|
||||
slice :: Vector v a => Int -> Int -> New v a -> New v a
|
||||
{-# INLINE_FUSED slice #-}
|
||||
slice i n m = apply (MVector.slice i n) m
|
||||
|
||||
init :: Vector v a => New v a -> New v a
|
||||
{-# INLINE_FUSED init #-}
|
||||
init m = apply MVector.init m
|
||||
|
||||
tail :: Vector v a => New v a -> New v a
|
||||
{-# INLINE_FUSED tail #-}
|
||||
tail m = apply MVector.tail m
|
||||
|
||||
take :: Vector v a => Int -> New v a -> New v a
|
||||
{-# INLINE_FUSED take #-}
|
||||
take n m = apply (MVector.take n) m
|
||||
|
||||
drop :: Vector v a => Int -> New v a -> New v a
|
||||
{-# INLINE_FUSED drop #-}
|
||||
drop n m = apply (MVector.drop n) m
|
||||
|
||||
unsafeSlice :: Vector v a => Int -> Int -> New v a -> New v a
|
||||
{-# INLINE_FUSED unsafeSlice #-}
|
||||
unsafeSlice i n m = apply (MVector.unsafeSlice i n) m
|
||||
|
||||
unsafeInit :: Vector v a => New v a -> New v a
|
||||
{-# INLINE_FUSED unsafeInit #-}
|
||||
unsafeInit m = apply MVector.unsafeInit m
|
||||
|
||||
unsafeTail :: Vector v a => New v a -> New v a
|
||||
{-# INLINE_FUSED unsafeTail #-}
|
||||
unsafeTail m = apply MVector.unsafeTail m
|
||||
|
||||
{-# RULES
|
||||
|
||||
"slice/unstream [New]" forall i n s.
|
||||
slice i n (unstream s) = unstream (Bundle.slice i n s)
|
||||
|
||||
"init/unstream [New]" forall s.
|
||||
init (unstream s) = unstream (Bundle.init s)
|
||||
|
||||
"tail/unstream [New]" forall s.
|
||||
tail (unstream s) = unstream (Bundle.tail s)
|
||||
|
||||
"take/unstream [New]" forall n s.
|
||||
take n (unstream s) = unstream (Bundle.take n s)
|
||||
|
||||
"drop/unstream [New]" forall n s.
|
||||
drop n (unstream s) = unstream (Bundle.drop n s)
|
||||
|
||||
"unsafeSlice/unstream [New]" forall i n s.
|
||||
unsafeSlice i n (unstream s) = unstream (Bundle.slice i n s)
|
||||
|
||||
"unsafeInit/unstream [New]" forall s.
|
||||
unsafeInit (unstream s) = unstream (Bundle.init s)
|
||||
|
||||
"unsafeTail/unstream [New]" forall s.
|
||||
unsafeTail (unstream s) = unstream (Bundle.tail s) #-}
|
||||
|
||||
|
||||
|
152
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Internal/Check.hs
vendored
Normal file
152
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Internal/Check.hs
vendored
Normal file
|
@ -0,0 +1,152 @@
|
|||
{-# LANGUAGE CPP #-}
|
||||
|
||||
-- |
|
||||
-- Module : Data.Vector.Internal.Check
|
||||
-- Copyright : (c) Roman Leshchinskiy 2009
|
||||
-- License : BSD-style
|
||||
--
|
||||
-- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
|
||||
-- Stability : experimental
|
||||
-- Portability : non-portable
|
||||
--
|
||||
-- Bounds checking infrastructure
|
||||
--
|
||||
|
||||
{-# LANGUAGE MagicHash #-}
|
||||
|
||||
module Data.Vector.Internal.Check (
|
||||
Checks(..), doChecks,
|
||||
|
||||
error, internalError,
|
||||
check, checkIndex, checkLength, checkSlice
|
||||
) where
|
||||
|
||||
import GHC.Base( Int(..) )
|
||||
import GHC.Prim( Int# )
|
||||
import Prelude hiding( error, (&&), (||), not )
|
||||
import qualified Prelude as P
|
||||
|
||||
-- NOTE: This is a workaround for GHC's weird behaviour where it doesn't inline
|
||||
-- these functions into unfoldings which makes the intermediate code size
|
||||
-- explode. See http://hackage.haskell.org/trac/ghc/ticket/5539.
|
||||
infixr 2 ||
|
||||
infixr 3 &&
|
||||
|
||||
not :: Bool -> Bool
|
||||
{-# INLINE not #-}
|
||||
not True = False
|
||||
not False = True
|
||||
|
||||
(&&) :: Bool -> Bool -> Bool
|
||||
{-# INLINE (&&) #-}
|
||||
False && _ = False
|
||||
True && x = x
|
||||
|
||||
(||) :: Bool -> Bool -> Bool
|
||||
{-# INLINE (||) #-}
|
||||
True || _ = True
|
||||
False || x = x
|
||||
|
||||
|
||||
data Checks = Bounds | Unsafe | Internal deriving( Eq )
|
||||
|
||||
doBoundsChecks :: Bool
|
||||
#ifdef VECTOR_BOUNDS_CHECKS
|
||||
doBoundsChecks = True
|
||||
#else
|
||||
doBoundsChecks = False
|
||||
#endif
|
||||
|
||||
doUnsafeChecks :: Bool
|
||||
#ifdef VECTOR_UNSAFE_CHECKS
|
||||
doUnsafeChecks = True
|
||||
#else
|
||||
doUnsafeChecks = False
|
||||
#endif
|
||||
|
||||
doInternalChecks :: Bool
|
||||
#ifdef VECTOR_INTERNAL_CHECKS
|
||||
doInternalChecks = True
|
||||
#else
|
||||
doInternalChecks = False
|
||||
#endif
|
||||
|
||||
|
||||
doChecks :: Checks -> Bool
|
||||
{-# INLINE doChecks #-}
|
||||
doChecks Bounds = doBoundsChecks
|
||||
doChecks Unsafe = doUnsafeChecks
|
||||
doChecks Internal = doInternalChecks
|
||||
|
||||
error_msg :: String -> Int -> String -> String -> String
|
||||
error_msg file line loc msg = file ++ ":" ++ show line ++ " (" ++ loc ++ "): " ++ msg
|
||||
|
||||
error :: String -> Int -> String -> String -> a
|
||||
{-# NOINLINE error #-}
|
||||
error file line loc msg
|
||||
= P.error $ error_msg file line loc msg
|
||||
|
||||
internalError :: String -> Int -> String -> String -> a
|
||||
{-# NOINLINE internalError #-}
|
||||
internalError file line loc msg
|
||||
= P.error $ unlines
|
||||
["*** Internal error in package vector ***"
|
||||
,"*** Please submit a bug report at http://trac.haskell.org/vector"
|
||||
,error_msg file line loc msg]
|
||||
|
||||
|
||||
checkError :: String -> Int -> Checks -> String -> String -> a
|
||||
{-# NOINLINE checkError #-}
|
||||
checkError file line kind loc msg
|
||||
= case kind of
|
||||
Internal -> internalError file line loc msg
|
||||
_ -> error file line loc msg
|
||||
|
||||
check :: String -> Int -> Checks -> String -> String -> Bool -> a -> a
|
||||
{-# INLINE check #-}
|
||||
check file line kind loc msg cond x
|
||||
| not (doChecks kind) || cond = x
|
||||
| otherwise = checkError file line kind loc msg
|
||||
|
||||
checkIndex_msg :: Int -> Int -> String
|
||||
{-# INLINE checkIndex_msg #-}
|
||||
checkIndex_msg (I# i#) (I# n#) = checkIndex_msg# i# n#
|
||||
|
||||
checkIndex_msg# :: Int# -> Int# -> String
|
||||
{-# NOINLINE checkIndex_msg# #-}
|
||||
checkIndex_msg# i# n# = "index out of bounds " ++ show (I# i#, I# n#)
|
||||
|
||||
checkIndex :: String -> Int -> Checks -> String -> Int -> Int -> a -> a
|
||||
{-# INLINE checkIndex #-}
|
||||
checkIndex file line kind loc i n x
|
||||
= check file line kind loc (checkIndex_msg i n) (i >= 0 && i<n) x
|
||||
|
||||
|
||||
checkLength_msg :: Int -> String
|
||||
{-# INLINE checkLength_msg #-}
|
||||
checkLength_msg (I# n#) = checkLength_msg# n#
|
||||
|
||||
checkLength_msg# :: Int# -> String
|
||||
{-# NOINLINE checkLength_msg# #-}
|
||||
checkLength_msg# n# = "negative length " ++ show (I# n#)
|
||||
|
||||
checkLength :: String -> Int -> Checks -> String -> Int -> a -> a
|
||||
{-# INLINE checkLength #-}
|
||||
checkLength file line kind loc n x
|
||||
= check file line kind loc (checkLength_msg n) (n >= 0) x
|
||||
|
||||
|
||||
checkSlice_msg :: Int -> Int -> Int -> String
|
||||
{-# INLINE checkSlice_msg #-}
|
||||
checkSlice_msg (I# i#) (I# m#) (I# n#) = checkSlice_msg# i# m# n#
|
||||
|
||||
checkSlice_msg# :: Int# -> Int# -> Int# -> String
|
||||
{-# NOINLINE checkSlice_msg# #-}
|
||||
checkSlice_msg# i# m# n# = "invalid slice " ++ show (I# i#, I# m#, I# n#)
|
||||
|
||||
checkSlice :: String -> Int -> Checks -> String -> Int -> Int -> Int -> a -> a
|
||||
{-# INLINE checkSlice #-}
|
||||
checkSlice file line kind loc i m n x
|
||||
= check file line kind loc (checkSlice_msg i m n)
|
||||
(i >= 0 && m >= 0 && i+m <= n) x
|
||||
|
416
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Mutable.hs
vendored
Normal file
416
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Mutable.hs
vendored
Normal file
|
@ -0,0 +1,416 @@
|
|||
{-# LANGUAGE CPP, DeriveDataTypeable, MultiParamTypeClasses, FlexibleInstances, BangPatterns, TypeFamilies #-}
|
||||
|
||||
-- |
|
||||
-- Module : Data.Vector.Mutable
|
||||
-- Copyright : (c) Roman Leshchinskiy 2008-2010
|
||||
-- License : BSD-style
|
||||
--
|
||||
-- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
|
||||
-- Stability : experimental
|
||||
-- Portability : non-portable
|
||||
--
|
||||
-- Mutable boxed vectors.
|
||||
--
|
||||
|
||||
module Data.Vector.Mutable (
|
||||
-- * Mutable boxed vectors
|
||||
MVector(..), IOVector, STVector,
|
||||
|
||||
-- * Accessors
|
||||
|
||||
-- ** Length information
|
||||
length, null,
|
||||
|
||||
-- ** Extracting subvectors
|
||||
slice, init, tail, take, drop, splitAt,
|
||||
unsafeSlice, unsafeInit, unsafeTail, unsafeTake, unsafeDrop,
|
||||
|
||||
-- ** Overlapping
|
||||
overlaps,
|
||||
|
||||
-- * Construction
|
||||
|
||||
-- ** Initialisation
|
||||
new, unsafeNew, replicate, replicateM, clone,
|
||||
|
||||
-- ** Growing
|
||||
grow, unsafeGrow,
|
||||
|
||||
-- ** Restricting memory usage
|
||||
clear,
|
||||
|
||||
-- * Accessing individual elements
|
||||
read, write, modify, swap,
|
||||
unsafeRead, unsafeWrite, unsafeModify, unsafeSwap,
|
||||
|
||||
-- * Modifying vectors
|
||||
nextPermutation,
|
||||
|
||||
-- ** Filling and copying
|
||||
set, copy, move, unsafeCopy, unsafeMove
|
||||
) where
|
||||
|
||||
import Control.Monad (when)
|
||||
import qualified Data.Vector.Generic.Mutable as G
|
||||
import Data.Primitive.Array
|
||||
import Control.Monad.Primitive
|
||||
|
||||
import Prelude hiding ( length, null, replicate, reverse, read,
|
||||
take, drop, splitAt, init, tail )
|
||||
|
||||
import Data.Typeable ( Typeable )
|
||||
|
||||
#include "vector.h"
|
||||
|
||||
-- | Mutable boxed vectors keyed on the monad they live in ('IO' or @'ST' s@).
|
||||
data MVector s a = MVector {-# UNPACK #-} !Int
|
||||
{-# UNPACK #-} !Int
|
||||
{-# UNPACK #-} !(MutableArray s a)
|
||||
deriving ( Typeable )
|
||||
|
||||
type IOVector = MVector RealWorld
|
||||
type STVector s = MVector s
|
||||
|
||||
-- NOTE: This seems unsafe, see http://trac.haskell.org/vector/ticket/54
|
||||
{-
|
||||
instance NFData a => NFData (MVector s a) where
|
||||
rnf (MVector i n arr) = unsafeInlineST $ force i
|
||||
where
|
||||
force !ix | ix < n = do x <- readArray arr ix
|
||||
rnf x `seq` force (ix+1)
|
||||
| otherwise = return ()
|
||||
-}
|
||||
|
||||
instance G.MVector MVector a where
|
||||
{-# INLINE basicLength #-}
|
||||
basicLength (MVector _ n _) = n
|
||||
|
||||
{-# INLINE basicUnsafeSlice #-}
|
||||
basicUnsafeSlice j m (MVector i _ arr) = MVector (i+j) m arr
|
||||
|
||||
{-# INLINE basicOverlaps #-}
|
||||
basicOverlaps (MVector i m arr1) (MVector j n arr2)
|
||||
= sameMutableArray arr1 arr2
|
||||
&& (between i j (j+n) || between j i (i+m))
|
||||
where
|
||||
between x y z = x >= y && x < z
|
||||
|
||||
{-# INLINE basicUnsafeNew #-}
|
||||
basicUnsafeNew n
|
||||
= do
|
||||
arr <- newArray n uninitialised
|
||||
return (MVector 0 n arr)
|
||||
|
||||
{-# INLINE basicInitialize #-}
|
||||
-- initialization is unnecessary for boxed vectors
|
||||
basicInitialize _ = return ()
|
||||
|
||||
{-# INLINE basicUnsafeReplicate #-}
|
||||
basicUnsafeReplicate n x
|
||||
= do
|
||||
arr <- newArray n x
|
||||
return (MVector 0 n arr)
|
||||
|
||||
{-# INLINE basicUnsafeRead #-}
|
||||
basicUnsafeRead (MVector i _ arr) j = readArray arr (i+j)
|
||||
|
||||
{-# INLINE basicUnsafeWrite #-}
|
||||
basicUnsafeWrite (MVector i _ arr) j x = writeArray arr (i+j) x
|
||||
|
||||
{-# INLINE basicUnsafeCopy #-}
|
||||
basicUnsafeCopy (MVector i n dst) (MVector j _ src)
|
||||
= copyMutableArray dst i src j n
|
||||
|
||||
basicUnsafeMove dst@(MVector iDst n arrDst) src@(MVector iSrc _ arrSrc)
|
||||
= case n of
|
||||
0 -> return ()
|
||||
1 -> readArray arrSrc iSrc >>= writeArray arrDst iDst
|
||||
2 -> do
|
||||
x <- readArray arrSrc iSrc
|
||||
y <- readArray arrSrc (iSrc + 1)
|
||||
writeArray arrDst iDst x
|
||||
writeArray arrDst (iDst + 1) y
|
||||
_
|
||||
| overlaps dst src
|
||||
-> case compare iDst iSrc of
|
||||
LT -> moveBackwards arrDst iDst iSrc n
|
||||
EQ -> return ()
|
||||
GT | (iDst - iSrc) * 2 < n
|
||||
-> moveForwardsLargeOverlap arrDst iDst iSrc n
|
||||
| otherwise
|
||||
-> moveForwardsSmallOverlap arrDst iDst iSrc n
|
||||
| otherwise -> G.basicUnsafeCopy dst src
|
||||
|
||||
{-# INLINE basicClear #-}
|
||||
basicClear v = G.set v uninitialised
|
||||
|
||||
{-# INLINE moveBackwards #-}
|
||||
moveBackwards :: PrimMonad m => MutableArray (PrimState m) a -> Int -> Int -> Int -> m ()
|
||||
moveBackwards !arr !dstOff !srcOff !len =
|
||||
INTERNAL_CHECK(check) "moveBackwards" "not a backwards move" (dstOff < srcOff)
|
||||
$ loopM len $ \ i -> readArray arr (srcOff + i) >>= writeArray arr (dstOff + i)
|
||||
|
||||
{-# INLINE moveForwardsSmallOverlap #-}
|
||||
-- Performs a move when dstOff > srcOff, optimized for when the overlap of the intervals is small.
|
||||
moveForwardsSmallOverlap :: PrimMonad m => MutableArray (PrimState m) a -> Int -> Int -> Int -> m ()
|
||||
moveForwardsSmallOverlap !arr !dstOff !srcOff !len =
|
||||
INTERNAL_CHECK(check) "moveForwardsSmallOverlap" "not a forward move" (dstOff > srcOff)
|
||||
$ do
|
||||
tmp <- newArray overlap uninitialised
|
||||
loopM overlap $ \ i -> readArray arr (dstOff + i) >>= writeArray tmp i
|
||||
loopM nonOverlap $ \ i -> readArray arr (srcOff + i) >>= writeArray arr (dstOff + i)
|
||||
loopM overlap $ \ i -> readArray tmp i >>= writeArray arr (dstOff + nonOverlap + i)
|
||||
where nonOverlap = dstOff - srcOff; overlap = len - nonOverlap
|
||||
|
||||
-- Performs a move when dstOff > srcOff, optimized for when the overlap of the intervals is large.
|
||||
moveForwardsLargeOverlap :: PrimMonad m => MutableArray (PrimState m) a -> Int -> Int -> Int -> m ()
|
||||
moveForwardsLargeOverlap !arr !dstOff !srcOff !len =
|
||||
INTERNAL_CHECK(check) "moveForwardsLargeOverlap" "not a forward move" (dstOff > srcOff)
|
||||
$ do
|
||||
queue <- newArray nonOverlap uninitialised
|
||||
loopM nonOverlap $ \ i -> readArray arr (srcOff + i) >>= writeArray queue i
|
||||
let mov !i !qTop = when (i < dstOff + len) $ do
|
||||
x <- readArray arr i
|
||||
y <- readArray queue qTop
|
||||
writeArray arr i y
|
||||
writeArray queue qTop x
|
||||
mov (i+1) (if qTop + 1 >= nonOverlap then 0 else qTop + 1)
|
||||
mov dstOff 0
|
||||
where nonOverlap = dstOff - srcOff
|
||||
|
||||
{-# INLINE loopM #-}
|
||||
loopM :: Monad m => Int -> (Int -> m a) -> m ()
|
||||
loopM !n k = let
|
||||
go i = when (i < n) (k i >> go (i+1))
|
||||
in go 0
|
||||
|
||||
uninitialised :: a
|
||||
uninitialised = error "Data.Vector.Mutable: uninitialised element"
|
||||
|
||||
-- Length information
|
||||
-- ------------------
|
||||
|
||||
-- | Length of the mutable vector.
|
||||
length :: MVector s a -> Int
|
||||
{-# INLINE length #-}
|
||||
length = G.length
|
||||
|
||||
-- | Check whether the vector is empty
|
||||
null :: MVector s a -> Bool
|
||||
{-# INLINE null #-}
|
||||
null = G.null
|
||||
|
||||
-- Extracting subvectors
|
||||
-- ---------------------
|
||||
|
||||
-- | Yield a part of the mutable vector without copying it.
|
||||
slice :: Int -> Int -> MVector s a -> MVector s a
|
||||
{-# INLINE slice #-}
|
||||
slice = G.slice
|
||||
|
||||
take :: Int -> MVector s a -> MVector s a
|
||||
{-# INLINE take #-}
|
||||
take = G.take
|
||||
|
||||
drop :: Int -> MVector s a -> MVector s a
|
||||
{-# INLINE drop #-}
|
||||
drop = G.drop
|
||||
|
||||
{-# INLINE splitAt #-}
|
||||
splitAt :: Int -> MVector s a -> (MVector s a, MVector s a)
|
||||
splitAt = G.splitAt
|
||||
|
||||
init :: MVector s a -> MVector s a
|
||||
{-# INLINE init #-}
|
||||
init = G.init
|
||||
|
||||
tail :: MVector s a -> MVector s a
|
||||
{-# INLINE tail #-}
|
||||
tail = G.tail
|
||||
|
||||
-- | Yield a part of the mutable vector without copying it. No bounds checks
|
||||
-- are performed.
|
||||
unsafeSlice :: Int -- ^ starting index
|
||||
-> Int -- ^ length of the slice
|
||||
-> MVector s a
|
||||
-> MVector s a
|
||||
{-# INLINE unsafeSlice #-}
|
||||
unsafeSlice = G.unsafeSlice
|
||||
|
||||
unsafeTake :: Int -> MVector s a -> MVector s a
|
||||
{-# INLINE unsafeTake #-}
|
||||
unsafeTake = G.unsafeTake
|
||||
|
||||
unsafeDrop :: Int -> MVector s a -> MVector s a
|
||||
{-# INLINE unsafeDrop #-}
|
||||
unsafeDrop = G.unsafeDrop
|
||||
|
||||
unsafeInit :: MVector s a -> MVector s a
|
||||
{-# INLINE unsafeInit #-}
|
||||
unsafeInit = G.unsafeInit
|
||||
|
||||
unsafeTail :: MVector s a -> MVector s a
|
||||
{-# INLINE unsafeTail #-}
|
||||
unsafeTail = G.unsafeTail
|
||||
|
||||
-- Overlapping
|
||||
-- -----------
|
||||
|
||||
-- | Check whether two vectors overlap.
|
||||
overlaps :: MVector s a -> MVector s a -> Bool
|
||||
{-# INLINE overlaps #-}
|
||||
overlaps = G.overlaps
|
||||
|
||||
-- Initialisation
|
||||
-- --------------
|
||||
|
||||
-- | Create a mutable vector of the given length.
|
||||
new :: PrimMonad m => Int -> m (MVector (PrimState m) a)
|
||||
{-# INLINE new #-}
|
||||
new = G.new
|
||||
|
||||
-- | Create a mutable vector of the given length. The memory is not initialized.
|
||||
unsafeNew :: PrimMonad m => Int -> m (MVector (PrimState m) a)
|
||||
{-# INLINE unsafeNew #-}
|
||||
unsafeNew = G.unsafeNew
|
||||
|
||||
-- | Create a mutable vector of the given length (0 if the length is negative)
|
||||
-- and fill it with an initial value.
|
||||
replicate :: PrimMonad m => Int -> a -> m (MVector (PrimState m) a)
|
||||
{-# INLINE replicate #-}
|
||||
replicate = G.replicate
|
||||
|
||||
-- | Create a mutable vector of the given length (0 if the length is negative)
|
||||
-- and fill it with values produced by repeatedly executing the monadic action.
|
||||
replicateM :: PrimMonad m => Int -> m a -> m (MVector (PrimState m) a)
|
||||
{-# INLINE replicateM #-}
|
||||
replicateM = G.replicateM
|
||||
|
||||
-- | Create a copy of a mutable vector.
|
||||
clone :: PrimMonad m => MVector (PrimState m) a -> m (MVector (PrimState m) a)
|
||||
{-# INLINE clone #-}
|
||||
clone = G.clone
|
||||
|
||||
-- Growing
|
||||
-- -------
|
||||
|
||||
-- | Grow a vector by the given number of elements. The number must be
|
||||
-- positive.
|
||||
grow :: PrimMonad m
|
||||
=> MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
|
||||
{-# INLINE grow #-}
|
||||
grow = G.grow
|
||||
|
||||
-- | Grow a vector by the given number of elements. The number must be
|
||||
-- positive but this is not checked.
|
||||
unsafeGrow :: PrimMonad m
|
||||
=> MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
|
||||
{-# INLINE unsafeGrow #-}
|
||||
unsafeGrow = G.unsafeGrow
|
||||
|
||||
-- Restricting memory usage
|
||||
-- ------------------------
|
||||
|
||||
-- | Reset all elements of the vector to some undefined value, clearing all
|
||||
-- references to external objects. This is usually a noop for unboxed vectors.
|
||||
clear :: PrimMonad m => MVector (PrimState m) a -> m ()
|
||||
{-# INLINE clear #-}
|
||||
clear = G.clear
|
||||
|
||||
-- Accessing individual elements
|
||||
-- -----------------------------
|
||||
|
||||
-- | Yield the element at the given position.
|
||||
read :: PrimMonad m => MVector (PrimState m) a -> Int -> m a
|
||||
{-# INLINE read #-}
|
||||
read = G.read
|
||||
|
||||
-- | Replace the element at the given position.
|
||||
write :: PrimMonad m => MVector (PrimState m) a -> Int -> a -> m ()
|
||||
{-# INLINE write #-}
|
||||
write = G.write
|
||||
|
||||
-- | Modify the element at the given position.
|
||||
modify :: PrimMonad m => MVector (PrimState m) a -> (a -> a) -> Int -> m ()
|
||||
{-# INLINE modify #-}
|
||||
modify = G.modify
|
||||
|
||||
-- | Swap the elements at the given positions.
|
||||
swap :: PrimMonad m => MVector (PrimState m) a -> Int -> Int -> m ()
|
||||
{-# INLINE swap #-}
|
||||
swap = G.swap
|
||||
|
||||
|
||||
-- | Yield the element at the given position. No bounds checks are performed.
|
||||
unsafeRead :: PrimMonad m => MVector (PrimState m) a -> Int -> m a
|
||||
{-# INLINE unsafeRead #-}
|
||||
unsafeRead = G.unsafeRead
|
||||
|
||||
-- | Replace the element at the given position. No bounds checks are performed.
|
||||
unsafeWrite :: PrimMonad m => MVector (PrimState m) a -> Int -> a -> m ()
|
||||
{-# INLINE unsafeWrite #-}
|
||||
unsafeWrite = G.unsafeWrite
|
||||
|
||||
-- | Modify the element at the given position. No bounds checks are performed.
|
||||
unsafeModify :: PrimMonad m => MVector (PrimState m) a -> (a -> a) -> Int -> m ()
|
||||
{-# INLINE unsafeModify #-}
|
||||
unsafeModify = G.unsafeModify
|
||||
|
||||
-- | Swap the elements at the given positions. No bounds checks are performed.
|
||||
unsafeSwap :: PrimMonad m => MVector (PrimState m) a -> Int -> Int -> m ()
|
||||
{-# INLINE unsafeSwap #-}
|
||||
unsafeSwap = G.unsafeSwap
|
||||
|
||||
-- Filling and copying
|
||||
-- -------------------
|
||||
|
||||
-- | Set all elements of the vector to the given value.
|
||||
set :: PrimMonad m => MVector (PrimState m) a -> a -> m ()
|
||||
{-# INLINE set #-}
|
||||
set = G.set
|
||||
|
||||
-- | Copy a vector. The two vectors must have the same length and may not
|
||||
-- overlap.
|
||||
copy :: PrimMonad m
|
||||
=> MVector (PrimState m) a -> MVector (PrimState m) a -> m ()
|
||||
{-# INLINE copy #-}
|
||||
copy = G.copy
|
||||
|
||||
-- | Copy a vector. The two vectors must have the same length and may not
|
||||
-- overlap. This is not checked.
|
||||
unsafeCopy :: PrimMonad m => MVector (PrimState m) a -- ^ target
|
||||
-> MVector (PrimState m) a -- ^ source
|
||||
-> m ()
|
||||
{-# INLINE unsafeCopy #-}
|
||||
unsafeCopy = G.unsafeCopy
|
||||
|
||||
-- | Move the contents of a vector. The two vectors must have the same
|
||||
-- length.
|
||||
--
|
||||
-- If the vectors do not overlap, then this is equivalent to 'copy'.
|
||||
-- Otherwise, the copying is performed as if the source vector were
|
||||
-- copied to a temporary vector and then the temporary vector was copied
|
||||
-- to the target vector.
|
||||
move :: PrimMonad m
|
||||
=> MVector (PrimState m) a -> MVector (PrimState m) a -> m ()
|
||||
{-# INLINE move #-}
|
||||
move = G.move
|
||||
|
||||
-- | Move the contents of a vector. The two vectors must have the same
|
||||
-- length, but this is not checked.
|
||||
--
|
||||
-- If the vectors do not overlap, then this is equivalent to 'unsafeCopy'.
|
||||
-- Otherwise, the copying is performed as if the source vector were
|
||||
-- copied to a temporary vector and then the temporary vector was copied
|
||||
-- to the target vector.
|
||||
unsafeMove :: PrimMonad m => MVector (PrimState m) a -- ^ target
|
||||
-> MVector (PrimState m) a -- ^ source
|
||||
-> m ()
|
||||
{-# INLINE unsafeMove #-}
|
||||
unsafeMove = G.unsafeMove
|
||||
|
||||
-- | Compute the next (lexicographically) permutation of given vector in-place.
|
||||
-- Returns False when input is the last permtuation
|
||||
nextPermutation :: (PrimMonad m,Ord e) => MVector (PrimState m) e -> m Bool
|
||||
{-# INLINE nextPermutation #-}
|
||||
nextPermutation = G.nextPermutation
|
1393
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Primitive.hs
vendored
Normal file
1393
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Primitive.hs
vendored
Normal file
File diff suppressed because it is too large
Load diff
366
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Primitive/Mutable.hs
vendored
Normal file
366
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Primitive/Mutable.hs
vendored
Normal file
|
@ -0,0 +1,366 @@
|
|||
{-# LANGUAGE CPP, DeriveDataTypeable, MultiParamTypeClasses, FlexibleInstances, ScopedTypeVariables #-}
|
||||
|
||||
-- |
|
||||
-- Module : Data.Vector.Primitive.Mutable
|
||||
-- Copyright : (c) Roman Leshchinskiy 2008-2010
|
||||
-- License : BSD-style
|
||||
--
|
||||
-- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
|
||||
-- Stability : experimental
|
||||
-- Portability : non-portable
|
||||
--
|
||||
-- Mutable primitive vectors.
|
||||
--
|
||||
|
||||
module Data.Vector.Primitive.Mutable (
|
||||
-- * Mutable vectors of primitive types
|
||||
MVector(..), IOVector, STVector, Prim,
|
||||
|
||||
-- * Accessors
|
||||
|
||||
-- ** Length information
|
||||
length, null,
|
||||
|
||||
-- ** Extracting subvectors
|
||||
slice, init, tail, take, drop, splitAt,
|
||||
unsafeSlice, unsafeInit, unsafeTail, unsafeTake, unsafeDrop,
|
||||
|
||||
-- ** Overlapping
|
||||
overlaps,
|
||||
|
||||
-- * Construction
|
||||
|
||||
-- ** Initialisation
|
||||
new, unsafeNew, replicate, replicateM, clone,
|
||||
|
||||
-- ** Growing
|
||||
grow, unsafeGrow,
|
||||
|
||||
-- ** Restricting memory usage
|
||||
clear,
|
||||
|
||||
-- * Accessing individual elements
|
||||
read, write, modify, swap,
|
||||
unsafeRead, unsafeWrite, unsafeModify, unsafeSwap,
|
||||
|
||||
-- * Modifying vectors
|
||||
nextPermutation,
|
||||
|
||||
-- ** Filling and copying
|
||||
set, copy, move, unsafeCopy, unsafeMove
|
||||
) where
|
||||
|
||||
import qualified Data.Vector.Generic.Mutable as G
|
||||
import Data.Primitive.ByteArray
|
||||
import Data.Primitive ( Prim, sizeOf )
|
||||
import Data.Word ( Word8 )
|
||||
import Control.Monad.Primitive
|
||||
import Control.Monad ( liftM )
|
||||
|
||||
import Control.DeepSeq ( NFData(rnf) )
|
||||
|
||||
import Prelude hiding ( length, null, replicate, reverse, map, read,
|
||||
take, drop, splitAt, init, tail )
|
||||
|
||||
import Data.Typeable ( Typeable )
|
||||
|
||||
-- Data.Vector.Internal.Check is unnecessary
|
||||
#define NOT_VECTOR_MODULE
|
||||
#include "vector.h"
|
||||
|
||||
-- | Mutable vectors of primitive types.
|
||||
data MVector s a = MVector {-# UNPACK #-} !Int
|
||||
{-# UNPACK #-} !Int
|
||||
{-# UNPACK #-} !(MutableByteArray s) -- ^ offset, length, underlying mutable byte array
|
||||
deriving ( Typeable )
|
||||
|
||||
type IOVector = MVector RealWorld
|
||||
type STVector s = MVector s
|
||||
|
||||
instance NFData (MVector s a) where
|
||||
rnf (MVector _ _ _) = ()
|
||||
|
||||
instance Prim a => G.MVector MVector a where
|
||||
basicLength (MVector _ n _) = n
|
||||
basicUnsafeSlice j m (MVector i _ arr)
|
||||
= MVector (i+j) m arr
|
||||
|
||||
{-# INLINE basicOverlaps #-}
|
||||
basicOverlaps (MVector i m arr1) (MVector j n arr2)
|
||||
= sameMutableByteArray arr1 arr2
|
||||
&& (between i j (j+n) || between j i (i+m))
|
||||
where
|
||||
between x y z = x >= y && x < z
|
||||
|
||||
{-# INLINE basicUnsafeNew #-}
|
||||
basicUnsafeNew n
|
||||
| n < 0 = error $ "Primitive.basicUnsafeNew: negative length: " ++ show n
|
||||
| n > mx = error $ "Primitive.basicUnsafeNew: length to large: " ++ show n
|
||||
| otherwise = MVector 0 n `liftM` newByteArray (n * size)
|
||||
where
|
||||
size = sizeOf (undefined :: a)
|
||||
mx = maxBound `div` size :: Int
|
||||
|
||||
{-# INLINE basicInitialize #-}
|
||||
basicInitialize (MVector off n v) =
|
||||
setByteArray v (off * size) (n * size) (0 :: Word8)
|
||||
where
|
||||
size = sizeOf (undefined :: a)
|
||||
|
||||
|
||||
{-# INLINE basicUnsafeRead #-}
|
||||
basicUnsafeRead (MVector i _ arr) j = readByteArray arr (i+j)
|
||||
|
||||
{-# INLINE basicUnsafeWrite #-}
|
||||
basicUnsafeWrite (MVector i _ arr) j x = writeByteArray arr (i+j) x
|
||||
|
||||
{-# INLINE basicUnsafeCopy #-}
|
||||
basicUnsafeCopy (MVector i n dst) (MVector j _ src)
|
||||
= copyMutableByteArray dst (i*sz) src (j*sz) (n*sz)
|
||||
where
|
||||
sz = sizeOf (undefined :: a)
|
||||
|
||||
{-# INLINE basicUnsafeMove #-}
|
||||
basicUnsafeMove (MVector i n dst) (MVector j _ src)
|
||||
= moveByteArray dst (i*sz) src (j*sz) (n * sz)
|
||||
where
|
||||
sz = sizeOf (undefined :: a)
|
||||
|
||||
{-# INLINE basicSet #-}
|
||||
basicSet (MVector i n arr) x = setByteArray arr i n x
|
||||
|
||||
-- Length information
|
||||
-- ------------------
|
||||
|
||||
-- | Length of the mutable vector.
|
||||
length :: Prim a => MVector s a -> Int
|
||||
{-# INLINE length #-}
|
||||
length = G.length
|
||||
|
||||
-- | Check whether the vector is empty
|
||||
null :: Prim a => MVector s a -> Bool
|
||||
{-# INLINE null #-}
|
||||
null = G.null
|
||||
|
||||
-- Extracting subvectors
|
||||
-- ---------------------
|
||||
|
||||
-- | Yield a part of the mutable vector without copying it.
|
||||
slice :: Prim a => Int -> Int -> MVector s a -> MVector s a
|
||||
{-# INLINE slice #-}
|
||||
slice = G.slice
|
||||
|
||||
take :: Prim a => Int -> MVector s a -> MVector s a
|
||||
{-# INLINE take #-}
|
||||
take = G.take
|
||||
|
||||
drop :: Prim a => Int -> MVector s a -> MVector s a
|
||||
{-# INLINE drop #-}
|
||||
drop = G.drop
|
||||
|
||||
splitAt :: Prim a => Int -> MVector s a -> (MVector s a, MVector s a)
|
||||
{-# INLINE splitAt #-}
|
||||
splitAt = G.splitAt
|
||||
|
||||
init :: Prim a => MVector s a -> MVector s a
|
||||
{-# INLINE init #-}
|
||||
init = G.init
|
||||
|
||||
tail :: Prim a => MVector s a -> MVector s a
|
||||
{-# INLINE tail #-}
|
||||
tail = G.tail
|
||||
|
||||
-- | Yield a part of the mutable vector without copying it. No bounds checks
|
||||
-- are performed.
|
||||
unsafeSlice :: Prim a
|
||||
=> Int -- ^ starting index
|
||||
-> Int -- ^ length of the slice
|
||||
-> MVector s a
|
||||
-> MVector s a
|
||||
{-# INLINE unsafeSlice #-}
|
||||
unsafeSlice = G.unsafeSlice
|
||||
|
||||
unsafeTake :: Prim a => Int -> MVector s a -> MVector s a
|
||||
{-# INLINE unsafeTake #-}
|
||||
unsafeTake = G.unsafeTake
|
||||
|
||||
unsafeDrop :: Prim a => Int -> MVector s a -> MVector s a
|
||||
{-# INLINE unsafeDrop #-}
|
||||
unsafeDrop = G.unsafeDrop
|
||||
|
||||
unsafeInit :: Prim a => MVector s a -> MVector s a
|
||||
{-# INLINE unsafeInit #-}
|
||||
unsafeInit = G.unsafeInit
|
||||
|
||||
unsafeTail :: Prim a => MVector s a -> MVector s a
|
||||
{-# INLINE unsafeTail #-}
|
||||
unsafeTail = G.unsafeTail
|
||||
|
||||
-- Overlapping
|
||||
-- -----------
|
||||
|
||||
-- | Check whether two vectors overlap.
|
||||
overlaps :: Prim a => MVector s a -> MVector s a -> Bool
|
||||
{-# INLINE overlaps #-}
|
||||
overlaps = G.overlaps
|
||||
|
||||
-- Initialisation
|
||||
-- --------------
|
||||
|
||||
-- | Create a mutable vector of the given length.
|
||||
new :: (PrimMonad m, Prim a) => Int -> m (MVector (PrimState m) a)
|
||||
{-# INLINE new #-}
|
||||
new = G.new
|
||||
|
||||
-- | Create a mutable vector of the given length. The memory is not initialized.
|
||||
unsafeNew :: (PrimMonad m, Prim a) => Int -> m (MVector (PrimState m) a)
|
||||
{-# INLINE unsafeNew #-}
|
||||
unsafeNew = G.unsafeNew
|
||||
|
||||
-- | Create a mutable vector of the given length (0 if the length is negative)
|
||||
-- and fill it with an initial value.
|
||||
replicate :: (PrimMonad m, Prim a) => Int -> a -> m (MVector (PrimState m) a)
|
||||
{-# INLINE replicate #-}
|
||||
replicate = G.replicate
|
||||
|
||||
-- | Create a mutable vector of the given length (0 if the length is negative)
|
||||
-- and fill it with values produced by repeatedly executing the monadic action.
|
||||
replicateM :: (PrimMonad m, Prim a) => Int -> m a -> m (MVector (PrimState m) a)
|
||||
{-# INLINE replicateM #-}
|
||||
replicateM = G.replicateM
|
||||
|
||||
-- | Create a copy of a mutable vector.
|
||||
clone :: (PrimMonad m, Prim a)
|
||||
=> MVector (PrimState m) a -> m (MVector (PrimState m) a)
|
||||
{-# INLINE clone #-}
|
||||
clone = G.clone
|
||||
|
||||
-- Growing
|
||||
-- -------
|
||||
|
||||
-- | Grow a vector by the given number of elements. The number must be
|
||||
-- positive.
|
||||
grow :: (PrimMonad m, Prim a)
|
||||
=> MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
|
||||
{-# INLINE grow #-}
|
||||
grow = G.grow
|
||||
|
||||
-- | Grow a vector by the given number of elements. The number must be
|
||||
-- positive but this is not checked.
|
||||
unsafeGrow :: (PrimMonad m, Prim a)
|
||||
=> MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
|
||||
{-# INLINE unsafeGrow #-}
|
||||
unsafeGrow = G.unsafeGrow
|
||||
|
||||
-- Restricting memory usage
|
||||
-- ------------------------
|
||||
|
||||
-- | Reset all elements of the vector to some undefined value, clearing all
|
||||
-- references to external objects. This is usually a noop for unboxed vectors.
|
||||
clear :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> m ()
|
||||
{-# INLINE clear #-}
|
||||
clear = G.clear
|
||||
|
||||
-- Accessing individual elements
|
||||
-- -----------------------------
|
||||
|
||||
-- | Yield the element at the given position.
|
||||
read :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> m a
|
||||
{-# INLINE read #-}
|
||||
read = G.read
|
||||
|
||||
-- | Replace the element at the given position.
|
||||
write :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> a -> m ()
|
||||
{-# INLINE write #-}
|
||||
write = G.write
|
||||
|
||||
-- | Modify the element at the given position.
|
||||
modify :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> (a -> a) -> Int -> m ()
|
||||
{-# INLINE modify #-}
|
||||
modify = G.modify
|
||||
|
||||
-- | Swap the elements at the given positions.
|
||||
swap :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> Int -> m ()
|
||||
{-# INLINE swap #-}
|
||||
swap = G.swap
|
||||
|
||||
|
||||
-- | Yield the element at the given position. No bounds checks are performed.
|
||||
unsafeRead :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> m a
|
||||
{-# INLINE unsafeRead #-}
|
||||
unsafeRead = G.unsafeRead
|
||||
|
||||
-- | Replace the element at the given position. No bounds checks are performed.
|
||||
unsafeWrite
|
||||
:: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> a -> m ()
|
||||
{-# INLINE unsafeWrite #-}
|
||||
unsafeWrite = G.unsafeWrite
|
||||
|
||||
-- | Modify the element at the given position. No bounds checks are performed.
|
||||
unsafeModify :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> (a -> a) -> Int -> m ()
|
||||
{-# INLINE unsafeModify #-}
|
||||
unsafeModify = G.unsafeModify
|
||||
|
||||
-- | Swap the elements at the given positions. No bounds checks are performed.
|
||||
unsafeSwap
|
||||
:: (PrimMonad m, Prim a) => MVector (PrimState m) a -> Int -> Int -> m ()
|
||||
{-# INLINE unsafeSwap #-}
|
||||
unsafeSwap = G.unsafeSwap
|
||||
|
||||
-- Filling and copying
|
||||
-- -------------------
|
||||
|
||||
-- | Set all elements of the vector to the given value.
|
||||
set :: (PrimMonad m, Prim a) => MVector (PrimState m) a -> a -> m ()
|
||||
{-# INLINE set #-}
|
||||
set = G.set
|
||||
|
||||
-- | Copy a vector. The two vectors must have the same length and may not
|
||||
-- overlap.
|
||||
copy :: (PrimMonad m, Prim a)
|
||||
=> MVector (PrimState m) a -- ^ target
|
||||
-> MVector (PrimState m) a -- ^ source
|
||||
-> m ()
|
||||
{-# INLINE copy #-}
|
||||
copy = G.copy
|
||||
|
||||
-- | Copy a vector. The two vectors must have the same length and may not
|
||||
-- overlap. This is not checked.
|
||||
unsafeCopy :: (PrimMonad m, Prim a)
|
||||
=> MVector (PrimState m) a -- ^ target
|
||||
-> MVector (PrimState m) a -- ^ source
|
||||
-> m ()
|
||||
{-# INLINE unsafeCopy #-}
|
||||
unsafeCopy = G.unsafeCopy
|
||||
|
||||
-- | Move the contents of a vector. The two vectors must have the same
|
||||
-- length.
|
||||
--
|
||||
-- If the vectors do not overlap, then this is equivalent to 'copy'.
|
||||
-- Otherwise, the copying is performed as if the source vector were
|
||||
-- copied to a temporary vector and then the temporary vector was copied
|
||||
-- to the target vector.
|
||||
move :: (PrimMonad m, Prim a)
|
||||
=> MVector (PrimState m) a -> MVector (PrimState m) a -> m ()
|
||||
{-# INLINE move #-}
|
||||
move = G.move
|
||||
|
||||
-- | Move the contents of a vector. The two vectors must have the same
|
||||
-- length, but this is not checked.
|
||||
--
|
||||
-- If the vectors do not overlap, then this is equivalent to 'unsafeCopy'.
|
||||
-- Otherwise, the copying is performed as if the source vector were
|
||||
-- copied to a temporary vector and then the temporary vector was copied
|
||||
-- to the target vector.
|
||||
unsafeMove :: (PrimMonad m, Prim a)
|
||||
=> MVector (PrimState m) a -- ^ target
|
||||
-> MVector (PrimState m) a -- ^ source
|
||||
-> m ()
|
||||
{-# INLINE unsafeMove #-}
|
||||
unsafeMove = G.unsafeMove
|
||||
|
||||
-- | Compute the next (lexicographically) permutation of given vector in-place.
|
||||
-- Returns False when input is the last permtuation
|
||||
nextPermutation :: (PrimMonad m,Ord e,Prim e) => MVector (PrimState m) e -> m Bool
|
||||
{-# INLINE nextPermutation #-}
|
||||
nextPermutation = G.nextPermutation
|
1489
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Storable.hs
vendored
Normal file
1489
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Storable.hs
vendored
Normal file
File diff suppressed because it is too large
Load diff
33
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Storable/Internal.hs
vendored
Normal file
33
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Storable/Internal.hs
vendored
Normal file
|
@ -0,0 +1,33 @@
|
|||
-- |
|
||||
-- Module : Data.Vector.Storable.Internal
|
||||
-- Copyright : (c) Roman Leshchinskiy 2009-2010
|
||||
-- License : BSD-style
|
||||
--
|
||||
-- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
|
||||
-- Stability : experimental
|
||||
-- Portability : non-portable
|
||||
--
|
||||
-- Ugly internal utility functions for implementing 'Storable'-based vectors.
|
||||
--
|
||||
|
||||
module Data.Vector.Storable.Internal (
|
||||
getPtr, setPtr, updPtr
|
||||
) where
|
||||
|
||||
import Foreign.ForeignPtr
|
||||
import Foreign.Ptr
|
||||
import GHC.ForeignPtr ( ForeignPtr(..) )
|
||||
import GHC.Ptr ( Ptr(..) )
|
||||
|
||||
getPtr :: ForeignPtr a -> Ptr a
|
||||
{-# INLINE getPtr #-}
|
||||
getPtr (ForeignPtr addr _) = Ptr addr
|
||||
|
||||
setPtr :: ForeignPtr a -> Ptr a -> ForeignPtr a
|
||||
{-# INLINE setPtr #-}
|
||||
setPtr (ForeignPtr _ c) (Ptr addr) = ForeignPtr addr c
|
||||
|
||||
updPtr :: (Ptr a -> Ptr a) -> ForeignPtr a -> ForeignPtr a
|
||||
{-# INLINE updPtr #-}
|
||||
updPtr f (ForeignPtr p c) = case f (Ptr p) of { Ptr q -> ForeignPtr q c }
|
||||
|
543
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Storable/Mutable.hs
vendored
Normal file
543
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Storable/Mutable.hs
vendored
Normal file
|
@ -0,0 +1,543 @@
|
|||
{-# LANGUAGE CPP, DeriveDataTypeable, FlexibleInstances, MagicHash, MultiParamTypeClasses, ScopedTypeVariables #-}
|
||||
|
||||
-- |
|
||||
-- Module : Data.Vector.Storable.Mutable
|
||||
-- Copyright : (c) Roman Leshchinskiy 2009-2010
|
||||
-- License : BSD-style
|
||||
--
|
||||
-- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
|
||||
-- Stability : experimental
|
||||
-- Portability : non-portable
|
||||
--
|
||||
-- Mutable vectors based on Storable.
|
||||
--
|
||||
|
||||
module Data.Vector.Storable.Mutable(
|
||||
-- * Mutable vectors of 'Storable' types
|
||||
MVector(..), IOVector, STVector, Storable,
|
||||
|
||||
-- * Accessors
|
||||
|
||||
-- ** Length information
|
||||
length, null,
|
||||
|
||||
-- ** Extracting subvectors
|
||||
slice, init, tail, take, drop, splitAt,
|
||||
unsafeSlice, unsafeInit, unsafeTail, unsafeTake, unsafeDrop,
|
||||
|
||||
-- ** Overlapping
|
||||
overlaps,
|
||||
|
||||
-- * Construction
|
||||
|
||||
-- ** Initialisation
|
||||
new, unsafeNew, replicate, replicateM, clone,
|
||||
|
||||
-- ** Growing
|
||||
grow, unsafeGrow,
|
||||
|
||||
-- ** Restricting memory usage
|
||||
clear,
|
||||
|
||||
-- * Accessing individual elements
|
||||
read, write, modify, swap,
|
||||
unsafeRead, unsafeWrite, unsafeModify, unsafeSwap,
|
||||
|
||||
-- * Modifying vectors
|
||||
|
||||
-- ** Filling and copying
|
||||
set, copy, move, unsafeCopy, unsafeMove,
|
||||
|
||||
-- * Unsafe conversions
|
||||
unsafeCast,
|
||||
|
||||
-- * Raw pointers
|
||||
unsafeFromForeignPtr, unsafeFromForeignPtr0,
|
||||
unsafeToForeignPtr, unsafeToForeignPtr0,
|
||||
unsafeWith
|
||||
) where
|
||||
|
||||
import Control.DeepSeq ( NFData(rnf) )
|
||||
|
||||
import qualified Data.Vector.Generic.Mutable as G
|
||||
import Data.Vector.Storable.Internal
|
||||
|
||||
import Foreign.Storable
|
||||
import Foreign.ForeignPtr
|
||||
|
||||
#if __GLASGOW_HASKELL__ >= 706
|
||||
import GHC.ForeignPtr (mallocPlainForeignPtrAlignedBytes)
|
||||
#elif __GLASGOW_HASKELL__ >= 700
|
||||
import Data.Primitive.ByteArray (MutableByteArray(..), newAlignedPinnedByteArray,
|
||||
unsafeFreezeByteArray)
|
||||
import GHC.Prim (byteArrayContents#, unsafeCoerce#)
|
||||
import GHC.ForeignPtr
|
||||
#endif
|
||||
|
||||
import Foreign.Ptr
|
||||
import Foreign.Marshal.Array ( advancePtr, copyArray, moveArray )
|
||||
|
||||
import Control.Monad.Primitive
|
||||
import Data.Primitive.Addr
|
||||
import Data.Primitive.Types (Prim)
|
||||
|
||||
import GHC.Word (Word8, Word16, Word32, Word64)
|
||||
import GHC.Ptr (Ptr(..))
|
||||
|
||||
import Prelude hiding ( length, null, replicate, reverse, map, read,
|
||||
take, drop, splitAt, init, tail )
|
||||
|
||||
import Data.Typeable ( Typeable )
|
||||
|
||||
-- Data.Vector.Internal.Check is not needed
|
||||
#define NOT_VECTOR_MODULE
|
||||
#include "vector.h"
|
||||
|
||||
-- | Mutable 'Storable'-based vectors
|
||||
data MVector s a = MVector {-# UNPACK #-} !Int
|
||||
{-# UNPACK #-} !(ForeignPtr a)
|
||||
deriving ( Typeable )
|
||||
|
||||
type IOVector = MVector RealWorld
|
||||
type STVector s = MVector s
|
||||
|
||||
instance NFData (MVector s a) where
|
||||
rnf (MVector _ _) = ()
|
||||
|
||||
instance Storable a => G.MVector MVector a where
|
||||
{-# INLINE basicLength #-}
|
||||
basicLength (MVector n _) = n
|
||||
|
||||
{-# INLINE basicUnsafeSlice #-}
|
||||
basicUnsafeSlice j m (MVector _ fp) = MVector m (updPtr (`advancePtr` j) fp)
|
||||
|
||||
-- FIXME: this relies on non-portable pointer comparisons
|
||||
{-# INLINE basicOverlaps #-}
|
||||
basicOverlaps (MVector m fp) (MVector n fq)
|
||||
= between p q (q `advancePtr` n) || between q p (p `advancePtr` m)
|
||||
where
|
||||
between x y z = x >= y && x < z
|
||||
p = getPtr fp
|
||||
q = getPtr fq
|
||||
|
||||
{-# INLINE basicUnsafeNew #-}
|
||||
basicUnsafeNew n
|
||||
| n < 0 = error $ "Storable.basicUnsafeNew: negative length: " ++ show n
|
||||
| n > mx = error $ "Storable.basicUnsafeNew: length too large: " ++ show n
|
||||
| otherwise = unsafePrimToPrim $ do
|
||||
fp <- mallocVector n
|
||||
return $ MVector n fp
|
||||
where
|
||||
size = sizeOf (undefined :: a)
|
||||
mx = maxBound `quot` size :: Int
|
||||
|
||||
{-# INLINE basicInitialize #-}
|
||||
basicInitialize = storableZero
|
||||
|
||||
{-# INLINE basicUnsafeRead #-}
|
||||
basicUnsafeRead (MVector _ fp) i
|
||||
= unsafePrimToPrim
|
||||
$ withForeignPtr fp (`peekElemOff` i)
|
||||
|
||||
{-# INLINE basicUnsafeWrite #-}
|
||||
basicUnsafeWrite (MVector _ fp) i x
|
||||
= unsafePrimToPrim
|
||||
$ withForeignPtr fp $ \p -> pokeElemOff p i x
|
||||
|
||||
{-# INLINE basicSet #-}
|
||||
basicSet = storableSet
|
||||
|
||||
{-# INLINE basicUnsafeCopy #-}
|
||||
basicUnsafeCopy (MVector n fp) (MVector _ fq)
|
||||
= unsafePrimToPrim
|
||||
$ withForeignPtr fp $ \p ->
|
||||
withForeignPtr fq $ \q ->
|
||||
copyArray p q n
|
||||
|
||||
{-# INLINE basicUnsafeMove #-}
|
||||
basicUnsafeMove (MVector n fp) (MVector _ fq)
|
||||
= unsafePrimToPrim
|
||||
$ withForeignPtr fp $ \p ->
|
||||
withForeignPtr fq $ \q ->
|
||||
moveArray p q n
|
||||
|
||||
storableZero :: forall a m. (Storable a, PrimMonad m) => MVector (PrimState m) a -> m ()
|
||||
{-# INLINE storableZero #-}
|
||||
storableZero (MVector n fp) = unsafePrimToPrim . withForeignPtr fp $ \(Ptr p) -> do
|
||||
let q = Addr p
|
||||
setAddr q byteSize (0 :: Word8)
|
||||
where
|
||||
x :: a
|
||||
x = undefined
|
||||
|
||||
byteSize :: Int
|
||||
byteSize = n * sizeOf x
|
||||
|
||||
storableSet :: (Storable a, PrimMonad m) => MVector (PrimState m) a -> a -> m ()
|
||||
{-# INLINE storableSet #-}
|
||||
storableSet (MVector n fp) x
|
||||
| n == 0 = return ()
|
||||
| otherwise = unsafePrimToPrim $
|
||||
case sizeOf x of
|
||||
1 -> storableSetAsPrim n fp x (undefined :: Word8)
|
||||
2 -> storableSetAsPrim n fp x (undefined :: Word16)
|
||||
4 -> storableSetAsPrim n fp x (undefined :: Word32)
|
||||
8 -> storableSetAsPrim n fp x (undefined :: Word64)
|
||||
_ -> withForeignPtr fp $ \p -> do
|
||||
poke p x
|
||||
|
||||
let do_set i
|
||||
| 2*i < n = do
|
||||
copyArray (p `advancePtr` i) p i
|
||||
do_set (2*i)
|
||||
| otherwise = copyArray (p `advancePtr` i) p (n-i)
|
||||
|
||||
do_set 1
|
||||
|
||||
storableSetAsPrim
|
||||
:: (Storable a, Prim b) => Int -> ForeignPtr a -> a -> b -> IO ()
|
||||
{-# INLINE [0] storableSetAsPrim #-}
|
||||
storableSetAsPrim n fp x y = withForeignPtr fp $ \(Ptr p) -> do
|
||||
poke (Ptr p) x
|
||||
let q = Addr p
|
||||
w <- readOffAddr q 0
|
||||
setAddr (q `plusAddr` sizeOf x) (n-1) (w `asTypeOf` y)
|
||||
|
||||
{-# INLINE mallocVector #-}
|
||||
mallocVector :: Storable a => Int -> IO (ForeignPtr a)
|
||||
mallocVector =
|
||||
#if __GLASGOW_HASKELL__ >= 706
|
||||
doMalloc undefined
|
||||
where
|
||||
doMalloc :: Storable b => b -> Int -> IO (ForeignPtr b)
|
||||
doMalloc dummy size =
|
||||
mallocPlainForeignPtrAlignedBytes (size * sizeOf dummy) (alignment dummy)
|
||||
#elif __GLASGOW_HASKELL__ >= 700
|
||||
doMalloc undefined
|
||||
where
|
||||
doMalloc :: Storable b => b -> Int -> IO (ForeignPtr b)
|
||||
doMalloc dummy size = do
|
||||
arr@(MutableByteArray arr#) <- newAlignedPinnedByteArray arrSize arrAlign
|
||||
newConcForeignPtr
|
||||
(Ptr (byteArrayContents# (unsafeCoerce# arr#)))
|
||||
-- Keep reference to mutable byte array until whole ForeignPtr goes out
|
||||
-- of scope.
|
||||
(touch arr)
|
||||
where
|
||||
arrSize = size * sizeOf dummy
|
||||
arrAlign = alignment dummy
|
||||
#else
|
||||
mallocForeignPtrArray
|
||||
#endif
|
||||
|
||||
-- Length information
|
||||
-- ------------------
|
||||
|
||||
-- | Length of the mutable vector.
|
||||
length :: Storable a => MVector s a -> Int
|
||||
{-# INLINE length #-}
|
||||
length = G.length
|
||||
|
||||
-- | Check whether the vector is empty
|
||||
null :: Storable a => MVector s a -> Bool
|
||||
{-# INLINE null #-}
|
||||
null = G.null
|
||||
|
||||
-- Extracting subvectors
|
||||
-- ---------------------
|
||||
|
||||
-- | Yield a part of the mutable vector without copying it.
|
||||
slice :: Storable a => Int -> Int -> MVector s a -> MVector s a
|
||||
{-# INLINE slice #-}
|
||||
slice = G.slice
|
||||
|
||||
take :: Storable a => Int -> MVector s a -> MVector s a
|
||||
{-# INLINE take #-}
|
||||
take = G.take
|
||||
|
||||
drop :: Storable a => Int -> MVector s a -> MVector s a
|
||||
{-# INLINE drop #-}
|
||||
drop = G.drop
|
||||
|
||||
splitAt :: Storable a => Int -> MVector s a -> (MVector s a, MVector s a)
|
||||
{-# INLINE splitAt #-}
|
||||
splitAt = G.splitAt
|
||||
|
||||
init :: Storable a => MVector s a -> MVector s a
|
||||
{-# INLINE init #-}
|
||||
init = G.init
|
||||
|
||||
tail :: Storable a => MVector s a -> MVector s a
|
||||
{-# INLINE tail #-}
|
||||
tail = G.tail
|
||||
|
||||
-- | Yield a part of the mutable vector without copying it. No bounds checks
|
||||
-- are performed.
|
||||
unsafeSlice :: Storable a
|
||||
=> Int -- ^ starting index
|
||||
-> Int -- ^ length of the slice
|
||||
-> MVector s a
|
||||
-> MVector s a
|
||||
{-# INLINE unsafeSlice #-}
|
||||
unsafeSlice = G.unsafeSlice
|
||||
|
||||
unsafeTake :: Storable a => Int -> MVector s a -> MVector s a
|
||||
{-# INLINE unsafeTake #-}
|
||||
unsafeTake = G.unsafeTake
|
||||
|
||||
unsafeDrop :: Storable a => Int -> MVector s a -> MVector s a
|
||||
{-# INLINE unsafeDrop #-}
|
||||
unsafeDrop = G.unsafeDrop
|
||||
|
||||
unsafeInit :: Storable a => MVector s a -> MVector s a
|
||||
{-# INLINE unsafeInit #-}
|
||||
unsafeInit = G.unsafeInit
|
||||
|
||||
unsafeTail :: Storable a => MVector s a -> MVector s a
|
||||
{-# INLINE unsafeTail #-}
|
||||
unsafeTail = G.unsafeTail
|
||||
|
||||
-- Overlapping
|
||||
-- -----------
|
||||
|
||||
-- | Check whether two vectors overlap.
|
||||
overlaps :: Storable a => MVector s a -> MVector s a -> Bool
|
||||
{-# INLINE overlaps #-}
|
||||
overlaps = G.overlaps
|
||||
|
||||
-- Initialisation
|
||||
-- --------------
|
||||
|
||||
-- | Create a mutable vector of the given length.
|
||||
new :: (PrimMonad m, Storable a) => Int -> m (MVector (PrimState m) a)
|
||||
{-# INLINE new #-}
|
||||
new = G.new
|
||||
|
||||
-- | Create a mutable vector of the given length. The memory is not initialized.
|
||||
unsafeNew :: (PrimMonad m, Storable a) => Int -> m (MVector (PrimState m) a)
|
||||
{-# INLINE unsafeNew #-}
|
||||
unsafeNew = G.unsafeNew
|
||||
|
||||
-- | Create a mutable vector of the given length (0 if the length is negative)
|
||||
-- and fill it with an initial value.
|
||||
replicate :: (PrimMonad m, Storable a) => Int -> a -> m (MVector (PrimState m) a)
|
||||
{-# INLINE replicate #-}
|
||||
replicate = G.replicate
|
||||
|
||||
-- | Create a mutable vector of the given length (0 if the length is negative)
|
||||
-- and fill it with values produced by repeatedly executing the monadic action.
|
||||
replicateM :: (PrimMonad m, Storable a) => Int -> m a -> m (MVector (PrimState m) a)
|
||||
{-# INLINE replicateM #-}
|
||||
replicateM = G.replicateM
|
||||
|
||||
-- | Create a copy of a mutable vector.
|
||||
clone :: (PrimMonad m, Storable a)
|
||||
=> MVector (PrimState m) a -> m (MVector (PrimState m) a)
|
||||
{-# INLINE clone #-}
|
||||
clone = G.clone
|
||||
|
||||
-- Growing
|
||||
-- -------
|
||||
|
||||
-- | Grow a vector by the given number of elements. The number must be
|
||||
-- positive.
|
||||
grow :: (PrimMonad m, Storable a)
|
||||
=> MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
|
||||
{-# INLINE grow #-}
|
||||
grow = G.grow
|
||||
|
||||
-- | Grow a vector by the given number of elements. The number must be
|
||||
-- positive but this is not checked.
|
||||
unsafeGrow :: (PrimMonad m, Storable a)
|
||||
=> MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
|
||||
{-# INLINE unsafeGrow #-}
|
||||
unsafeGrow = G.unsafeGrow
|
||||
|
||||
-- Restricting memory usage
|
||||
-- ------------------------
|
||||
|
||||
-- | Reset all elements of the vector to some undefined value, clearing all
|
||||
-- references to external objects. This is usually a noop for unboxed vectors.
|
||||
clear :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> m ()
|
||||
{-# INLINE clear #-}
|
||||
clear = G.clear
|
||||
|
||||
-- Accessing individual elements
|
||||
-- -----------------------------
|
||||
|
||||
-- | Yield the element at the given position.
|
||||
read :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> m a
|
||||
{-# INLINE read #-}
|
||||
read = G.read
|
||||
|
||||
-- | Replace the element at the given position.
|
||||
write
|
||||
:: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> a -> m ()
|
||||
{-# INLINE write #-}
|
||||
write = G.write
|
||||
|
||||
-- | Modify the element at the given position.
|
||||
modify :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> (a -> a) -> Int -> m ()
|
||||
{-# INLINE modify #-}
|
||||
modify = G.modify
|
||||
|
||||
-- | Swap the elements at the given positions.
|
||||
swap
|
||||
:: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> Int -> m ()
|
||||
{-# INLINE swap #-}
|
||||
swap = G.swap
|
||||
|
||||
|
||||
-- | Yield the element at the given position. No bounds checks are performed.
|
||||
unsafeRead :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> m a
|
||||
{-# INLINE unsafeRead #-}
|
||||
unsafeRead = G.unsafeRead
|
||||
|
||||
-- | Replace the element at the given position. No bounds checks are performed.
|
||||
unsafeWrite
|
||||
:: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> a -> m ()
|
||||
{-# INLINE unsafeWrite #-}
|
||||
unsafeWrite = G.unsafeWrite
|
||||
|
||||
-- | Modify the element at the given position. No bounds checks are performed.
|
||||
unsafeModify :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> (a -> a) -> Int -> m ()
|
||||
{-# INLINE unsafeModify #-}
|
||||
unsafeModify = G.unsafeModify
|
||||
|
||||
-- | Swap the elements at the given positions. No bounds checks are performed.
|
||||
unsafeSwap
|
||||
:: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> Int -> m ()
|
||||
{-# INLINE unsafeSwap #-}
|
||||
unsafeSwap = G.unsafeSwap
|
||||
|
||||
-- Filling and copying
|
||||
-- -------------------
|
||||
|
||||
-- | Set all elements of the vector to the given value.
|
||||
set :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> a -> m ()
|
||||
{-# INLINE set #-}
|
||||
set = G.set
|
||||
|
||||
-- | Copy a vector. The two vectors must have the same length and may not
|
||||
-- overlap.
|
||||
copy :: (PrimMonad m, Storable a)
|
||||
=> MVector (PrimState m) a -- ^ target
|
||||
-> MVector (PrimState m) a -- ^ source
|
||||
-> m ()
|
||||
{-# INLINE copy #-}
|
||||
copy = G.copy
|
||||
|
||||
-- | Copy a vector. The two vectors must have the same length and may not
|
||||
-- overlap. This is not checked.
|
||||
unsafeCopy :: (PrimMonad m, Storable a)
|
||||
=> MVector (PrimState m) a -- ^ target
|
||||
-> MVector (PrimState m) a -- ^ source
|
||||
-> m ()
|
||||
{-# INLINE unsafeCopy #-}
|
||||
unsafeCopy = G.unsafeCopy
|
||||
|
||||
-- | Move the contents of a vector. The two vectors must have the same
|
||||
-- length.
|
||||
--
|
||||
-- If the vectors do not overlap, then this is equivalent to 'copy'.
|
||||
-- Otherwise, the copying is performed as if the source vector were
|
||||
-- copied to a temporary vector and then the temporary vector was copied
|
||||
-- to the target vector.
|
||||
move :: (PrimMonad m, Storable a)
|
||||
=> MVector (PrimState m) a -> MVector (PrimState m) a -> m ()
|
||||
{-# INLINE move #-}
|
||||
move = G.move
|
||||
|
||||
-- | Move the contents of a vector. The two vectors must have the same
|
||||
-- length, but this is not checked.
|
||||
--
|
||||
-- If the vectors do not overlap, then this is equivalent to 'unsafeCopy'.
|
||||
-- Otherwise, the copying is performed as if the source vector were
|
||||
-- copied to a temporary vector and then the temporary vector was copied
|
||||
-- to the target vector.
|
||||
unsafeMove :: (PrimMonad m, Storable a)
|
||||
=> MVector (PrimState m) a -- ^ target
|
||||
-> MVector (PrimState m) a -- ^ source
|
||||
-> m ()
|
||||
{-# INLINE unsafeMove #-}
|
||||
unsafeMove = G.unsafeMove
|
||||
|
||||
-- Unsafe conversions
|
||||
-- ------------------
|
||||
|
||||
-- | /O(1)/ Unsafely cast a mutable vector from one element type to another.
|
||||
-- The operation just changes the type of the underlying pointer and does not
|
||||
-- modify the elements.
|
||||
--
|
||||
-- The resulting vector contains as many elements as can fit into the
|
||||
-- underlying memory block.
|
||||
--
|
||||
unsafeCast :: forall a b s.
|
||||
(Storable a, Storable b) => MVector s a -> MVector s b
|
||||
{-# INLINE unsafeCast #-}
|
||||
unsafeCast (MVector n fp)
|
||||
= MVector ((n * sizeOf (undefined :: a)) `div` sizeOf (undefined :: b))
|
||||
(castForeignPtr fp)
|
||||
|
||||
-- Raw pointers
|
||||
-- ------------
|
||||
|
||||
-- | Create a mutable vector from a 'ForeignPtr' with an offset and a length.
|
||||
--
|
||||
-- Modifying data through the 'ForeignPtr' afterwards is unsafe if the vector
|
||||
-- could have been frozen before the modification.
|
||||
--
|
||||
-- If your offset is 0 it is more efficient to use 'unsafeFromForeignPtr0'.
|
||||
unsafeFromForeignPtr :: Storable a
|
||||
=> ForeignPtr a -- ^ pointer
|
||||
-> Int -- ^ offset
|
||||
-> Int -- ^ length
|
||||
-> MVector s a
|
||||
{-# INLINE_FUSED unsafeFromForeignPtr #-}
|
||||
unsafeFromForeignPtr fp i n = unsafeFromForeignPtr0 fp' n
|
||||
where
|
||||
fp' = updPtr (`advancePtr` i) fp
|
||||
|
||||
{-# RULES
|
||||
"unsafeFromForeignPtr fp 0 n -> unsafeFromForeignPtr0 fp n " forall fp n.
|
||||
unsafeFromForeignPtr fp 0 n = unsafeFromForeignPtr0 fp n #-}
|
||||
|
||||
|
||||
-- | /O(1)/ Create a mutable vector from a 'ForeignPtr' and a length.
|
||||
--
|
||||
-- It is assumed the pointer points directly to the data (no offset).
|
||||
-- Use `unsafeFromForeignPtr` if you need to specify an offset.
|
||||
--
|
||||
-- Modifying data through the 'ForeignPtr' afterwards is unsafe if the vector
|
||||
-- could have been frozen before the modification.
|
||||
unsafeFromForeignPtr0 :: Storable a
|
||||
=> ForeignPtr a -- ^ pointer
|
||||
-> Int -- ^ length
|
||||
-> MVector s a
|
||||
{-# INLINE unsafeFromForeignPtr0 #-}
|
||||
unsafeFromForeignPtr0 fp n = MVector n fp
|
||||
|
||||
-- | Yield the underlying 'ForeignPtr' together with the offset to the data
|
||||
-- and its length. Modifying the data through the 'ForeignPtr' is
|
||||
-- unsafe if the vector could have frozen before the modification.
|
||||
unsafeToForeignPtr :: Storable a => MVector s a -> (ForeignPtr a, Int, Int)
|
||||
{-# INLINE unsafeToForeignPtr #-}
|
||||
unsafeToForeignPtr (MVector n fp) = (fp, 0, n)
|
||||
|
||||
-- | /O(1)/ Yield the underlying 'ForeignPtr' together with its length.
|
||||
--
|
||||
-- You can assume the pointer points directly to the data (no offset).
|
||||
--
|
||||
-- Modifying the data through the 'ForeignPtr' is unsafe if the vector could
|
||||
-- have frozen before the modification.
|
||||
unsafeToForeignPtr0 :: Storable a => MVector s a -> (ForeignPtr a, Int)
|
||||
{-# INLINE unsafeToForeignPtr0 #-}
|
||||
unsafeToForeignPtr0 (MVector n fp) = (fp, n)
|
||||
|
||||
-- | Pass a pointer to the vector's data to the IO action. Modifying data
|
||||
-- through the pointer is unsafe if the vector could have been frozen before
|
||||
-- the modification.
|
||||
unsafeWith :: Storable a => IOVector a -> (Ptr a -> IO b) -> IO b
|
||||
{-# INLINE unsafeWith #-}
|
||||
unsafeWith (MVector _ fp) = withForeignPtr fp
|
||||
|
1488
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Unboxed.hs
vendored
Normal file
1488
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Unboxed.hs
vendored
Normal file
File diff suppressed because it is too large
Load diff
408
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Unboxed/Base.hs
vendored
Normal file
408
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Unboxed/Base.hs
vendored
Normal file
|
@ -0,0 +1,408 @@
|
|||
{-# LANGUAGE BangPatterns, CPP, MultiParamTypeClasses, TypeFamilies, FlexibleContexts #-}
|
||||
#if __GLASGOW_HASKELL__ >= 707
|
||||
{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
|
||||
#endif
|
||||
{-# OPTIONS_HADDOCK hide #-}
|
||||
|
||||
-- |
|
||||
-- Module : Data.Vector.Unboxed.Base
|
||||
-- Copyright : (c) Roman Leshchinskiy 2009-2010
|
||||
-- License : BSD-style
|
||||
--
|
||||
-- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
|
||||
-- Stability : experimental
|
||||
-- Portability : non-portable
|
||||
--
|
||||
-- Adaptive unboxed vectors: basic implementation
|
||||
--
|
||||
|
||||
module Data.Vector.Unboxed.Base (
|
||||
MVector(..), IOVector, STVector, Vector(..), Unbox
|
||||
) where
|
||||
|
||||
import qualified Data.Vector.Generic as G
|
||||
import qualified Data.Vector.Generic.Mutable as M
|
||||
|
||||
import qualified Data.Vector.Primitive as P
|
||||
|
||||
import Control.DeepSeq ( NFData(rnf) )
|
||||
|
||||
import Control.Monad.Primitive
|
||||
import Control.Monad ( liftM )
|
||||
|
||||
import Data.Word ( Word8, Word16, Word32, Word64 )
|
||||
import Data.Int ( Int8, Int16, Int32, Int64 )
|
||||
import Data.Complex
|
||||
|
||||
#if !MIN_VERSION_base(4,8,0)
|
||||
import Data.Word ( Word )
|
||||
#endif
|
||||
|
||||
#if __GLASGOW_HASKELL__ >= 707
|
||||
import Data.Typeable ( Typeable )
|
||||
#else
|
||||
import Data.Typeable ( Typeable1(..), Typeable2(..), mkTyConApp,
|
||||
mkTyCon3
|
||||
)
|
||||
#endif
|
||||
|
||||
import Data.Data ( Data(..) )
|
||||
|
||||
-- Data.Vector.Internal.Check is unused
|
||||
#define NOT_VECTOR_MODULE
|
||||
#include "vector.h"
|
||||
|
||||
data family MVector s a
|
||||
data family Vector a
|
||||
|
||||
type IOVector = MVector RealWorld
|
||||
type STVector s = MVector s
|
||||
|
||||
type instance G.Mutable Vector = MVector
|
||||
|
||||
class (G.Vector Vector a, M.MVector MVector a) => Unbox a
|
||||
|
||||
instance NFData (Vector a) where rnf !_ = ()
|
||||
instance NFData (MVector s a) where rnf !_ = ()
|
||||
|
||||
-- -----------------
|
||||
-- Data and Typeable
|
||||
-- -----------------
|
||||
#if __GLASGOW_HASKELL__ >= 707
|
||||
deriving instance Typeable Vector
|
||||
deriving instance Typeable MVector
|
||||
#else
|
||||
vectorTyCon = mkTyCon3 "vector"
|
||||
|
||||
instance Typeable1 Vector where
|
||||
typeOf1 _ = mkTyConApp (vectorTyCon "Data.Vector.Unboxed" "Vector") []
|
||||
|
||||
instance Typeable2 MVector where
|
||||
typeOf2 _ = mkTyConApp (vectorTyCon "Data.Vector.Unboxed.Mutable" "MVector") []
|
||||
#endif
|
||||
|
||||
instance (Data a, Unbox a) => Data (Vector a) where
|
||||
gfoldl = G.gfoldl
|
||||
toConstr _ = error "toConstr"
|
||||
gunfold _ _ = error "gunfold"
|
||||
dataTypeOf _ = G.mkType "Data.Vector.Unboxed.Vector"
|
||||
dataCast1 = G.dataCast
|
||||
|
||||
-- ----
|
||||
-- Unit
|
||||
-- ----
|
||||
|
||||
newtype instance MVector s () = MV_Unit Int
|
||||
newtype instance Vector () = V_Unit Int
|
||||
|
||||
instance Unbox ()
|
||||
|
||||
instance M.MVector MVector () where
|
||||
{-# INLINE basicLength #-}
|
||||
{-# INLINE basicUnsafeSlice #-}
|
||||
{-# INLINE basicOverlaps #-}
|
||||
{-# INLINE basicUnsafeNew #-}
|
||||
{-# INLINE basicInitialize #-}
|
||||
{-# INLINE basicUnsafeRead #-}
|
||||
{-# INLINE basicUnsafeWrite #-}
|
||||
{-# INLINE basicClear #-}
|
||||
{-# INLINE basicSet #-}
|
||||
{-# INLINE basicUnsafeCopy #-}
|
||||
{-# INLINE basicUnsafeGrow #-}
|
||||
|
||||
basicLength (MV_Unit n) = n
|
||||
|
||||
basicUnsafeSlice _ m (MV_Unit _) = MV_Unit m
|
||||
|
||||
basicOverlaps _ _ = False
|
||||
|
||||
basicUnsafeNew n = return (MV_Unit n)
|
||||
|
||||
-- Nothing to initialize
|
||||
basicInitialize _ = return ()
|
||||
|
||||
basicUnsafeRead (MV_Unit _) _ = return ()
|
||||
|
||||
basicUnsafeWrite (MV_Unit _) _ () = return ()
|
||||
|
||||
basicClear _ = return ()
|
||||
|
||||
basicSet (MV_Unit _) () = return ()
|
||||
|
||||
basicUnsafeCopy (MV_Unit _) (MV_Unit _) = return ()
|
||||
|
||||
basicUnsafeGrow (MV_Unit n) m = return $ MV_Unit (n+m)
|
||||
|
||||
instance G.Vector Vector () where
|
||||
{-# INLINE basicUnsafeFreeze #-}
|
||||
basicUnsafeFreeze (MV_Unit n) = return $ V_Unit n
|
||||
|
||||
{-# INLINE basicUnsafeThaw #-}
|
||||
basicUnsafeThaw (V_Unit n) = return $ MV_Unit n
|
||||
|
||||
{-# INLINE basicLength #-}
|
||||
basicLength (V_Unit n) = n
|
||||
|
||||
{-# INLINE basicUnsafeSlice #-}
|
||||
basicUnsafeSlice _ m (V_Unit _) = V_Unit m
|
||||
|
||||
{-# INLINE basicUnsafeIndexM #-}
|
||||
basicUnsafeIndexM (V_Unit _) _ = return ()
|
||||
|
||||
{-# INLINE basicUnsafeCopy #-}
|
||||
basicUnsafeCopy (MV_Unit _) (V_Unit _) = return ()
|
||||
|
||||
{-# INLINE elemseq #-}
|
||||
elemseq _ = seq
|
||||
|
||||
|
||||
-- ---------------
|
||||
-- Primitive types
|
||||
-- ---------------
|
||||
|
||||
#define primMVector(ty,con) \
|
||||
instance M.MVector MVector ty where { \
|
||||
{-# INLINE basicLength #-} \
|
||||
; {-# INLINE basicUnsafeSlice #-} \
|
||||
; {-# INLINE basicOverlaps #-} \
|
||||
; {-# INLINE basicUnsafeNew #-} \
|
||||
; {-# INLINE basicInitialize #-} \
|
||||
; {-# INLINE basicUnsafeReplicate #-} \
|
||||
; {-# INLINE basicUnsafeRead #-} \
|
||||
; {-# INLINE basicUnsafeWrite #-} \
|
||||
; {-# INLINE basicClear #-} \
|
||||
; {-# INLINE basicSet #-} \
|
||||
; {-# INLINE basicUnsafeCopy #-} \
|
||||
; {-# INLINE basicUnsafeGrow #-} \
|
||||
; basicLength (con v) = M.basicLength v \
|
||||
; basicUnsafeSlice i n (con v) = con $ M.basicUnsafeSlice i n v \
|
||||
; basicOverlaps (con v1) (con v2) = M.basicOverlaps v1 v2 \
|
||||
; basicUnsafeNew n = con `liftM` M.basicUnsafeNew n \
|
||||
; basicInitialize (con v) = M.basicInitialize v \
|
||||
; basicUnsafeReplicate n x = con `liftM` M.basicUnsafeReplicate n x \
|
||||
; basicUnsafeRead (con v) i = M.basicUnsafeRead v i \
|
||||
; basicUnsafeWrite (con v) i x = M.basicUnsafeWrite v i x \
|
||||
; basicClear (con v) = M.basicClear v \
|
||||
; basicSet (con v) x = M.basicSet v x \
|
||||
; basicUnsafeCopy (con v1) (con v2) = M.basicUnsafeCopy v1 v2 \
|
||||
; basicUnsafeMove (con v1) (con v2) = M.basicUnsafeMove v1 v2 \
|
||||
; basicUnsafeGrow (con v) n = con `liftM` M.basicUnsafeGrow v n }
|
||||
|
||||
#define primVector(ty,con,mcon) \
|
||||
instance G.Vector Vector ty where { \
|
||||
{-# INLINE basicUnsafeFreeze #-} \
|
||||
; {-# INLINE basicUnsafeThaw #-} \
|
||||
; {-# INLINE basicLength #-} \
|
||||
; {-# INLINE basicUnsafeSlice #-} \
|
||||
; {-# INLINE basicUnsafeIndexM #-} \
|
||||
; {-# INLINE elemseq #-} \
|
||||
; basicUnsafeFreeze (mcon v) = con `liftM` G.basicUnsafeFreeze v \
|
||||
; basicUnsafeThaw (con v) = mcon `liftM` G.basicUnsafeThaw v \
|
||||
; basicLength (con v) = G.basicLength v \
|
||||
; basicUnsafeSlice i n (con v) = con $ G.basicUnsafeSlice i n v \
|
||||
; basicUnsafeIndexM (con v) i = G.basicUnsafeIndexM v i \
|
||||
; basicUnsafeCopy (mcon mv) (con v) = G.basicUnsafeCopy mv v \
|
||||
; elemseq _ = seq }
|
||||
|
||||
newtype instance MVector s Int = MV_Int (P.MVector s Int)
|
||||
newtype instance Vector Int = V_Int (P.Vector Int)
|
||||
instance Unbox Int
|
||||
primMVector(Int, MV_Int)
|
||||
primVector(Int, V_Int, MV_Int)
|
||||
|
||||
newtype instance MVector s Int8 = MV_Int8 (P.MVector s Int8)
|
||||
newtype instance Vector Int8 = V_Int8 (P.Vector Int8)
|
||||
instance Unbox Int8
|
||||
primMVector(Int8, MV_Int8)
|
||||
primVector(Int8, V_Int8, MV_Int8)
|
||||
|
||||
newtype instance MVector s Int16 = MV_Int16 (P.MVector s Int16)
|
||||
newtype instance Vector Int16 = V_Int16 (P.Vector Int16)
|
||||
instance Unbox Int16
|
||||
primMVector(Int16, MV_Int16)
|
||||
primVector(Int16, V_Int16, MV_Int16)
|
||||
|
||||
newtype instance MVector s Int32 = MV_Int32 (P.MVector s Int32)
|
||||
newtype instance Vector Int32 = V_Int32 (P.Vector Int32)
|
||||
instance Unbox Int32
|
||||
primMVector(Int32, MV_Int32)
|
||||
primVector(Int32, V_Int32, MV_Int32)
|
||||
|
||||
newtype instance MVector s Int64 = MV_Int64 (P.MVector s Int64)
|
||||
newtype instance Vector Int64 = V_Int64 (P.Vector Int64)
|
||||
instance Unbox Int64
|
||||
primMVector(Int64, MV_Int64)
|
||||
primVector(Int64, V_Int64, MV_Int64)
|
||||
|
||||
|
||||
newtype instance MVector s Word = MV_Word (P.MVector s Word)
|
||||
newtype instance Vector Word = V_Word (P.Vector Word)
|
||||
instance Unbox Word
|
||||
primMVector(Word, MV_Word)
|
||||
primVector(Word, V_Word, MV_Word)
|
||||
|
||||
newtype instance MVector s Word8 = MV_Word8 (P.MVector s Word8)
|
||||
newtype instance Vector Word8 = V_Word8 (P.Vector Word8)
|
||||
instance Unbox Word8
|
||||
primMVector(Word8, MV_Word8)
|
||||
primVector(Word8, V_Word8, MV_Word8)
|
||||
|
||||
newtype instance MVector s Word16 = MV_Word16 (P.MVector s Word16)
|
||||
newtype instance Vector Word16 = V_Word16 (P.Vector Word16)
|
||||
instance Unbox Word16
|
||||
primMVector(Word16, MV_Word16)
|
||||
primVector(Word16, V_Word16, MV_Word16)
|
||||
|
||||
newtype instance MVector s Word32 = MV_Word32 (P.MVector s Word32)
|
||||
newtype instance Vector Word32 = V_Word32 (P.Vector Word32)
|
||||
instance Unbox Word32
|
||||
primMVector(Word32, MV_Word32)
|
||||
primVector(Word32, V_Word32, MV_Word32)
|
||||
|
||||
newtype instance MVector s Word64 = MV_Word64 (P.MVector s Word64)
|
||||
newtype instance Vector Word64 = V_Word64 (P.Vector Word64)
|
||||
instance Unbox Word64
|
||||
primMVector(Word64, MV_Word64)
|
||||
primVector(Word64, V_Word64, MV_Word64)
|
||||
|
||||
|
||||
newtype instance MVector s Float = MV_Float (P.MVector s Float)
|
||||
newtype instance Vector Float = V_Float (P.Vector Float)
|
||||
instance Unbox Float
|
||||
primMVector(Float, MV_Float)
|
||||
primVector(Float, V_Float, MV_Float)
|
||||
|
||||
newtype instance MVector s Double = MV_Double (P.MVector s Double)
|
||||
newtype instance Vector Double = V_Double (P.Vector Double)
|
||||
instance Unbox Double
|
||||
primMVector(Double, MV_Double)
|
||||
primVector(Double, V_Double, MV_Double)
|
||||
|
||||
|
||||
newtype instance MVector s Char = MV_Char (P.MVector s Char)
|
||||
newtype instance Vector Char = V_Char (P.Vector Char)
|
||||
instance Unbox Char
|
||||
primMVector(Char, MV_Char)
|
||||
primVector(Char, V_Char, MV_Char)
|
||||
|
||||
-- ----
|
||||
-- Bool
|
||||
-- ----
|
||||
|
||||
fromBool :: Bool -> Word8
|
||||
{-# INLINE fromBool #-}
|
||||
fromBool True = 1
|
||||
fromBool False = 0
|
||||
|
||||
toBool :: Word8 -> Bool
|
||||
{-# INLINE toBool #-}
|
||||
toBool 0 = False
|
||||
toBool _ = True
|
||||
|
||||
newtype instance MVector s Bool = MV_Bool (P.MVector s Word8)
|
||||
newtype instance Vector Bool = V_Bool (P.Vector Word8)
|
||||
|
||||
instance Unbox Bool
|
||||
|
||||
instance M.MVector MVector Bool where
|
||||
{-# INLINE basicLength #-}
|
||||
{-# INLINE basicUnsafeSlice #-}
|
||||
{-# INLINE basicOverlaps #-}
|
||||
{-# INLINE basicUnsafeNew #-}
|
||||
{-# INLINE basicInitialize #-}
|
||||
{-# INLINE basicUnsafeReplicate #-}
|
||||
{-# INLINE basicUnsafeRead #-}
|
||||
{-# INLINE basicUnsafeWrite #-}
|
||||
{-# INLINE basicClear #-}
|
||||
{-# INLINE basicSet #-}
|
||||
{-# INLINE basicUnsafeCopy #-}
|
||||
{-# INLINE basicUnsafeGrow #-}
|
||||
basicLength (MV_Bool v) = M.basicLength v
|
||||
basicUnsafeSlice i n (MV_Bool v) = MV_Bool $ M.basicUnsafeSlice i n v
|
||||
basicOverlaps (MV_Bool v1) (MV_Bool v2) = M.basicOverlaps v1 v2
|
||||
basicUnsafeNew n = MV_Bool `liftM` M.basicUnsafeNew n
|
||||
basicInitialize (MV_Bool v) = M.basicInitialize v
|
||||
basicUnsafeReplicate n x = MV_Bool `liftM` M.basicUnsafeReplicate n (fromBool x)
|
||||
basicUnsafeRead (MV_Bool v) i = toBool `liftM` M.basicUnsafeRead v i
|
||||
basicUnsafeWrite (MV_Bool v) i x = M.basicUnsafeWrite v i (fromBool x)
|
||||
basicClear (MV_Bool v) = M.basicClear v
|
||||
basicSet (MV_Bool v) x = M.basicSet v (fromBool x)
|
||||
basicUnsafeCopy (MV_Bool v1) (MV_Bool v2) = M.basicUnsafeCopy v1 v2
|
||||
basicUnsafeMove (MV_Bool v1) (MV_Bool v2) = M.basicUnsafeMove v1 v2
|
||||
basicUnsafeGrow (MV_Bool v) n = MV_Bool `liftM` M.basicUnsafeGrow v n
|
||||
|
||||
instance G.Vector Vector Bool where
|
||||
{-# INLINE basicUnsafeFreeze #-}
|
||||
{-# INLINE basicUnsafeThaw #-}
|
||||
{-# INLINE basicLength #-}
|
||||
{-# INLINE basicUnsafeSlice #-}
|
||||
{-# INLINE basicUnsafeIndexM #-}
|
||||
{-# INLINE elemseq #-}
|
||||
basicUnsafeFreeze (MV_Bool v) = V_Bool `liftM` G.basicUnsafeFreeze v
|
||||
basicUnsafeThaw (V_Bool v) = MV_Bool `liftM` G.basicUnsafeThaw v
|
||||
basicLength (V_Bool v) = G.basicLength v
|
||||
basicUnsafeSlice i n (V_Bool v) = V_Bool $ G.basicUnsafeSlice i n v
|
||||
basicUnsafeIndexM (V_Bool v) i = toBool `liftM` G.basicUnsafeIndexM v i
|
||||
basicUnsafeCopy (MV_Bool mv) (V_Bool v) = G.basicUnsafeCopy mv v
|
||||
elemseq _ = seq
|
||||
|
||||
-- -------
|
||||
-- Complex
|
||||
-- -------
|
||||
|
||||
newtype instance MVector s (Complex a) = MV_Complex (MVector s (a,a))
|
||||
newtype instance Vector (Complex a) = V_Complex (Vector (a,a))
|
||||
|
||||
instance (Unbox a) => Unbox (Complex a)
|
||||
|
||||
instance (Unbox a) => M.MVector MVector (Complex a) where
|
||||
{-# INLINE basicLength #-}
|
||||
{-# INLINE basicUnsafeSlice #-}
|
||||
{-# INLINE basicOverlaps #-}
|
||||
{-# INLINE basicUnsafeNew #-}
|
||||
{-# INLINE basicInitialize #-}
|
||||
{-# INLINE basicUnsafeReplicate #-}
|
||||
{-# INLINE basicUnsafeRead #-}
|
||||
{-# INLINE basicUnsafeWrite #-}
|
||||
{-# INLINE basicClear #-}
|
||||
{-# INLINE basicSet #-}
|
||||
{-# INLINE basicUnsafeCopy #-}
|
||||
{-# INLINE basicUnsafeGrow #-}
|
||||
basicLength (MV_Complex v) = M.basicLength v
|
||||
basicUnsafeSlice i n (MV_Complex v) = MV_Complex $ M.basicUnsafeSlice i n v
|
||||
basicOverlaps (MV_Complex v1) (MV_Complex v2) = M.basicOverlaps v1 v2
|
||||
basicUnsafeNew n = MV_Complex `liftM` M.basicUnsafeNew n
|
||||
basicInitialize (MV_Complex v) = M.basicInitialize v
|
||||
basicUnsafeReplicate n (x :+ y) = MV_Complex `liftM` M.basicUnsafeReplicate n (x,y)
|
||||
basicUnsafeRead (MV_Complex v) i = uncurry (:+) `liftM` M.basicUnsafeRead v i
|
||||
basicUnsafeWrite (MV_Complex v) i (x :+ y) = M.basicUnsafeWrite v i (x,y)
|
||||
basicClear (MV_Complex v) = M.basicClear v
|
||||
basicSet (MV_Complex v) (x :+ y) = M.basicSet v (x,y)
|
||||
basicUnsafeCopy (MV_Complex v1) (MV_Complex v2) = M.basicUnsafeCopy v1 v2
|
||||
basicUnsafeMove (MV_Complex v1) (MV_Complex v2) = M.basicUnsafeMove v1 v2
|
||||
basicUnsafeGrow (MV_Complex v) n = MV_Complex `liftM` M.basicUnsafeGrow v n
|
||||
|
||||
instance (Unbox a) => G.Vector Vector (Complex a) where
|
||||
{-# INLINE basicUnsafeFreeze #-}
|
||||
{-# INLINE basicUnsafeThaw #-}
|
||||
{-# INLINE basicLength #-}
|
||||
{-# INLINE basicUnsafeSlice #-}
|
||||
{-# INLINE basicUnsafeIndexM #-}
|
||||
{-# INLINE elemseq #-}
|
||||
basicUnsafeFreeze (MV_Complex v) = V_Complex `liftM` G.basicUnsafeFreeze v
|
||||
basicUnsafeThaw (V_Complex v) = MV_Complex `liftM` G.basicUnsafeThaw v
|
||||
basicLength (V_Complex v) = G.basicLength v
|
||||
basicUnsafeSlice i n (V_Complex v) = V_Complex $ G.basicUnsafeSlice i n v
|
||||
basicUnsafeIndexM (V_Complex v) i
|
||||
= uncurry (:+) `liftM` G.basicUnsafeIndexM v i
|
||||
basicUnsafeCopy (MV_Complex mv) (V_Complex v)
|
||||
= G.basicUnsafeCopy mv v
|
||||
elemseq _ (x :+ y) z = G.elemseq (undefined :: Vector a) x
|
||||
$ G.elemseq (undefined :: Vector a) y z
|
||||
|
||||
-- ------
|
||||
-- Tuples
|
||||
-- ------
|
||||
|
||||
#define DEFINE_INSTANCES
|
||||
#include "unbox-tuple-instances"
|
307
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Unboxed/Mutable.hs
vendored
Normal file
307
third_party/bazel/rules_haskell/examples/vector/Data/Vector/Unboxed/Mutable.hs
vendored
Normal file
|
@ -0,0 +1,307 @@
|
|||
{-# LANGUAGE CPP #-}
|
||||
|
||||
-- |
|
||||
-- Module : Data.Vector.Unboxed.Mutable
|
||||
-- Copyright : (c) Roman Leshchinskiy 2009-2010
|
||||
-- License : BSD-style
|
||||
--
|
||||
-- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
|
||||
-- Stability : experimental
|
||||
-- Portability : non-portable
|
||||
--
|
||||
-- Mutable adaptive unboxed vectors
|
||||
--
|
||||
|
||||
module Data.Vector.Unboxed.Mutable (
|
||||
-- * Mutable vectors of primitive types
|
||||
MVector(..), IOVector, STVector, Unbox,
|
||||
|
||||
-- * Accessors
|
||||
|
||||
-- ** Length information
|
||||
length, null,
|
||||
|
||||
-- ** Extracting subvectors
|
||||
slice, init, tail, take, drop, splitAt,
|
||||
unsafeSlice, unsafeInit, unsafeTail, unsafeTake, unsafeDrop,
|
||||
|
||||
-- ** Overlapping
|
||||
overlaps,
|
||||
|
||||
-- * Construction
|
||||
|
||||
-- ** Initialisation
|
||||
new, unsafeNew, replicate, replicateM, clone,
|
||||
|
||||
-- ** Growing
|
||||
grow, unsafeGrow,
|
||||
|
||||
-- ** Restricting memory usage
|
||||
clear,
|
||||
|
||||
-- * Zipping and unzipping
|
||||
zip, zip3, zip4, zip5, zip6,
|
||||
unzip, unzip3, unzip4, unzip5, unzip6,
|
||||
|
||||
-- * Accessing individual elements
|
||||
read, write, modify, swap,
|
||||
unsafeRead, unsafeWrite, unsafeModify, unsafeSwap,
|
||||
|
||||
-- * Modifying vectors
|
||||
nextPermutation,
|
||||
|
||||
-- ** Filling and copying
|
||||
set, copy, move, unsafeCopy, unsafeMove
|
||||
) where
|
||||
|
||||
import Data.Vector.Unboxed.Base
|
||||
import qualified Data.Vector.Generic.Mutable as G
|
||||
import Data.Vector.Fusion.Util ( delayed_min )
|
||||
import Control.Monad.Primitive
|
||||
|
||||
import Prelude hiding ( length, null, replicate, reverse, map, read,
|
||||
take, drop, splitAt, init, tail,
|
||||
zip, zip3, unzip, unzip3 )
|
||||
|
||||
-- don't import an unused Data.Vector.Internal.Check
|
||||
#define NOT_VECTOR_MODULE
|
||||
#include "vector.h"
|
||||
|
||||
-- Length information
|
||||
-- ------------------
|
||||
|
||||
-- | Length of the mutable vector.
|
||||
length :: Unbox a => MVector s a -> Int
|
||||
{-# INLINE length #-}
|
||||
length = G.length
|
||||
|
||||
-- | Check whether the vector is empty
|
||||
null :: Unbox a => MVector s a -> Bool
|
||||
{-# INLINE null #-}
|
||||
null = G.null
|
||||
|
||||
-- Extracting subvectors
|
||||
-- ---------------------
|
||||
|
||||
-- | Yield a part of the mutable vector without copying it.
|
||||
slice :: Unbox a => Int -> Int -> MVector s a -> MVector s a
|
||||
{-# INLINE slice #-}
|
||||
slice = G.slice
|
||||
|
||||
take :: Unbox a => Int -> MVector s a -> MVector s a
|
||||
{-# INLINE take #-}
|
||||
take = G.take
|
||||
|
||||
drop :: Unbox a => Int -> MVector s a -> MVector s a
|
||||
{-# INLINE drop #-}
|
||||
drop = G.drop
|
||||
|
||||
splitAt :: Unbox a => Int -> MVector s a -> (MVector s a, MVector s a)
|
||||
{-# INLINE splitAt #-}
|
||||
splitAt = G.splitAt
|
||||
|
||||
init :: Unbox a => MVector s a -> MVector s a
|
||||
{-# INLINE init #-}
|
||||
init = G.init
|
||||
|
||||
tail :: Unbox a => MVector s a -> MVector s a
|
||||
{-# INLINE tail #-}
|
||||
tail = G.tail
|
||||
|
||||
-- | Yield a part of the mutable vector without copying it. No bounds checks
|
||||
-- are performed.
|
||||
unsafeSlice :: Unbox a
|
||||
=> Int -- ^ starting index
|
||||
-> Int -- ^ length of the slice
|
||||
-> MVector s a
|
||||
-> MVector s a
|
||||
{-# INLINE unsafeSlice #-}
|
||||
unsafeSlice = G.unsafeSlice
|
||||
|
||||
unsafeTake :: Unbox a => Int -> MVector s a -> MVector s a
|
||||
{-# INLINE unsafeTake #-}
|
||||
unsafeTake = G.unsafeTake
|
||||
|
||||
unsafeDrop :: Unbox a => Int -> MVector s a -> MVector s a
|
||||
{-# INLINE unsafeDrop #-}
|
||||
unsafeDrop = G.unsafeDrop
|
||||
|
||||
unsafeInit :: Unbox a => MVector s a -> MVector s a
|
||||
{-# INLINE unsafeInit #-}
|
||||
unsafeInit = G.unsafeInit
|
||||
|
||||
unsafeTail :: Unbox a => MVector s a -> MVector s a
|
||||
{-# INLINE unsafeTail #-}
|
||||
unsafeTail = G.unsafeTail
|
||||
|
||||
-- Overlapping
|
||||
-- -----------
|
||||
|
||||
-- | Check whether two vectors overlap.
|
||||
overlaps :: Unbox a => MVector s a -> MVector s a -> Bool
|
||||
{-# INLINE overlaps #-}
|
||||
overlaps = G.overlaps
|
||||
|
||||
-- Initialisation
|
||||
-- --------------
|
||||
|
||||
-- | Create a mutable vector of the given length.
|
||||
new :: (PrimMonad m, Unbox a) => Int -> m (MVector (PrimState m) a)
|
||||
{-# INLINE new #-}
|
||||
new = G.new
|
||||
|
||||
-- | Create a mutable vector of the given length. The memory is not initialized.
|
||||
unsafeNew :: (PrimMonad m, Unbox a) => Int -> m (MVector (PrimState m) a)
|
||||
{-# INLINE unsafeNew #-}
|
||||
unsafeNew = G.unsafeNew
|
||||
|
||||
-- | Create a mutable vector of the given length (0 if the length is negative)
|
||||
-- and fill it with an initial value.
|
||||
replicate :: (PrimMonad m, Unbox a) => Int -> a -> m (MVector (PrimState m) a)
|
||||
{-# INLINE replicate #-}
|
||||
replicate = G.replicate
|
||||
|
||||
-- | Create a mutable vector of the given length (0 if the length is negative)
|
||||
-- and fill it with values produced by repeatedly executing the monadic action.
|
||||
replicateM :: (PrimMonad m, Unbox a) => Int -> m a -> m (MVector (PrimState m) a)
|
||||
{-# INLINE replicateM #-}
|
||||
replicateM = G.replicateM
|
||||
|
||||
-- | Create a copy of a mutable vector.
|
||||
clone :: (PrimMonad m, Unbox a)
|
||||
=> MVector (PrimState m) a -> m (MVector (PrimState m) a)
|
||||
{-# INLINE clone #-}
|
||||
clone = G.clone
|
||||
|
||||
-- Growing
|
||||
-- -------
|
||||
|
||||
-- | Grow a vector by the given number of elements. The number must be
|
||||
-- positive.
|
||||
grow :: (PrimMonad m, Unbox a)
|
||||
=> MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
|
||||
{-# INLINE grow #-}
|
||||
grow = G.grow
|
||||
|
||||
-- | Grow a vector by the given number of elements. The number must be
|
||||
-- positive but this is not checked.
|
||||
unsafeGrow :: (PrimMonad m, Unbox a)
|
||||
=> MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
|
||||
{-# INLINE unsafeGrow #-}
|
||||
unsafeGrow = G.unsafeGrow
|
||||
|
||||
-- Restricting memory usage
|
||||
-- ------------------------
|
||||
|
||||
-- | Reset all elements of the vector to some undefined value, clearing all
|
||||
-- references to external objects. This is usually a noop for unboxed vectors.
|
||||
clear :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> m ()
|
||||
{-# INLINE clear #-}
|
||||
clear = G.clear
|
||||
|
||||
-- Accessing individual elements
|
||||
-- -----------------------------
|
||||
|
||||
-- | Yield the element at the given position.
|
||||
read :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> Int -> m a
|
||||
{-# INLINE read #-}
|
||||
read = G.read
|
||||
|
||||
-- | Replace the element at the given position.
|
||||
write :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> Int -> a -> m ()
|
||||
{-# INLINE write #-}
|
||||
write = G.write
|
||||
|
||||
-- | Modify the element at the given position.
|
||||
modify :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> (a -> a) -> Int -> m ()
|
||||
{-# INLINE modify #-}
|
||||
modify = G.modify
|
||||
|
||||
-- | Swap the elements at the given positions.
|
||||
swap :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> Int -> Int -> m ()
|
||||
{-# INLINE swap #-}
|
||||
swap = G.swap
|
||||
|
||||
|
||||
-- | Yield the element at the given position. No bounds checks are performed.
|
||||
unsafeRead :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> Int -> m a
|
||||
{-# INLINE unsafeRead #-}
|
||||
unsafeRead = G.unsafeRead
|
||||
|
||||
-- | Replace the element at the given position. No bounds checks are performed.
|
||||
unsafeWrite
|
||||
:: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> Int -> a -> m ()
|
||||
{-# INLINE unsafeWrite #-}
|
||||
unsafeWrite = G.unsafeWrite
|
||||
|
||||
-- | Modify the element at the given position. No bounds checks are performed.
|
||||
unsafeModify :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> (a -> a) -> Int -> m ()
|
||||
{-# INLINE unsafeModify #-}
|
||||
unsafeModify = G.unsafeModify
|
||||
|
||||
-- | Swap the elements at the given positions. No bounds checks are performed.
|
||||
unsafeSwap
|
||||
:: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> Int -> Int -> m ()
|
||||
{-# INLINE unsafeSwap #-}
|
||||
unsafeSwap = G.unsafeSwap
|
||||
|
||||
-- Filling and copying
|
||||
-- -------------------
|
||||
|
||||
-- | Set all elements of the vector to the given value.
|
||||
set :: (PrimMonad m, Unbox a) => MVector (PrimState m) a -> a -> m ()
|
||||
{-# INLINE set #-}
|
||||
set = G.set
|
||||
|
||||
-- | Copy a vector. The two vectors must have the same length and may not
|
||||
-- overlap.
|
||||
copy :: (PrimMonad m, Unbox a)
|
||||
=> MVector (PrimState m) a -- ^ target
|
||||
-> MVector (PrimState m) a -- ^ source
|
||||
-> m ()
|
||||
{-# INLINE copy #-}
|
||||
copy = G.copy
|
||||
|
||||
-- | Copy a vector. The two vectors must have the same length and may not
|
||||
-- overlap. This is not checked.
|
||||
unsafeCopy :: (PrimMonad m, Unbox a)
|
||||
=> MVector (PrimState m) a -- ^ target
|
||||
-> MVector (PrimState m) a -- ^ source
|
||||
-> m ()
|
||||
{-# INLINE unsafeCopy #-}
|
||||
unsafeCopy = G.unsafeCopy
|
||||
|
||||
-- | Move the contents of a vector. The two vectors must have the same
|
||||
-- length.
|
||||
--
|
||||
-- If the vectors do not overlap, then this is equivalent to 'copy'.
|
||||
-- Otherwise, the copying is performed as if the source vector were
|
||||
-- copied to a temporary vector and then the temporary vector was copied
|
||||
-- to the target vector.
|
||||
move :: (PrimMonad m, Unbox a)
|
||||
=> MVector (PrimState m) a -> MVector (PrimState m) a -> m ()
|
||||
{-# INLINE move #-}
|
||||
move = G.move
|
||||
|
||||
-- | Move the contents of a vector. The two vectors must have the same
|
||||
-- length, but this is not checked.
|
||||
--
|
||||
-- If the vectors do not overlap, then this is equivalent to 'unsafeCopy'.
|
||||
-- Otherwise, the copying is performed as if the source vector were
|
||||
-- copied to a temporary vector and then the temporary vector was copied
|
||||
-- to the target vector.
|
||||
unsafeMove :: (PrimMonad m, Unbox a)
|
||||
=> MVector (PrimState m) a -- ^ target
|
||||
-> MVector (PrimState m) a -- ^ source
|
||||
-> m ()
|
||||
{-# INLINE unsafeMove #-}
|
||||
unsafeMove = G.unsafeMove
|
||||
|
||||
-- | Compute the next (lexicographically) permutation of given vector in-place.
|
||||
-- Returns False when input is the last permtuation
|
||||
nextPermutation :: (PrimMonad m,Ord e,Unbox e) => MVector (PrimState m) e -> m Bool
|
||||
{-# INLINE nextPermutation #-}
|
||||
nextPermutation = G.nextPermutation
|
||||
|
||||
#define DEFINE_MUTABLE
|
||||
#include "unbox-tuple-instances"
|
Loading…
Add table
Add a link
Reference in a new issue