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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
//! Homogenous storage module
mod grid;
mod index;
mod iter;
mod view;

pub use self::{
    grid::{Grid, Row, RowLike, *},
    index::*,
    iter::*,
    view::*,
};
#[cfg(feature = "rayon")]
use crate::query::ParallelQueryIter;
use crate::{
    data::Data,
    key::Key,
    query::{Filter, Query, QueryIter, QueryMaskIter},
    Match,
};
use std::hash::Hash;

/// A [`Table`] stores hierarchical values of the same data type in a cache-friendly way.
///
/// For example, a [`Table`] containing 4 columns for types `A`, `B`, `C` and `D`:
///
/// |   key   | A | B | C | D |
/// |---------|---|---|---|---|
/// |   foo   | 1 | 2 | 3 | 4 |
/// | foo.bar | 5 | 6 | 7 | 8 |
/// |   ...   | - | - | - | - |
#[derive(Debug)]
pub struct Table<K> {
    index: Index<K>,
    grid: Grid,
}

impl<K> std::default::Default for Table<K> {
    fn default() -> Self {
        Self {
            index: Default::default(),
            grid: Default::default(),
        }
    }
}

impl<K> Table<K> {
    /// create a new empty table
    ///
    /// Table columns become fixed after the first insertion
    pub fn new() -> Self {
        Self::default()
    }

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

    /// create a new table with the given columns and reserver capacity for each columns
    pub fn with_capacity<V: RowLike>(n: usize) -> Self {
        Self {
            index: Index::with_capacity(n),
            grid: Grid::with_capacity::<V>(n),
        }
    }

    /// returns true if this table has a column of type `T`
    pub fn contains<T: Data>(&self) -> bool {
        self.grid.contains::<T>()
    }

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

    /// Number of rows in this table
    pub fn len_rows(&self) -> usize {
        self.grid.len_rows()
    }

    /// number of roots in this table
    pub fn len_roots(&self) -> usize {
        self.index.len_roots()
    }

    /// returns true if this table is indexed
    pub fn is_indexed(&self) -> bool {
        self.index.is_indexed()
    }

    /// return true if this table is empty
    pub fn is_empty(&self) -> bool {
        self.index.keys().is_empty()
    }

    /// returns true if the this table contains these columns
    ///
    /// if `partial` is true, this table should contains at least these columns.
    ///
    /// if `partial` is false this table should contain exactly these columns
    ///
    /// For tuples of [Data], match table panics if the same type is present more than once
    pub fn matches<V: RowLike>(&self, partial: bool) -> bool {
        V::match_grid(&self.grid, partial)
    }

    /// returns true if the this table contains these columns
    ///
    /// if `partial` is true, this table should contains at least these columns.
    ///
    /// if `partial` is false this table should contain exactly these columns
    ///
    /// For tuples of [Data], match table panics if the same type is present more than once
    pub fn match_row(&self, row: &Row, partial: bool) -> bool {
        row.match_grid(&self.grid, partial)
    }

