| 1 |
use alloc::vec::Vec; |
| 2 |
|
| 3 |
/// An item in a [`Table`]. |
| 4 |
pub trait Item { |
| 5 |
/// The type of identifier for the item. |
| 6 |
type Id: Id; |
| 7 |
|
| 8 |
/// Return `True` if the item is deleted. |
| 9 |
fn is_deleted(&self) -> bool; |
| 10 |
} |
| 11 |
|
| 12 |
/// An identifier for referring to an item in a [`Table`]. |
| 13 |
pub trait Id: IdPrivate { |
| 14 |
/// Return the index of the item in the table. |
| 15 |
fn index(&self) -> usize; |
| 16 |
} |
| 17 |
|
| 18 |
mod id_private { |
| 19 |
pub trait IdPrivate { |
| 20 |
fn new(id: usize) -> Self; |
| 21 |
} |
| 22 |
} |
| 23 |
pub(super) use id_private::IdPrivate; |
| 24 |
|
| 25 |
/// A table of items. |
| 26 |
/// |
| 27 |
/// Each item has a unique identifier. |
| 28 |
/// Items can be deleted without changing the identifiers of other items. |
| 29 |
#[derive(Debug)] |
| 30 |
pub struct Table<T>(Vec<T>); |
| 31 |
|
| 32 |
impl<T> Table<T> { |
| 33 |
pub(super) fn new() -> Self { |
| 34 |
Table(Vec::new()) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
impl<T: Item> Table<T> { |
| 39 |
pub(super) fn next_id(&self) -> T::Id { |
| 40 |
T::Id::new(self.0.len()) |
| 41 |
} |
| 42 |
|
| 43 |
pub(super) fn push(&mut self, item: T) -> &mut T { |
| 44 |
self.0.push(item); |
| 45 |
self.0.last_mut().unwrap() |
| 46 |
} |
| 47 |
|
| 48 |
/// Number of items, including deleted items. |
| 49 |
pub(super) fn len(&self) -> usize { |
| 50 |
self.0.len() |
| 51 |
} |
| 52 |
|
| 53 |
/// Return `True` if there are no non-deleted items. |
| 54 |
pub fn is_empty(&self) -> bool { |
| 55 |
self.into_iter().next().is_none() |
| 56 |
} |
| 57 |
|
| 58 |
/// Number of non-deleted items. |
| 59 |
pub fn count(&self) -> usize { |
| 60 |
self.into_iter().count() |
| 61 |
} |
| 62 |
|
| 63 |
/// Return a reference to an item. |
| 64 |
pub fn get(&self, id: T::Id) -> &T { |
| 65 |
self.0.get(id.index()).unwrap() |
| 66 |
} |
| 67 |
|
| 68 |
/// Return a mutable reference to a segment. |
| 69 |
pub fn get_mut(&mut self, id: T::Id) -> &mut T { |
| 70 |
self.0.get_mut(id.index()).unwrap() |
| 71 |
} |
| 72 |
|
| 73 |
/// Return an iterator for the segments. |
| 74 |
pub fn iter(&self) -> TableIter<'_, T> { |
| 75 |
self.into_iter() |
| 76 |
} |
| 77 |
|
| 78 |
/// Return a mutable iterator for the segments. |
| 79 |
pub fn iter_mut(&mut self) -> TableIterMut<'_, T> { |
| 80 |
self.into_iter() |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
impl<'a, T: Item> IntoIterator for &'a Table<T> { |
| 85 |
type Item = &'a T; |
| 86 |
type IntoIter = TableIter<'a, T>; |
| 87 |
fn into_iter(self) -> TableIter<'a, T> { |
| 88 |
TableIter { |
| 89 |
iter: self.0.iter(), |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
impl<'a, T: Item> IntoIterator for &'a mut Table<T> { |
| 95 |
type Item = &'a mut T; |
| 96 |
type IntoIter = TableIterMut<'a, T>; |
| 97 |
fn into_iter(self) -> TableIterMut<'a, T> { |
| 98 |
TableIterMut { |
| 99 |
iter: self.0.iter_mut(), |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/// An iterator for non-deleted items in a [`Table`]. |
| 105 |
#[derive(Debug)] |
| 106 |
pub struct TableIter<'a, T> { |
| 107 |
iter: core::slice::Iter<'a, T>, |
| 108 |
} |
| 109 |
|
| 110 |
impl<'a, T: Item> Iterator for TableIter<'a, T> { |
| 111 |
type Item = &'a T; |
| 112 |
fn next(&mut self) -> Option<&'a T> { |
| 113 |
self.iter.find(|item| !item.is_deleted()) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/// An iterator for non-deleted items in a [`Table`]. |
| 118 |
#[derive(Debug)] |
| 119 |
pub struct TableIterMut<'a, T> { |
| 120 |
iter: core::slice::IterMut<'a, T>, |
| 121 |
} |
| 122 |
|
| 123 |
impl<'a, T: Item> Iterator for TableIterMut<'a, T> { |
| 124 |
type Item = &'a mut T; |
| 125 |
fn next(&mut self) -> Option<&'a mut T> { |
| 126 |
self.iter.find(|item| !item.is_deleted()) |
| 127 |
} |
| 128 |
} |