-- GENERATED by C->Haskell Compiler, version 0.28.3 Switcheroo, 25 November 2017 (Haskell)
-- Edit the ORIGNAL .chs file instead!


{-# LINE 1 "src/IGraph/Structure.chs" #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module IGraph.Structure
    ( inducedSubgraph
    , closeness
    , betweenness
    , eigenvectorCentrality
    , pagerank
    , personalizedPagerank
    ) where
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Marshal.Utils as C2HSImp
import qualified Foreign.Ptr as C2HSImp



import           Control.Monad
import           Data.Either               (fromRight)
import           Data.Hashable             (Hashable)
import qualified Data.HashMap.Strict       as M
import           Data.Serialize            (Serialize, decode)
import           System.IO.Unsafe          (unsafePerformIO)

import Foreign
import Foreign.C.Types

import           IGraph
import           IGraph.Mutable
import IGraph.Internal
{-# LINE 23 "src/IGraph/Structure.chs" #-}

import IGraph.Internal.Constants
{-# LINE 24 "src/IGraph/Structure.chs" #-}




inducedSubgraph :: (Hashable v, Eq v, Serialize v) => LGraph d v e -> [Int] -> LGraph d v e
inducedSubgraph gr vs = unsafePerformIO $ do
    vs' <- fromList $ map fromIntegral vs
    vsptr <- igraphVsVector vs'
    igraphInducedSubgraph (_graph gr) vsptr IgraphSubgraphCreateFromScratch >>=
        unsafeFreeze . MLGraph

-- | Closeness centrality
closeness :: [Int]  -- ^ vertices
          -> LGraph d v e
          -> Maybe [Double]  -- ^ optional edge weights
          -> Neimode
          -> Bool   -- ^ whether to normalize
          -> [Double]
closeness vs gr ws mode normal = unsafePerformIO $ do
    vs' <- fromList $ map fromIntegral vs
    vsptr <- igraphVsVector vs'
    vptr <- igraphVectorNew 0
    ws' <- case ws of
        Just w -> fromList w
        _      -> liftM Vector $ newForeignPtr_ $ castPtr nullPtr
    igraphCloseness (_graph gr) vptr vsptr mode ws' normal
    toList vptr

-- | Betweenness centrality
betweenness :: [Int]
            -> LGraph d v e
            -> Maybe [Double]
            -> [Double]
betweenness vs gr ws = unsafePerformIO $ do
    vs' <- fromList $ map fromIntegral vs
    vsptr <- igraphVsVector vs'
    vptr <- igraphVectorNew 0
    ws' <- case ws of
        Just w -> fromList w
        _      -> liftM Vector $ newForeignPtr_ $ castPtr nullPtr
    igraphBetweenness (_graph gr) vptr vsptr True ws' False
    toList vptr

-- | Eigenvector centrality
eigenvectorCentrality :: LGraph d v e
                      -> Maybe [Double]
                      -> [Double]
eigenvectorCentrality gr ws = unsafePerformIO $ do
    vptr <- igraphVectorNew 0
    ws' <- case ws of
        Just w -> fromList w
        _      -> liftM Vector $ newForeignPtr_ $ castPtr nullPtr
    arparck <- igraphArpackNew
    igraphEigenvectorCentrality (_graph gr) vptr nullPtr True True ws' arparck
    toList vptr

-- | Google's PageRank
pagerank :: Graph d
         => LGraph d v e
         -> Maybe [Double]  -- ^ edge weights
         -> Double  -- ^ damping factor, usually around 0.85
         -> [Double]
pagerank gr ws d
    | n == 0 = []
    | otherwise = unsafePerformIO $ alloca $ \p -> do
        vptr <- igraphVectorNew 0
        vsptr <- igraphVsAll
        ws' <- case ws of
            Just w -> if length w /= m
                then error "pagerank: incorrect length of edge weight vector"
                else fromList w
            _ -> liftM Vector $ newForeignPtr_ $ castPtr nullPtr
        igraphPagerank (_graph gr) IgraphPagerankAlgoPrpack vptr p vsptr
            (isDirected gr) d ws' nullPtr
        toList vptr
  where
    n = nNodes gr
    m = nEdges gr

-- | Personalized PageRank.
personalizedPagerank :: Graph d
                     => LGraph d v e
                     -> [Double]   -- ^ reset probability
                     -> Maybe [Double]
                     -> Double
                     -> [Double]
personalizedPagerank gr reset ws d
    | n == 0 = []
    | length reset /= n = error "personalizedPagerank: incorrect length of reset vector"
    | otherwise = unsafePerformIO $ alloca $ \p -> do
        vptr <- igraphVectorNew 0
        vsptr <- igraphVsAll
        ws' <- case ws of
            Just w -> if length w /= m
                then error "pagerank: incorrect length of edge weight vector"
                else fromList w
            _ -> liftM Vector $ newForeignPtr_ $ castPtr nullPtr
        reset' <- fromList reset
        igraphPersonalizedPagerank (_graph gr) IgraphPagerankAlgoPrpack vptr p vsptr
            (isDirected gr) d reset' ws' nullPtr
        toList vptr
  where
    n = nNodes gr
    m = nEdges gr

igraphInducedSubgraph :: (IGraph) -> (IGraphVs) -> (SubgraphImplementation) -> IO ((IGraph))
igraphInducedSubgraph a1 a3 a4 =
  (withIGraph) a1 $ \a1' ->
  allocaIGraph $ \a2' ->
  (withIGraphVs) a3 $ \a3' ->
  let {a4' = (fromIntegral . fromEnum) a4} in
  igraphInducedSubgraph'_ a1' a2' a3' a4' >>= \res ->
  addIGraphFinalizer  a2'>>= \a2'' ->
  return (a2'')

{-# LINE 134 "src/IGraph/Structure.chs" #-}


igraphCloseness :: (IGraph) -> (Vector) -> (IGraphVs) -> (Neimode) -> (Vector) -> (Bool) -> IO ()
igraphCloseness a1 a2 a3 a4 a5 a6 =
  (withIGraph) a1 $ \a1' ->
  (withVector) a2 $ \a2' ->
  (withIGraphVs) a3 $ \a3' ->
  let {a4' = (fromIntegral . fromEnum) a4} in
  (withVector) a5 $ \a5' ->
  let {a6' = C2HSImp.fromBool a6} in
  igraphCloseness'_ a1' a2' a3' a4' a5' a6' >>= \res ->
  return ()

{-# LINE 141 "src/IGraph/Structure.chs" #-}


igraphBetweenness :: (IGraph) -> (Vector) -> (IGraphVs) -> (Bool) -> (Vector) -> (Bool) -> IO ()
igraphBetweenness a1 a2 a3 a4 a5 a6 =
  (withIGraph) a1 $ \a1' ->
  (withVector) a2 $ \a2' ->
  (withIGraphVs) a3 $ \a3' ->
  let {a4' = C2HSImp.fromBool a4} in
  (withVector) a5 $ \a5' ->
  let {a6' = C2HSImp.fromBool a6} in
  igraphBetweenness'_ a1' a2' a3' a4' a5' a6' >>= \res ->
  return ()

{-# LINE 148 "src/IGraph/Structure.chs" #-}


igraphEigenvectorCentrality :: (IGraph) -> (Vector) -> (Ptr CDouble) -> (Bool) -> (Bool) -> (Vector) -> (ArpackOpt) -> IO ()
igraphEigenvectorCentrality a1 a2 a3 a4 a5 a6 a7 =
  (withIGraph) a1 $ \a1' ->
  (withVector) a2 $ \a2' ->
  let {a3' = id a3} in
  let {a4' = C2HSImp.fromBool a4} in
  let {a5' = C2HSImp.fromBool a5} in
  (withVector) a6 $ \a6' ->
  (withArpackOpt) a7 $ \a7' ->
  igraphEigenvectorCentrality'_ a1' a2' a3' a4' a5' a6' a7' >>= \res ->
  return ()

{-# LINE 156 "src/IGraph/Structure.chs" #-}


igraphPagerank :: (IGraph) -> (PagerankAlgo) -> (Vector) -> (Ptr CDouble) -> (IGraphVs) -> (Bool) -> (Double) -> (Vector) -> (Ptr ()) -> IO ()
igraphPagerank a1 a2 a3 a4 a5 a6 a7 a8 a9 =
  (withIGraph) a1 $ \a1' ->
  let {a2' = (fromIntegral . fromEnum) a2} in
  (withVector) a3 $ \a3' ->
  let {a4' = id a4} in
  (withIGraphVs) a5 $ \a5' ->
  let {a6' = C2HSImp.fromBool a6} in
  let {a7' = realToFrac a7} in
  (withVector) a8 $ \a8' ->
  let {a9' = id a9} in
  igraphPagerank'_ a1' a2' a3' a4' a5' a6' a7' a8' a9' >>= \res ->
  return ()

{-# LINE 168 "src/IGraph/Structure.chs" #-}


igraphPersonalizedPagerank :: (IGraph) -> (PagerankAlgo) -> (Vector) -> (Ptr CDouble) -> (IGraphVs) -> (Bool) -> (Double) -> (Vector) -> (Vector) -> (Ptr ()) -> IO ()
igraphPersonalizedPagerank a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 =
  (withIGraph) a1 $ \a1' ->
  let {a2' = (fromIntegral . fromEnum) a2} in
  (withVector) a3 $ \a3' ->
  let {a4' = id a4} in
  (withIGraphVs) a5 $ \a5' ->
  let {a6' = C2HSImp.fromBool a6} in
  let {a7' = realToFrac a7} in
  (withVector) a8 $ \a8' ->
  (withVector) a9 $ \a9' ->
  let {a10' = id a10} in
  igraphPersonalizedPagerank'_ a1' a2' a3' a4' a5' a6' a7' a8' a9' a10' >>= \res ->
  return ()

{-# LINE 181 "src/IGraph/Structure.chs" #-}


foreign import ccall safe "IGraph/Structure.chs.h __c2hs_wrapped__igraph_induced_subgraph"
  igraphInducedSubgraph'_ :: ((C2HSImp.Ptr (IGraph)) -> ((C2HSImp.Ptr (IGraph)) -> ((C2HSImp.Ptr (IGraphVs)) -> (C2HSImp.CInt -> (IO C2HSImp.CInt)))))

foreign import ccall safe "IGraph/Structure.chs.h __c2hs_wrapped__igraph_closeness"
  igraphCloseness'_ :: ((C2HSImp.Ptr (IGraph)) -> ((C2HSImp.Ptr (Vector)) -> ((C2HSImp.Ptr (IGraphVs)) -> (C2HSImp.CInt -> ((C2HSImp.Ptr (Vector)) -> (C2HSImp.CInt -> (IO C2HSImp.CInt)))))))

foreign import ccall safe "IGraph/Structure.chs.h __c2hs_wrapped__igraph_betweenness"
  igraphBetweenness'_ :: ((C2HSImp.Ptr (IGraph)) -> ((C2HSImp.Ptr (Vector)) -> ((C2HSImp.Ptr (IGraphVs)) -> (C2HSImp.CInt -> ((C2HSImp.Ptr (Vector)) -> (C2HSImp.CInt -> (IO C2HSImp.CInt)))))))

foreign import ccall safe "IGraph/Structure.chs.h igraph_eigenvector_centrality"
  igraphEigenvectorCentrality'_ :: ((C2HSImp.Ptr (IGraph)) -> ((C2HSImp.Ptr (Vector)) -> ((C2HSImp.Ptr C2HSImp.CDouble) -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (Vector)) -> ((C2HSImp.Ptr (ArpackOpt)) -> (IO C2HSImp.CInt))))))))

foreign import ccall safe "IGraph/Structure.chs.h __c2hs_wrapped__igraph_pagerank"
  igraphPagerank'_ :: ((C2HSImp.Ptr (IGraph)) -> (C2HSImp.CInt -> ((C2HSImp.Ptr (Vector)) -> ((C2HSImp.Ptr C2HSImp.CDouble) -> ((C2HSImp.Ptr (IGraphVs)) -> (C2HSImp.CInt -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (Vector)) -> ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt))))))))))

foreign import ccall safe "IGraph/Structure.chs.h __c2hs_wrapped__igraph_personalized_pagerank"
  igraphPersonalizedPagerank'_ :: ((C2HSImp.Ptr (IGraph)) -> (C2HSImp.CInt -> ((C2HSImp.Ptr (Vector)) -> ((C2HSImp.Ptr C2HSImp.CDouble) -> ((C2HSImp.Ptr (IGraphVs)) -> (C2HSImp.CInt -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (Vector)) -> ((C2HSImp.Ptr (Vector)) -> ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt)))))))))))