    /// creates an immutable view of this table
    pub fn view_ref(&self) -> View<'_, K> {
        View::new_ref(&self.index, self.grid.grid_ref())
    }

    /// creates an mutable view of this table
    pub fn view_mut(&mut self) -> View<'_, K> {
        View::new_mut(&self.index, self.grid.grid_mut())
    }

    /// returns the root keys and children of this table
    ///
    /// panics in debug if the table is not indexed
    pub fn roots(&self) -> impl QueryIter<Item = (&Key<K>, impl QueryIter<Item = &Key<K>> + '_)> {
        if self.is_indexed() {
            Box::new(RootsIter::new(&self.index))
        } else {
            debug_assert!(false, "impossible to get root keys on an unindexed table");
            Box::new(std::iter::empty()) as Box<dyn QueryIter<Item = (&Key<K>, _)>>
        }
    }

    /// returns the keys in this table
    pub fn keys(&self) -> &KeySet<K> {
        &self.index.keys()
    }

    /// return ordered keys in this table
    pub fn keys_indexed(&self) -> impl QueryIter<Item = &Key<K>> {
        let idx = self.view_ref().index();
        idx.index().iter().map(|x| &idx.keys()[*x])
    }

    /// returns an iterator containing items in this table matching the given query
    pub fn iter<'a, Q>(&'a self) -> Box<dyn QueryIter<Item = (&'a Key<K>, Q::Item<'a>)> + 'a>
    where
        Q: Query + 'static,
    {
        self.iter_filter::<Q, ()>()
    }

    /// returns an iterator containing items in this table matching the given query and filter
    pub fn iter_filter<'a, Q, F>(
        &'a self,
    ) -> Box<dyn QueryIter<Item = (&'a Key<K>, Q::Item<'a>)> + 'a>
    where
        Q: Query + 'static,
        F: Filter,
    {
        assert!(Q::READ_ONLY);
        let view = self.view_ref();
        if F::match_view(&view) {
            Q::iter(view)
        } else {
            Box::new(std::iter::empty())
        }
    }

    /// returns an iterator containing items ordered by key in this table matching the given query
    pub fn iter_indexed<'a, Q>(
        &'a self,
    ) -> Box<dyn QueryIter<Item = (&'a Key<K>, Q::Item<'a>)> + 'a>
    where
        Q: Query + 'static,
    {
        self.iter_indexed_filter::<Q, ()>()
    }

    /// returns an iterator containing items ordered by key in this table matching the given query and filter
    pub fn iter_indexed_filter<'a, Q, F>(
        &'a self,
    ) -> Box<dyn QueryIter<Item = (&'a Key<K>, Q::Item<'a>)> + 'a>
    where
        Q: Query + 'static,
        F: Filter,
    {
        assert!(Q::READ_ONLY);
        debug_assert!(self.is_indexed());
        let view = self.view_ref();
        if F::match_view(&view) {
            Q::iter_indexed(view)
        } else {
            Box::new(std::iter::empty())
        }
    }

    /// returns an iterator ordered by key & filtered out by mask containing items in this table matching the given query  
    pub fn mask<'a, 'b, Q>(
        &'a self,
        mask: &'b impl Match<K>,
    ) -> impl QueryMaskIter<Item = (&'a Key<K>, Q::Item<'a>)>
    where
        'b: 'a,
        Q: Query + 'static,
    {
        self.mask_filter::<Q, ()>(mask)
    }

    /// returns an iterator ordered by key & filtered out by mask containing items in this table matching the given query and filter
    pub fn mask_filter<'a, 'b, Q, F>(
        &'a self,
        mask: &'b impl Match<K>,
    ) -> impl QueryMaskIter<Item = (&'a Key<K>, Q::Item<'a>)>
    where
        'b: 'a,
        Q: Query + 'static,
        F: Filter,
    {
        assert!(Q::READ_ONLY);
        let view = self.view_ref();
        F::match_view(&view)
            .then(|| Q::mask(view, mask))
            .into_iter()
            .flatten()
    }

    /// iterator over [Key] - mutable? [Query::Item] if this table contains the columns specified in the given [Query]
    pub fn iter_mut<'a, Q>(
        &'a mut self,
    ) -> Box<dyn QueryIter<Item = (&'a Key<K>, Q::Item<'a>)> + 'a>
    where
        Q: Query + 'static,
    {
        self.iter_mut_filter::<Q, ()>()
    }

    /// iterator over [Key] - mutable? [Query::Item] if this table contains the columns specified in the given [Query] and [Filter]
    pub fn iter_mut_filter<'a, Q, F>(
        &'a mut self,
    ) -> Box<dyn QueryIter<Item = (&'a Key<K>, Q::Item<'a>)> + 'a>
    where
        Q: Query + 'static,
        F: Filter,
    {
        assert!(!Q::READ_ONLY);
        let view = self.view_mut();
        if F::match_view(&view) {
            Q::iter(view)
        } else {
            Box::new(std::iter::empty())
        }
    }

    /// iterator over [Key] - mutable? [Query::Item] ordered by [Key] if this table contains the columns specified in the given [Query]
    pub fn iter_mut_indexed<Q>(&mut self) -> Box<dyn QueryIter<Item = (&Key<K>, Q::Item<'_>)> + '_>
    where
        Q: Query + 'static,
    {
        self.iter_mut_indexed_filter::<Q, ()>()
    }

    /// iterator over [Key] - mutable? [Query::Item] ordered by [Key] if this table contains the columns specified in the given [Query] and [Filter]
    pub fn iter_mut_indexed_filter<Q, F>(
        &mut self,
    ) -> Box<dyn QueryIter<Item = (&Key<K>, Q::Item<'_>)> + '_>
    where
        Q: Query + 'static,
        F: Filter,
    {
        assert!(!Q::READ_ONLY);
        debug_assert!(self.is_indexed());
        let view = self.view_mut();
        if F::match_view(&view) {
            Q::iter_indexed(view)
        } else {
            Box::new(std::iter::empty())
        }
    }

    /// iterator over [Key] - mutable? [Query::Item] ordered by [Key] matching the given mask (see: [Match] [Mask](crate::Mask)) if this table contains the columns specified in the given [Query] and [Filter]
    pub fn mask_mut<'a, 'b, Q>(
        &'a mut self,
        mask: &'b impl Match<K>,
    ) -> impl QueryMaskIter<Item = (&Key<K>, Q::Item<'a>)>
    where
        'b: 'a,
        Q: Query + 'static,
    {
        self.mask_mut_filter::<Q, ()>(mask)
    }

    /// iterator over [Key] - mutable? [Query::Item] ordered by [Key] matching the given mask (see: [Match] [Mask][crate::Mask] if this table contains the columns specified in the given [Query]
    pub fn mask_mut_filter<'a, 'b, Q, F>(
        &'a mut self,
        mask: &'b impl Match<K>,
    ) -> impl QueryMaskIter<Item = (&Key<K>, Q::Item<'a>)>
    where
        'b: 'a,
        Q: Query + 'static,
        F: Filter,
    {
        assert!(!Q::READ_ONLY);
        let view = self.view_mut();
        F::match_view(&view)
            .then(|| Q::mask(view, mask))
            .into_iter()
            .flatten()
    }
}

#[cfg(feature = "rayon")]
impl<K> Table<K> {
    /// if this table contains columns specified by the [Query], returns a parallel iterator of key - [Query::Item]
    pub fn par_iter<'a, Q>(
        &'a self,
    ) -> Option<impl ParallelQueryIter<Item = (&'a Key<K>, Q::Item<'a>)>>
    where
        K: Send + Sync,
        Q: Query + 'static,
        Q::Item<'a>: Send + Sync,
    {
        self.par_iter_filter::<Q, ()>()
    }

    /// if this table contains columns specified by the [Query] & [Filter], returns a parallel iterator of key - [Query::Item]
    pub fn par_iter_filter<'a, Q, F>(
        &'a self,
    ) -> Option<impl ParallelQueryIter<Item = (&'a Key<K>, Q::Item<'a>)>>
    where
        K: Send + Sync,
        Q: Query + 'static,
        Q::Item<'a>: Send + Sync,
        F: Filter,
    {
        assert!(Q::READ_ONLY);
        debug_assert!(self.is_indexed());
        let view = self.view_ref();
        if F::match_view(&view) {
            Q::par_iter(view)
        } else {
            None
        }
    }

    /// parallel iterator over [Key] - mutable? [Query::Item] if this table contains the columns specified in the given [Query]
    pub fn par_iter_mut<'a, Q>(
        &'a mut self,
    ) -> Option<impl ParallelQueryIter<Item = (&'a Key<K>, Q::Item<'a>)>>
    where
        K: Send + Sync,
        Q: Query + 'static,
        Q::Item<'a>: Send + Sync,
    {
        self.par_iter_mut_filter::<Q, ()>()
    }

    /// parallel iterator over [Key] - mutable? [Query::Item] if this table contains the columns specified in the given [Query] and [Filter]
    pub fn par_iter_mut_filter<'a, Q, F>(
        &'a mut self,
    ) -> Option<impl ParallelQueryIter<Item = (&'a Key<K>, Q::Item<'a>)>>
    where
        K: Send + Sync,
        Q: Query + 'static,
        Q::Item<'a>: Send + Sync,
        F: Filter,
    {
        assert!(!Q::READ_ONLY);
        debug_assert!(self.is_indexed());
        let view = self.view_mut();
        if F::match_view(&view) {
            Q::par_iter(view)
        } else {
            None
        }
    }
}

