networkit_rs/
centrality.rs

1use cxx::UniquePtr;
2use miette::IntoDiagnostic;
3
4use crate::{
5    base::{Algorithm, DynAlgorithm, EdgeDirection},
6    bridge::{self, *},
7    community::ValueIter,
8    tools::NodeIter,
9};
10
11pub trait Centrality: Algorithm {
12    fn centralization(&mut self) -> f64;
13    fn maximum(&mut self) -> f64;
14    fn ranking(&mut self) -> RankIter;
15    fn score(&mut self, v: u64) -> f64;
16    fn scores(&mut self) -> ValueIter;
17}
18
19pub struct RankIter {
20    pub(crate) ks: Vec<u64>,
21    pub(crate) vs: Vec<f64>,
22    pub(crate) at: usize,
23}
24
25impl Iterator for RankIter {
26    type Item = (u64, f64);
27
28    fn next(&mut self) -> Option<Self::Item> {
29        if self.at >= self.ks.len() {
30            None
31        } else {
32            let k = self.ks[self.at];
33            let v = self.vs[self.at];
34            self.at += 1;
35            Some((k, v))
36        }
37    }
38}
39
40pub struct ApproxBetweenness {
41    inner: UniquePtr<bridge::ApproxBetweenness>,
42}
43
44impl ApproxBetweenness {
45    pub fn new(
46        g: &crate::Graph,
47        epsilon: Option<f64>,
48        delta: Option<f64>,
49        universal_constant: Option<f64>,
50    ) -> Self {
51        Self {
52            inner: NewApproxBetweenness(
53                g,
54                epsilon.unwrap_or(0.01),
55                delta.unwrap_or(0.1),
56                universal_constant.unwrap_or(1.),
57            ),
58        }
59    }
60}
61
62impl Centrality for ApproxBetweenness {
63    fn centralization(&mut self) -> f64 {
64        self.inner.pin_mut().centralization()
65    }
66
67    fn maximum(&mut self) -> f64 {
68        self.inner.pin_mut().maximum()
69    }
70
71    fn ranking(&mut self) -> RankIter {
72        let mut ks = vec![];
73        let mut vs = vec![];
74        ApproxBetweennessRanking(self.inner.pin_mut(), &mut ks, &mut vs);
75        RankIter { ks, vs, at: 0 }
76    }
77
78    fn score(&mut self, node: u64) -> f64 {
79        self.inner.pin_mut().score(node)
80    }
81
82    fn scores(&mut self) -> ValueIter {
83        ValueIter {
84            inner: ApproxBetweennessScores(self.inner.pin_mut()),
85            at: 0,
86        }
87    }
88}
89
90impl Algorithm for ApproxBetweenness {
91    fn run(&mut self) -> miette::Result<()> {
92        self.inner.pin_mut().run().into_diagnostic()
93    }
94
95    fn has_finished(&self) -> bool {
96        self.inner.hasFinished()
97    }
98}
99
100pub struct ApproxCloseness {
101    inner: UniquePtr<bridge::ApproxCloseness>,
102}
103
104#[derive(Default)]
105#[repr(u8)]
106pub enum ApproxClosenessType {
107    #[default]
108    OutBound = 0,
109    InBound = 1,
110    Sum = 2,
111}
112
113impl ApproxCloseness {
114    pub fn new(
115        g: &crate::Graph,
116        n_samples: u64,
117        epsilon: Option<f64>,
118        normalized: bool,
119        t: ApproxClosenessType,
120    ) -> Self {
121        Self {
122            inner: NewApproxCloseness(g, n_samples, epsilon.unwrap_or(0.1), normalized, t as u8),
123        }
124    }
125
126    pub fn get_square_error_estimates(&mut self) -> ValueIter {
127        ValueIter {
128            inner: ApproxClosenessGetSquareErrorEstimates(self.inner.pin_mut()),
129            at: 0,
130        }
131    }
132}
133
134impl Centrality for ApproxCloseness {
135    fn centralization(&mut self) -> f64 {
136        self.inner.pin_mut().centralization()
137    }
138
139    fn maximum(&mut self) -> f64 {
140        self.inner.pin_mut().maximum()
141    }
142
143    fn ranking(&mut self) -> RankIter {
144        let mut ks = vec![];
145        let mut vs = vec![];
146        ApproxClosenessRanking(self.inner.pin_mut(), &mut ks, &mut vs);
147        RankIter { ks, vs, at: 0 }
148    }
149
150    fn score(&mut self, node: u64) -> f64 {
151        self.inner.pin_mut().score(node)
152    }
153
154    fn scores(&mut self) -> ValueIter {
155        ValueIter {
156            inner: ApproxClosenessScores(self.inner.pin_mut()),
157            at: 0,
158        }
159    }
160}
161
162impl Algorithm for ApproxCloseness {
163    fn run(&mut self) -> miette::Result<()> {
164        self.inner.pin_mut().run().into_diagnostic()
165    }
166
167    fn has_finished(&self) -> bool {
168        self.inner.hasFinished()
169    }
170}
171
172pub struct ApproxElectricalCloseness {
173    inner: UniquePtr<bridge::ApproxElectricalCloseness>,
174}
175
176impl ApproxElectricalCloseness {
177    pub fn new(g: &crate::Graph, epsilon: Option<f64>, kappa: Option<f64>) -> Self {
178        Self {
179            inner: NewApproxElectricalCloseness(g, epsilon.unwrap_or(0.1), kappa.unwrap_or(0.3)),
180        }
181    }
182
183    pub fn compute_exact_diagonal(&self, tol: Option<f64>) -> ValueIter {
184        ValueIter {
185            inner: ApproxElectricalClosenessComputeExactDiagonal(&self.inner, tol.unwrap_or(1e-9)),
186            at: 0,
187        }
188    }
189    pub fn get_diagonal(&self) -> ValueIter {
190        ValueIter {
191            inner: ApproxElectricalClosenessGetDiagonal(&self.inner),
192            at: 0,
193        }
194    }
195}
196
197impl Centrality for ApproxElectricalCloseness {
198    fn centralization(&mut self) -> f64 {
199        self.inner.pin_mut().centralization()
200    }
201
202    fn maximum(&mut self) -> f64 {
203        self.inner.pin_mut().maximum()
204    }
205
206    fn ranking(&mut self) -> RankIter {
207        let mut ks = vec![];
208        let mut vs = vec![];
209        ApproxElectricalClosenessRanking(self.inner.pin_mut(), &mut ks, &mut vs);
210        RankIter { ks, vs, at: 0 }
211    }
212
213    fn score(&mut self, node: u64) -> f64 {
214        self.inner.pin_mut().score(node)
215    }
216
217    fn scores(&mut self) -> ValueIter {
218        ValueIter {
219            inner: ApproxElectricalClosenessScores(self.inner.pin_mut()),
220            at: 0,
221        }
222    }
223}
224
225impl Algorithm for ApproxElectricalCloseness {
226    fn run(&mut self) -> miette::Result<()> {
227        self.inner.pin_mut().run().into_diagnostic()
228    }
229
230    fn has_finished(&self) -> bool {
231        self.inner.hasFinished()
232    }
233}
234
235pub struct ApproxGroupBetweenness {
236    inner: UniquePtr<bridge::ApproxGroupBetweenness>,
237}
238
239impl ApproxGroupBetweenness {
240    pub fn new(g: &crate::Graph, group_size: u64, epsilon: f64) -> Self {
241        Self {
242            inner: NewApproxGroupBetweenness(g, group_size, epsilon),
243        }
244    }
245    pub fn group_max_betweenness(&self) -> impl Iterator<Item = u64> {
246        NodeIter {
247            nodes: ApproxGroupBetweennessGroupMaxBetweenness(&self.inner),
248            at: 0,
249        }
250    }
251    pub fn score_of_group(&self, nodes: &[u64], normalized: bool) -> impl Iterator<Item = u64> {
252        NodeIter {
253            nodes: ApproxGroupBetweennessScoreOfGroup(&self.inner, nodes, normalized),
254            at: 0,
255        }
256    }
257}
258
259impl Algorithm for ApproxGroupBetweenness {
260    fn run(&mut self) -> miette::Result<()> {
261        self.inner.pin_mut().run().into_diagnostic()
262    }
263
264    fn has_finished(&self) -> bool {
265        self.inner.hasFinished()
266    }
267}
268
269pub struct ApproxSpanningEdge {
270    inner: UniquePtr<bridge::ApproxSpanningEdge>,
271}
272
273impl ApproxSpanningEdge {
274    pub fn new(g: &crate::Graph, epsilon: Option<f64>) -> Self {
275        Self {
276            inner: NewApproxSpanningEdge(g, epsilon.unwrap_or(0.1)),
277        }
278    }
279
280    pub fn scores(&self) -> ValueIter {
281        ValueIter {
282            inner: ApproxSpanningEdgeScores(&self.inner),
283            at: 0,
284        }
285    }
286}
287
288impl Algorithm for ApproxSpanningEdge {
289    fn run(&mut self) -> miette::Result<()> {
290        self.inner.pin_mut().run().into_diagnostic()
291    }
292
293    fn has_finished(&self) -> bool {
294        self.inner.hasFinished()
295    }
296}
297
298pub struct Betweenness {
299    inner: UniquePtr<bridge::Betweenness>,
300}
301
302impl Betweenness {
303    pub fn new(g: &crate::Graph, normalized: bool, compute_edge_centrality: bool) -> Self {
304        Self {
305            inner: NewBetweenness(g, normalized, compute_edge_centrality),
306        }
307    }
308
309    pub fn edge_scores(&mut self) -> ValueIter {
310        ValueIter {
311            inner: BetweennessEdgeScores(self.inner.pin_mut()),
312            at: 0,
313        }
314    }
315}
316
317impl Centrality for Betweenness {
318    fn centralization(&mut self) -> f64 {
319        self.inner.pin_mut().centralization()
320    }
321
322    fn maximum(&mut self) -> f64 {
323        self.inner.pin_mut().maximum()
324    }
325
326    fn ranking(&mut self) -> RankIter {
327        let mut ks = vec![];
328        let mut vs = vec![];
329        BetweennessRanking(self.inner.pin_mut(), &mut ks, &mut vs);
330        RankIter { ks, vs, at: 0 }
331    }
332
333    fn score(&mut self, node: u64) -> f64 {
334        self.inner.pin_mut().score(node)
335    }
336
337    fn scores(&mut self) -> ValueIter {
338        ValueIter {
339            inner: BetweennessScores(self.inner.pin_mut()),
340            at: 0,
341        }
342    }
343}
344
345impl Algorithm for Betweenness {
346    fn run(&mut self) -> miette::Result<()> {
347        self.inner.pin_mut().run().into_diagnostic()
348    }
349
350    fn has_finished(&self) -> bool {
351        self.inner.hasFinished()
352    }
353}
354
355pub struct Closeness {
356    inner: UniquePtr<bridge::Closeness>,
357}
358
359#[derive(Default)]
360#[repr(u8)]
361pub enum ClosenessVariant {
362    #[default]
363    Standard = 0,
364    Generalized = 1,
365}
366
367impl Closeness {
368    pub fn new(g: &crate::Graph, normalized: bool, variant: ClosenessVariant) -> Self {
369        Self {
370            inner: NewCloseness(g, normalized, variant as u8),
371        }
372    }
373}
374
375impl Centrality for Closeness {
376    fn centralization(&mut self) -> f64 {
377        self.inner.pin_mut().centralization()
378    }
379
380    fn maximum(&mut self) -> f64 {
381        self.inner.pin_mut().maximum()
382    }
383
384    fn ranking(&mut self) -> RankIter {
385        let mut ks = vec![];
386        let mut vs = vec![];
387        ClosenessRanking(self.inner.pin_mut(), &mut ks, &mut vs);
388        RankIter { ks, vs, at: 0 }
389    }
390
391    fn score(&mut self, node: u64) -> f64 {
392        self.inner.pin_mut().score(node)
393    }
394
395    fn scores(&mut self) -> ValueIter {
396        ValueIter {
397            inner: ClosenessScores(self.inner.pin_mut()),
398            at: 0,
399        }
400    }
401}
402
403impl Algorithm for Closeness {
404    fn run(&mut self) -> miette::Result<()> {
405        self.inner.pin_mut().run().into_diagnostic()
406    }
407
408    fn has_finished(&self) -> bool {
409        self.inner.hasFinished()
410    }
411}
412
413pub struct CoreDecomposition {
414    inner: UniquePtr<bridge::CoreDecomposition>,
415}
416
417impl CoreDecomposition {
418    pub fn new(
419        g: &crate::Graph,
420        normalized: bool,
421        enforce_bucket_queue_algorithm: bool,
422        store_node_order: bool,
423    ) -> Self {
424        Self {
425            inner: NewCoreDecomposition(
426                g,
427                normalized,
428                enforce_bucket_queue_algorithm,
429                store_node_order,
430            ),
431        }
432    }
433
434    pub fn get_cover(&self) -> crate::Cover {
435        CoreDecompositionGetCover(&self.inner).into()
436    }
437    pub fn get_partition(&self) -> crate::Partition {
438        CoreDecompositionGetPartition(&self.inner).into()
439    }
440    pub fn get_node_order(&self) -> impl Iterator<Item = u64> {
441        NodeIter {
442            nodes: CoreDecompositionGetNodeOrder(&self.inner),
443            at: 0,
444        }
445    }
446    pub fn max_core_number(&self) -> u64 {
447        self.inner.maxCoreNumber()
448    }
449}
450
451impl Centrality for CoreDecomposition {
452    fn centralization(&mut self) -> f64 {
453        self.inner.pin_mut().centralization()
454    }
455
456    fn maximum(&mut self) -> f64 {
457        self.inner.pin_mut().maximum()
458    }
459
460    fn ranking(&mut self) -> RankIter {
461        let mut ks = vec![];
462        let mut vs = vec![];
463        CoreDecompositionRanking(self.inner.pin_mut(), &mut ks, &mut vs);
464        RankIter { ks, vs, at: 0 }
465    }
466
467    fn score(&mut self, node: u64) -> f64 {
468        self.inner.pin_mut().score(node)
469    }
470
471    fn scores(&mut self) -> ValueIter {
472        ValueIter {
473            inner: CoreDecompositionScores(self.inner.pin_mut()),
474            at: 0,
475        }
476    }
477}
478
479impl Algorithm for CoreDecomposition {
480    fn run(&mut self) -> miette::Result<()> {
481        self.inner.pin_mut().run().into_diagnostic()
482    }
483
484    fn has_finished(&self) -> bool {
485        self.inner.hasFinished()
486    }
487}
488
489pub struct DegreeCentrality {
490    inner: UniquePtr<bridge::DegreeCentrality>,
491}
492
493impl DegreeCentrality {
494    pub fn new(g: &crate::Graph, normalized: bool, out_deg: bool, ignore_self_loops: bool) -> Self {
495        Self {
496            inner: NewDegreeCentrality(g, normalized, out_deg, ignore_self_loops),
497        }
498    }
499}
500
501impl Centrality for DegreeCentrality {
502    fn centralization(&mut self) -> f64 {
503        self.inner.pin_mut().centralization()
504    }
505
506    fn maximum(&mut self) -> f64 {
507        self.inner.pin_mut().maximum()
508    }
509
510    fn ranking(&mut self) -> RankIter {
511        let mut ks = vec![];
512        let mut vs = vec![];
513        DegreeCentralityRanking(self.inner.pin_mut(), &mut ks, &mut vs);
514        RankIter { ks, vs, at: 0 }
515    }
516
517    fn score(&mut self, node: u64) -> f64 {
518        self.inner.pin_mut().score(node)
519    }
520
521    fn scores(&mut self) -> ValueIter {
522        ValueIter {
523            inner: DegreeCentralityScores(self.inner.pin_mut()),
524            at: 0,
525        }
526    }
527}
528
529impl Algorithm for DegreeCentrality {
530    fn run(&mut self) -> miette::Result<()> {
531        self.inner.pin_mut().run().into_diagnostic()
532    }
533
534    fn has_finished(&self) -> bool {
535        self.inner.hasFinished()
536    }
537}
538
539pub struct DynApproxBetweenness {
540    inner: UniquePtr<bridge::DynApproxBetweenness>,
541}
542
543impl DynApproxBetweenness {
544    pub fn new(
545        g: &crate::Graph,
546        epsilon: Option<f64>,
547        delta: Option<f64>,
548        store_predecessors: bool,
549        universal_constant: Option<f64>,
550    ) -> Self {
551        Self {
552            inner: NewDynApproxBetweenness(
553                g,
554                epsilon.unwrap_or(0.01),
555                delta.unwrap_or(0.1),
556                store_predecessors,
557                universal_constant.unwrap_or(1.),
558            ),
559        }
560    }
561    pub fn get_number_of_samples(&self) -> u64 {
562        self.inner.getNumberOfSamples()
563    }
564}
565
566impl Centrality for DynApproxBetweenness {
567    fn centralization(&mut self) -> f64 {
568        self.inner.pin_mut().centralization()
569    }
570
571    fn maximum(&mut self) -> f64 {
572        self.inner.pin_mut().maximum()
573    }
574
575    fn ranking(&mut self) -> RankIter {
576        let mut ks = vec![];
577        let mut vs = vec![];
578        DynApproxBetweennessRanking(self.inner.pin_mut(), &mut ks, &mut vs);
579        RankIter { ks, vs, at: 0 }
580    }
581
582    fn score(&mut self, node: u64) -> f64 {
583        self.inner.pin_mut().score(node)
584    }
585
586    fn scores(&mut self) -> ValueIter {
587        ValueIter {
588            inner: DynApproxBetweennessScores(self.inner.pin_mut()),
589            at: 0,
590        }
591    }
592}
593
594impl Algorithm for DynApproxBetweenness {
595    fn run(&mut self) -> miette::Result<()> {
596        self.inner.pin_mut().run().into_diagnostic()
597    }
598
599    fn has_finished(&self) -> bool {
600        self.inner.hasFinished()
601    }
602}
603
604impl DynAlgorithm for DynApproxBetweenness {
605    fn update(&mut self, e: crate::base::GraphEvent) {
606        DynApproxBetweennessUpdate(self.inner.pin_mut(), e.kind as u8, e.u, e.v, e.ew)
607    }
608
609    fn update_batch(&mut self, es: &[crate::base::GraphEvent]) {
610        let mut kinds = Vec::with_capacity(es.len());
611        let mut us = Vec::with_capacity(es.len());
612        let mut vs = Vec::with_capacity(es.len());
613        let mut ews = Vec::with_capacity(es.len());
614        for ev in es {
615            kinds.push(ev.kind as u8);
616            us.push(ev.u);
617            vs.push(ev.v);
618            ews.push(ev.ew);
619        }
620        DynApproxBetweennessUpdateBatch(self.inner.pin_mut(), &kinds, &us, &vs, &ews);
621    }
622}
623
624pub struct DynBetweenness {
625    inner: UniquePtr<bridge::DynBetweenness>,
626}
627
628impl DynBetweenness {
629    pub fn new(g: &crate::Graph) -> Self {
630        Self {
631            inner: NewDynBetweenness(g),
632        }
633    }
634}
635
636impl Centrality for DynBetweenness {
637    fn centralization(&mut self) -> f64 {
638        self.inner.pin_mut().centralization()
639    }
640
641    fn maximum(&mut self) -> f64 {
642        self.inner.pin_mut().maximum()
643    }
644
645    fn ranking(&mut self) -> RankIter {
646        let mut ks = vec![];
647        let mut vs = vec![];
648        DynBetweennessRanking(self.inner.pin_mut(), &mut ks, &mut vs);
649        RankIter { ks, vs, at: 0 }
650    }
651
652    fn score(&mut self, node: u64) -> f64 {
653        self.inner.pin_mut().score(node)
654    }
655
656    fn scores(&mut self) -> ValueIter {
657        ValueIter {
658            inner: DynBetweennessScores(self.inner.pin_mut()),
659            at: 0,
660        }
661    }
662}
663
664impl Algorithm for DynBetweenness {
665    fn run(&mut self) -> miette::Result<()> {
666        self.inner.pin_mut().run().into_diagnostic()
667    }
668
669    fn has_finished(&self) -> bool {
670        self.inner.hasFinished()
671    }
672}
673
674impl DynAlgorithm for DynBetweenness {
675    fn update(&mut self, e: crate::base::GraphEvent) {
676        DynBetweennessUpdate(self.inner.pin_mut(), e.kind as u8, e.u, e.v, e.ew)
677    }
678
679    fn update_batch(&mut self, es: &[crate::base::GraphEvent]) {
680        let mut kinds = Vec::with_capacity(es.len());
681        let mut us = Vec::with_capacity(es.len());
682        let mut vs = Vec::with_capacity(es.len());
683        let mut ews = Vec::with_capacity(es.len());
684        for ev in es {
685            kinds.push(ev.kind as u8);
686            us.push(ev.u);
687            vs.push(ev.v);
688            ews.push(ev.ew);
689        }
690        DynBetweennessUpdateBatch(self.inner.pin_mut(), &kinds, &us, &vs, &ews);
691    }
692}
693
694pub struct DynBetweennessOneNode {
695    inner: UniquePtr<bridge::DynBetweennessOneNode>,
696}
697
698impl DynBetweennessOneNode {
699    pub fn new(g: &mut crate::Graph, x: u64) -> Self {
700        Self {
701            inner: NewDynBetweennessOneNode(g.inner.pin_mut(), x),
702        }
703    }
704    pub fn run(&mut self) {
705        self.inner.pin_mut().run()
706    }
707    pub fn compute_score(&mut self, e: crate::base::GraphEvent) -> f64 {
708        DynBetweennessOneNodeComputeScore(self.inner.pin_mut(), e.kind as u8, e.u, e.v, e.ew)
709    }
710    pub fn get_distance(&mut self, u: u64, v: u64) -> f64 {
711        self.inner.pin_mut().getDistance(u, v)
712    }
713    pub fn get_sigma(&mut self, u: u64, v: u64) -> f64 {
714        self.inner.pin_mut().getSigma(u, v)
715    }
716    pub fn get_sigma_x(&mut self, u: u64, v: u64) -> f64 {
717        self.inner.pin_mut().getSigmax(u, v)
718    }
719    pub fn get_bcx(&mut self) -> f64 {
720        self.inner.pin_mut().getbcx()
721    }
722}
723
724impl DynAlgorithm for DynBetweennessOneNode {
725    fn update(&mut self, e: crate::base::GraphEvent) {
726        DynBetweennessOneNodeUpdate(self.inner.pin_mut(), e.kind as u8, e.u, e.v, e.ew)
727    }
728
729    fn update_batch(&mut self, es: &[crate::base::GraphEvent]) {
730        let mut kinds = Vec::with_capacity(es.len());
731        let mut us = Vec::with_capacity(es.len());
732        let mut vs = Vec::with_capacity(es.len());
733        let mut ews = Vec::with_capacity(es.len());
734        for ev in es {
735            kinds.push(ev.kind as u8);
736            us.push(ev.u);
737            vs.push(ev.v);
738            ews.push(ev.ew);
739        }
740        DynBetweennessOneNodeUpdateBatch(self.inner.pin_mut(), &kinds, &us, &vs, &ews);
741    }
742}
743
744pub struct DynKatzCentrality {
745    inner: UniquePtr<bridge::DynKatzCentrality>,
746}
747
748impl DynKatzCentrality {
749    pub fn new(g: &crate::Graph, k: u64, group_only: bool, tolerance: Option<f64>) -> Self {
750        Self {
751            inner: NewDynKatzCentrality(g, k, group_only, tolerance.unwrap_or(1e-9)),
752        }
753    }
754    pub fn are_distinguished(&mut self, u: u64, v: u64) -> bool {
755        self.inner.pin_mut().areDistinguished(u, v)
756    }
757
758    pub fn bound(&mut self, u: u64) -> f64 {
759        self.inner.pin_mut().bound(u)
760    }
761
762    pub fn top(&mut self, n: Option<u64>) -> impl Iterator<Item = u64> {
763        NodeIter {
764            at: 0,
765            nodes: DynKatzCentralityTop(self.inner.pin_mut(), n.unwrap_or(0)),
766        }
767    }
768}
769
770impl Centrality for DynKatzCentrality {
771    fn centralization(&mut self) -> f64 {
772        self.inner.pin_mut().centralization()
773    }
774
775    fn maximum(&mut self) -> f64 {
776        self.inner.pin_mut().maximum()
777    }
778
779    fn ranking(&mut self) -> RankIter {
780        let mut ks = vec![];
781        let mut vs = vec![];
782        DynKatzCentralityRanking(self.inner.pin_mut(), &mut ks, &mut vs);
783        RankIter { ks, vs, at: 0 }
784    }
785
786    fn score(&mut self, node: u64) -> f64 {
787        self.inner.pin_mut().score(node)
788    }
789
790    fn scores(&mut self) -> ValueIter {
791        ValueIter {
792            inner: DynKatzCentralityScores(self.inner.pin_mut()),
793            at: 0,
794        }
795    }
796}
797
798impl Algorithm for DynKatzCentrality {
799    fn run(&mut self) -> miette::Result<()> {
800        self.inner.pin_mut().run().into_diagnostic()
801    }
802
803    fn has_finished(&self) -> bool {
804        self.inner.hasFinished()
805    }
806}
807
808impl DynAlgorithm for DynKatzCentrality {
809    fn update(&mut self, e: crate::base::GraphEvent) {
810        DynKatzCentralityUpdate(self.inner.pin_mut(), e.kind as u8, e.u, e.v, e.ew)
811    }
812
813    fn update_batch(&mut self, es: &[crate::base::GraphEvent]) {
814        let mut kinds = Vec::with_capacity(es.len());
815        let mut us = Vec::with_capacity(es.len());
816        let mut vs = Vec::with_capacity(es.len());
817        let mut ews = Vec::with_capacity(es.len());
818        for ev in es {
819            kinds.push(ev.kind as u8);
820            us.push(ev.u);
821            vs.push(ev.v);
822            ews.push(ev.ew);
823        }
824        DynKatzCentralityUpdateBatch(self.inner.pin_mut(), &kinds, &us, &vs, &ews);
825    }
826}
827
828pub struct DynTopHarmonicCloseness {
829    inner: UniquePtr<bridge::DynTopHarmonicCloseness>,
830}
831
832impl DynTopHarmonicCloseness {
833    pub fn new(g: &crate::Graph, k: u64, use_bfs_bound: bool) -> Self {
834        Self {
835            inner: NewDynTopHarmonicCloseness(g, k, use_bfs_bound),
836        }
837    }
838
839    pub fn ranking(&mut self) -> RankIter {
840        let mut ks = vec![];
841        let mut vs = vec![];
842        DynTopHarmonicClosenessRanking(self.inner.pin_mut(), &mut ks, &mut vs);
843        RankIter { ks, vs, at: 0 }
844    }
845
846    pub fn reset(&mut self) {
847        self.inner.pin_mut().reset()
848    }
849
850    pub fn top_k_nodes_list(&mut self, include_trail: bool) -> impl Iterator<Item = u64> {
851        NodeIter {
852            at: 0,
853            nodes: DynTopHarmonicClosenessTopkNodesList(self.inner.pin_mut(), include_trail),
854        }
855    }
856    pub fn top_k_scores_list(&mut self, include_trail: bool) -> impl Iterator<Item = f64> {
857        ValueIter {
858            at: 0,
859            inner: DynTopHarmonicClosenessTopkScoresList(self.inner.pin_mut(), include_trail),
860        }
861    }
862}
863
864impl Algorithm for DynTopHarmonicCloseness {
865    fn run(&mut self) -> miette::Result<()> {
866        self.inner.pin_mut().run().into_diagnostic()
867    }
868
869    fn has_finished(&self) -> bool {
870        self.inner.hasFinished()
871    }
872}
873
874impl DynAlgorithm for DynTopHarmonicCloseness {
875    fn update(&mut self, e: crate::base::GraphEvent) {
876        DynTopHarmonicClosenessUpdate(self.inner.pin_mut(), e.kind as u8, e.u, e.v, e.ew)
877    }
878
879    fn update_batch(&mut self, es: &[crate::base::GraphEvent]) {
880        let mut kinds = Vec::with_capacity(es.len());
881        let mut us = Vec::with_capacity(es.len());
882        let mut vs = Vec::with_capacity(es.len());
883        let mut ews = Vec::with_capacity(es.len());
884        for ev in es {
885            kinds.push(ev.kind as u8);
886            us.push(ev.u);
887            vs.push(ev.v);
888            ews.push(ev.ew);
889        }
890        DynTopHarmonicClosenessUpdateBatch(self.inner.pin_mut(), &kinds, &us, &vs, &ews);
891    }
892}
893
894pub struct EigenvectorCentrality {
895    inner: UniquePtr<bridge::EigenvectorCentrality>,
896}
897
898impl EigenvectorCentrality {
899    pub fn new(g: &crate::Graph, tol: Option<f64>) -> Self {
900        Self {
901            inner: NewEigenvectorCentrality(g, tol.unwrap_or(1e-9)),
902        }
903    }
904}
905
906impl Centrality for EigenvectorCentrality {
907    fn centralization(&mut self) -> f64 {
908        self.inner.pin_mut().centralization()
909    }
910
911    fn maximum(&mut self) -> f64 {
912        self.inner.pin_mut().maximum()
913    }
914
915    fn ranking(&mut self) -> RankIter {
916        let mut ks = vec![];
917        let mut vs = vec![];
918        EigenvectorCentralityRanking(self.inner.pin_mut(), &mut ks, &mut vs);
919        RankIter { ks, vs, at: 0 }
920    }
921
922    fn score(&mut self, node: u64) -> f64 {
923        self.inner.pin_mut().score(node)
924    }
925
926    fn scores(&mut self) -> ValueIter {
927        ValueIter {
928            inner: EigenvectorCentralityScores(self.inner.pin_mut()),
929            at: 0,
930        }
931    }
932}
933
934impl Algorithm for EigenvectorCentrality {
935    fn run(&mut self) -> miette::Result<()> {
936        self.inner.pin_mut().run().into_diagnostic()
937    }
938
939    fn has_finished(&self) -> bool {
940        self.inner.hasFinished()
941    }
942}
943
944pub struct EstimateBetweenness {
945    inner: UniquePtr<bridge::EstimateBetweenness>,
946}
947
948impl EstimateBetweenness {
949    pub fn new(g: &crate::Graph, n_samples: u64, normalized: bool, parallel: bool) -> Self {
950        Self {
951            inner: NewEstimateBetweenness(g, n_samples, normalized, parallel),
952        }
953    }
954}
955
956impl Centrality for EstimateBetweenness {
957    fn centralization(&mut self) -> f64 {
958        self.inner.pin_mut().centralization()
959    }
960
961    fn maximum(&mut self) -> f64 {
962        self.inner.pin_mut().maximum()
963    }
964
965    fn ranking(&mut self) -> RankIter {
966        let mut ks = vec![];
967        let mut vs = vec![];
968        EstimateBetweennessRanking(self.inner.pin_mut(), &mut ks, &mut vs);
969        RankIter { ks, vs, at: 0 }
970    }
971
972    fn score(&mut self, node: u64) -> f64 {
973        self.inner.pin_mut().score(node)
974    }
975
976    fn scores(&mut self) -> ValueIter {
977        ValueIter {
978            inner: EstimateBetweennessScores(self.inner.pin_mut()),
979            at: 0,
980        }
981    }
982}
983
984impl Algorithm for EstimateBetweenness {
985    fn run(&mut self) -> miette::Result<()> {
986        self.inner.pin_mut().run().into_diagnostic()
987    }
988
989    fn has_finished(&self) -> bool {
990        self.inner.hasFinished()
991    }
992}
993
994pub struct ForestCentrality {
995    inner: UniquePtr<bridge::ForestCentrality>,
996}
997
998impl ForestCentrality {
999    pub fn new(g: &crate::Graph, root: u64, epsilon: Option<f64>, kappa: Option<f64>) -> Self {
1000        Self {
1001            inner: NewForestCentrality(g, root, epsilon.unwrap_or(0.1), kappa.unwrap_or(0.3)),
1002        }
1003    }
1004
1005    pub fn get_number_of_samples(&self) -> u64 {
1006        self.inner.getNumberOfSamples()
1007    }
1008
1009    pub fn get_diagonal(&self) -> impl Iterator<Item = f64> {
1010        ValueIter {
1011            inner: ForestCentralityGetDiagonal(&self.inner),
1012            at: 0,
1013        }
1014    }
1015}
1016
1017impl Centrality for ForestCentrality {
1018    fn centralization(&mut self) -> f64 {
1019        self.inner.pin_mut().centralization()
1020    }
1021
1022    fn maximum(&mut self) -> f64 {
1023        self.inner.pin_mut().maximum()
1024    }
1025
1026    fn ranking(&mut self) -> RankIter {
1027        let mut ks = vec![];
1028        let mut vs = vec![];
1029        ForestCentralityRanking(self.inner.pin_mut(), &mut ks, &mut vs);
1030        RankIter { ks, vs, at: 0 }
1031    }
1032
1033    fn score(&mut self, node: u64) -> f64 {
1034        self.inner.pin_mut().score(node)
1035    }
1036
1037    fn scores(&mut self) -> ValueIter {
1038        ValueIter {
1039            inner: ForestCentralityScores(self.inner.pin_mut()),
1040            at: 0,
1041        }
1042    }
1043}
1044
1045impl Algorithm for ForestCentrality {
1046    fn run(&mut self) -> miette::Result<()> {
1047        self.inner.pin_mut().run().into_diagnostic()
1048    }
1049
1050    fn has_finished(&self) -> bool {
1051        self.inner.hasFinished()
1052    }
1053}
1054
1055pub struct GedWalk {
1056    inner: UniquePtr<bridge::GedWalk>,
1057}
1058
1059#[derive(Default, Copy, Clone)]
1060#[repr(u8)]
1061pub enum GedWalkBoundStrategy {
1062    No,
1063    Spectral,
1064    #[default]
1065    Geometric,
1066    AdaptiveGeometric,
1067}
1068
1069#[derive(Default, Copy, Clone)]
1070#[repr(u8)]
1071pub enum GedWalkGreedyStrategy {
1072    #[default]
1073    Lazy,
1074    Stochastic,
1075}
1076
1077impl GedWalk {
1078    pub fn new(
1079        g: &crate::Graph,
1080        k: Option<u64>,
1081        init_epsilon: Option<f64>,
1082        alpha: Option<f64>,
1083        bs: GedWalkBoundStrategy,
1084        gs: GedWalkGreedyStrategy,
1085        spectral_delta: Option<f64>,
1086    ) -> Self {
1087        Self {
1088            inner: NewGedWalk(
1089                g,
1090                k.unwrap_or(1),
1091                init_epsilon.unwrap_or(0.1),
1092                alpha.unwrap_or(1.),
1093                bs as u8,
1094                gs as u8,
1095                spectral_delta.unwrap_or(0.5),
1096            ),
1097        }
1098    }
1099    pub fn get_approximate_score(&self) -> f64 {
1100        self.inner.getApproximateScore()
1101    }
1102    pub fn group_max_ged_walk(&self) -> impl Iterator<Item = u64> {
1103        NodeIter {
1104            nodes: GedWalkGroupMaxGedWalk(&self.inner),
1105            at: 0,
1106        }
1107    }
1108    pub fn score_of_group(&mut self, group: &[u64], epsilon: Option<f64>) -> f64 {
1109        GedWalkScoreOfGroup(self.inner.pin_mut(), group, epsilon.unwrap_or(0.1))
1110    }
1111}
1112
1113impl Algorithm for GedWalk {
1114    fn run(&mut self) -> miette::Result<()> {
1115        self.inner.pin_mut().run().into_diagnostic()
1116    }
1117
1118    fn has_finished(&self) -> bool {
1119        self.inner.hasFinished()
1120    }
1121}
1122
1123pub struct GroupCloseness {
1124    inner: UniquePtr<bridge::GroupCloseness>,
1125}
1126
1127impl GroupCloseness {
1128    pub fn new(g: &crate::Graph, k: Option<u64>, h: Option<u64>) -> Self {
1129        Self {
1130            inner: NewGroupCloseness(g, k.unwrap_or(1), h.unwrap_or(0)),
1131        }
1132    }
1133    pub fn score_of_group(&mut self, group: &[u64]) -> f64 {
1134        GroupClosenessScoreOfGroup(self.inner.pin_mut(), group)
1135    }
1136    pub fn group_max_closeness(&mut self) -> impl Iterator<Item = u64> {
1137        NodeIter {
1138            at: 0,
1139            nodes: GroupClosenessGroupMaxCloseness(self.inner.pin_mut()),
1140        }
1141    }
1142    pub fn compute_farness(&self, group: &[u64], h: Option<u64>) -> f64 {
1143        GroupClosenessComputeFarness(&self.inner, group, h.unwrap_or(u64::MAX))
1144    }
1145}
1146
1147impl Algorithm for GroupCloseness {
1148    fn run(&mut self) -> miette::Result<()> {
1149        self.inner.pin_mut().run().into_diagnostic()
1150    }
1151
1152    fn has_finished(&self) -> bool {
1153        self.inner.hasFinished()
1154    }
1155}
1156
1157pub struct GroupClosenessGrowShrink {
1158    inner: UniquePtr<bridge::GroupClosenessGrowShrink>,
1159}
1160
1161impl GroupClosenessGrowShrink {
1162    pub fn new(
1163        g: &crate::Graph,
1164        group: &[u64],
1165        extended: bool,
1166        insertions: Option<u64>,
1167        max_iterations: Option<u64>,
1168    ) -> Self {
1169        Self {
1170            inner: NewGroupClosenessGrowShrink(
1171                g,
1172                group,
1173                extended,
1174                insertions.unwrap_or(0),
1175                max_iterations.unwrap_or(100),
1176            ),
1177        }
1178    }
1179    pub fn number_of_iterations(&self) -> u64 {
1180        self.inner.numberOfIterations()
1181    }
1182    pub fn group_max_closeness(&mut self) -> impl Iterator<Item = u64> {
1183        NodeIter {
1184            at: 0,
1185            nodes: GroupClosenessGrowShrinkGroupMaxCloseness(&self.inner),
1186        }
1187    }
1188}
1189
1190impl Algorithm for GroupClosenessGrowShrink {
1191    fn run(&mut self) -> miette::Result<()> {
1192        self.inner.pin_mut().run().into_diagnostic()
1193    }
1194
1195    fn has_finished(&self) -> bool {
1196        self.inner.hasFinished()
1197    }
1198}
1199
1200pub struct GroupClosenessLocalSearch {
1201    inner: UniquePtr<bridge::GroupClosenessLocalSearch>,
1202}
1203
1204impl GroupClosenessLocalSearch {
1205    pub fn new(
1206        g: &crate::Graph,
1207        group: &[u64],
1208        run_grow_shrink: bool,
1209        max_iterations: Option<u64>,
1210    ) -> Self {
1211        Self {
1212            inner: NewGroupClosenessLocalSearch(
1213                g,
1214                group,
1215                run_grow_shrink,
1216                max_iterations.unwrap_or(u64::MAX),
1217            ),
1218        }
1219    }
1220    pub fn number_of_iterations(&self) -> u64 {
1221        self.inner.numberOfIterations()
1222    }
1223    pub fn group_max_closeness(&mut self) -> impl Iterator<Item = u64> {
1224        NodeIter {
1225            at: 0,
1226            nodes: GroupClosenessLocalSearchGroupMaxCloseness(&self.inner),
1227        }
1228    }
1229}
1230
1231impl Algorithm for GroupClosenessLocalSearch {
1232    fn run(&mut self) -> miette::Result<()> {
1233        self.inner.pin_mut().run().into_diagnostic()
1234    }
1235
1236    fn has_finished(&self) -> bool {
1237        self.inner.hasFinished()
1238    }
1239}
1240
1241pub struct GroupClosenessLocalSwaps {
1242    inner: UniquePtr<bridge::GroupClosenessLocalSwaps>,
1243}
1244
1245impl GroupClosenessLocalSwaps {
1246    pub fn new(g: &crate::Graph, group: &[u64], max_swaps: Option<u64>) -> Self {
1247        Self {
1248            inner: NewGroupClosenessLocalSwaps(g, group, max_swaps.unwrap_or(100)),
1249        }
1250    }
1251    pub fn number_of_swaps(&self) -> u64 {
1252        self.inner.numberOfSwaps()
1253    }
1254    pub fn group_max_closeness(&mut self) -> impl Iterator<Item = u64> {
1255        NodeIter {
1256            at: 0,
1257            nodes: GroupClosenessLocalSwapsGroupMaxCloseness(&self.inner),
1258        }
1259    }
1260}
1261
1262impl Algorithm for GroupClosenessLocalSwaps {
1263    fn run(&mut self) -> miette::Result<()> {
1264        self.inner.pin_mut().run().into_diagnostic()
1265    }
1266
1267    fn has_finished(&self) -> bool {
1268        self.inner.hasFinished()
1269    }
1270}
1271
1272pub struct GroupDegree {
1273    inner: UniquePtr<bridge::GroupDegree>,
1274}
1275
1276impl GroupDegree {
1277    pub fn new(g: &crate::Graph, k: Option<u64>, count_group_nodes: bool) -> Self {
1278        Self {
1279            inner: NewGroupDegree(g, k.unwrap_or(1), count_group_nodes),
1280        }
1281    }
1282    pub fn get_score(&mut self) -> u64 {
1283        self.inner.pin_mut().getScore()
1284    }
1285    pub fn score_of_group(&self, group: &[u64]) -> f64 {
1286        GroupDegreeScoreOfGroup(&self.inner, group)
1287    }
1288    pub fn group_max_degree(&mut self) -> impl Iterator<Item = u64> {
1289        NodeIter {
1290            at: 0,
1291            nodes: GroupDegreeGroupMaxDegree(self.inner.pin_mut()),
1292        }
1293    }
1294}
1295
1296impl Algorithm for GroupDegree {
1297    fn run(&mut self) -> miette::Result<()> {
1298        self.inner.pin_mut().run().into_diagnostic()
1299    }
1300
1301    fn has_finished(&self) -> bool {
1302        self.inner.hasFinished()
1303    }
1304}
1305
1306pub struct GroupHarmonicCloseness {
1307    inner: UniquePtr<bridge::GroupHarmonicCloseness>,
1308}
1309
1310impl GroupHarmonicCloseness {
1311    pub fn new(g: &crate::Graph, k: Option<u64>) -> Self {
1312        Self {
1313            inner: NewGroupHarmonicCloseness(g, k.unwrap_or(1)),
1314        }
1315    }
1316    pub fn score_of_group(g: &crate::Graph, group: &[u64]) -> f64 {
1317        GroupHarmonicClosenessScoreOfGroup(g, group)
1318    }
1319    pub fn group_max_harmonic_degree(&mut self) -> impl Iterator<Item = u64> {
1320        NodeIter {
1321            at: 0,
1322            nodes: GroupHarmonicClosenessGroupMaxHarmonicCloseness(self.inner.pin_mut()),
1323        }
1324    }
1325}
1326
1327impl Algorithm for GroupHarmonicCloseness {
1328    fn run(&mut self) -> miette::Result<()> {
1329        self.inner.pin_mut().run().into_diagnostic()
1330    }
1331
1332    fn has_finished(&self) -> bool {
1333        self.inner.hasFinished()
1334    }
1335}
1336
1337pub struct HarmonicCloseness {
1338    inner: UniquePtr<bridge::HarmonicCloseness>,
1339}
1340
1341impl HarmonicCloseness {
1342    pub fn new(g: &crate::Graph, normalized: bool) -> Self {
1343        Self {
1344            inner: NewHarmonicCloseness(g, normalized),
1345        }
1346    }
1347}
1348
1349impl Centrality for HarmonicCloseness {
1350    fn centralization(&mut self) -> f64 {
1351        self.inner.pin_mut().centralization()
1352    }
1353
1354    fn maximum(&mut self) -> f64 {
1355        self.inner.pin_mut().maximum()
1356    }
1357
1358    fn ranking(&mut self) -> RankIter {
1359        let mut ks = vec![];
1360        let mut vs = vec![];
1361        HarmonicClosenessRanking(self.inner.pin_mut(), &mut ks, &mut vs);
1362        RankIter { ks, vs, at: 0 }
1363    }
1364
1365    fn score(&mut self, node: u64) -> f64 {
1366        self.inner.pin_mut().score(node)
1367    }
1368
1369    fn scores(&mut self) -> ValueIter {
1370        ValueIter {
1371            inner: HarmonicClosenessScores(self.inner.pin_mut()),
1372            at: 0,
1373        }
1374    }
1375}
1376
1377impl Algorithm for HarmonicCloseness {
1378    fn run(&mut self) -> miette::Result<()> {
1379        self.inner.pin_mut().run().into_diagnostic()
1380    }
1381
1382    fn has_finished(&self) -> bool {
1383        self.inner.hasFinished()
1384    }
1385}
1386
1387pub struct KPathCentrality {
1388    inner: UniquePtr<bridge::KPathCentrality>,
1389}
1390
1391impl KPathCentrality {
1392    pub fn new(g: &crate::Graph, alpha: Option<f64>, k: Option<u64>) -> Self {
1393        Self {
1394            inner: NewKPathCentrality(g, alpha.unwrap_or(0.2), k.unwrap_or(0)),
1395        }
1396    }
1397}
1398
1399impl Centrality for KPathCentrality {
1400    fn centralization(&mut self) -> f64 {
1401        self.inner.pin_mut().centralization()
1402    }
1403
1404    fn maximum(&mut self) -> f64 {
1405        self.inner.pin_mut().maximum()
1406    }
1407
1408    fn ranking(&mut self) -> RankIter {
1409        let mut ks = vec![];
1410        let mut vs = vec![];
1411        KPathCentralityRanking(self.inner.pin_mut(), &mut ks, &mut vs);
1412        RankIter { ks, vs, at: 0 }
1413    }
1414
1415    fn score(&mut self, node: u64) -> f64 {
1416        self.inner.pin_mut().score(node)
1417    }
1418
1419    fn scores(&mut self) -> ValueIter {
1420        ValueIter {
1421            inner: KPathCentralityScores(self.inner.pin_mut()),
1422            at: 0,
1423        }
1424    }
1425}
1426
1427impl Algorithm for KPathCentrality {
1428    fn run(&mut self) -> miette::Result<()> {
1429        self.inner.pin_mut().run().into_diagnostic()
1430    }
1431
1432    fn has_finished(&self) -> bool {
1433        self.inner.hasFinished()
1434    }
1435}
1436
1437pub struct KadabraBetweenness {
1438    inner: UniquePtr<bridge::KadabraBetweenness>,
1439}
1440
1441impl KadabraBetweenness {
1442    pub fn new(
1443        g: &crate::Graph,
1444        err: Option<f64>,
1445        delta: Option<f64>,
1446        deterministic: bool,
1447        k: Option<u64>,
1448        union_sample: Option<u64>,
1449        start_factor: Option<u64>,
1450    ) -> Self {
1451        Self {
1452            inner: NewKadabraBetweenness(
1453                g,
1454                err.unwrap_or(0.01),
1455                delta.unwrap_or(0.1),
1456                deterministic,
1457                k.unwrap_or(0),
1458                union_sample.unwrap_or(0),
1459                start_factor.unwrap_or(100),
1460            ),
1461        }
1462    }
1463
1464    pub fn ranking(&mut self) -> RankIter {
1465        let mut ks = vec![];
1466        let mut vs = vec![];
1467        KadabraBetweennessRanking(self.inner.pin_mut(), &mut ks, &mut vs);
1468        RankIter { ks, vs, at: 0 }
1469    }
1470
1471    pub fn scores(&mut self) -> ValueIter {
1472        ValueIter {
1473            inner: KadabraBetweennessScores(self.inner.pin_mut()),
1474            at: 0,
1475        }
1476    }
1477
1478    pub fn get_number_of_iterations(&self) -> u64 {
1479        self.inner.getNumberOfIterations()
1480    }
1481
1482    pub fn get_omega(&self) -> f64 {
1483        self.inner.getOmega()
1484    }
1485
1486    pub fn top_k_nodes_list(&mut self) -> impl Iterator<Item = u64> {
1487        NodeIter {
1488            at: 0,
1489            nodes: KadabraBetweennessTopkNodesList(self.inner.pin_mut()),
1490        }
1491    }
1492    pub fn top_k_scores_list(&mut self) -> impl Iterator<Item = f64> {
1493        ValueIter {
1494            at: 0,
1495            inner: KadabraBetweennessTopkScoresList(self.inner.pin_mut()),
1496        }
1497    }
1498}
1499
1500impl Algorithm for KadabraBetweenness {
1501    fn run(&mut self) -> miette::Result<()> {
1502        self.inner.pin_mut().run().into_diagnostic()
1503    }
1504
1505    fn has_finished(&self) -> bool {
1506        self.inner.hasFinished()
1507    }
1508}
1509
1510pub struct KatzCentrality {
1511    inner: UniquePtr<bridge::KatzCentrality>,
1512}
1513
1514impl KatzCentrality {
1515    pub fn new(g: &crate::Graph, alpha: Option<f64>, beta: Option<f64>, tol: Option<f64>) -> Self {
1516        Self {
1517            inner: NewKatzCentrality(
1518                g,
1519                alpha.unwrap_or(0.),
1520                beta.unwrap_or(0.2),
1521                tol.unwrap_or(1e-8),
1522            ),
1523        }
1524    }
1525    pub fn set_edge_direction(&mut self, dir: EdgeDirection) {
1526        KatzCentralitySetEdgeDirection(self.inner.pin_mut(), dir == EdgeDirection::OutEdges)
1527    }
1528}
1529
1530impl Centrality for KatzCentrality {
1531    fn centralization(&mut self) -> f64 {
1532        self.inner.pin_mut().centralization()
1533    }
1534
1535    fn maximum(&mut self) -> f64 {
1536        self.inner.pin_mut().maximum()
1537    }
1538
1539    fn ranking(&mut self) -> RankIter {
1540        let mut ks = vec![];
1541        let mut vs = vec![];
1542        KatzCentralityRanking(self.inner.pin_mut(), &mut ks, &mut vs);
1543        RankIter { ks, vs, at: 0 }
1544    }
1545
1546    fn score(&mut self, node: u64) -> f64 {
1547        self.inner.pin_mut().score(node)
1548    }
1549
1550    fn scores(&mut self) -> ValueIter {
1551        ValueIter {
1552            inner: KatzCentralityScores(self.inner.pin_mut()),
1553            at: 0,
1554        }
1555    }
1556}
1557
1558impl Algorithm for KatzCentrality {
1559    fn run(&mut self) -> miette::Result<()> {
1560        self.inner.pin_mut().run().into_diagnostic()
1561    }
1562
1563    fn has_finished(&self) -> bool {
1564        self.inner.hasFinished()
1565    }
1566}
1567
1568pub struct LaplacianCentrality {
1569    inner: UniquePtr<bridge::LaplacianCentrality>,
1570}
1571
1572impl LaplacianCentrality {
1573    pub fn new(g: &crate::Graph, normalized: bool) -> Self {
1574        Self {
1575            inner: NewLaplacianCentrality(g, normalized),
1576        }
1577    }
1578}
1579
1580impl Centrality for LaplacianCentrality {
1581    fn centralization(&mut self) -> f64 {
1582        self.inner.pin_mut().centralization()
1583    }
1584
1585    fn maximum(&mut self) -> f64 {
1586        self.inner.pin_mut().maximum()
1587    }
1588
1589    fn ranking(&mut self) -> RankIter {
1590        let mut ks = vec![];
1591        let mut vs = vec![];
1592        LaplacianCentralityRanking(self.inner.pin_mut(), &mut ks, &mut vs);
1593        RankIter { ks, vs, at: 0 }
1594    }
1595
1596    fn score(&mut self, node: u64) -> f64 {
1597        self.inner.pin_mut().score(node)
1598    }
1599
1600    fn scores(&mut self) -> ValueIter {
1601        ValueIter {
1602            inner: LaplacianCentralityScores(self.inner.pin_mut()),
1603            at: 0,
1604        }
1605    }
1606}
1607
1608impl Algorithm for LaplacianCentrality {
1609    fn run(&mut self) -> miette::Result<()> {
1610        self.inner.pin_mut().run().into_diagnostic()
1611    }
1612
1613    fn has_finished(&self) -> bool {
1614        self.inner.hasFinished()
1615    }
1616}
1617
1618pub struct LocalClusteringCoefficient {
1619    inner: UniquePtr<bridge::LocalClusteringCoefficient>,
1620}
1621
1622impl LocalClusteringCoefficient {
1623    pub fn new(g: &crate::Graph, turbo: bool) -> Self {
1624        Self {
1625            inner: NewLocalClusteringCoefficient(g, turbo),
1626        }
1627    }
1628}
1629
1630impl Centrality for LocalClusteringCoefficient {
1631    fn centralization(&mut self) -> f64 {
1632        self.inner.pin_mut().centralization()
1633    }
1634
1635    fn maximum(&mut self) -> f64 {
1636        self.inner.pin_mut().maximum()
1637    }
1638
1639    fn ranking(&mut self) -> RankIter {
1640        let mut ks = vec![];
1641        let mut vs = vec![];
1642        LocalClusteringCoefficientRanking(self.inner.pin_mut(), &mut ks, &mut vs);
1643        RankIter { ks, vs, at: 0 }
1644    }
1645
1646    fn score(&mut self, node: u64) -> f64 {
1647        self.inner.pin_mut().score(node)
1648    }
1649
1650    fn scores(&mut self) -> ValueIter {
1651        ValueIter {
1652            inner: LocalClusteringCoefficientScores(self.inner.pin_mut()),
1653            at: 0,
1654        }
1655    }
1656}
1657
1658impl Algorithm for LocalClusteringCoefficient {
1659    fn run(&mut self) -> miette::Result<()> {
1660        self.inner.pin_mut().run().into_diagnostic()
1661    }
1662
1663    fn has_finished(&self) -> bool {
1664        self.inner.hasFinished()
1665    }
1666}
1667
1668pub struct LocalPartitionCoverage {
1669    inner: UniquePtr<bridge::LocalPartitionCoverage>,
1670}
1671
1672impl LocalPartitionCoverage {
1673    pub fn new(g: &crate::Graph, partition: &crate::Partition) -> Self {
1674        Self {
1675            inner: NewLocalPartitionCoverage(g, partition),
1676        }
1677    }
1678}
1679
1680impl Centrality for LocalPartitionCoverage {
1681    fn centralization(&mut self) -> f64 {
1682        self.inner.pin_mut().centralization()
1683    }
1684
1685    fn maximum(&mut self) -> f64 {
1686        self.inner.pin_mut().maximum()
1687    }
1688
1689    fn ranking(&mut self) -> RankIter {
1690        let mut ks = vec![];
1691        let mut vs = vec![];
1692        LocalPartitionCoverageRanking(self.inner.pin_mut(), &mut ks, &mut vs);
1693        RankIter { ks, vs, at: 0 }
1694    }
1695
1696    fn score(&mut self, node: u64) -> f64 {
1697        self.inner.pin_mut().score(node)
1698    }
1699
1700    fn scores(&mut self) -> ValueIter {
1701        ValueIter {
1702            inner: LocalPartitionCoverageScores(self.inner.pin_mut()),
1703            at: 0,
1704        }
1705    }
1706}
1707
1708impl Algorithm for LocalPartitionCoverage {
1709    fn run(&mut self) -> miette::Result<()> {
1710        self.inner.pin_mut().run().into_diagnostic()
1711    }
1712
1713    fn has_finished(&self) -> bool {
1714        self.inner.hasFinished()
1715    }
1716}
1717
1718pub struct LocalSquareClusteringCoefficient {
1719    inner: UniquePtr<bridge::LocalSquareClusteringCoefficient>,
1720}
1721
1722impl LocalSquareClusteringCoefficient {
1723    pub fn new(g: &crate::Graph) -> Self {
1724        Self {
1725            inner: NewLocalSquareClusteringCoefficient(g),
1726        }
1727    }
1728}
1729
1730impl Centrality for LocalSquareClusteringCoefficient {
1731    fn centralization(&mut self) -> f64 {
1732        self.inner.pin_mut().centralization()
1733    }
1734
1735    fn maximum(&mut self) -> f64 {
1736        self.inner.pin_mut().maximum()
1737    }
1738
1739    fn ranking(&mut self) -> RankIter {
1740        let mut ks = vec![];
1741        let mut vs = vec![];
1742        LocalSquareClusteringCoefficientRanking(self.inner.pin_mut(), &mut ks, &mut vs);
1743        RankIter { ks, vs, at: 0 }
1744    }
1745
1746    fn score(&mut self, node: u64) -> f64 {
1747        self.inner.pin_mut().score(node)
1748    }
1749
1750    fn scores(&mut self) -> ValueIter {
1751        ValueIter {
1752            inner: LocalSquareClusteringCoefficientScores(self.inner.pin_mut()),
1753            at: 0,
1754        }
1755    }
1756}
1757
1758impl Algorithm for LocalSquareClusteringCoefficient {
1759    fn run(&mut self) -> miette::Result<()> {
1760        self.inner.pin_mut().run().into_diagnostic()
1761    }
1762
1763    fn has_finished(&self) -> bool {
1764        self.inner.hasFinished()
1765    }
1766}
1767
1768pub struct PageRank {
1769    inner: UniquePtr<bridge::PageRank>,
1770}
1771
1772#[derive(Default)]
1773#[repr(u8)]
1774pub enum PageRankNorm {
1775    L1 = 0,
1776    #[default]
1777    L2 = 1,
1778}
1779
1780impl PageRank {
1781    pub fn new(
1782        g: &crate::Graph,
1783        damp: Option<f64>,
1784        tol: Option<f64>,
1785        normalized: bool,
1786        distribute_sinks: bool,
1787    ) -> Self {
1788        Self {
1789            inner: NewPageRank(
1790                g,
1791                damp.unwrap_or(0.85),
1792                tol.unwrap_or(1e-9),
1793                normalized,
1794                distribute_sinks,
1795            ),
1796        }
1797    }
1798    pub fn set_max_iterations(&mut self, max_iter: u64) {
1799        PageRankSetMaxIterations(self.inner.pin_mut(), max_iter)
1800    }
1801    pub fn set_norm(&mut self, norm: PageRankNorm) {
1802        PageRankSetNorm(self.inner.pin_mut(), norm as u8)
1803    }
1804    pub fn number_of_iterations(&self) -> u64 {
1805        self.inner.numberOfIterations()
1806    }
1807}
1808
1809impl Centrality for PageRank {
1810    fn centralization(&mut self) -> f64 {
1811        self.inner.pin_mut().centralization()
1812    }
1813
1814    fn maximum(&mut self) -> f64 {
1815        self.inner.pin_mut().maximum()
1816    }
1817
1818    fn ranking(&mut self) -> RankIter {
1819        let mut ks = vec![];
1820        let mut vs = vec![];
1821        PageRankRanking(self.inner.pin_mut(), &mut ks, &mut vs);
1822        RankIter { ks, vs, at: 0 }
1823    }
1824
1825    fn score(&mut self, node: u64) -> f64 {
1826        self.inner.pin_mut().score(node)
1827    }
1828
1829    fn scores(&mut self) -> ValueIter {
1830        ValueIter {
1831            inner: PageRankScores(self.inner.pin_mut()),
1832            at: 0,
1833        }
1834    }
1835}
1836
1837impl Algorithm for PageRank {
1838    fn run(&mut self) -> miette::Result<()> {
1839        self.inner.pin_mut().run().into_diagnostic()
1840    }
1841
1842    fn has_finished(&self) -> bool {
1843        self.inner.hasFinished()
1844    }
1845}
1846
1847pub struct PermanenceCentrality {
1848    inner: UniquePtr<bridge::PermanenceCentrality>,
1849}
1850
1851impl PermanenceCentrality {
1852    pub fn new(g: &crate::Graph, p: &crate::Partition) -> Self {
1853        Self {
1854            inner: NewPermanenceCentrality(g, p),
1855        }
1856    }
1857    pub fn get_permanence(&mut self, u: u64) -> f64 {
1858        self.inner.pin_mut().getPermanence(u)
1859    }
1860    pub fn get_intra_clustering(&mut self, u: u64) -> f64 {
1861        self.inner.pin_mut().getIntraClustering(u)
1862    }
1863}
1864
1865impl Algorithm for PermanenceCentrality {
1866    fn run(&mut self) -> miette::Result<()> {
1867        self.inner.pin_mut().run().into_diagnostic()
1868    }
1869
1870    fn has_finished(&self) -> bool {
1871        self.inner.hasFinished()
1872    }
1873}
1874
1875pub struct Sfigality {
1876    inner: UniquePtr<bridge::Sfigality>,
1877}
1878
1879impl Sfigality {
1880    pub fn new(g: &crate::Graph) -> Self {
1881        Self {
1882            inner: NewSfigality(g),
1883        }
1884    }
1885}
1886
1887impl Centrality for Sfigality {
1888    fn centralization(&mut self) -> f64 {
1889        self.inner.pin_mut().centralization()
1890    }
1891
1892    fn maximum(&mut self) -> f64 {
1893        self.inner.pin_mut().maximum()
1894    }
1895
1896    fn ranking(&mut self) -> RankIter {
1897        let mut ks = vec![];
1898        let mut vs = vec![];
1899        SfigalityRanking(self.inner.pin_mut(), &mut ks, &mut vs);
1900        RankIter { ks, vs, at: 0 }
1901    }
1902
1903    fn score(&mut self, node: u64) -> f64 {
1904        self.inner.pin_mut().score(node)
1905    }
1906
1907    fn scores(&mut self) -> ValueIter {
1908        ValueIter {
1909            inner: SfigalityScores(self.inner.pin_mut()),
1910            at: 0,
1911        }
1912    }
1913}
1914
1915impl Algorithm for Sfigality {
1916    fn run(&mut self) -> miette::Result<()> {
1917        self.inner.pin_mut().run().into_diagnostic()
1918    }
1919
1920    fn has_finished(&self) -> bool {
1921        self.inner.hasFinished()
1922    }
1923}
1924
1925pub struct SpanningEdgeCentrality {
1926    inner: UniquePtr<bridge::SpanningEdgeCentrality>,
1927}
1928
1929impl SpanningEdgeCentrality {
1930    pub fn new(g: &crate::Graph, tol: Option<f64>) -> Self {
1931        Self {
1932            inner: NewSpanningEdgeCentrality(g, tol.unwrap_or(0.1)),
1933        }
1934    }
1935    pub fn run_approximation(&mut self) {
1936        self.inner.pin_mut().runApproximation()
1937    }
1938    pub fn run_parallel_approximation(&mut self) {
1939        self.inner.pin_mut().runParallelApproximation()
1940    }
1941}
1942
1943impl Centrality for SpanningEdgeCentrality {
1944    fn centralization(&mut self) -> f64 {
1945        self.inner.pin_mut().centralization()
1946    }
1947
1948    fn maximum(&mut self) -> f64 {
1949        self.inner.pin_mut().maximum()
1950    }
1951
1952    fn ranking(&mut self) -> RankIter {
1953        let mut ks = vec![];
1954        let mut vs = vec![];
1955        SpanningEdgeCentralityRanking(self.inner.pin_mut(), &mut ks, &mut vs);
1956        RankIter { ks, vs, at: 0 }
1957    }
1958
1959    fn score(&mut self, node: u64) -> f64 {
1960        self.inner.pin_mut().score(node)
1961    }
1962
1963    fn scores(&mut self) -> ValueIter {
1964        ValueIter {
1965            inner: SpanningEdgeCentralityScores(self.inner.pin_mut()),
1966            at: 0,
1967        }
1968    }
1969}
1970
1971impl Algorithm for SpanningEdgeCentrality {
1972    fn run(&mut self) -> miette::Result<()> {
1973        self.inner.pin_mut().run().into_diagnostic()
1974    }
1975
1976    fn has_finished(&self) -> bool {
1977        self.inner.hasFinished()
1978    }
1979}
1980
1981pub struct TopCloseness {
1982    inner: UniquePtr<bridge::TopCloseness>,
1983}
1984
1985impl TopCloseness {
1986    pub fn new(g: &crate::Graph, k: u64, first_heu: bool, sec_heu: bool) -> Self {
1987        Self {
1988            inner: NewTopCloseness(g, k, first_heu, sec_heu),
1989        }
1990    }
1991
1992    pub fn top_k_nodes_list(&mut self, include_trail: bool) -> impl Iterator<Item = u64> {
1993        NodeIter {
1994            at: 0,
1995            nodes: TopClosenessTopkNodesList(self.inner.pin_mut(), include_trail),
1996        }
1997    }
1998    pub fn top_k_scores_list(&mut self, include_trail: bool) -> impl Iterator<Item = f64> {
1999        ValueIter {
2000            at: 0,
2001            inner: TopClosenessTopkScoresList(self.inner.pin_mut(), include_trail),
2002        }
2003    }
2004    pub fn restrict_top_k_computation_to_nodes(&mut self, nodes: &[u64]) {
2005        TopClosenessRestrictTopKComputationToNodes(self.inner.pin_mut(), nodes)
2006    }
2007}
2008
2009impl Algorithm for TopCloseness {
2010    fn run(&mut self) -> miette::Result<()> {
2011        self.inner.pin_mut().run().into_diagnostic()
2012    }
2013
2014    fn has_finished(&self) -> bool {
2015        self.inner.hasFinished()
2016    }
2017}
2018
2019pub struct TopHarmonicCloseness {
2020    inner: UniquePtr<bridge::TopHarmonicCloseness>,
2021}
2022
2023impl TopHarmonicCloseness {
2024    pub fn new(g: &crate::Graph, k: u64, use_nb_bound: bool) -> Self {
2025        Self {
2026            inner: NewTopHarmonicCloseness(g, k, use_nb_bound),
2027        }
2028    }
2029
2030    pub fn top_k_nodes_list(&mut self, include_trail: bool) -> impl Iterator<Item = u64> {
2031        NodeIter {
2032            at: 0,
2033            nodes: TopHarmonicClosenessTopkNodesList(self.inner.pin_mut(), include_trail),
2034        }
2035    }
2036    pub fn top_k_scores_list(&mut self, include_trail: bool) -> impl Iterator<Item = f64> {
2037        ValueIter {
2038            at: 0,
2039            inner: TopHarmonicClosenessTopkScoresList(self.inner.pin_mut(), include_trail),
2040        }
2041    }
2042    pub fn restrict_top_k_computation_to_nodes(&mut self, nodes: &[u64]) {
2043        TopHarmonicClosenessRestrictTopKComputationToNodes(self.inner.pin_mut(), nodes)
2044    }
2045}
2046
2047impl Algorithm for TopHarmonicCloseness {
2048    fn run(&mut self) -> miette::Result<()> {
2049        self.inner.pin_mut().run().into_diagnostic()
2050    }
2051
2052    fn has_finished(&self) -> bool {
2053        self.inner.hasFinished()
2054    }
2055}