typarena 0.1.0

Type-keyed arena storage with stable per-type columns.
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
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::any::TypeId;
use core::hash::{BuildHasher, Hash};

use hashbrown::hash_map::Entry;
use hashbrown::{DefaultHashBuilder, HashMap};
use indexmap::IndexMap;

use crate::any_index_map::DynIndexMap;
use crate::{ColumnId, MaybeSend, MaybeSync};

/// Heterogeneous, key-addressed table.
///
/// Stores values of many types under a shared key `K`. Each value
/// type occupies its own column; a lookup hashes the value's
/// [`TypeId`] to a column index, then resolves `K` within that
/// column.
///
/// ## Mental model
///
/// |  key  | `Position` col | `Name` col |
/// |-------|----------------|------------|
/// | `e0`  | (1.0, 2.0)     | "player"   |
/// | `e1`  | (3.0, 4.0)     | -          |
pub struct TypeTable<K, S = DefaultHashBuilder> {
    /// Maps each value type to its column's id in `columns`.
    indices: HashMap<TypeId, ColumnId>,
    columns: Vec<DynIndexMap<K, S>>,
    hasher: S,
}

impl<K> TypeTable<K> {
    /// Creates an empty table using the default hasher.
    pub fn new() -> Self {
        Self::with_hasher(DefaultHashBuilder::default())
    }
}

impl<K, S> TypeTable<K, S> {
    /// Creates an empty table seeded with `hasher`.
    ///
    /// Every column clones `hasher` for its own [`IndexMap`].
    pub fn with_hasher(hasher: S) -> Self {
        Self {
            indices: HashMap::new(),
            columns: Vec::new(),
            hasher,
        }
    }
}

impl<K, S> TypeTable<K, S>
where
    K: Eq + Hash + 'static,
    S: BuildHasher,
{
    /// Returns a reference to the value of type `V` at `key`, or
    /// `None` if it is absent.
    pub fn get<V: 'static>(&self, key: &K) -> Option<&V> {
        self.column::<V>()?.get(key)
    }

    /// Returns a mutable reference to the value of type `V` at `key`,
    /// or `None` if it is absent.
    pub fn get_mut<V: 'static>(&mut self, key: &K) -> Option<&mut V> {
        self.column_mut::<V>()?.get_mut(key)
    }

    /// Returns the [`ColumnId`] for `V`, or `None` if no value of
    /// type `V` has been inserted yet.
    ///
    /// Stable for the table's lifetime, so a hot path that always
    /// touches the same `V` can resolve the id once and reuse it with
    /// [`Self::get_by_column`], skipping the per-call [`TypeId`] hash
    /// lookup.
    pub fn type_column<V: 'static>(&self) -> Option<ColumnId> {
        self.indices.get(&TypeId::of::<V>()).copied()
    }

    /// Like [`Self::get`], but reaches the column by a pre-resolved
    /// [`ColumnId`] instead of hashing `V`'s [`TypeId`].
    ///
    /// Returns `None` if `col` is out of bounds, its column holds a
    /// different type, or `key` is absent.
    pub fn get_by_column<V: 'static>(
        &self,
        col: ColumnId,
        key: &K,
    ) -> Option<&V> {
        self.columns.get(col.index())?.downcast_ref::<V>()?.get(key)
    }

    /// Like [`Self::get_mut`], but reaches the column by a
    /// pre-resolved [`ColumnId`] instead of hashing `V`'s [`TypeId`].
    ///
    /// Returns `None` if `col` is out of bounds, its column holds a
    /// different type, or `key` is absent.
    pub fn get_mut_by_column<V: 'static>(
        &mut self,
        col: ColumnId,
        key: &K,
    ) -> Option<&mut V> {
        self.columns
            .get_mut(col.index())?
            .downcast_mut::<V>()?
            .get_mut(key)
    }

    /// Returns `true` if a value of type `V` is present at `key`.
    pub fn contains<V: 'static>(&self, key: &K) -> bool {
        self.column::<V>().is_some_and(|c| c.contains_key(key))
    }

    /// Removes and returns the value of type `V` at `key`.
    ///
    /// Removal swaps the entry with the last in its column, so it is
    /// *O(1)* but does not preserve column order.
    pub fn remove<V: 'static>(&mut self, key: &K) -> Option<V> {
        self.column_mut::<V>()?.swap_remove(key)
    }

    /// Like [`Self::remove`], but reaches the column by a
    /// pre-resolved [`ColumnId`] instead of hashing `V`'s
    /// [`TypeId`].
    ///
    /// Returns `None` if `col` is out of bounds, its column holds a
    /// different type, or `key` is absent.
    pub fn remove_by_column<V: 'static>(
        &mut self,
        key: &K,
        col: ColumnId,
    ) -> Option<V> {
        self.columns
            .get_mut(col.index())?
            .downcast_mut::<V>()?
            .swap_remove(key)
    }

    /// Removes `key` from every column, dropping the whole row.
    ///
    /// Each column is swap-removed independently without knowing its
    /// value type. Returns `true` if any column held `key`.
    pub fn remove_row(&mut self, key: &K) -> bool {
        let mut removed = false;
        for column in self.columns.iter_mut() {
            removed |= column.dyn_swap_remove(key);
        }
        removed
    }

    /// Returns `true` if any column holds a value at `key`.
    pub fn contains_row(&self, key: &K) -> bool {
        self.columns.iter().any(|c| c.dyn_contains(key))
    }

    /// Returns the number of values stored in the column for `V`.
    pub fn len<V: 'static>(&self) -> usize {
        self.column::<V>().map_or(0, IndexMap::len)
    }

    /// Returns `true` if the column for `V` holds no values.
    pub fn is_empty<V: 'static>(&self) -> bool {
        self.len::<V>() == 0
    }

    /// Iterates `(key, value)` pairs in the column for `V`.
    ///
    /// Yields nothing if no column for `V` has been created yet.
    pub fn iter<V: 'static>(&self) -> impl Iterator<Item = (&K, &V)> {
        self.column::<V>().into_iter().flat_map(IndexMap::iter)
    }

    /// Removes every value in the column for `V`, leaving it empty.
    ///
    /// Returns `true` if a column for `V` exists, or `false` if one
    /// has not been created yet.
    pub fn clear<V: 'static>(&mut self) -> bool {
        if let Some(column) = self.column_mut::<V>() {
            column.clear();
            return true;
        }
        false
    }

    /// Returns the [`IndexMap`] column for `V`, if one exists.
    fn column<V: 'static>(&self) -> Option<&IndexMap<K, V, S>> {
        let col = *self.indices.get(&TypeId::of::<V>())?;
        self.columns[col.index()].downcast_ref::<V>()
    }

    /// Returns a mutable [`IndexMap`] column for `V`, if one exists.
    fn column_mut<V: 'static>(
        &mut self,
    ) -> Option<&mut IndexMap<K, V, S>> {
        let col = *self.indices.get(&TypeId::of::<V>())?;
        self.columns[col.index()].downcast_mut::<V>()
    }
}

