mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 06:44:35 +00:00
rustc_index: Add some map-like APIs to IndexVec
This commit is contained in:
parent
ac2d9fc509
commit
fbe5e5c0ee
@ -474,9 +474,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
|||||||
res
|
res
|
||||||
} else {
|
} else {
|
||||||
// Associate an HirId to both ids even if there is no resolution.
|
// Associate an HirId to both ids even if there is no resolution.
|
||||||
self.node_id_to_hir_id.ensure_contains_elem(new_node_id, || None);
|
let _old = self
|
||||||
debug_assert!(self.node_id_to_hir_id[new_node_id].is_none());
|
.node_id_to_hir_id
|
||||||
self.node_id_to_hir_id[new_node_id] = Some(hir::HirId::make_owner(new_id));
|
.insert(new_node_id, hir::HirId::make_owner(new_id));
|
||||||
|
debug_assert!(_old.is_none());
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
let ident = *ident;
|
let ident = *ident;
|
||||||
|
@ -469,11 +469,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
|||||||
let def_id = self.resolver.local_def_id(owner);
|
let def_id = self.resolver.local_def_id(owner);
|
||||||
|
|
||||||
// Always allocate the first `HirId` for the owner itself.
|
// Always allocate the first `HirId` for the owner itself.
|
||||||
self.node_id_to_hir_id.ensure_contains_elem(owner, || None);
|
let _old = self.node_id_to_hir_id.insert(owner, hir::HirId::make_owner(def_id));
|
||||||
if let Some(_lowered) = self.node_id_to_hir_id[owner] {
|
debug_assert_eq!(_old, None);
|
||||||
panic!("with_hir_id_owner must not be called multiple times on owner {:?}", def_id);
|
|
||||||
}
|
|
||||||
self.node_id_to_hir_id[owner] = Some(hir::HirId::make_owner(def_id));
|
|
||||||
|
|
||||||
let current_owner = std::mem::replace(&mut self.current_hir_id_owner, def_id);
|
let current_owner = std::mem::replace(&mut self.current_hir_id_owner, def_id);
|
||||||
let current_local_counter =
|
let current_local_counter =
|
||||||
@ -484,8 +481,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
|||||||
self.current_hir_id_owner = current_owner;
|
self.current_hir_id_owner = current_owner;
|
||||||
self.item_local_id_counter = current_local_counter;
|
self.item_local_id_counter = current_local_counter;
|
||||||
|
|
||||||
self.owners.ensure_contains_elem(def_id, || None);
|
let _old = self.owners.insert(def_id, item);
|
||||||
self.owners[def_id] = Some(item);
|
debug_assert!(_old.is_none());
|
||||||
|
|
||||||
def_id
|
def_id
|
||||||
}
|
}
|
||||||
@ -499,18 +496,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
|||||||
fn lower_node_id(&mut self, ast_node_id: NodeId) -> hir::HirId {
|
fn lower_node_id(&mut self, ast_node_id: NodeId) -> hir::HirId {
|
||||||
assert_ne!(ast_node_id, DUMMY_NODE_ID);
|
assert_ne!(ast_node_id, DUMMY_NODE_ID);
|
||||||
|
|
||||||
self.node_id_to_hir_id.ensure_contains_elem(ast_node_id, || None);
|
*self.node_id_to_hir_id.get_or_insert_with(ast_node_id, || {
|
||||||
if let Some(existing_hir_id) = self.node_id_to_hir_id[ast_node_id] {
|
|
||||||
existing_hir_id
|
|
||||||
} else {
|
|
||||||
// Generate a new `HirId`.
|
// Generate a new `HirId`.
|
||||||
let owner = self.current_hir_id_owner;
|
let owner = self.current_hir_id_owner;
|
||||||
let local_id = self.item_local_id_counter;
|
let local_id = self.item_local_id_counter;
|
||||||
self.item_local_id_counter.increment_by(1);
|
self.item_local_id_counter.increment_by(1);
|
||||||
let hir_id = hir::HirId { owner, local_id };
|
hir::HirId { owner, local_id }
|
||||||
self.node_id_to_hir_id[ast_node_id] = Some(hir_id);
|
})
|
||||||
hir_id
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_id(&mut self) -> hir::HirId {
|
fn next_id(&mut self) -> hir::HirId {
|
||||||
|
@ -1072,13 +1072,9 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn ensure_row(&mut self, row: R) -> &mut HybridBitSet<C> {
|
fn ensure_row(&mut self, row: R) -> &mut HybridBitSet<C> {
|
||||||
// Instantiate any missing rows up to and including row `row` with an
|
// Instantiate any missing rows up to and including row `row` with an empty HybridBitSet.
|
||||||
// empty HybridBitSet.
|
|
||||||
self.rows.ensure_contains_elem(row, || None);
|
|
||||||
|
|
||||||
// Then replace row `row` with a full HybridBitSet if necessary.
|
// Then replace row `row` with a full HybridBitSet if necessary.
|
||||||
let num_columns = self.num_columns;
|
self.rows.get_or_insert_with(row, || HybridBitSet::new_empty(self.num_columns))
|
||||||
self.rows[row].get_or_insert_with(|| HybridBitSet::new_empty(num_columns))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the cell at `(row, column)` to true. Put another way, insert
|
/// 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> {
|
impl<I: Idx, T: Clone> IndexVec<I, T> {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn resize(&mut self, new_len: usize, value: T) {
|
pub fn resize(&mut self, new_len: usize, value: T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user