impl<K: Eq + Hash> Table<K> {
    /// returns true if this table contains the given key
    pub fn contains_key(&self, key: &Key<K>) -> bool {
        self.index.keys().contains(key)
    }

    /// get a single reference that matches the given [Query] and key
    pub fn get<Q: Query>(&self, key: &Key<K>) -> Option<Q::Item<'_>> {
        self.get_filter::<Q, ()>(key)
    }

    /// get a single reference that matches the given [Query], [Filter] and [Key]
    pub fn get_filter<Q: Query, F: Filter>(&self, key: &Key<K>) -> Option<Q::Item<'_>> {
        assert!(Q::READ_ONLY);
        let mut view = self.view_ref();
        if F::match_view(&view) {
            Q::get(&mut view, key)
        } else {
            None
        }
    }

    /// get a single mutable reference that matches the given [Query] and [Key]
    pub fn get_mut<Q: Query>(&mut self, key: &Key<K>) -> Option<Q::Item<'_>> {
        self.get_mut_filter::<Q, ()>(key)
    }

    /// get a single mutable reference that matches the given [Query], [Filter] and [Key]
    pub fn get_mut_filter<Q: Query, F: Filter>(&mut self, key: &Key<K>) -> Option<Q::Item<'_>> {
        assert!(!Q::READ_ONLY);
        let mut view = self.view_mut();
        if F::match_view(&view) {
            Q::get(&mut view, key)
        } else {
            None
        }
    }

    /// update the given key with a value
    ///
    /// This update can be partial
    ///
    /// returns the old values or the provided value if the key does not exists or cannot be inserted in this table
    pub fn update<V: RowLike>(&mut self, key: &Key<K>, value: V) -> Result<Row, V> {
        if let Some(index) = self.index.keys().get_index_of(key) {
            self.grid.update(index, value)
        } else {
            Err(value)
        }
    }

    /// update the given key with a row
    ///
    /// This update can be partial
    ///
    /// returns the old values or the provided value if the key does not exists or cannot be inserted in this table
    pub fn update_row(&mut self, key: &Key<K>, row: Row) -> Result<Row, Row> {
        if let Some(index) = self.index.keys().get_index_of(key) {
            self.grid.update_row(index, row)
        } else {
            Err(row)
        }
    }
}

