rustc_index: Add some map-like APIs to IndexVec
This commit is contained in:
parent
ac2d9fc509
commit
fbe5e5c0ee
4 changed files with 28 additions and 24 deletions
|
@ -1072,13 +1072,9 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
|
|||
}
|
||||
|
||||
fn ensure_row(&mut self, row: R) -> &mut HybridBitSet<C> {
|
||||
// Instantiate any missing rows up to and including row `row` with an
|
||||
// empty HybridBitSet.
|
||||
self.rows.ensure_contains_elem(row, || None);
|
||||
|
||||
// Instantiate any missing rows up to and including row `row` with an empty HybridBitSet.
|
||||
// Then replace row `row` with a full HybridBitSet if necessary.
|
||||
let num_columns = self.num_columns;
|
||||
self.rows[row].get_or_insert_with(|| HybridBitSet::new_empty(num_columns))
|
||||
self.rows.get_or_insert_with(row, || HybridBitSet::new_empty(self.num_columns))
|
||||
}
|
||||
|
||||
/// Sets the cell at `(row, column)` to true. Put another way, insert
|
||||
|
|
|
@ -720,6 +720,21 @@ impl<I: Idx, T> IndexVec<I, T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// `IndexVec` is often used as a map, so it provides some map-like APIs.
|
||||
impl<I: Idx, T> IndexVec<I, Option<T>> {
|
||||
#[inline]
|
||||
pub fn insert(&mut self, index: I, value: T) -> Option<T> {
|
||||
self.ensure_contains_elem(index, || None);
|
||||
self[index].replace(value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_or_insert_with(&mut self, index: I, value: impl FnOnce() -> T) -> &mut T {
|
||||
self.ensure_contains_elem(index, || None);
|
||||
self[index].get_or_insert_with(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Idx, T: Clone> IndexVec<I, T> {
|
||||
#[inline]
|
||||
pub fn resize(&mut self, new_len: usize, value: T) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue