|
| 1 | +//! Id indexed sequence of hash sets. |
| 2 | +use std::{hash::BuildHasherDefault, marker::PhantomData}; |
| 3 | + |
| 4 | +use table_seq::set_seq::{SetSeq, SetSeqSet, SetSeqSetMut}; |
| 5 | +use zwohash::ZwoHasher; |
| 6 | + |
| 7 | +use crate::Id; |
| 8 | + |
| 9 | +/// Indexed sequence of hash sets. |
| 10 | +/// |
| 11 | +/// This type serves as a memory and runtime efficient replacement for `IdVec<I, HashSet<T>>`. |
| 12 | +pub struct IdSetSeq<I: Id, T, S = BuildHasherDefault<ZwoHasher>> { |
| 13 | + set_seq: SetSeq<T, S>, |
| 14 | + _phantom: PhantomData<I>, |
| 15 | +} |
| 16 | + |
| 17 | +impl<I: Id, T, S> std::ops::Deref for IdSetSeq<I, T, S> { |
| 18 | + type Target = SetSeq<T, S>; |
| 19 | + |
| 20 | + #[inline(always)] |
| 21 | + fn deref(&self) -> &Self::Target { |
| 22 | + &self.set_seq |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl<I: Id, T, S> IdSetSeq<I, T, S> { |
| 27 | + /// Discards all sets in the sequence. |
| 28 | + #[inline(always)] |
| 29 | + pub fn clear(&mut self) { |
| 30 | + self.set_seq.clear(); |
| 31 | + } |
| 32 | + |
| 33 | + /// Resizes the sequence by appending empty sets or discarding trailing sets. |
| 34 | + #[inline(always)] |
| 35 | + pub fn resize(&mut self, len: usize) { |
| 36 | + self.set_seq.resize(len); |
| 37 | + } |
| 38 | + |
| 39 | + /// Ensures that the sequence contains a set for the given id by appending emtpy sets if the |
| 40 | + /// sequence was too short. |
| 41 | + /// |
| 42 | + /// Provides mutable access to the set for the given id. |
| 43 | + #[inline(always)] |
| 44 | + pub fn grow_for(&mut self, id: I) -> SetSeqSetMut<T, S> { |
| 45 | + self.set_seq.grow_for(id.id_index()) |
| 46 | + } |
| 47 | + |
| 48 | + /// Provides shared access to the set for a given id, panics if the id is out-of-bounds. |
| 49 | + /// |
| 50 | + /// This is used instead of [`std::ops::Index`], as it returns a value of the custom |
| 51 | + /// reference-like [`SetSeqSet`] type. |
| 52 | + /// |
| 53 | + /// Panics if `id.id_index() >= self.len()`. |
| 54 | + #[inline(always)] |
| 55 | + pub fn at(&self, id: I) -> SetSeqSet<T, S> { |
| 56 | + self.set_seq.at(id.id_index()) |
| 57 | + } |
| 58 | + |
| 59 | + /// Provides mutable access to the set for a given id, panics if the id is out-of-bounds. |
| 60 | + /// |
| 61 | + /// This is used instead of [`std::ops::IndexMut`], as it returns a value of the custom |
| 62 | + /// reference-like [`SetSeqSetMut`] type. |
| 63 | + /// |
| 64 | + /// Panics if `id.id_index() >= self.len()`. |
| 65 | + #[inline(always)] |
| 66 | + pub fn at_mut(&mut self, id: I) -> SetSeqSetMut<T, S> { |
| 67 | + self.set_seq.at_mut(id.id_index()) |
| 68 | + } |
| 69 | + |
| 70 | + /// Provides shared access to the set for a given id. |
| 71 | + /// |
| 72 | + /// This returns `None` if `id.id_index() >= self.len()`. |
| 73 | + #[inline(always)] |
| 74 | + pub fn get(&self, id: I) -> Option<SetSeqSet<T, S>> { |
| 75 | + self.set_seq.get(id.id_index()) |
| 76 | + } |
| 77 | + |
| 78 | + /// Provides mutable access to the set for a given id. |
| 79 | + /// |
| 80 | + /// This returns `None` if `id.id_index() >= self.len()`. |
| 81 | + #[inline(always)] |
| 82 | + pub fn get_mut(&mut self, id: I) -> Option<SetSeqSetMut<T, S>> { |
| 83 | + self.set_seq.get_mut(id.id_index()) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl<I: Id, T, S: Default> Default for IdSetSeq<I, T, S> { |
| 88 | + fn default() -> Self { |
| 89 | + Self { |
| 90 | + set_seq: Default::default(), |
| 91 | + _phantom: PhantomData, |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments