gitql_std/number/
mod.rs

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
use std::collections::HashMap;
use std::ops::Rem;

use gitql_ast::types::dynamic::DynamicType;
use gitql_ast::types::float::FloatType;
use gitql_ast::types::integer::IntType;
use gitql_ast::types::optional::OptionType;
use gitql_ast::types::variant::VariantType;
use gitql_core::signature::Signature;
use gitql_core::signature::StandardFunction;
use gitql_core::values::base::Value;
use gitql_core::values::float::FloatValue;
use gitql_core::values::integer::IntValue;
use gitql_core::values::null::NullValue;

use crate::meta_types::first_element_type;

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, StandardFunction>) {
    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![Box::new(VariantType {
                variants: vec![Box::new(IntType), Box::new(FloatType)],
            })],
            return_type: Box::new(DynamicType {
                function: first_element_type,
            }),
        },
    );
    map.insert(
        "pi",
        Signature {
            parameters: vec![],
            return_type: Box::new(FloatType),
        },
    );
    map.insert(
        "floor",
        Signature {
            parameters: vec![Box::new(FloatType)],
            return_type: Box::new(IntType),
        },
    );
    map.insert(
        "round",
        Signature {
            parameters: vec![
                Box::new(FloatType),
                Box::new(OptionType {
                    base: Some(Box::new(IntType)),
                }),
            ],
            return_type: Box::new(FloatType),
        },
    );
    map.insert(
        "square",
        Signature {
            parameters: vec![Box::new(IntType)],
            return_type: Box::new(IntType),
        },
    );
    map.insert(
        "sin",
        Signature {
            parameters: vec![Box::new(FloatType)],
            return_type: Box::new(FloatType),
        },
    );
    map.insert(
        "asin",
        Signature {
            parameters: vec![Box::new(FloatType)],
            return_type: Box::new(FloatType),
        },
    );
    map.insert(
        "cos",
        Signature {
            parameters: vec![Box::new(FloatType)],
            return_type: Box::new(FloatType),
        },
    );
    map.insert(
        "acos",
        Signature {
            parameters: vec![Box::new(FloatType)],
            return_type: Box::new(FloatType),
        },
    );
    map.insert(
        "tan",
        Signature {
            parameters: vec![Box::new(FloatType)],
            return_type: Box::new(FloatType),
        },
    );
    map.insert(
        "atan",
        Signature {
            parameters: vec![Box::new(FloatType)],
            return_type: Box::new(FloatType),
        },
    );
    map.insert(
        "atn2",
        Signature {
            parameters: vec![Box::new(FloatType), Box::new(FloatType)],
            return_type: Box::new(FloatType),
        },
    );
    map.insert(
        "sign",
        Signature {
            parameters: vec![Box::new(VariantType {
                variants: vec![Box::new(IntType), Box::new(FloatType)],
            })],
            return_type: Box::new(IntType),
        },
    );
    map.insert(
        "mod",
        Signature {
            parameters: vec![Box::new(IntType), Box::new(IntType)],
            return_type: Box::new(IntType),
        },
    );
    map.insert(
        "rand",
        Signature {
            parameters: vec![Box::new(OptionType {
                base: Some(Box::new(FloatType)),
            })],
            return_type: Box::new(FloatType),
        },
    );
}

pub fn numeric_abs(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let input_type = inputs[0].data_type();
    if input_type.is_float() {
        return Box::new(FloatValue {
            value: inputs[0].as_float().unwrap().abs(),
        });
    }

    Box::new(IntValue {
        value: inputs[0].as_int().unwrap().abs(),
    })
}

pub fn numeric_pi(_inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let pi = std::f64::consts::PI;
    Box::new(FloatValue { value: pi })
}

pub fn numeric_floor(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let float_value = inputs[0].as_float().unwrap();
    Box::new(IntValue {
        value: float_value.floor() as i64,
    })
}

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

pub fn numeric_square(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let int_value = inputs[0].as_int().unwrap();
    Box::new(IntValue {
        value: int_value * int_value,
    })
}

pub fn numeric_sin(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let float_value = inputs[0].as_float().unwrap();
    Box::new(FloatValue {
        value: f64::sin(float_value),
    })
}

pub fn numeric_asin(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let float_value = inputs[0].as_float().unwrap();
    Box::new(FloatValue {
        value: f64::asin(float_value),
    })
}

pub fn numeric_cos(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let float_value = inputs[0].as_float().unwrap();
    Box::new(FloatValue {
        value: f64::cos(float_value),
    })
}

pub fn numeric_acos(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let float_value = inputs[0].as_float().unwrap();
    Box::new(FloatValue {
        value: f64::acos(float_value),
    })
}

pub fn numeric_tan(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let float_value = inputs[0].as_float().unwrap();
    Box::new(FloatValue {
        value: f64::tan(float_value),
    })
}

pub fn numeric_atan(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let float_value = inputs[0].as_float().unwrap();
    Box::new(FloatValue {
        value: f64::atan(float_value),
    })
}

pub fn numeric_atn2(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let first = inputs[0].as_float().unwrap();
    let other = inputs[1].as_float().unwrap();
    Box::new(FloatValue {
        value: f64::atan2(first, other),
    })
}

pub fn numeric_sign(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let value = &inputs[0];
    if value.data_type().is_int() {
        let value = value.as_int().unwrap().signum();
        return Box::new(IntValue { value });
    }

    let float_value = value.as_float().unwrap();
    let value = if float_value == 0.0 {
        0
    } else if float_value > 0.0 {
        1
    } else {
        -1
    };
    Box::new(IntValue { value })
}

pub fn numeric_mod(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let other = inputs[1].as_int().unwrap();
    if other == 0 {
        return Box::new(NullValue);
    }

    let first = inputs[0].as_int().unwrap();
    let value = first.rem(other);
    Box::new(IntValue { value })
}

pub fn numeric_rand(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let mut rng: StdRng = match inputs.first() {
        Some(s) => SeedableRng::seed_from_u64(s.as_int().unwrap().try_into().unwrap()),
        None => SeedableRng::from_entropy(),
    };

    Box::new(FloatValue {
        value: rng.sample(Uniform::from(0.0..1.0)),
    })
}