kvtree 0.1.0

Heterogenous in memory key value tree storage
Documentation
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Grid storage module

mod cell;
mod column;
mod row;

pub use self::{cell::*, column::*, row::*};
use crate::data::Data;
use ahash::AHashMap;
use std::any::TypeId;

/// Array of struct storage
///
/// Each columns contains the same number of rows
///
/// For example, a [`Grid`] containing 4 columns for types `A`, `B`, `C` and `D`:
///
/// |   row index   | A | B | C | D |
/// |---------------|---|---|---|---|
/// |       0       | A | B | C | D |
/// |       1       | A | B | C | D |
/// |      ...      | - | - | - | - |
#[derive(Debug, Default)]
pub struct Grid {
    columns: AHashMap<TypeId, Box<dyn Column>>,
}

impl Grid {
    /// create a new empty grid
    pub fn new() -> Grid {
        Grid::default()
    }

    /// create a new grid with the given columns
    pub fn new_with<V: RowLike>() -> Grid {
        Self::with_capacity::<V>(0)
    }

    /// create a new grid with the given columns and reserver slots for each columns
    pub fn with_capacity<V: RowLike>(n: usize) -> Grid {
        Grid {
            columns: V::new_columns_with_capatity(n)
                .into_iter()
                .map(|x| (x.column_type(), x))
                .collect(),
        }
    }

    /// return true if this grid contains the given data
    pub fn contains<T: Data>(&self) -> bool {
        self.columns.contains_key(&T::ID)
    }

    /// return true if this grid contains the given type
    pub fn contains_type(&self, ty: &TypeId) -> bool {
        self.columns.contains_key(ty)
    }

    /// Number of columns in this grid
    pub fn len_cols(&self) -> usize {
        self.columns.len()
    }

    /// return true if this grid has columns
    pub fn has_cols(&self) -> bool {
        !self.columns.is_empty()
    }

    /// Number of rows in this grid
    pub fn len_rows(&self) -> usize {
        self.columns.values().next().map_or(0, |x| x.len())
    }

    /// returns true if the grid has no rows
    pub fn is_empty(&self) -> bool {
        self.columns.values().next().map_or(true, |x| x.is_empty())
    }

    /// insert the given column in this grid
    ///
    /// if this grid is empty, anything can be inserted
    ///
    /// returns the provided columns if the insertion is not possible
    pub fn insert<V: RowLike>(&mut self, columns: V) -> Result<(), V> {
        if self.columns.is_empty() {
            self.columns = columns
                .into_columns()
                .into_iter()
                .map(|x| (x.column_type(), x))
                .collect();
            Ok(())
        } else if V::match_grid(self, false) {
            for cell in columns.into_cells() {
                self.columns
                    .get_mut(&(*cell).cell_type_id())
                    .unwrap()
                    .push_cell(cell);
            }
            Ok(())
        } else {
            Err(columns)
        }
    }

    /// insert the given column in this grid
    ///
    /// if this grid is empty, anything can be inserted
    ///
    /// returns the provided row if insertion fails
    pub fn insert_row(&mut self, row: Row) -> Result<(), Row> {
        if self.columns.is_empty() {
            self.columns = row.into_iter().map(|(x, y)| (x, y.into_column())).collect();
            Ok(())
        } else if row.match_grid(self, false) {
            for (x, y) in row {
                self.columns.get_mut(&x).unwrap().push_cell(y);
            }
            Ok(())
        } else {
            Err(row)
        }
    }

    /// update the given row
    ///
    /// update can be partial (ie: contain less columns than this grid)
    ///
    /// if the provided columns cannot be inserted an error is returned
    pub fn update<V: RowLike>(&mut self, index: usize, columns: V) -> Result<Row, V> {
        if V::match_grid(self, true) {
            let mut row = Row::default();
            for cell in columns.into_cells() {
                row.upsert_cell(
                    self.columns
                        .get_mut(&(*cell).cell_type_id())
                        .unwrap()
                        .replace(index, cell),
                );
            }
            Ok(row)
        } else {
            Err(columns)
        }
    }

