Skip to content

Commit cf3f9b9

Browse files
committed
use ensure_contains_elem instead of resize_to_elem
1 parent cd507e9 commit cf3f9b9

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/librustc_data_structures/bitvec.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,14 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
293293
///
294294
/// Returns true if this changed the matrix, and false otherwise.
295295
pub fn add(&mut self, row: R, column: C) -> bool {
296-
self.vector.resize_to_elem(row, || SparseBitSet::new());
297-
if let None = self.vector.get(row) {
298-
self.vector.push(SparseBitSet::new());
299-
}
300-
296+
debug!(
297+
"add(row={:?}, column={:?}, current_len={})",
298+
row,
299+
column,
300+
self.vector.len()
301+
);
302+
self.vector
303+
.ensure_contains_elem(row, || SparseBitSet::new());
301304
self.vector[row].insert(column)
302305
}
303306

@@ -321,7 +324,8 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
321324

322325
if read != write {
323326
if self.vector.get(read).is_some() {
324-
self.vector.resize_to_elem(write, || SparseBitSet::new());
327+
self.vector
328+
.ensure_contains_elem(write, || SparseBitSet::new());
325329
let (bit_set_read, bit_set_write) = self.vector.pick2_mut(read, write);
326330

327331
for read_chunk in bit_set_read.chunks() {
@@ -335,7 +339,8 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
335339

336340
/// Merge a row, `from`, into the `into` row.
337341
pub fn merge_into(&mut self, into: R, from: &SparseBitSet<C>) -> bool {
338-
self.vector.resize_to_elem(into, || SparseBitSet::new());
342+
self.vector
343+
.ensure_contains_elem(into, || SparseBitSet::new());
339344
self.vector[into].insert_from(from)
340345
}
341346

src/librustc_data_structures/indexed_vec.rs

+12
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,18 @@ impl<I: Idx, T> IndexVec<I, T> {
518518
}
519519

520520
impl<I: Idx, T: Clone> IndexVec<I, T> {
521+
/// Grows the index vector so that it contains an entry for
522+
/// `elem`; if that is already true, then has no
523+
/// effect. Otherwise, inserts new values as needed by invoking
524+
/// `fill_value`.
525+
#[inline]
526+
pub fn ensure_contains_elem(&mut self, elem: I, fill_value: impl FnMut() -> T) {
527+
let min_new_len = elem.index() + 1;
528+
if self.len() < min_new_len {
529+
self.raw.resize_with(min_new_len, fill_value);
530+
}
531+
}
532+
521533
#[inline]
522534
pub fn resize(&mut self, new_len: usize, value: T) {
523535
self.raw.resize(new_len, value)

0 commit comments

Comments
 (0)