#ifndef NK_GRAPH_H #define NK_GRAPH_H #include #include #include #include #include #include "rust/cxx.h" namespace NetworKit { using namespace std; // GRAPH inline unique_ptr NewGraph(count n, bool weighted, bool directed, bool edgesIndexed) { return make_unique(n, weighted, directed, edgesIndexed); } inline unique_ptr CopyGraph(const Graph &g) { return make_unique(g); } class GraphNodeIter { Graph::NodeIterator cur; Graph::NodeIterator end; public: GraphNodeIter(Graph::NodeIterator cur_, Graph::NodeIterator end_) : cur(cur_), end(end_) {} inline bool advance(node &u) { if (cur != end) { u = *cur; ++cur; return true; } else { return false; } } }; inline unique_ptr NewGraphNodeIter(const Graph &g) { auto range = g.nodeRange(); return make_unique(range.begin(), range.end()); } class GraphEdgeIter { Graph::EdgeIterator cur; Graph::EdgeIterator end; public: GraphEdgeIter(Graph::EdgeIterator cur_, Graph::EdgeIterator end_) : cur(cur_), end(end_) {} inline bool advance(node &u, node &v) { if (cur != end) { u = (*cur).u; v = (*cur).v; ++cur; return true; } else { return false; } } }; inline unique_ptr NewGraphEdgeIter(const Graph &g) { auto range = g.edgeRange(); return make_unique(range.begin(), range.end()); } class GraphEdgeWeightIter { Graph::EdgeWeightIterator cur; Graph::EdgeWeightIterator end; public: GraphEdgeWeightIter(Graph::EdgeWeightIterator cur_, Graph::EdgeWeightIterator end_) : cur(cur_), end(end_) {} inline bool advance(node &u, node &v, edgeweight &wt) { if (cur != end) { u = (*cur).u; v = (*cur).v; wt = (*cur).weight; ++cur; return true; } else { return false; } } }; inline unique_ptr NewGraphEdgeWeightIter(const Graph &g) { auto range = g.edgeWeightRange(); return make_unique(range.begin(), range.end()); } class GraphNeighbourIter { Graph::NeighborIterator cur; Graph::NeighborIterator end; public: GraphNeighbourIter(Graph::NeighborIterator cur_, Graph::NeighborIterator end_) : cur(cur_), end(end_) {} inline bool advance(node &u) { if (cur != end) { u = *cur; ++cur; return true; } else { return false; } } }; inline unique_ptr NewGraphNeighbourIter(const Graph &g, node u, bool in_neighbours) { if (in_neighbours) { auto range = g.inNeighborRange(u); return make_unique(range.begin(), range.end()); } else { auto range = g.neighborRange(u); return make_unique(range.begin(), range.end()); } } class GraphNeighbourWeightIter { Graph::NeighborWeightIterator cur; Graph::NeighborWeightIterator end; public: GraphNeighbourWeightIter(Graph::NeighborWeightIterator cur_, Graph::NeighborWeightIterator end_) : cur(cur_), end(end_) {} inline node current() const { return (*cur).first; } inline edgeweight current_weight() const { return (*cur).second; } inline bool advance(node &u, edgeweight &wt) { if (cur != end) { u = (*cur).first; wt = (*cur).second; ++cur; return true; } else { return false; } } }; inline unique_ptr NewGraphNeighbourWeightIter(const Graph &g, node u, bool in_neighbours) { if (in_neighbours) { auto range = g.weightInNeighborRange(u); return make_unique(range.begin(), range.end()); } else { auto range = g.weightNeighborRange(u); return make_unique(range.begin(), range.end()); } } // GRAPH BUILDER inline unique_ptr NewGraphBuilder(count n, bool weighted, bool directed) { return make_unique(n, weighted, directed); } inline unique_ptr GraphBuilderCompleteGraph(GraphBuilder &builder, bool parallel) { return make_unique(builder.completeGraph(parallel)); } // GRAPH TOOLS namespace GraphTools { inline unique_ptr GTCopyNodes(const Graph &G) { return make_unique(copyNodes(G)); } inline unique_ptr GTCreateAugmentedGraph(const Graph &G, node &root) { auto ret = createAugmentedGraph(G); root = ret.second; return make_unique(move(ret.first)); } inline unique_ptr GTGetCompactedGraph(const Graph &G, bool random) { unordered_map map; if (random) { map = getRandomContinuousNodeIds(G); } else { map = getContinuousNodeIds(G); } return make_unique(getCompactedGraph(G, map)); } inline double GTInVolume(const Graph &G, rust::Slice nodes) { return inVolume(G, nodes.begin(), nodes.end()); } inline void GTRandomEdge(const Graph &G, bool uniform, node &src, node &dst) { auto ret = randomEdge(G, uniform); src = ret.first; dst = ret.second; } inline void GTRandomEdges(const Graph &G, count n, rust::Vec &src, rust::Vec &dst) { auto ret = randomEdges(G, n); for (auto &&pair : ret) { src.push_back(pair.first); dst.push_back(pair.second); } } inline unique_ptr> GTRandomNodes(const Graph &G, count n) { return make_unique>(randomNodes(G, n)); } inline void GTRemoveEdgesFromIsolatedSet(Graph &G, rust::Slice nodes) { removeEdgesFromIsolatedSet(G, nodes.begin(), nodes.end()); } inline void GTSize(const Graph &G, count &n_nodes, count &n_edges) { auto ret = size(G); n_nodes = ret.first; n_edges = ret.second; } inline unique_ptr GTSubgraphAndNeighborsFromNodes(const Graph &G, rust::Slice nodes, bool includeOutNeighbors = false, bool includeInNeighbors = false) { unordered_set ns(nodes.begin(), nodes.end()); return make_unique(subgraphAndNeighborsFromNodes(G, ns, includeOutNeighbors, includeInNeighbors)); } inline unique_ptr GTSubgraphFromNodes(const Graph &G, rust::Slice nodes) { unordered_set ns(nodes.begin(), nodes.end()); return make_unique(subgraphFromNodes(G, ns)); } inline unique_ptr GTToUndirected(const Graph &G) { return make_unique(toUndirected(G)); } inline unique_ptr GTToUnweighted(const Graph &G) { return make_unique(toUnweighted(G)); } inline unique_ptr GTToWeighted(const Graph &G) { return make_unique(toWeighted(G)); } inline unique_ptr> GTTopologicalSort(const Graph &G) { return make_unique>(topologicalSort(G)); } inline unique_ptr GTTranspose(const Graph &G) { return make_unique(transpose(G)); } inline double GTVolume(const Graph &G, rust::Slice nodes) { if (nodes.empty()) { return volume(G); } else { return volume(G, nodes.begin(), nodes.end()); } } } // PARTITION inline unique_ptr NewPartition(index z) { return make_unique(z); } inline unique_ptr CopyPartition(const Partition &p) { return make_unique(p); } inline unique_ptr> PTSubsetSizes(const Partition &p) { return make_unique>(p.subsetSizes()); } inline void PTSubsetSizeMap(const Partition &p, rust::Vec &ks, rust::Vec &sz) { for (auto &&pair : p.subsetSizeMap()) { ks.push_back(pair.first); sz.push_back(pair.second); } } inline void PTGetMembers(const Partition &p, index s, rust::Vec &rs) { for (auto &&res : p.getMembers(s)) { rs.push_back(res); } } inline void PTGetSubsetIds(const Partition &p, rust::Vec &rs) { for (auto &&res : p.getSubsetIds()) { rs.push_back(res); } } inline unique_ptr PTGetName(const Partition &p) { return make_unique(p.getName()); } inline void PTSetName(Partition &p, rust::Str name) { string n(name); p.setName(n); } // COVER inline unique_ptr NewCover() { return make_unique(); } inline unique_ptr NewCoverWithSize(index z) { return make_unique(z); } inline unique_ptr NewCoverFromPartition(const Partition &p) { return make_unique(p); } inline unique_ptr CopyCover(const Cover &c) { return make_unique(c); } inline void CVGetMembers(const Cover &p, index s, rust::Vec &rs) { for (auto &&res : p.getMembers(s)) { rs.push_back(res); } } inline void CVGetSubsetIds(const Cover &p, rust::Vec &rs) { for (auto &&res : p.getSubsetIds()) { rs.push_back(res); } } inline void CVSubsetSizeMap(const Cover &p, rust::Vec &ks, rust::Vec &sz) { for (auto &&pair : p.subsetSizeMap()) { ks.push_back(pair.first); sz.push_back(pair.second); } } inline unique_ptr> CVSubsetSizes(const Cover &p) { return make_unique>(p.subsetSizes()); } inline unique_ptr> CVSubsetsOf(const Cover &c, node e) { auto r = c.subsetsOf(e); return make_unique>(r.begin(), r.end()); } } #endif // NK_GRAPH_H