Commit 62fb8045 authored by Kai Zhang's avatar Kai Zhang

add directed graph instance

parent 67d1be7c
...@@ -59,6 +59,8 @@ test-suite tests ...@@ -59,6 +59,8 @@ test-suite tests
hs-source-dirs: tests hs-source-dirs: tests
main-is: test.hs main-is: test.hs
other-modules: other-modules:
Test.Basic
Test.Utils
default-language: Haskell2010 default-language: Haskell2010
build-depends: build-depends:
...@@ -67,6 +69,7 @@ test-suite tests ...@@ -67,6 +69,7 @@ test-suite tests
, tasty , tasty
, tasty-golden , tasty-golden
, tasty-hunit , tasty-hunit
, random
source-repository head source-repository head
type: git type: git
......
...@@ -46,12 +46,19 @@ class MGraph d => Graph d where ...@@ -46,12 +46,19 @@ class MGraph d => Graph d where
nEdges :: LGraph d v e -> Int nEdges :: LGraph d v e -> Int
nEdges (LGraph g _) = igraphEcount g nEdges (LGraph g _) = igraphEcount g
{-
edges :: LGraph d v e -> [Edge] edges :: LGraph d v e -> [Edge]
edges (LGraph g _) = unsafePerformIO $ do edges (LGraph g _) = unsafePerformIO $ do
es <- igraphEsAll IgraphEdgeorderFrom es <- igraphEsAll IgraphEdgeorderFrom
eit <- igraphEitNew g es eit <- igraphEitNew g es
eids <- eitToList eit eids <- eitToList eit
mapM (igraphEdge g) eids mapM (igraphEdge g) eids
-}
edges :: LGraph d v e -> [Edge]
edges gr@(LGraph g _) = unsafePerformIO $ mapM (igraphEdge g) [0..n-1]
where
n = nEdges gr
nodeLab :: Read v => LGraph d v e -> Node -> v nodeLab :: Read v => LGraph d v e -> Node -> v
nodeLab (LGraph g _) i = read $ igraphCattributeVAS g vertexAttr i nodeLab (LGraph g _) i = read $ igraphCattributeVAS g vertexAttr i
...@@ -64,6 +71,7 @@ class MGraph d => Graph d where ...@@ -64,6 +71,7 @@ class MGraph d => Graph d where
instance Graph U where instance Graph U where
instance Graph D where
mkGraph :: (Graph d, Hashable v, Read v, Eq v, Show v, Show e) => (Node, Maybe [v]) -> ([Edge], Maybe [e]) -> LGraph d v e mkGraph :: (Graph d, Hashable v, Read v, Eq v, Show v, Show e) => (Node, Maybe [v]) -> ([Edge], Maybe [e]) -> LGraph d v e
......
...@@ -25,11 +25,20 @@ class MGraph d where ...@@ -25,11 +25,20 @@ class MGraph d where
new :: PrimMonad m => Int -> m (MLGraph (PrimState m) d v e) new :: PrimMonad m => Int -> m (MLGraph (PrimState m) d v e)
addNodes :: PrimMonad m => Int -> MLGraph(PrimState m) d v e -> m () addNodes :: PrimMonad m => Int -> MLGraph(PrimState m) d v e -> m ()
addNodes n (MLGraph g) = unsafePrimToPrim $ igraphAddVertices g n nullPtr
addLNodes :: (Show v, PrimMonad m) addLNodes :: (Show v, PrimMonad m)
=> Int -- ^ the number of new vertices add to the graph => Int -- ^ the number of new vertices add to the graph
-> [v] -- ^ vertices' labels -> [v] -- ^ vertices' labels
-> MLGraph (PrimState m) d v e -> m () -> MLGraph (PrimState m) d v e -> m ()
addLNodes n labels (MLGraph g)
| n /= length labels = error "addLVertices: incorrect number of labels"
| otherwise = unsafePrimToPrim $ do
let attr = makeAttributeRecord vertexAttr labels
alloca $ \ptr -> do
poke ptr attr
vptr <- listToVectorP [castPtr ptr]
withVectorPPtr vptr $ \p -> igraphAddVertices g n $ castPtr p
addEdges :: PrimMonad m => [(Int, Int)] -> MLGraph (PrimState m) d v e -> m () addEdges :: PrimMonad m => [(Int, Int)] -> MLGraph (PrimState m) d v e -> m ()
...@@ -41,16 +50,24 @@ data D ...@@ -41,16 +50,24 @@ data D
instance MGraph U where instance MGraph U where
new n = unsafePrimToPrim $ igraphInit >>= igraphNew n False >>= return . MLGraph new n = unsafePrimToPrim $ igraphInit >>= igraphNew n False >>= return . MLGraph
addNodes n (MLGraph g) = unsafePrimToPrim $ igraphAddVertices g n nullPtr addEdges es (MLGraph g) = unsafePrimToPrim $ do
vec <- listToVector xs
igraphAddEdges g vec nullPtr
where
xs = concatMap ( \(a,b) -> [fromIntegral a, fromIntegral b] ) es
addLNodes n labels (MLGraph g) addLEdges es (MLGraph g) = unsafePrimToPrim $ do
| n /= length labels = error "addLVertices: incorrect number of labels" vec <- listToVector $ concat xs
| otherwise = unsafePrimToPrim $ do let attr = makeAttributeRecord edgeAttr vs
let attr = makeAttributeRecord vertexAttr labels alloca $ \ptr -> do
alloca $ \ptr -> do poke ptr attr
poke ptr attr vptr <- listToVectorP [castPtr ptr]
vptr <- listToVectorP [castPtr ptr] withVectorPPtr vptr $ \p -> igraphAddEdges g vec $ castPtr p
withVectorPPtr vptr $ \p -> igraphAddVertices g n $ castPtr p where
(xs, vs) = unzip $ map ( \(a,b,v) -> ([fromIntegral a, fromIntegral b], v) ) es
instance MGraph D where
new n = unsafePrimToPrim $ igraphInit >>= igraphNew n True >>= return . MLGraph
addEdges es (MLGraph g) = unsafePrimToPrim $ do addEdges es (MLGraph g) = unsafePrimToPrim $ do
vec <- listToVector xs vec <- listToVector xs
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment