trs_dataframe/dataframe/
index.rs

1use data_value::DataValue;
2
3use super::{column_store::ColumnFrame, Key};
4
5/// Index for the fast search in the candidates
6/// This is used for joining the dataframes by the given keys
7#[derive(Debug)]
8pub struct Index {
9    /// indexes for the values in the [`ColumnFrame`]
10    index: Vec<usize>,
11    /// actual values under the keys
12    values: Vec<Vec<DataValue>>,
13}
14
15impl Index {
16    /// Create the index for the given keys and the [`ColumnFrame`] for the given keys.
17    /// This will enumerate the values and store them in the index with current values
18    pub fn new(key: Vec<Key>, df: &ColumnFrame) -> Self {
19        let selected = df.select(Some(key.as_slice()));
20        let mut this = Self {
21            index: Vec::new(),
22            values: Vec::new(),
23        };
24
25        for (index, candidate) in selected.rows().into_iter().enumerate() {
26            this.index.push(index);
27            this.values.push(candidate.to_vec());
28        }
29        this
30    }
31
32    pub fn get(&self, values: &[DataValue]) -> Option<usize> {
33        self.values
34            .iter()
35            .position(|row| row.iter().zip(values.iter()).all(|(a, b)| a == b))
36            .map(|idx| self.index[idx])
37    }
38}