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
//! Implementations of various graph algorithms that can be run on the graph.
//!
//! The algorithms are grouped into modules based on the type of graph they can be run on.
//!
//! To run an algorithm simply import the module and call the function.
//!
//! # Examples
//!
//! ```rust
//! use raphtory::algorithms::degree::{average_degree};
//! use raphtory::db::graph::Graph;
//!  
//!  let g = Graph::new(1);
//!  let vs = vec![
//!      (1, 1, 2),
//!      (2, 1, 3),
//!      (3, 2, 1),
//!      (4, 3, 2),
//!      (5, 1, 4),
//!      (6, 4, 5),
//!   ];
//!
//!  for (t, src, dst) in &vs {
//!    g.add_edge(*t, *src, *dst, &vec![], None);
//!  };
//! println!("average_degree: {:?}", average_degree(&g));
//! ```

pub mod clustering_coefficient;
pub mod connected_components;
pub mod degree;
pub mod directed_graph_density;
pub mod generic_taint;
pub mod hits;
pub mod local_clustering_coefficient;
pub mod local_triangle_count;
pub mod pagerank;
pub mod reciprocity;
pub mod triangle_count;
pub mod triplet_count;

use num_traits::{abs, Bounded, Zero};
use std::ops::{Add, AddAssign, Div, Mul, Range, Sub};

use crate::core::agg::Init;

struct InitOneF32();
impl Init<f32> for InitOneF32 {
    fn init() -> f32 {
        1.0f32
    }
}

#[derive(PartialEq, PartialOrd, Copy, Clone, Debug)]
struct MulF32(f32);

const MUL_F32_ZERO: MulF32 = MulF32(1.0f32);

impl Zero for MulF32 {
    fn zero() -> Self {
        MUL_F32_ZERO
    }

    fn set_zero(&mut self) {
        *self = Zero::zero();
    }

    fn is_zero(&self) -> bool {
        *self == MUL_F32_ZERO
    }
}

impl Add for MulF32 {
    type Output = MulF32;

    fn add(self, rhs: Self) -> Self::Output {
        MulF32(self.0 + rhs.0)
    }
}

impl AddAssign for MulF32 {
    fn add_assign(&mut self, rhs: Self) {
        self.0 = self.0 + rhs.0
    }
}

impl Sub for MulF32 {
    type Output = MulF32;

    fn sub(self, rhs: Self) -> Self::Output {
        MulF32(self.0 - rhs.0)
    }
}

impl Div for MulF32 {
    type Output = MulF32;

    fn div(self, rhs: Self) -> Self::Output {
        MulF32(self.0 / rhs.0)
    }
}

impl Mul for MulF32 {
    type Output = MulF32;

    fn mul(self, rhs: Self) -> Self::Output {
        MulF32(self.0 * rhs.0)
    }
}

impl Bounded for MulF32 {
    fn min_value() -> Self {
        MulF32(f32::MIN)
    }

    fn max_value() -> Self {
        MulF32(f32::MAX)
    }
}

#[derive(PartialEq, PartialOrd, Copy, Clone, Debug)]
struct SumF32(f32);

impl Zero for SumF32 {
    fn zero() -> Self {
        SumF32(0.0f32)
    }

    fn set_zero(&mut self) {
        *self = Zero::zero();
    }

    fn is_zero(&self) -> bool {
        *self == SumF32(1.0f32)
    }
}

impl Add for SumF32 {
    type Output = SumF32;

    fn add(self, rhs: Self) -> Self::Output {
        SumF32(self.0 + rhs.0)
    }
}

impl AddAssign for SumF32 {
    fn add_assign(&mut self, rhs: Self) {
        self.0 = self.0 + rhs.0
    }
}

impl Sub for SumF32 {
    type Output = SumF32;

    fn sub(self, rhs: Self) -> Self::Output {
        SumF32(self.0 - rhs.0)
    }
}

impl Div for SumF32 {
    type Output = SumF32;

    fn div(self, rhs: Self) -> Self::Output {
        SumF32(self.0 / rhs.0)
    }
}

impl Mul for SumF32 {
    type Output = SumF32;

    fn mul(self, rhs: Self) -> Self::Output {
        SumF32(self.0 * rhs.0)
    }
}

impl Bounded for SumF32 {
    fn min_value() -> Self {
        SumF32(f32::MIN)
    }

    fn max_value() -> Self {
        SumF32(f32::MAX)
    }
}

#[derive(PartialEq, PartialOrd, Copy, Clone, Debug)]
struct Bool(bool);

impl Zero for Bool {
    fn zero() -> Self {
        Bool(false)
    }

    fn set_zero(&mut self) {
        *self = Zero::zero();
    }

    fn is_zero(&self) -> bool {
        *self == Bool(false)
    }
}

impl Add for Bool {
    type Output = Bool;

    fn add(self, rhs: Self) -> Self::Output {
        rhs
    }
}