Commit 67d1be7c authored by Kai Zhang's avatar Kai Zhang

add edges

parent 02184221
...@@ -18,6 +18,20 @@ char** igraph_strvector_get_(igraph_strvector_t* s, long int i) ...@@ -18,6 +18,20 @@ char** igraph_strvector_get_(igraph_strvector_t* s, long int i)
return x; return x;
} }
igraph_arpack_options_t* igraph_arpack_new()
{
igraph_arpack_options_t *arpack = (igraph_arpack_options_t*) malloc(sizeof(igraph_arpack_options_t));
igraph_arpack_options_init(arpack);
return arpack;
}
void igraph_arpack_destroy(igraph_arpack_options_t* arpack)
{
if (arpack)
free(arpack);
arpack = NULL;
}
void haskelligraph_init() void haskelligraph_init()
{ {
/* attach attribute table */ /* attach attribute table */
......
...@@ -45,6 +45,7 @@ library ...@@ -45,6 +45,7 @@ library
, unordered-containers , unordered-containers
, hashable , hashable
, split , split
, data-default-class
extra-libraries: igraph extra-libraries: igraph
hs-source-dirs: src hs-source-dirs: src
...@@ -52,3 +53,21 @@ library ...@@ -52,3 +53,21 @@ library
build-tools: c2hs >=0.25.0 build-tools: c2hs >=0.25.0
C-Sources: C-Sources:
cbits/haskelligraph.c cbits/haskelligraph.c
test-suite tests
type: exitcode-stdio-1.0
hs-source-dirs: tests
main-is: test.hs
other-modules:
default-language: Haskell2010
build-depends:
base
, haskell-igraph
, tasty
, tasty-golden
, tasty-hunit
source-repository head
type: git
location: https://github.com/kaizhang/haskell-igraph.git
{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleContexts #-}
module IGraph where module IGraph
( LGraph(..)
, U
, D
, Graph(..)
, mkGraph
, unsafeFreeze
, unsafeThaw
, thaw
, neighbors
, pre
, suc
) where
import Control.Monad (liftM) import Control.Monad (liftM)
import Control.Monad.ST (runST) import Control.Monad.ST (runST)
...@@ -16,10 +30,13 @@ import IGraph.Internal.Constants ...@@ -16,10 +30,13 @@ import IGraph.Internal.Constants
import IGraph.Internal.Attribute import IGraph.Internal.Attribute
import IGraph.Internal.Selector import IGraph.Internal.Selector
type Node = Int
type Edge = (Node, Node)
-- | graph with labeled nodes and edges -- | graph with labeled nodes and edges
data LGraph d v e = LGraph data LGraph d v e = LGraph
{ _graph :: IGraphPtr { _graph :: IGraphPtr
, _nodeLabelToId :: M.HashMap v [Int] } , _labelToNode :: M.HashMap v [Node] }
class MGraph d => Graph d where class MGraph d => Graph d where
...@@ -29,10 +46,17 @@ class MGraph d => Graph d where ...@@ -29,10 +46,17 @@ 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
nodeLab :: Read v => LGraph d v e -> Int -> v edges :: LGraph d v e -> [Edge]
edges (LGraph g _) = unsafePerformIO $ do
es <- igraphEsAll IgraphEdgeorderFrom
eit <- igraphEitNew g es
eids <- eitToList eit
mapM (igraphEdge g) eids
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
edgeLab :: Read e => LGraph d v e -> (Int, Int) -> e edgeLab :: Read e => LGraph d v e -> Edge -> e
edgeLab (LGraph g _) (fr,to) = read $ igraphCattributeEAS g edgeAttr $ igraphGetEid g fr to True True edgeLab (LGraph g _) (fr,to) = read $ igraphCattributeEAS g edgeAttr $ igraphGetEid g fr to True True
edgeLabByEid :: Read e => LGraph d v e -> Int -> e edgeLabByEid :: Read e => LGraph d v e -> Int -> e
...@@ -42,7 +66,7 @@ class MGraph d => Graph d where ...@@ -42,7 +66,7 @@ class MGraph d => Graph d where
instance Graph U where instance Graph U where
mkGraph :: (Graph d, Hashable v, Read v, Eq v, Show v, Show e) => (Int, Maybe [v]) -> ([(Int, Int)], 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
mkGraph (n, vattr) (es,eattr) = runST $ do mkGraph (n, vattr) (es,eattr) = runST $ do
g <- new 0 g <- new 0
let addV | isNothing vattr = addNodes n g let addV | isNothing vattr = addNodes n g
...@@ -70,7 +94,7 @@ unsafeThaw (LGraph g _) = return $ MLGraph g ...@@ -70,7 +94,7 @@ unsafeThaw (LGraph g _) = return $ MLGraph g
thaw :: (PrimMonad m, Graph d) => LGraph d v e -> m (MLGraph (PrimState m) d v e) thaw :: (PrimMonad m, Graph d) => LGraph d v e -> m (MLGraph (PrimState m) d v e)
thaw (LGraph g _) = unsafePrimToPrim . liftM MLGraph . igraphCopy $ g thaw (LGraph g _) = unsafePrimToPrim . liftM MLGraph . igraphCopy $ g
neighbors :: LGraph d v e -> Int -> [Int] neighbors :: LGraph d v e -> Node -> [Node]
neighbors gr i = unsafePerformIO $ do neighbors gr i = unsafePerformIO $ do
vs <- igraphVsNew vs <- igraphVsNew
igraphVsAdj vs i IgraphAll igraphVsAdj vs i IgraphAll
...@@ -78,7 +102,7 @@ neighbors gr i = unsafePerformIO $ do ...@@ -78,7 +102,7 @@ neighbors gr i = unsafePerformIO $ do
vitToList vit vitToList vit
-- | Find all Nodes that have a link from the given Node. -- | Find all Nodes that have a link from the given Node.
suc :: LGraph D v e -> Int -> [Int] suc :: LGraph D v e -> Node -> [Node]
suc gr i = unsafePerformIO $ do suc gr i = unsafePerformIO $ do
vs <- igraphVsNew vs <- igraphVsNew
igraphVsAdj vs i IgraphOut igraphVsAdj vs i IgraphOut
...@@ -86,7 +110,7 @@ suc gr i = unsafePerformIO $ do ...@@ -86,7 +110,7 @@ suc gr i = unsafePerformIO $ do
vitToList vit vitToList vit
-- | Find all Nodes that link to to the given Node. -- | Find all Nodes that link to to the given Node.
pre :: LGraph D v e -> Int -> [Int] pre :: LGraph D v e -> Node -> [Node]
pre gr i = unsafePerformIO $ do pre gr i = unsafePerformIO $ do
vs <- igraphVsNew vs <- igraphVsNew
igraphVsAdj vs i IgraphIn igraphVsAdj vs i IgraphIn
......
...@@ -9,20 +9,4 @@ import Foreign.C.Types ...@@ -9,20 +9,4 @@ import Foreign.C.Types
{#pointer *igraph_arpack_options_t as ArpackOptPtr foreign finalizer igraph_arpack_destroy newtype#} {#pointer *igraph_arpack_options_t as ArpackOptPtr foreign finalizer igraph_arpack_destroy newtype#}
#c
igraph_arpack_options_t* igraph_arpack_new()
{
igraph_arpack_options_t *arpack = (igraph_arpack_options_t*) malloc(sizeof(igraph_arpack_options_t));
igraph_arpack_options_init(arpack);
return arpack;
}
void igraph_arpack_destroy(igraph_arpack_options_t* arpack)
{
if (arpack)
free(arpack);
arpack = NULL;
}
#endc
{#fun igraph_arpack_new as ^ { } -> `ArpackOptPtr' #} {#fun igraph_arpack_new as ^ { } -> `ArpackOptPtr' #}
...@@ -6,3 +6,5 @@ import Foreign ...@@ -6,3 +6,5 @@ import Foreign
#include "cbits/haskelligraph.c" #include "cbits/haskelligraph.c"
{#enum igraph_neimode_t as Neimode {underscoreToCase} deriving (Show, Eq) #} {#enum igraph_neimode_t as Neimode {underscoreToCase} deriving (Show, Eq) #}
{#enum igraph_edgeorder_type_t as EdgeOrderType {underscoreToCase} deriving (Show, Eq) #}
...@@ -8,6 +8,7 @@ import System.IO.Unsafe (unsafePerformIO) ...@@ -8,6 +8,7 @@ import System.IO.Unsafe (unsafePerformIO)
{#import IGraph.Internal.Initialization #} {#import IGraph.Internal.Initialization #}
{#import IGraph.Internal.Data #} {#import IGraph.Internal.Data #}
{#import IGraph.Internal.Constants #}
#include "cbits/haskelligraph.c" #include "cbits/haskelligraph.c"
...@@ -31,6 +32,14 @@ igraphNew n directed _ = igraphNew' n directed ...@@ -31,6 +32,14 @@ igraphNew n directed _ = igraphNew' n directed
{#fun pure igraph_get_eid_ as igraphGetEid { `IGraphPtr', `Int', `Int', `Bool', `Bool' } -> `Int' #} {#fun pure igraph_get_eid_ as igraphGetEid { `IGraphPtr', `Int', `Int', `Bool', `Bool' } -> `Int' #}
{#fun igraph_edge as igraphEdge' { `IGraphPtr', `Int', id `Ptr CInt', id `Ptr CInt' } -> `Int' #}
igraphEdge :: IGraphPtr -> Int -> IO (Int, Int)
igraphEdge g i = alloca $ \fr -> alloca $ \to -> do
igraphEdge' g i fr to
fr' <- peek fr
to' <- peek to
return (fromIntegral fr', fromIntegral to')
-- Adding and Deleting Vertices and Edges -- Adding and Deleting Vertices and Edges
{# fun igraph_add_vertices as ^ { `IGraphPtr', `Int', id `Ptr ()' } -> `()' #} {# fun igraph_add_vertices as ^ { `IGraphPtr', `Int', id `Ptr ()' } -> `()' #}
......
...@@ -72,3 +72,55 @@ vitToList vit = do ...@@ -72,3 +72,55 @@ vitToList vit = do
igraphVitNext vit igraphVitNext vit
acc <- vitToList vit acc <- vitToList vit
return $ cur : acc return $ cur : acc
-- Edge Selector
{#pointer *igraph_es_t as IGraphEsPtr foreign finalizer igraph_es_destroy newtype #}
{#fun igraph_es_all as ^ { +, `EdgeOrderType' } -> `IGraphEsPtr' #}
-- Edge iterator
{#pointer *igraph_eit_t as IGraphEitPtr foreign finalizer igraph_eit_destroy newtype #}
#c
igraph_eit_t* igraph_eit_new(const igraph_t *graph, igraph_es_t es) {
igraph_eit_t* eit = (igraph_eit_t*) malloc (sizeof (igraph_eit_t));
igraph_eit_create(graph, es, eit);
return eit;
}
igraph_bool_t igraph_eit_end(igraph_eit_t *eit) {
return IGRAPH_EIT_END(*eit);
}
void igraph_eit_next(igraph_eit_t *eit) {
IGRAPH_EIT_NEXT(*eit);
}
igraph_integer_t igraph_eit_get(igraph_eit_t *eit) {
return IGRAPH_EIT_GET(*eit);
}
#endc
{#fun igraph_eit_new as ^ { `IGraphPtr', %`IGraphEsPtr' } -> `IGraphEitPtr' #}
{#fun igraph_eit_end as ^ { `IGraphEitPtr' } -> `Bool' #}
{#fun igraph_eit_next as ^ { `IGraphEitPtr' } -> `()' #}
{#fun igraph_eit_get as ^ { `IGraphEitPtr' } -> `Int' #}
eitToList :: IGraphEitPtr -> IO [Int]
eitToList eit = do
isEnd <- igraphEitEnd eit
if isEnd
then return []
else do
cur <- igraphEitGet eit
igraphEitNext eit
acc <- eitToList eit
return $ cur : acc
...@@ -5,6 +5,7 @@ module IGraph.Layout ...@@ -5,6 +5,7 @@ module IGraph.Layout
import Foreign (nullPtr) import Foreign (nullPtr)
import Control.Applicative ((<$>)) import Control.Applicative ((<$>))
import System.IO.Unsafe (unsafePerformIO) import System.IO.Unsafe (unsafePerformIO)
import Data.Default.Class
import IGraph import IGraph
import IGraph.Internal.Clique import IGraph.Internal.Clique
...@@ -16,6 +17,12 @@ data LayoutOpt = LayoutOpt ...@@ -16,6 +17,12 @@ data LayoutOpt = LayoutOpt
, _nIter :: Int , _nIter :: Int
} deriving (Show) } deriving (Show)
instance Default LayoutOpt where
def = LayoutOpt
{ _seed = Nothing
, _nIter = 10000
}
kamadaKawai :: Graph d => LGraph d v e -> Double -> Double -> Double -> Double -> LayoutOpt -> [(Double, Double)] kamadaKawai :: Graph d => LGraph d v e -> Double -> Double -> Double -> Double -> LayoutOpt -> [(Double, Double)]
kamadaKawai gr sigma initemp coolexp kkconst opt = unsafePerformIO $ do kamadaKawai gr sigma initemp coolexp kkconst opt = unsafePerformIO $ do
mptr <- mat mptr <- mat
......
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