Commit 10860cd9 authored by Kai Zhang's avatar Kai Zhang

implement freeze and delEdges

parent 54994870
-- Initial igraph-bindings.cabal generated by cabal init. For further -- Initial igraph-bindings.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/ -- documentation, see http://haskell.org/cabal/users-guide/
name: haskell-igraph name: haskell-igraph
version: 0.1.0 version: 0.1.0
-- synopsis: -- synopsis:
-- description: -- description:
license: MIT license: MIT
license-file: LICENSE license-file: LICENSE
author: Kai Zhang author: Kai Zhang
maintainer: kai@kzhang.org maintainer: kai@kzhang.org
-- copyright: -- copyright:
category: Math category: Math
build-type: Simple build-type: Simple
extra-source-files: cbits extra-source-files: cbits/haskelligraph.c
cabal-version: >=1.22 cabal-version: >=1.22
library library
exposed-modules: exposed-modules:
IGraph.Internal.Initialization IGraph.Internal.Initialization
IGraph.Internal.Constants IGraph.Internal.Constants
IGraph.Internal.Arpack IGraph.Internal.Arpack
...@@ -35,8 +35,8 @@ library ...@@ -35,8 +35,8 @@ library
IGraph.Community IGraph.Community
IGraph.Read IGraph.Read
IGraph.Layout IGraph.Layout
-- other-modules: -- other-modules:
-- other-extensions: -- other-extensions:
build-depends: build-depends:
base >=4.0 && <5.0 base >=4.0 && <5.0
, bytestring >=0.9 , bytestring >=0.9
...@@ -58,12 +58,12 @@ test-suite tests ...@@ -58,12 +58,12 @@ test-suite tests
type: exitcode-stdio-1.0 type: exitcode-stdio-1.0
hs-source-dirs: tests hs-source-dirs: tests
main-is: test.hs main-is: test.hs
other-modules: other-modules:
Test.Basic Test.Basic
Test.Utils Test.Utils
default-language: Haskell2010 default-language: Haskell2010
build-depends: build-depends:
base base
, haskell-igraph , haskell-igraph
, tasty , tasty
......
...@@ -9,6 +9,7 @@ module IGraph ...@@ -9,6 +9,7 @@ module IGraph
, fromLabeledEdges , fromLabeledEdges
, unsafeFreeze , unsafeFreeze
, freeze
, unsafeThaw , unsafeThaw
, thaw , thaw
...@@ -101,6 +102,11 @@ unsafeFreeze (MLGraph g) = return $ LGraph g labToId ...@@ -101,6 +102,11 @@ unsafeFreeze (MLGraph g) = return $ LGraph g labToId
nV = igraphVcount g nV = igraphVcount g
labels = map (read . igraphCattributeVAS g vertexAttr) [0 .. nV-1] labels = map (read . igraphCattributeVAS g vertexAttr) [0 .. nV-1]
freeze :: (Hashable v, Eq v, Read v, PrimMonad m) => MLGraph (PrimState m) d v e -> m (LGraph d v e)
freeze (MLGraph g) = do
g' <- unsafePrimToPrim $ igraphCopy g
unsafeFreeze (MLGraph g')
unsafeThaw :: PrimMonad m => LGraph d v e -> m (MLGraph (PrimState m) d v e) unsafeThaw :: PrimMonad m => LGraph d v e -> m (MLGraph (PrimState m) d v e)
unsafeThaw (LGraph g _) = return $ MLGraph g unsafeThaw (LGraph g _) = return $ MLGraph g
...@@ -130,4 +136,3 @@ pre gr i = unsafePerformIO $ do ...@@ -130,4 +136,3 @@ pre gr i = unsafePerformIO $ do
igraphVsAdj vs i IgraphIn igraphVsAdj vs i IgraphIn
vit <- igraphVitNew (_graph gr) vs vit <- igraphVitNew (_graph gr) vs
vitToList vit vitToList vit
...@@ -80,6 +80,8 @@ vitToList vit = do ...@@ -80,6 +80,8 @@ vitToList vit = do
{#fun igraph_es_all as ^ { +, `EdgeOrderType' } -> `IGraphEsPtr' #} {#fun igraph_es_all as ^ { +, `EdgeOrderType' } -> `IGraphEsPtr' #}
{# fun igraph_es_vector as ^ { +, `VectorPtr' } -> `IGraphEsPtr' #}
-- Edge iterator -- Edge iterator
...@@ -124,3 +126,6 @@ eitToList eit = do ...@@ -124,3 +126,6 @@ eitToList eit = do
acc <- eitToList eit acc <- eitToList eit
return $ cur : acc return $ cur : acc
-- delete edges
{# fun igraph_delete_edges as ^ { `IGraphPtr', %`IGraphEsPtr' } -> `Int' #}
...@@ -5,6 +5,7 @@ import Foreign ...@@ -5,6 +5,7 @@ import Foreign
import Control.Monad.Primitive import Control.Monad.Primitive
import IGraph.Internal.Graph import IGraph.Internal.Graph
import IGraph.Internal.Selector
import IGraph.Internal.Data import IGraph.Internal.Data
import IGraph.Internal.Attribute import IGraph.Internal.Attribute
import IGraph.Internal.Initialization import IGraph.Internal.Initialization
...@@ -44,6 +45,8 @@ class MGraph d where ...@@ -44,6 +45,8 @@ class MGraph d where
addLEdges :: (PrimMonad m, Show e) => [LEdge e] -> MLGraph (PrimState m) d v e -> m () addLEdges :: (PrimMonad m, Show e) => [LEdge e] -> MLGraph (PrimState m) d v e -> m ()
delEdges :: PrimMonad m => [(Int, Int)] -> MLGraph (PrimState m) d v e -> m ()
data U data U
data D data D
...@@ -66,6 +69,14 @@ instance MGraph U where ...@@ -66,6 +69,14 @@ instance MGraph U where
where where
(xs, vs) = unzip $ map ( \(a,b,v) -> ([fromIntegral a, fromIntegral b], v) ) es (xs, vs) = unzip $ map ( \(a,b,v) -> ([fromIntegral a, fromIntegral b], v) ) es
delEdges es (MLGraph g) = unsafePrimToPrim $ do
vptr <- listToVector $ map fromIntegral eids
esptr <- igraphEsVector vptr
igraphDeleteEdges g esptr
return ()
where
eids = flip map es $ \(fr, to) -> igraphGetEid g fr to False True
instance MGraph D where instance MGraph D where
new n = unsafePrimToPrim $ igraphInit >>= igraphNew n True >>= return . MLGraph new n = unsafePrimToPrim $ igraphInit >>= igraphNew n True >>= return . MLGraph
...@@ -84,3 +95,11 @@ instance MGraph D where ...@@ -84,3 +95,11 @@ instance MGraph D where
withVectorPPtr vptr $ \p -> igraphAddEdges g vec $ castPtr p withVectorPPtr vptr $ \p -> igraphAddEdges g vec $ castPtr p
where where
(xs, vs) = unzip $ map ( \(a,b,v) -> ([fromIntegral a, fromIntegral b], v) ) es (xs, vs) = unzip $ map ( \(a,b,v) -> ([fromIntegral a, fromIntegral b], v) ) es
delEdges es (MLGraph g) = unsafePrimToPrim $ do
vptr <- listToVector $ map fromIntegral eids
esptr <- igraphEsVector vptr
igraphDeleteEdges g esptr
return ()
where
eids = flip map es $ \(fr, to) -> igraphGetEid g fr to True True
...@@ -2,6 +2,7 @@ module Test.Basic ...@@ -2,6 +2,7 @@ module Test.Basic
( tests ( tests
) where ) where
import Control.Monad.ST
import Test.Tasty import Test.Tasty
import Test.Tasty.HUnit import Test.Tasty.HUnit
import Test.Utils import Test.Utils
...@@ -9,10 +10,12 @@ import System.IO.Unsafe ...@@ -9,10 +10,12 @@ import System.IO.Unsafe
import Data.List import Data.List
import IGraph import IGraph
import IGraph.Mutable
tests :: TestTree tests :: TestTree
tests = testGroup "Basic tests" tests = testGroup "Basic tests"
[ graphCreation [ graphCreation
, graphEdit
] ]
graphCreation :: TestTree graphCreation :: TestTree
...@@ -26,3 +29,13 @@ graphCreation = testGroup "Graph creation" ...@@ -26,3 +29,13 @@ graphCreation = testGroup "Graph creation"
edgeList = sort $ unsafePerformIO $ randEdges 1000 100 edgeList = sort $ unsafePerformIO $ randEdges 1000 100
gr = mkGraph (100,Nothing) (edgeList, Nothing) :: LGraph D () () gr = mkGraph (100,Nothing) (edgeList, Nothing) :: LGraph D () ()
simple = mkGraph (3,Nothing) ([(0,1),(1,2),(2,0)],Nothing) :: LGraph D () () simple = mkGraph (3,Nothing) ([(0,1),(1,2),(2,0)],Nothing) :: LGraph D () ()
graphEdit :: TestTree
graphEdit = testGroup "Graph editing"
[ testCase "" $ [(1,2)] @=? (sort $ edges simple') ]
where
simple = mkGraph (3,Nothing) ([(0,1),(1,2),(2,0)],Nothing) :: LGraph U () ()
simple' = runST $ do
g <- thaw simple
delEdges [(0,1),(0,2)] g
freeze g
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