gitql_std/array/
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
297
298
299
300
301
302
303
304
305
306
307
308
309
use gitql_ast::types::any::AnyType;
use gitql_ast::types::array::ArrayType;
use gitql_ast::types::dynamic::DynamicType;
use gitql_ast::types::integer::IntType;
use gitql_ast::types::text::TextType;
use gitql_core::dynamic_types::array_element_type;
use gitql_core::dynamic_types::array_of_type;
use gitql_core::dynamic_types::first_element_type;
use gitql_core::dynamic_types::second_element_type;
use gitql_core::signature::Function;
use gitql_core::signature::Signature;
use gitql_core::values::array::ArrayValue;
use gitql_core::values::base::Value;
use gitql_core::values::integer::IntValue;
use gitql_core::values::null::NullValue;
use gitql_core::values::text::TextValue;

use rand::seq::SliceRandom;

use std::collections::HashMap;

#[inline(always)]
pub fn register_std_array_functions(map: &mut HashMap<&'static str, Function>) {
    map.insert("array_append", array_append);
    map.insert("array_prepend", array_prepend);
    map.insert("array_remove", array_remove);
    map.insert("array_cat", array_cat);
    map.insert("array_length", array_length);
    map.insert("array_shuffle", array_shuffle);
    map.insert("array_position", array_position);
    map.insert("array_positions", array_positions);
    map.insert("array_dims", array_dims);
    map.insert("array_replace", array_replace);
    map.insert("trim_array", array_trim);
}

#[inline(always)]
pub fn register_std_array_function_signatures(map: &mut HashMap<&'static str, Signature>) {
    map.insert(
        "array_append",
        Signature {
            parameters: vec![
                Box::new(ArrayType {
                    base: Box::new(AnyType),
                }),
                Box::new(DynamicType {
                    function: |elements| array_element_type(first_element_type(elements)),
                }),
            ],
            return_type: Box::new(DynamicType {
                function: first_element_type,
            }),
        },
    );
    map.insert(
        "array_prepend",
        Signature {
            parameters: vec![
                Box::new(AnyType),
                Box::new(DynamicType {
                    function: |elements| array_of_type(first_element_type(elements)),
                }),
            ],
            return_type: Box::new(DynamicType {
                function: second_element_type,
            }),
        },
    );
    map.insert(
        "array_remove",
        Signature {
            parameters: vec![
                Box::new(ArrayType {
                    base: Box::new(AnyType),
                }),
                Box::new(DynamicType {
                    function: |elements| array_element_type(first_element_type(elements)),
                }),
            ],
            return_type: Box::new(DynamicType {
                function: first_element_type,
            }),
        },
    );
    map.insert(
        "array_cat",
        Signature {
            parameters: vec![
                Box::new(ArrayType {
                    base: Box::new(AnyType),
                }),
                Box::new(DynamicType {
                    function: first_element_type,
                }),
            ],
            return_type: Box::new(DynamicType {
                function: first_element_type,
            }),
        },
    );
    map.insert(
        "array_length",
        Signature {
            parameters: vec![Box::new(ArrayType {
                base: Box::new(AnyType),
            })],
            return_type: Box::new(IntType),
        },
    );
    map.insert(
        "array_shuffle",
        Signature {
            parameters: vec![Box::new(ArrayType {
                base: Box::new(AnyType),
            })],
            return_type: Box::new(DynamicType {
                function: first_element_type,
            }),
        },
    );
    map.insert(
        "array_position",
        Signature {
            parameters: vec![
                Box::new(ArrayType {
                    base: Box::new(AnyType),
                }),
                Box::new(DynamicType {
                    function: |elements| array_element_type(first_element_type(elements)),
                }),
            ],
            return_type: Box::new(IntType),
        },
    );
    map.insert(
        "array_dims",
        Signature {
            parameters: vec![Box::new(ArrayType {
                base: Box::new(AnyType),
            })],
            return_type: Box::new(TextType),
        },
    );
    map.insert(
        "array_replace",
        Signature {
            parameters: vec![
                Box::new(ArrayType {
                    base: Box::new(AnyType),
                }),
                Box::new(DynamicType {
                    function: |elements| array_element_type(first_element_type(elements)),
                }),
                Box::new(DynamicType {
                    function: |elements| array_element_type(first_element_type(elements)),
                }),
            ],
            return_type: Box::new(DynamicType {
                function: first_element_type,
            }),
        },
    );
    map.insert(
        "trim_array",
        Signature {
            parameters: vec![
                Box::new(ArrayType {
                    base: Box::new(AnyType),
                }),
                Box::new(IntType),
            ],
            return_type: Box::new(DynamicType {
                function: first_element_type,
            }),
        },
    );
}