impl<K: Hash + Ord> Table<K> {
    /// inserts the given key value if the key is not already present
    ///
    /// Returns the provided key value if the entry exists or cannot be inserted
    pub fn insert<V: RowLike>(&mut self, key: Key<K>, value: V) -> Result<(), (Key<K>, V)> {
        if !self.grid.has_cols() {
            self.index.insert(key);
            self.grid.insert(value).map_err(|_| unreachable!()).unwrap();
            Ok(())
        } else if !self.index.contains(&key) && V::match_grid(&self.grid, false) {
            self.grid.insert(value).map_err(|_| unreachable!())?;
            self.index.insert(key);
            Ok(())
        } else {
            Err((key, value))
        }
    }

    /// inserts the given key value if the key is not already present
    ///
    /// Returns the provided key value if the entry exists or cannot be inserted
    pub fn insert_row(&mut self, key: Key<K>, row: Row) -> Result<(), (Key<K>, Row)> {
        if !self.grid.has_cols() {
            self.index.insert(key);
            self.grid
                .insert_row(row)
                .map_err(|_| unreachable!())
                .unwrap();
            Ok(())
        } else if !self.index.keys().contains(&key) && row.match_grid(&self.grid, false) {
            self.grid.insert_row(row).map_err(|_| unreachable!())?;
            self.index.insert(key);
            Ok(())
        } else {
            Err((key, row))
        }
    }

    /// update or insert the given key/value
    ///
    /// returns the old values
    ///
    /// if the key is already present then a partial update can be done
    pub fn upsert<V: RowLike>(&mut self, key: Key<K>, value: V) -> Result<Row, (Key<K>, V)>
    where
        K: Clone,
    {
        match self.index.insert(key) {
            (index, true) => {
                if let Err(x) = self.grid.insert(value) {
                    Err((self.index.swap_remove_index(index).unwrap(), x))
                } else {
                    Ok(Row::default())
                }
            }
            (index, false) => self
                .grid
                .update(index, value)
                .map_err(|x| (self.index.keys().get_index(index).cloned().unwrap(), x)),
        }
    }

    /// update or insert the given key/value
    ///
    /// returns the old values
    ///
    /// if the key is already present then a partial update can be done
    pub fn upsert_row(&mut self, key: Key<K>, row: Row) -> Result<Row, (Key<K>, Row)>
    where
        K: Clone,
    {
        match self.index.insert(key) {
            (index, true) => {
                if let Err(x) = self.grid.insert_row(row) {
                    Err((self.index.swap_remove_index(index).unwrap(), x))
                } else {
                    Ok(Row::default())
                }
            }
            (index, false) => self
                .grid
                .update_row(index, row)
                .map_err(|x| (self.index.keys().get_index(index).cloned().unwrap(), x)),
        }
    }

    /// remove the given key and it's columns from this table
    ///
    /// return the old row if any
    pub fn remove(&mut self, key: &Key<K>) -> Option<(Key<K>, Row)> {
        self.index
            .swap_remove(key)
            .map(|(x, y)| (x, self.grid.swap_remove(y)))
    }