impl<K, S> TypeTable<K, S>
where
    K: Eq + Hash + 'static,
    S: BuildHasher + Clone + 'static,
{
    /// Inserts `value` under `key` in the column for `V`.
    ///
    /// Returns the previous value at `key`, if one was present.
    pub fn insert<V: 'static>(
        &mut self,
        key: K,
        value: V,
    ) -> Option<V>
    where
        IndexMap<K, V, S>: MaybeSend + MaybeSync,
    {
        let col = self.ensure_column::<V>();
        // SAFETY: `ensure_column` just assigned this column to V.
        let column = unsafe {
            self.columns[col.index()].downcast_unchecked_mut::<V>()
        };
        column.insert(key, value)
    }

    /// Like [`Self::insert`], but reaches the column by a
    /// pre-resolved [`ColumnId`] instead of hashing `V`'s
    /// [`TypeId`].
    ///
    /// No-op if `col` is out of bounds or its column holds a
    /// different type.
    pub fn insert_by_column<V: 'static>(
        &mut self,
        key: K,
        value: V,
        col: ColumnId,
    ) -> Option<V> {
        self.columns
            .get_mut(col.index())?
            .downcast_mut::<V>()?
            .insert(key, value)
    }

    /// Ensures the column for `V` exists and returns its
    /// [`ColumnId`].
    ///
    /// Like [`Self::type_column`] but creates the column on first
    /// call rather than returning `None`. The id is stable for the
    /// table's lifetime.
    pub fn ensure_column<V: 'static>(&mut self) -> ColumnId
    where
        IndexMap<K, V, S>: MaybeSend + MaybeSync,
    {
        match self.indices.entry(TypeId::of::<V>()) {
            Entry::Occupied(e) => *e.get(),
            Entry::Vacant(e) => {
                let col = ColumnId::new(self.columns.len());
                self.columns.push(Box::new(
                    IndexMap::<K, V, S>::with_hasher(
                        self.hasher.clone(),
                    ),
                ));
                *e.insert(col)
            }
        }
    }
}