pub fn array_append(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let mut array = inputs[0].as_array().unwrap();
    let element = &inputs[1];
    array.push(element.to_owned());
    Box::new(ArrayValue {
        values: array,
        base_type: inputs[0].data_type().clone(),
    })
}

pub fn array_prepend(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let element = &inputs[0];
    let mut array = inputs[1].as_array().unwrap();
    array.insert(0, element.clone());
    Box::new(ArrayValue {
        values: array,
        base_type: inputs[1].data_type().clone(),
    })
}

pub fn array_remove(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let array = inputs[0].as_array().unwrap();
    let element_to_remove = &inputs[1];
    let array_after_remove = array
        .into_iter()
        .filter(|element| !element_to_remove.equals(element))
        .collect();
    Box::new(ArrayValue {
        values: array_after_remove,
        base_type: inputs[0].data_type().clone(),
    })
}

pub fn array_cat(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let mut first = inputs[0].as_array().unwrap();
    let mut other = inputs[1].as_array().unwrap();
    let mut result = Vec::with_capacity(first.len() + other.len());
    result.append(&mut first);
    result.append(&mut other);
    Box::new(ArrayValue {
        values: result,
        base_type: inputs[0].data_type().clone(),
    })
}

pub fn array_length(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let array = inputs[0].as_array().unwrap();
    let value = array.len() as i64;
    Box::new(IntValue { value })
}

pub fn array_shuffle(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let array_type = &inputs[0].data_type();
    let element_type = &array_type
        .as_any()
        .downcast_ref::<ArrayType>()
        .unwrap()
        .base;

    let mut array = inputs[0].as_array().unwrap();
    array.shuffle(&mut rand::thread_rng());
    Box::new(ArrayValue {
        values: array,
        base_type: element_type.clone(),
    })
}

pub fn array_position(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let array = inputs[0].as_array().unwrap();
    let elemnet = &inputs[1];
    if let Some(index) = array.iter().position(|r| r.equals(elemnet)) {
        return Box::new(IntValue {
            value: (index + 1) as i64,
        });
    }

    Box::new(NullValue)
}

pub fn array_positions(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let array = inputs[0].as_array().unwrap();
    let target = &inputs[1];
    let mut positions: Vec<Box<dyn Value>> = vec![];
    for (index, element) in array.into_iter().enumerate() {
        if element.equals(target) {
            positions.push(Box::new(IntValue {
                value: (index + 1) as i64,
            }));
        }
    }
    Box::new(ArrayValue {
        values: positions,
        base_type: Box::new(IntType),
    })
}

pub fn array_dims(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let array_type = inputs[0].data_type();
    Box::new(TextValue {
        value: array_type.to_string(),
    })
}

pub fn array_replace(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let array_type = inputs[0].data_type();
    let mut array_values = inputs[0].as_array().unwrap();
    let from = &inputs[1];
    let to = &inputs[2];
    for element in &mut array_values {
        if element.equals(from) {
            *element = to.clone();
        }
    }

    Box::new(ArrayValue {
        values: array_values,
        base_type: array_type,
    })
}

pub fn array_trim(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let mut array = inputs[0].as_array().unwrap();
    let array_type = inputs[0].data_type();
    let array_len = array.len();
    let n = i64::min(array.len().try_into().unwrap(), inputs[1].as_int().unwrap());
    array.truncate(array_len - n as usize);
    Box::new(ArrayValue {
        values: array,
        base_type: array_type,
    })
}