diff options
Diffstat (limited to 'VectorRing.hs')
-rw-r--r-- | VectorRing.hs | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/VectorRing.hs b/VectorRing.hs index 2ddac72..4362fa7 100644 --- a/VectorRing.hs +++ b/VectorRing.hs | |||
@@ -1,17 +1,36 @@ | |||
1 | module VectorRing where | 1 | module VectorRing where |
2 | 2 | ||
3 | import Data.Vector.Unboxed.Mutable | 3 | import Data.Vector.Storable.Mutable |
4 | import RingBuffer | 4 | import RingBuffer |
5 | 5 | ||
6 | import Foreign.Ptr | ||
7 | import Foreign.ForeignPtr | ||
8 | import Foreign.Storable | ||
9 | import GHC.Exts (RealWorld) | ||
10 | |||
11 | data Point = Point | ||
12 | { pointX :: Double | ||
13 | , pointY :: Double | ||
14 | } | ||
15 | deriving (Eq,Ord,Show) | ||
16 | |||
17 | instance Storable Point where | ||
18 | sizeOf _ = 2 * sizeOf (0::Double) | ||
19 | alignment _ = alignment (0::Double) | ||
20 | peek ptr = Point <$> peek (castPtr ptr) <*> peekElemOff (castPtr ptr) 1 | ||
21 | poke ptr (Point x y) = do poke (castPtr ptr) x | ||
22 | pokeElemOff (castPtr ptr) 1 y | ||
23 | |||
6 | -- | Typical usage: | 24 | -- | Typical usage: |
7 | -- | 25 | -- |
8 | -- > ringBuffer <- newRing capacity (VectorRing.new capacity) | 26 | -- > v <- unsafeNew capacity |
9 | new :: Unbox a => Int -> IO (TargetBuffer a) | 27 | -- > ringBuffer <- newRing capacity (VectorRing.new v) |
10 | new sz = do | 28 | new :: Storable a => MVector RealWorld a -> IO (TargetBuffer a) |
11 | v <- unsafeNew sz | 29 | new v = return TargetBuffer |
12 | return () :: IO () | 30 | { syncBuffer = \_ -> return () |
13 | return TargetBuffer | 31 | , updateBuffer = \i u -> write v i u |
14 | { syncBuffer = \_ -> return () | 32 | } |
15 | , updateBuffer = \i u -> write v i u | ||
16 | } | ||
17 | 33 | ||
34 | withData :: Storable a => MVector RealWorld a -> (Int -> Ptr a -> IO b) -> IO b | ||
35 | withData v f = let (fptr,len) = unsafeToForeignPtr0 v | ||
36 | in withForeignPtr fptr $ f len | ||