impl<K> Default for TypeTable<K> {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use alloc::string::String;

    use super::{ColumnId, TypeTable};

    #[derive(Debug, PartialEq, Clone, Copy)]
    struct Position(f32, f32);

    #[derive(Debug, PartialEq, Clone)]
    struct Name(String);

    #[test]
    fn insert_and_get() {
        let mut table = TypeTable::<u32>::new();
        table.insert(0, Position(1.0, 2.0));
        assert_eq!(
            table.get::<Position>(&0),
            Some(&Position(1.0, 2.0))
        );
    }

    #[test]
    fn heterogeneous_by_shared_key() {
        let mut table = TypeTable::<u32>::new();
        table.insert(7, Position(3.0, 4.0));
        table.insert(7, Name(String::from("player")));
        assert_eq!(
            table.get::<Position>(&7),
            Some(&Position(3.0, 4.0))
        );
        assert_eq!(
            table.get::<Name>(&7),
            Some(&Name(String::from("player")))
        );
    }

    #[test]
    fn insert_returns_previous() {
        let mut table = TypeTable::<u32>::new();
        assert_eq!(table.insert(1, Position(0.0, 0.0)), None);
        assert_eq!(
            table.insert(1, Position(9.0, 9.0)),
            Some(Position(0.0, 0.0))
        );
    }

    #[test]
    fn get_mut_modifies_value() {
        let mut table = TypeTable::<u32>::new();
        table.insert(1, Position(0.0, 0.0));
        table.get_mut::<Position>(&1).unwrap().0 = 5.0;
        assert_eq!(
            table.get::<Position>(&1),
            Some(&Position(5.0, 0.0))
        );
    }

    #[test]
    fn remove_returns_value() {
        let mut table = TypeTable::<u32>::new();
        table.insert(1, Position(5.0, 5.0));
        assert_eq!(
            table.remove::<Position>(&1),
            Some(Position(5.0, 5.0))
        );
        assert!(table.get::<Position>(&1).is_none());
        assert!(table.remove::<Position>(&1).is_none());
    }

    #[test]
    fn remove_swaps_last_into_place() {
        let mut table = TypeTable::<u32>::new();
        table.insert(0, Position(0.0, 0.0));
        table.insert(1, Position(1.0, 1.0));
        table.insert(2, Position(2.0, 2.0));

        // Swap-remove the first entry; the others remain reachable.
        assert_eq!(
            table.remove::<Position>(&0),
            Some(Position(0.0, 0.0))
        );
        assert_eq!(
            table.get::<Position>(&2),
            Some(&Position(2.0, 2.0))
        );
        assert_eq!(
            table.get::<Position>(&1),
            Some(&Position(1.0, 1.0))
        );
        assert_eq!(table.len::<Position>(), 2);
    }

    #[test]
    fn remove_row_drops_every_column() {
        let mut table = TypeTable::<u32>::new();
        table.insert(1, Position(1.0, 1.0));
        table.insert(1, Name(String::from("player")));

        assert!(table.remove_row(&1));
        assert!(table.get::<Position>(&1).is_none());
        assert!(table.get::<Name>(&1).is_none());
        // A second removal finds nothing left.
        assert!(!table.remove_row(&1));
    }

    #[test]
    fn contains_row_reports_any_column() {
        let mut table = TypeTable::<u32>::new();
        assert!(!table.contains_row(&1));
        table.insert(1, Name(String::from("player")));
        assert!(table.contains_row(&1));
        assert!(!table.contains_row(&2));
    }

    #[test]
    fn type_isolation() {
        let mut table = TypeTable::<u32>::new();
        table.insert(0, 50u64);
        assert!(table.get::<u32>(&0).is_none());
        assert_eq!(table.get::<u64>(&0), Some(&50u64));
    }

    #[test]
    fn contains_reflects_presence() {
        let mut table = TypeTable::<u32>::new();
        assert!(!table.contains::<Position>(&0));
        table.insert(0, Position(0.0, 0.0));
        assert!(table.contains::<Position>(&0));
    }

    #[test]
    fn clear_empties_column() {
        let mut table = TypeTable::<u32>::new();
        table.insert(0, Position(0.0, 0.0));
        table.insert(1, Position(1.0, 1.0));

        assert!(table.clear::<Position>());
        assert_eq!(table.len::<Position>(), 0);
        assert!(table.get::<Position>(&0).is_none());
    }

