Commit 5b8ed8af authored by Kai Zhang's avatar Kai Zhang

minor

parent 0c650a74
...@@ -189,18 +189,20 @@ fromLabeledEdges es = mkGraph labels es' ...@@ -189,18 +189,20 @@ fromLabeledEdges es = mkGraph labels es'
labelToId = M.fromList $ zip labels [0..] labelToId = M.fromList $ zip labels [0..]
-- | Create a graph from a stream of labeled edges. -- | Create a graph from a stream of labeled edges.
fromLabeledEdges' :: (SingI d, Hashable v, Serialize v, Eq v, Serialize e) fromLabeledEdges' :: (MonadUnliftIO m, SingI d, Hashable v, Serialize v, Eq v, Serialize e)
=> a -- ^ Input, usually a file => a -- ^ Input, usually a file
-> (a -> ConduitT () ((v, v), e) IO ()) -- ^ deserialize the input into a stream of edges -> (a -> ConduitT () ((v, v), e) m ()) -- ^ deserialize the input into a stream of edges
-> IO (Graph d v e) -> m (Graph d v e)
fromLabeledEdges' input mkConduit = do fromLabeledEdges' input mkConduit = do
(labelToId, _, ne) <- runConduit $ mkConduit input .| (labelToId, _, ne) <- runConduit $ mkConduit input .|
foldlC f (M.empty, 0::Int, 0::Int) foldlC f (M.empty, 0::Int, 0::Int)
allocaVectorN (2*ne) $ \evec -> allocaBSVectorN ne $ \bsvec -> do let action evec bsvec = do
let getId x = M.lookupDefault undefined x labelToId let getId x = M.lookupDefault undefined x labelToId
runConduit $ mkConduit input .| runConduit $ mkConduit input .|
mapC (\((v1, v2), e) -> ((getId v1, getId v2), e)) .| mapC (\((v1, v2), e) -> ((getId v1, getId v2), e)) .|
deserializeGraph (fst $ unzip $ sortBy (comparing snd) $ M.toList labelToId) evec bsvec deserializeGraph (fst $ unzip $ sortBy (comparing snd) $ M.toList labelToId) evec bsvec
withRunInIO $ \runInIO -> allocaVectorN (2*ne) $ \evec ->
allocaBSVectorN ne $ \bsvec -> (runInIO $ action evec bsvec)
where where
f (vs, nn, ne) ((v1, v2), _) = f (vs, nn, ne) ((v1, v2), _) =
let (vs', nn') = add v1 $ add v2 (vs, nn) let (vs', nn') = add v1 $ add v2 (vs, nn)
...@@ -210,11 +212,11 @@ fromLabeledEdges' input mkConduit = do ...@@ -210,11 +212,11 @@ fromLabeledEdges' input mkConduit = do
then (m, i) then (m, i)
else (M.insert v i m, i + 1) else (M.insert v i m, i + 1)
deserializeGraph :: (SingI d, Hashable v, Serialize v, Eq v, Serialize e) deserializeGraph :: (MonadIO m, SingI d, Hashable v, Serialize v, Eq v, Serialize e)
=> [v] => [v]
-> Ptr Vector -- ^ a vector that is sufficient to hold all edges -> Ptr Vector -- ^ a vector that is sufficient to hold all edges
-> Ptr BSVector -> Ptr BSVector
-> ConduitT (LEdge e) o IO (Graph d v e) -> ConduitT (LEdge e) o m (Graph d v e)
deserializeGraph nds evec bsvec = do deserializeGraph nds evec bsvec = do
let f i ((fr, to), attr) = liftIO $ do let f i ((fr, to), attr) = liftIO $ do
igraphVectorSet evec (i*2) $ fromIntegral fr igraphVectorSet evec (i*2) $ fromIntegral fr
...@@ -222,8 +224,8 @@ deserializeGraph nds evec bsvec = do ...@@ -222,8 +224,8 @@ deserializeGraph nds evec bsvec = do
bsvectorSet bsvec i $ encode attr bsvectorSet bsvec i $ encode attr
return $ i + 1 return $ i + 1
_ <- foldMC f 0 _ <- foldMC f 0
gr@(MGraph g) <- new 0
liftIO $ do liftIO $ do
gr@(MGraph g) <- new 0
addLNodes nds gr addLNodes nds gr
withBSAttr edgeAttr bsvec $ \ptr -> withBSAttr edgeAttr bsvec $ \ptr ->
withPtrs [ptr] (igraphAddEdges g evec . castPtr) withPtrs [ptr] (igraphAddEdges g evec . castPtr)
......
...@@ -9,7 +9,6 @@ import Data.Hashable (Hashable) ...@@ -9,7 +9,6 @@ import Data.Hashable (Hashable)
import System.IO.Unsafe (unsafePerformIO) import System.IO.Unsafe (unsafePerformIO)
import Foreign import Foreign
import qualified Foreign.Ptr as C2HSImp
import IGraph import IGraph
{#import IGraph.Internal #} {#import IGraph.Internal #}
......
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