Commit c6136304 authored by Kai Zhang's avatar Kai Zhang

refinement

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