    /// update the given row
    ///
    /// update can be partial (ie: contain less columns than this grid)
    ///
    /// if the provided columns cannot be inserted an error is returned
    pub fn update_row(&mut self, index: usize, row: Row) -> Result<Row, Row> {
        if row.match_grid(self, true) {
            let mut old = Row::default();
            for (x, y) in row {
                old.upsert_cell(self.columns.get_mut(&x).unwrap().replace(index, y));
            }
            Ok(old)
        } else {
            Err(row)
        }
    }

    /// remove swap the given row from this grid
    ///
    /// panics if the index is out of bounds
    pub fn swap_remove(&mut self, index: usize) -> Row {
        self.columns
            .iter_mut()
            .map(|(_, x)| x.swap_remove(index))
            .collect()
    }

    /// insert the given column in this grid
    ///
    /// if this grid has no defined, columns anything can be insert
    ///
    /// panics if the `columns` do no match the existing columns
    pub fn extend<V: RowLike>(&mut self, columns: impl IntoIterator<Item = V>) {
        if self.columns.is_empty() {
            *self = columns.into_iter().collect();
        } else if V::match_grid(self, false) {
            for x in columns {
                for y in x.into_cells() {
                    self.columns
                        .get_mut(&(*y).cell_type_id())
                        .unwrap()
                        .push_cell(y);
                }
            }
        } else {
            panic!("columns <=> grid missmatch")
        }
    }

    /// create a new reference to this grid
    pub fn grid_ref(&self) -> Ref<'_> {
        Ref {
            columns: self.columns.iter().map(|(x, y)| (*x, y.as_dyn())).collect(),
        }
    }

    /// create a new mutable reference to this grid
    pub fn grid_mut(&mut self) -> Mut<'_> {
        Mut {
            columns: self
                .columns
                .iter_mut()
                .map(|(x, y)| (*x, y.as_dyn_mut()))
                .collect(),
        }
    }
}

impl<V: RowLike> FromIterator<V> for Grid {
    fn from_iter<T: IntoIterator<Item = V>>(iter: T) -> Self {
        let iter = iter.into_iter();
        let mut columns: AHashMap<TypeId, Box<dyn Column>> =
            V::new_columns_with_capatity(match iter.size_hint() {
                (_, Some(x)) => x,
                (x, _) => x,
            })
            .into_iter()
            .map(|x| ((*x).column_type(), x))
            .collect();
        for x in iter {
            for y in x.into_cells() {
                columns.get_mut(&(*y).cell_type_id()).unwrap().push_cell(y);
            }
        }
        Grid { columns }
    }
}

impl<V: RowLike> std::iter::Extend<V> for Grid {
    fn extend<T: IntoIterator<Item = V>>(&mut self, iter: T) {
        Grid::extend(self, iter)
    }
}

impl From<Row> for Grid {
    fn from(value: Row) -> Self {
        Grid {
            columns: value
                .into_iter()
                .map(|(x, y)| (x, y.into_column()))
                .collect(),
        }
    }
}

impl FromIterator<Box<dyn Column>> for Grid {
    fn from_iter<T: IntoIterator<Item = Box<dyn Column>>>(iter: T) -> Self {
        Grid {
            columns: iter.into_iter().map(|x| (x.column_type(), x)).collect(),
        }
    }
}

impl From<Vec<Box<dyn Column>>> for Grid {
    fn from(value: Vec<Box<dyn Column>>) -> Self {
        value.into_iter().collect()
    }
}

impl FromIterator<Box<dyn Cell>> for Grid {
    fn from_iter<T: IntoIterator<Item = Box<dyn Cell>>>(iter: T) -> Self {
        Grid {
            columns: iter
                .into_iter()
                .map(|x| ((*x).cell_type_id(), x.into_column()))
                .collect(),
        }
    }
}

