summaryrefslogtreecommitdiff
path: root/VectorRing.hs
diff options
context:
space:
mode:
Diffstat (limited to 'VectorRing.hs')
-rw-r--r--VectorRing.hs39
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 @@
1module VectorRing where 1module VectorRing where
2 2
3import Data.Vector.Unboxed.Mutable 3import Data.Vector.Storable.Mutable
4import RingBuffer 4import RingBuffer
5 5
6import Foreign.Ptr
7import Foreign.ForeignPtr
8import Foreign.Storable
9import GHC.Exts (RealWorld)
10
11data Point = Point
12 { pointX :: Double
13 , pointY :: Double
14 }
15 deriving (Eq,Ord,Show)
16
17instance 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
9new :: Unbox a => Int -> IO (TargetBuffer a) 27-- > ringBuffer <- newRing capacity (VectorRing.new v)
10new sz = do 28new :: Storable a => MVector RealWorld a -> IO (TargetBuffer a)
11 v <- unsafeNew sz 29new 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
34withData :: Storable a => MVector RealWorld a -> (Int -> Ptr a -> IO b) -> IO b
35withData v f = let (fptr,len) = unsafeToForeignPtr0 v
36 in withForeignPtr fptr $ f len