    /// Extend this table with the given entries
    pub fn extend<V: RowLike, I: IntoIterator<Item = (Key<K>, V)>>(
        &mut self,
        values: I,
    ) -> Result<(), I> {
        if V::match_grid(&self.grid, false) {
            for (k, v) in values {
                match self.index.insert(k) {
                    (_, true) => {
                        self.grid.insert(v).map_err(|_| unreachable!()).unwrap();
                    }
                    (index, false) => {
                        self.grid
                            .update(index, v)
                            .map_err(|_| unreachable!())
                            .unwrap();
                    }
                }
            }
            Ok(())
        } else {
            Err(values)
        }
    }
}

impl<K: Ord> Table<K> {
    /// build an index for this table
    pub fn build_index(&mut self) {
        self.index.build()
    }
}

impl<K: Ord + Send + Sync> Table<K> {
    #[cfg(feature = "rayon")]
    /// build an index for this table in parallel
    pub fn par_build_index(&mut self) {
        self.index.par_build()
    }
}

impl<K: Hash + Ord, T: RowLike> From<(Key<K>, T)> for Table<K> {
    fn from((k, v): (Key<K>, T)) -> Self {
        let mut new = Self {
            index: k.into(),
            grid: v.into_columns().into(),
        };
        new.build_index();
        new
    }
}

impl<K: Hash + Ord> From<(Key<K>, Row)> for Table<K> {
    fn from((k, v): (Key<K>, Row)) -> Self {
        Self {
            index: k.into(),
            grid: v.into(),
        }
    }
}

impl<K: Hash + Ord, V: RowLike> FromIterator<(Key<K>, V)> for Table<K> {
    fn from_iter<T: IntoIterator<Item = (Key<K>, V)>>(iter: T) -> Self {
        let (index, grid): (Index<K>, Grid) = iter.into_iter().unzip();
        assert_eq!(index.len(), grid.len_rows());
        let mut tbl = Self { index, grid };
        tbl.build_index();
        tbl
    }
}

impl<K: Hash + Ord, V: RowLike> From<Vec<(Key<K>, V)>> for Table<K> {
    fn from(value: Vec<(Key<K>, V)>) -> Self {
        value.into_iter().collect()
    }
}

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

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_deeply_nested_alloc() {
        let mut k: Key<&'static str> = Key::from(["a"]);
        const NB: usize = 2000;
        let mut t = Table::with_capacity::<(usize,)>(NB);
        for x in 0..NB {
            k = k + "abc";
            t.insert(k.clone(), (x,)).unwrap();
        }
        assert_eq!(t.len_rows(), NB);
        t.build_index();
        assert_eq!(t.len_roots(), 1);
        assert_eq!(t.get::<&usize>(&Key::from(["a", "abc"])), Some(&0));
        *t.get_mut::<&mut usize>(&Key::from(["a", "abc"])).unwrap() += 12;
        assert_eq!(t.get::<&usize>(&Key::from(["a", "abc"])), Some(&12));
        assert_eq!(
            t.get_mut::<(&mut usize, &usize)>(&Key::from(["a", "abc"])),
            None
        );
        assert_eq!(t.get::<&usize>(&k), Some(&(NB - 1)));
        t.insert(Key::from(["c"]), (1usize,)).unwrap();
        t.insert(Key::from(["b"]), (1usize,)).unwrap();
        t.build_index();
        assert_eq!(t.len_rows(), NB + 2);
        assert_eq!(t.len_roots(), 3);
        assert_eq!(t.len_roots(), 3);
    }

    #[test]
    fn test_roots() {
        let ks = [
            Key::new("a"),
            Key::from(["a", "b"]),
            Key::from(["a", "c"]),
            Key::new("z"),
            Key::from(["z", "b"]),
            Key::from(["z", "c"]),
        ];
        let mut t = Table::with_capacity::<(String, usize, bool)>(6);
        ks.iter().enumerate().for_each(|(i, x)| {
            t.insert(x.clone(), (format!("{x:?}"), i, i % 2 == 0))
                .unwrap();
        });
        assert_eq!(
            t.roots().map(|(x, y)| (x, y.collect())).collect::<Vec<_>>(),
            vec![
                (&ks[0], vec![&ks[1], &ks[2]]),
                (&ks[3], vec![&ks[4], &ks[5]])
            ]
        );
        assert_eq!(
            t.iter_indexed::<(&String, &usize, &bool)>()
                .map(|(x, (a, b, c))| (x, (a.clone(), *b, *c)))
                .collect::<Vec<_>>(),
            ks.iter()
                .enumerate()
                .map(|(i, x)| (x, (format!("{x:?}"), i, i % 2 == 0)))
                .collect::<Vec<_>>()
        )
    }
}