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
use gitql_core::dynamic_types::first_element_type;
use gitql_core::signature::Function;
use gitql_core::signature::Signature;
use gitql_core::types::DataType;
use gitql_core::value::Value;

use std::collections::HashMap;
use std::ops::Rem;

use rand::distributions::Uniform;
use rand::rngs::StdRng;
use rand::Rng;
use rand::SeedableRng;

#[inline(always)]
pub fn register_std_number_functions(map: &mut HashMap<&'static str, Function>) {
    map.insert("abs", numeric_abs);
    map.insert("pi", numeric_pi);
    map.insert("floor", numeric_floor);
    map.insert("round", numeric_round);
    map.insert("square", numeric_square);
    map.insert("sin", numeric_sin);
    map.insert("asin", numeric_asin);
    map.insert("cos", numeric_cos);
    map.insert("acos", numeric_acos);
    map.insert("tan", numeric_tan);
    map.insert("atan", numeric_atan);
    map.insert("atn2", numeric_atn2);
    map.insert("sign", numeric_sign);
    map.insert("mod", numeric_mod);
    map.insert("rand", numeric_rand);
}

#[inline(always)]
pub fn register_std_number_function_signatures(map: &mut HashMap<&'static str, Signature>) {
    map.insert(
        "abs",
        Signature {
            parameters: vec![DataType::Variant(vec![DataType::Integer, DataType::Float])],
            return_type: DataType::Dynamic(first_element_type),
        },
    );
    map.insert(
        "pi",
        Signature {
            parameters: vec![],
            return_type: DataType::Float,
        },
    );
    map.insert(
        "floor",
        Signature {
            parameters: vec![DataType::Float],
            return_type: DataType::Integer,
        },
    );
    map.insert(
        "round",
        Signature {
            parameters: vec![
                DataType::Float,
                DataType::Optional(Box::new(DataType::Integer)),
            ],
            return_type: DataType::Float,
        },
    );
    map.insert(
        "square",
        Signature {
            parameters: vec![DataType::Integer],
            return_type: DataType::Integer,
        },
    );
    map.insert(
        "sin",
        Signature {
            parameters: vec![DataType::Float],
            return_type: DataType::Float,
        },
    );
    map.insert(
        "asin",
        Signature {
            parameters: vec![DataType::Float],
            return_type: DataType::Float,
        },
    );
    map.insert(
        "cos",
        Signature {
            parameters: vec![DataType::Float],
            return_type: DataType::Float,
        },
    );
    map.insert(
        "acos",
        Signature {
            parameters: vec![DataType::Float],
            return_type: DataType::Float,
        },
    );
    map.insert(
        "tan",
        Signature {
            parameters: vec![DataType::Float],
            return_type: DataType::Float,
        },
    );
    map.insert(
        "atan",
        Signature {
            parameters: vec![DataType::Float],
            return_type: DataType::Float,
        },
    );
    map.insert(
        "atn2",
        Signature {
            parameters: vec![DataType::Float, DataType::Float],
            return_type: DataType::Float,
        },
    );
    map.insert(
        "sign",
        Signature {
            parameters: vec![DataType::Variant(vec![DataType::Integer, DataType::Float])],
            return_type: DataType::Integer,
        },
    );
    map.insert(
        "mod",
        Signature {
            parameters: vec![DataType::Integer, DataType::Integer],
            return_type: DataType::Integer,
        },
    );
    map.insert(
        "rand",
        Signature {
            parameters: vec![DataType::Optional(Box::new(DataType::Float))],
            return_type: DataType::Float,
        },
    );
}

pub fn numeric_abs(inputs: &[Value]) -> Value {
    let input_type = inputs[0].data_type();
    if input_type.is_float() {
        return Value::Float(inputs[0].as_float().abs());
    }
    Value::Integer(inputs[0].as_int().abs())
}

pub fn numeric_pi(_inputs: &[Value]) -> Value {
    let pi = std::f64::consts::PI;
    Value::Float(pi)
}

pub fn numeric_floor(inputs: &[Value]) -> Value {
    let float_value = inputs[0].as_float();
    Value::Integer(float_value.floor() as i64)
}

pub fn numeric_round(inputs: &[Value]) -> Value {
    let number = inputs[0].as_float();
    let decimal_places = if inputs.len() == 2 {
        inputs[1].as_int()
    } else {
        0
    };
    let multiplier = 10_f64.powi(decimal_places.try_into().unwrap());
    let result = (number * multiplier).round() / multiplier;
    Value::Float(result)
}

pub fn numeric_square(inputs: &[Value]) -> Value {
    let int_value = inputs[0].as_int();
    Value::Integer(int_value * int_value)
}

pub fn numeric_sin(inputs: &[Value]) -> Value {
    let float_value = inputs[0].as_float();
    Value::Float(f64::sin(float_value))
}

pub fn numeric_asin(inputs: &[Value]) -> Value {
    let float_value = inputs[0].as_float();
    Value::Float(f64::asin(float_value))
}

pub fn numeric_cos(inputs: &[Value]) -> Value {
    let float_value = inputs[0].as_float();
    Value::Float(f64::cos(float_value))
}

pub fn numeric_acos(inputs: &[Value]) -> Value {
    let float_value = inputs[0].as_float();
    Value::Float(f64::acos(float_value))
}

pub fn numeric_tan(inputs: &[Value]) -> Value {
    let float_value = inputs[0].as_float();
    Value::Float(f64::tan(float_value))
}

pub fn numeric_atan(inputs: &[Value]) -> Value {
    let float_value = inputs[0].as_float();
    Value::Float(f64::atan(float_value))
}

pub fn numeric_atn2(inputs: &[Value]) -> Value {
    let first = inputs[0].as_float();
    let other = inputs[1].as_float();
    Value::Float(f64::atan2(first, other))
}

pub fn numeric_sign(inputs: &[Value]) -> Value {
    let value = &inputs[0];
    if value.data_type().is_int() {
        let int_value = value.as_int();
        return Value::Integer(int_value.signum());
    }

    let float_value = value.as_float();
    if float_value == 0.0 {
        Value::Integer(0)
    } else if float_value > 0.0 {
        Value::Integer(1)
    } else {
        Value::Integer(-1)
    }
}

pub fn numeric_mod(inputs: &[Value]) -> Value {
    let other = &inputs[1];
    if other.as_int() == 0 {
        return Value::Null;
    }

    let first = &inputs[0];
    Value::Integer(first.as_int().rem(other.as_int()))
}

pub fn numeric_rand(inputs: &[Value]) -> Value {
    let mut rng: StdRng = match inputs.first() {
        Some(s) => SeedableRng::seed_from_u64(s.as_int().try_into().unwrap()),
        None => SeedableRng::from_entropy(),
    };
    Value::Float(rng.sample(Uniform::from(0.0..1.0)))
}