    #[test]
    fn clear_absent_column_returns_false() {
        let mut table = TypeTable::<u32>::new();
        assert!(!table.clear::<Position>());
    }

    #[test]
    fn iter_yields_all_pairs() {
        let mut table = TypeTable::<u32>::new();
        table.insert(0, Position(0.0, 0.0));
        table.insert(1, Position(1.0, 1.0));

        let mut keys = table
            .iter::<Position>()
            .map(|(k, _)| *k)
            .collect::<alloc::vec::Vec<_>>();
        keys.sort();
        assert_eq!(keys, [0, 1]);
    }

    #[test]
    fn type_column_none_before_insert() {
        let table = TypeTable::<u32>::new();
        assert_eq!(table.type_column::<Position>(), None);
    }

    #[test]
    fn type_column_stable_across_inserts() {
        let mut table = TypeTable::<u32>::new();
        table.insert(0, Position(0.0, 0.0));
        let col = table.type_column::<Position>().unwrap();

        // Inserting more values, even of other types, must not move
        // the existing column.
        table.insert(1, Position(1.0, 1.0));
        table.insert(0, Name(String::from("player")));
        assert_eq!(table.type_column::<Position>(), Some(col));
    }

    #[test]
    fn ensure_column_is_idempotent() {
        let mut table = TypeTable::<u32>::new();
        let first = table.ensure_column::<Position>();
        let second = table.ensure_column::<Position>();
        assert_eq!(first, second);
        // And matches the lazily resolved id.
        assert_eq!(table.type_column::<Position>(), Some(first));
    }

    #[test]
    fn ensure_column_distinct_per_type() {
        let mut table = TypeTable::<u32>::new();
        let pos = table.ensure_column::<Position>();
        let name = table.ensure_column::<Name>();
        assert_ne!(pos, name);
    }

    #[test]
    fn get_by_column_matches_get() {
        let mut table = TypeTable::<u32>::new();
        table.insert(0, Position(1.0, 2.0));
        let col = table.type_column::<Position>().unwrap();
        assert_eq!(
            table.get_by_column::<Position>(col, &0),
            table.get::<Position>(&0),
        );
    }

    #[test]
    fn get_by_column_wrong_type_returns_none() {
        let mut table = TypeTable::<u32>::new();
        table.insert(0, Position(1.0, 2.0));
        let col = table.type_column::<Position>().unwrap();
        // The column at `col` holds `Position`, not `Name`.
        assert_eq!(table.get_by_column::<Name>(col, &0), None);
    }

    #[test]
    fn get_by_column_out_of_bounds_returns_none() {
        let mut table = TypeTable::<u32>::new();
        table.insert(0, Position(1.0, 2.0));
        assert_eq!(
            table.get_by_column::<Position>(
                ColumnId::PLACEHOLDER,
                &0,
            ),
            None,
        );
    }

    #[test]
    fn get_by_column_absent_key_returns_none() {
        let mut table = TypeTable::<u32>::new();
        table.insert(0, Position(1.0, 2.0));
        let col = table.type_column::<Position>().unwrap();
        assert_eq!(table.get_by_column::<Position>(col, &9), None);
    }

    #[test]
    fn get_mut_by_column_modifies_value() {
        let mut table = TypeTable::<u32>::new();
        table.insert(1, Position(0.0, 0.0));
        let col = table.type_column::<Position>().unwrap();
        table.get_mut_by_column::<Position>(col, &1).unwrap().0 = 5.0;
        assert_eq!(
            table.get::<Position>(&1),
            Some(&Position(5.0, 0.0))
        );
    }

    #[test]
    fn get_mut_by_column_wrong_type_returns_none() {
        let mut table = TypeTable::<u32>::new();
        table.insert(0, Position(1.0, 2.0));
        let col = table.type_column::<Position>().unwrap();
        assert_eq!(table.get_mut_by_column::<Name>(col, &0), None);
    }

    #[test]
    fn get_mut_by_column_out_of_bounds_returns_none() {
        let mut table = TypeTable::<u32>::new();
        table.insert(0, Position(1.0, 2.0));
        assert_eq!(
            table.get_mut_by_column::<Position>(
                ColumnId::PLACEHOLDER,
                &0,
            ),
            None,
        );
    }

    #[test]
    fn get_mut_by_column_absent_key_returns_none() {
        let mut table = TypeTable::<u32>::new();
        table.insert(0, Position(1.0, 2.0));
        let col = table.type_column::<Position>().unwrap();
        assert_eq!(
            table.get_mut_by_column::<Position>(col, &9),
            None
        );
    }
}