Commit c6136304 authored by Kai Zhang's avatar Kai Zhang

refinement

parent 565fc95d
...@@ -3,20 +3,6 @@ ...@@ -3,20 +3,6 @@
#include <igraph/igraph.h> #include <igraph/igraph.h>
igraph_vector_t* igraph_vector_new(long int size)
{
igraph_vector_t* vector = (igraph_vector_t*) malloc (sizeof (igraph_vector_t));
igraph_vector_init(vector, size);
return vector;
}
igraph_vector_ptr_t* igraph_vector_ptr_new(long int size)
{
igraph_vector_ptr_t* vptr = (igraph_vector_ptr_t*) malloc (sizeof (igraph_vector_ptr_t));
igraph_vector_ptr_init(vptr, size);
return vptr;
}
igraph_strvector_t* igraph_strvector_new(long int size) igraph_strvector_t* igraph_strvector_new(long int size)
{ {
igraph_strvector_t* vector = (igraph_strvector_t*) malloc (sizeof (igraph_strvector_t)); igraph_strvector_t* vector = (igraph_strvector_t*) malloc (sizeof (igraph_strvector_t));
......
...@@ -14,7 +14,8 @@ cliques :: (Int, Int) -- ^ Minimum and maximum size of the cliques to be return ...@@ -14,7 +14,8 @@ cliques :: (Int, Int) -- ^ Minimum and maximum size of the cliques to be return
-- No bound will be used if negative or zero -- No bound will be used if negative or zero
-> LGraph d v e -> LGraph d v e
-> [[Int]] -- ^ cliques represented by node ids -> [[Int]] -- ^ cliques represented by node ids
cliques (lo, hi) (LGraph g) = unsafePerformIO $ allocaVectorP $ \vpptr -> do cliques (lo, hi) (LGraph g) = unsafePerformIO $ do
vpptr <- igraphVectorPtrNew 0
_ <- igraphCliques g vpptr lo hi _ <- igraphCliques g vpptr lo hi
(map.map) truncate <$> vectorPPtrToList vpptr (map.map) truncate <$> vectorPPtrToList vpptr
...@@ -22,6 +23,7 @@ maximalCliques :: (Int, Int) -- ^ Minimum and maximum size of the cliques to be ...@@ -22,6 +23,7 @@ maximalCliques :: (Int, Int) -- ^ Minimum and maximum size of the cliques to be
-- No bound will be used if negative or zero -- No bound will be used if negative or zero
-> LGraph d v e -> LGraph d v e
-> [[Int]] -- ^ cliques represented by node ids -> [[Int]] -- ^ cliques represented by node ids
maximalCliques (lo, hi) (LGraph g) = unsafePerformIO $ allocaVectorP $ \vpptr -> do maximalCliques (lo, hi) (LGraph g) = unsafePerformIO $ do
vpptr <- igraphVectorPtrNew 0
_ <- igraphMaximalCliques g vpptr lo hi _ <- igraphMaximalCliques g vpptr lo hi
(map.map) truncate <$> vectorPPtrToList vpptr (map.map) truncate <$> vectorPPtrToList vpptr
...@@ -22,18 +22,14 @@ communityLeadingEigenvector :: LGraph U v e ...@@ -22,18 +22,14 @@ communityLeadingEigenvector :: LGraph U v e
-> Int -- ^ number of steps -> Int -- ^ number of steps
-> [[Int]] -> [[Int]]
communityLeadingEigenvector g@(LGraph gr) ws step = unsafePerformIO $ do communityLeadingEigenvector g@(LGraph gr) ws step = unsafePerformIO $ do
arparck <- igraphArpackNew ap <- igraphArpackNew
vec <- igraphVectorNew 0 vptr <- igraphVectorNew 0
withArpackOptPtr arparck $ \ap -> withVectorPtr vec $ \vptr -> case ws of wptr <- case ws of
Just xs -> do Just w -> listToVector w
ws' <- listToVector xs _ -> liftM VectorPtr $ newForeignPtr_ $ castPtr nullPtr
withVectorPtr ws' $ \wptr -> igraphCommunityLeadingEigenvector gr wptr nullPtr vptr step ap nullPtr
igraphCommunityLeadingEigenvector gr wptr nullPtr vptr step ap nullPtr False nullPtr nullPtr nullPtr nullFunPtr nullPtr
False nullPtr nullPtr nullPtr nullFunPtr nullPtr xs <- vectorPtrToList vptr
_ -> igraphCommunityLeadingEigenvector gr nullPtr nullPtr vptr step ap nullPtr
False nullPtr nullPtr nullPtr nullFunPtr nullPtr
xs <- vectorPtrToList vec
return $ map f $ groupBy ((==) `on` snd) $ sortBy (comparing snd) $ zip [0..] xs return $ map f $ groupBy ((==) `on` snd) $ sortBy (comparing snd) $ zip [0..] xs
where where
f = fst . unzip f = fst . unzip
...@@ -11,15 +11,15 @@ import Foreign.C.Types ...@@ -11,15 +11,15 @@ import Foreign.C.Types
#include "cbits/haskelligraph.c" #include "cbits/haskelligraph.c"
{#fun igraph_community_leading_eigenvector as ^ { `IGraphPtr' {#fun igraph_community_leading_eigenvector as ^ { `IGraphPtr'
, id `Ptr VectorPtr' , `VectorPtr'
, id `Ptr MatrixPtr' , id `Ptr MatrixPtr'
, id `Ptr VectorPtr' , `VectorPtr'
, `Int' , `Int'
, id `Ptr ArpackOptPtr' , `ArpackOptPtr'
, id `Ptr CDouble' , id `Ptr CDouble'
, `Bool' , `Bool'
, id `Ptr VectorPtr' , id `Ptr VectorPtr'
, `VectorPPtr' , id `Ptr VectorPPtr'
, id `Ptr VectorPtr' , id `Ptr VectorPtr'
, id `T' , id `T'
, id `Ptr ()' , id `Ptr ()'
......
...@@ -14,7 +14,7 @@ import System.IO.Unsafe (unsafePerformIO) ...@@ -14,7 +14,7 @@ import System.IO.Unsafe (unsafePerformIO)
-- Construtors and destructors -- Construtors and destructors
{#fun igraph_vector_new as ^ { `Int' } -> `VectorPtr' #} {#fun igraph_vector_init as igraphVectorNew { +, `Int' } -> `VectorPtr' #}
listToVector :: [Double] -> IO VectorPtr listToVector :: [Double] -> IO VectorPtr
listToVector xs = do listToVector xs = do
...@@ -55,13 +55,9 @@ vectorPtrToList vptr = do ...@@ -55,13 +55,9 @@ vectorPtrToList vptr = do
{#fun igraph_vector_size as ^ { `VectorPtr' } -> `Int' #} {#fun igraph_vector_size as ^ { `VectorPtr' } -> `Int' #}
data VectorP {#pointer *igraph_vector_ptr_t as VectorPPtr foreign finalizer igraph_vector_ptr_destroy_all newtype#}
{#pointer *igraph_vector_ptr_t as VectorPPtr -> VectorP #}
{#fun igraph_vector_ptr_new as ^ { `Int' } -> `VectorPPtr' #} {#fun igraph_vector_ptr_init as igraphVectorPtrNew { +, `Int' } -> `VectorPPtr' #}
{#fun igraph_vector_ptr_destroy as ^ { `VectorPPtr' } -> `()' #}
{#fun igraph_vector_ptr_destroy_all as ^ { `VectorPPtr' } -> `()' #}
{#fun igraph_vector_ptr_e as ^ { `VectorPPtr', `Int' } -> `Ptr ()' #} {#fun igraph_vector_ptr_e as ^ { `VectorPPtr', `Int' } -> `Ptr ()' #}
{#fun igraph_vector_ptr_set as ^ { `VectorPPtr', `Int', id `Ptr ()' } -> `()' #} {#fun igraph_vector_ptr_set as ^ { `VectorPPtr', `Int', id `Ptr ()' } -> `()' #}
...@@ -83,13 +79,6 @@ vectorPPtrToList vpptr = do ...@@ -83,13 +79,6 @@ vectorPPtrToList vpptr = do
fptr <- newForeignPtr_ $ castPtr vptr fptr <- newForeignPtr_ $ castPtr vptr
vectorPtrToList $ VectorPtr fptr vectorPtrToList $ VectorPtr fptr
allocaVectorP :: (VectorPPtr -> IO b) -> IO b
allocaVectorP fn = do
vptr <- igraphVectorPtrNew 0
r <- fn vptr
igraphVectorPtrDestroyAll vptr
return r
data StrVector data StrVector
{#pointer *igraph_strvector_t as StrVectorPtr -> StrVector #} {#pointer *igraph_strvector_t as StrVectorPtr -> StrVector #}
......
...@@ -50,7 +50,7 @@ instance MGraph MLGraph U where ...@@ -50,7 +50,7 @@ instance MGraph MLGraph U where
alloca $ \ptr -> do alloca $ \ptr -> do
poke ptr attr poke ptr attr
vptr <- listToVectorP [castPtr ptr] vptr <- listToVectorP [castPtr ptr]
igraphAddVertices g n (castPtr vptr) withVectorPPtr vptr $ \p -> igraphAddVertices g n $ castPtr p
addEdges es (MLGraph g) = unsafePrimToPrim $ do addEdges es (MLGraph g) = unsafePrimToPrim $ do
vec <- listToVector xs vec <- listToVector xs
...@@ -64,6 +64,6 @@ instance MGraph MLGraph U where ...@@ -64,6 +64,6 @@ instance MGraph MLGraph U where
alloca $ \ptr -> do alloca $ \ptr -> do
poke ptr attr poke ptr attr
vptr <- listToVectorP [castPtr ptr] vptr <- listToVectorP [castPtr ptr]
igraphAddEdges g vec (castPtr vptr) 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
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