impl From<Vec<Box<dyn Cell>>> for Grid {
    fn from(value: Vec<Box<dyn Cell>>) -> Self {
        value.into_iter().collect()
    }
}

/// A wrapper over a grid reference
#[derive(Default, Debug, Clone)]
pub struct Ref<'a> {
    columns: AHashMap<TypeId, &'a dyn Column>,
}

impl<'a> Ref<'a> {
    /// number of rows
    pub fn len_rows(&self) -> usize {
        self.columns.values().next().map_or(0, |x| x.len())
    }

    /// returns true if there is at least 1 row
    pub fn has_rows(&self) -> bool {
        self.columns
            .values()
            .next()
            .map_or(false, |x| !x.is_empty())
    }

    /// number of column
    pub fn len_cols(&self) -> usize {
        self.columns.len()
    }

    /// returns true if there is at least 1 column
    pub fn has_cols(&self) -> bool {
        !self.columns.is_empty()
    }

    /// returns true if this if a column of type `T` exists
    pub fn contains<T: Data>(&self) -> bool {
        self.columns.contains_key(&T::ID)
    }

    /// returns true if a column of [TypeId] exiss
    pub fn contains_type(&self, id: &TypeId) -> bool {
        self.columns.contains_key(id)
    }

    /// Get a slice to a column
    pub fn read<T: Data>(&self) -> Option<&'a [T]> {
        self.columns
            .get(&T::ID)
            .map(|x| x.as_dyn().as_slice::<T>().unwrap())
    }
}

/// A wrapper over a mutable grid
#[derive(Default, Debug)]
pub struct Mut<'a> {
    columns: AHashMap<TypeId, &'a mut dyn Column>,
}

impl<'a> Mut<'a> {
    /// returns true if there is at least 1 row
    pub fn len_rows(&self) -> usize {
        self.columns.values().next().map_or(0, |x| x.len())
    }

    /// returns true if there is at least 1 row
    pub fn has_rows(&self) -> bool {
        self.columns
            .values()
            .next()
            .map_or(false, |x| !x.is_empty())
    }

    /// number of column
    pub fn len_cols(&self) -> usize {
        self.columns.len()
    }

    /// returns true if there is at least 1 column
    pub fn has_cols(&self) -> bool {
        !self.columns.is_empty()
    }

    /// returns true if this if a column of type `T` exists
    pub fn contains<T: Data>(&self) -> bool {
        self.columns.contains_key(&T::ID)
    }

    /// returns true if a column of [TypeId] exiss
    pub fn contains_type(&self, id: &TypeId) -> bool {
        self.columns.contains_key(id)
    }

    /// Get a slice to a column
    pub fn read<T: Data>(&mut self, refs: &mut Ref<'a>) -> Option<&'a [T]> {
        if let std::collections::hash_map::Entry::Vacant(e) = refs.columns.entry(T::ID) {
            e.insert(self.columns.remove(&T::ID)?);
        }
        refs.read::<T>()
    }

    /// Get a mutable slice to a column
    pub fn write<T: Data>(&mut self) -> Option<&'a mut [T]> {
        self.columns
            .remove(&T::ID)
            .map(|x| x.as_mut_slice::<T>().unwrap())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_extend() {
        let mut grid = Grid::new();
        grid.extend([
            (10, "a", 11.0),
            (11, "b", 12.0),
            (12, "c", 13.0),
            (13, "d", 14.0),
            (14, "e", 16.0),
        ]);
        assert_eq!(grid.len_cols(), 3);
        assert_eq!(grid.len_rows(), 5);
        grid.extend([(10, "f", 11.0), (11, "g", 12.0), (12, "h", 13.0)]);
        assert_eq!(grid.len_cols(), 3);
        assert_eq!(grid.len_rows(), 8);
    }
}