networkit-rs 0.1.0

Rust bindings for Networkit
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#ifndef NK_GRAPH_H
#define NK_GRAPH_H

#include <networkit/graph/Graph.hpp>
#include <networkit/structures/Cover.hpp>
#include <networkit/structures/Partition.hpp>
#include <networkit/graph/GraphBuilder.hpp>
#include <networkit/graph/GraphTools.hpp>
#include "rust/cxx.h"

namespace NetworKit
{

    using namespace std;

    // GRAPH

    inline unique_ptr<Graph> NewGraph(count n, bool weighted, bool directed, bool edgesIndexed)
    {
        return make_unique<Graph>(n, weighted, directed, edgesIndexed);
    }

    inline unique_ptr<Graph> CopyGraph(const Graph &g)
    {
        return make_unique<Graph>(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<GraphNodeIter> NewGraphNodeIter(const Graph &g)
    {
        auto range = g.nodeRange();
        return make_unique<GraphNodeIter>(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<GraphEdgeIter> NewGraphEdgeIter(const Graph &g)
    {
        auto range = g.edgeRange();
        return make_unique<GraphEdgeIter>(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<GraphEdgeWeightIter> NewGraphEdgeWeightIter(const Graph &g)
    {
        auto range = g.edgeWeightRange();
        return make_unique<GraphEdgeWeightIter>(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<GraphNeighbourIter> NewGraphNeighbourIter(const Graph &g, node u, bool in_neighbours)
    {
        if (in_neighbours)
        {
            auto range = g.inNeighborRange(u);
            return make_unique<GraphNeighbourIter>(range.begin(), range.end());
        }
        else
        {
            auto range = g.neighborRange(u);
            return make_unique<GraphNeighbourIter>(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<GraphNeighbourWeightIter> NewGraphNeighbourWeightIter(const Graph &g, node u, bool in_neighbours)
    {
        if (in_neighbours)
        {
            auto range = g.weightInNeighborRange(u);
            return make_unique<GraphNeighbourWeightIter>(range.begin(), range.end());
        }
        else
        {
            auto range = g.weightNeighborRange(u);
            return make_unique<GraphNeighbourWeightIter>(range.begin(), range.end());
        }
    }

    // GRAPH BUILDER

    inline unique_ptr<GraphBuilder> NewGraphBuilder(count n, bool weighted, bool directed)
    {
        return make_unique<GraphBuilder>(n, weighted, directed);
    }

    inline unique_ptr<Graph> GraphBuilderCompleteGraph(GraphBuilder &builder, bool parallel)
    {
        return make_unique<Graph>(builder.completeGraph(parallel));
    }

    // GRAPH TOOLS

    namespace GraphTools
    {
        inline unique_ptr<Graph> GTCopyNodes(const Graph &G)
        {
            return make_unique<Graph>(copyNodes(G));
        }
        inline unique_ptr<Graph> GTCreateAugmentedGraph(const Graph &G, node &root)
        {
            auto ret = createAugmentedGraph(G);
            root = ret.second;
            return make_unique<Graph>(move(ret.first));
        }
        inline unique_ptr<Graph> GTGetCompactedGraph(const Graph &G, bool random)
        {
            unordered_map<node, node> map;
            if (random)
            {
                map = getRandomContinuousNodeIds(G);
            }
            else
            {
                map = getContinuousNodeIds(G);
            }
            return make_unique<Graph>(getCompactedGraph(G, map));
        }

        inline double GTInVolume(const Graph &G, rust::Slice<const node> 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<node> &src, rust::Vec<node> &dst)
        {
            auto ret = randomEdges(G, n);
            for (auto &&pair : ret)
            {
                src.push_back(pair.first);
                dst.push_back(pair.second);
            }
        }
        inline unique_ptr<vector<node>> GTRandomNodes(const Graph &G, count n)
        {
            return make_unique<vector<node>>(randomNodes(G, n));
        }
        inline void GTRemoveEdgesFromIsolatedSet(Graph &G, rust::Slice<const node> 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<Graph> GTSubgraphAndNeighborsFromNodes(const Graph &G, rust::Slice<const node> nodes, bool includeOutNeighbors = false, bool includeInNeighbors = false)
        {
            unordered_set<node> ns(nodes.begin(), nodes.end());
            return make_unique<Graph>(subgraphAndNeighborsFromNodes(G, ns, includeOutNeighbors, includeInNeighbors));
        }
        inline unique_ptr<Graph> GTSubgraphFromNodes(const Graph &G, rust::Slice<const node> nodes)
        {
            unordered_set<node> ns(nodes.begin(), nodes.end());
            return make_unique<Graph>(subgraphFromNodes(G, ns));
        }
        inline unique_ptr<Graph> GTToUndirected(const Graph &G)
        {
            return make_unique<Graph>(toUndirected(G));
        }
        inline unique_ptr<Graph> GTToUnweighted(const Graph &G)
        {
            return make_unique<Graph>(toUnweighted(G));
        }
        inline unique_ptr<Graph> GTToWeighted(const Graph &G)
        {
            return make_unique<Graph>(toWeighted(G));
        }
        inline unique_ptr<vector<node>> GTTopologicalSort(const Graph &G)
        {
            return make_unique<vector<node>>(topologicalSort(G));
        }
        inline unique_ptr<Graph> GTTranspose(const Graph &G)
        {
            return make_unique<Graph>(transpose(G));
        }
        inline double GTVolume(const Graph &G, rust::Slice<const node> nodes)
        {
            if (nodes.empty())
            {
                return volume(G);
            }
            else
            {
                return volume(G, nodes.begin(), nodes.end());
            }
        }
    }

    // PARTITION

    inline unique_ptr<Partition> NewPartition(index z)
    {
        return make_unique<Partition>(z);
    }

    inline unique_ptr<Partition> CopyPartition(const Partition &p)
    {
        return make_unique<Partition>(p);
    }

    inline unique_ptr<vector<count>> PTSubsetSizes(const Partition &p)
    {
        return make_unique<vector<count>>(p.subsetSizes());
    }

    inline void PTSubsetSizeMap(const Partition &p, rust::Vec<node> &ks, rust::Vec<node> &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<node> &rs)
    {
        for (auto &&res : p.getMembers(s))
        {
            rs.push_back(res);
        }
    }
    inline void PTGetSubsetIds(const Partition &p, rust::Vec<node> &rs)
    {
        for (auto &&res : p.getSubsetIds())
        {
            rs.push_back(res);
        }
    }
    inline unique_ptr<string> PTGetName(const Partition &p)
    {
        return make_unique<string>(p.getName());
    }
    inline void PTSetName(Partition &p, rust::Str name)
    {
        string n(name);
        p.setName(n);
    }

    // COVER

    inline unique_ptr<Cover> NewCover()
    {
        return make_unique<Cover>();
    }

    inline unique_ptr<Cover> NewCoverWithSize(index z)
    {
        return make_unique<Cover>(z);
    }

    inline unique_ptr<Cover> NewCoverFromPartition(const Partition &p)
    {
        return make_unique<Cover>(p);
    }

    inline unique_ptr<Cover> CopyCover(const Cover &c)
    {
        return make_unique<Cover>(c);
    }

    inline void CVGetMembers(const Cover &p, index s, rust::Vec<node> &rs)
    {
        for (auto &&res : p.getMembers(s))
        {
            rs.push_back(res);
        }
    }
    inline void CVGetSubsetIds(const Cover &p, rust::Vec<node> &rs)
    {
        for (auto &&res : p.getSubsetIds())
        {
            rs.push_back(res);
        }
    }
    inline void CVSubsetSizeMap(const Cover &p, rust::Vec<node> &ks, rust::Vec<node> &sz)
    {
        for (auto &&pair : p.subsetSizeMap())
        {
            ks.push_back(pair.first);
            sz.push_back(pair.second);
        }
    }

    inline unique_ptr<vector<count>> CVSubsetSizes(const Cover &p)
    {
        return make_unique<vector<count>>(p.subsetSizes());
    }

    inline unique_ptr<vector<count>> CVSubsetsOf(const Cover &c, node e)
    {
        auto r = c.subsetsOf(e);
        return make_unique<vector<count>>(r.begin(), r.end());
    }
}

#endif // NK_GRAPH_H