Skip to content

Commit

Permalink
Rename universe_size as domain_size.
Browse files Browse the repository at this point in the history
Because `domain` is a more obvious term than `universe` for this
concept.
  • Loading branch information
nnethercote committed Aug 24, 2018
1 parent 63d6649 commit 180052d
Showing 1 changed file with 32 additions and 36 deletions.
68 changes: 32 additions & 36 deletions src/librustc_data_structures/indexed_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,34 @@ impl<T: Idx> fmt::Debug for IdxSet<T> {
}

impl<T: Idx> IdxSet<T> {
fn new(init: Word, universe_size: usize) -> Self {
let num_words = (universe_size + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
fn new(init: Word, domain_size: usize) -> Self {
let num_words = (domain_size + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
IdxSet {
_pd: Default::default(),
bits: vec![init; num_words],
}
}

/// Creates set holding every element whose index falls in range 0..universe_size.
pub fn new_filled(universe_size: usize) -> Self {
let mut result = Self::new(!0, universe_size);
result.trim_to(universe_size);
/// Creates set holding every element whose index falls in range 0..domain_size.
pub fn new_filled(domain_size: usize) -> Self {
let mut result = Self::new(!0, domain_size);
result.trim_to(domain_size);
result
}

/// Creates set holding no elements.
pub fn new_empty(universe_size: usize) -> Self {
Self::new(0, universe_size)
pub fn new_empty(domain_size: usize) -> Self {
Self::new(0, domain_size)
}

/// Duplicates as a hybrid set.
pub fn to_hybrid(&self) -> HybridIdxSet<T> {
// This universe_size may be slightly larger than the one specified
// This domain_size may be slightly larger than the one specified
// upon creation, due to rounding up to a whole word. That's ok.
let universe_size = self.bits.len() * BITS_PER_WORD;
let domain_size = self.bits.len() * BITS_PER_WORD;

// Note: we currently don't bother trying to make a Sparse set.
HybridIdxSet::Dense(self.to_owned(), universe_size)
HybridIdxSet::Dense(self.to_owned(), domain_size)
}

/// Removes all elements
Expand All @@ -105,29 +105,29 @@ impl<T: Idx> IdxSet<T> {
}
}

/// Sets all elements up to `universe_size`
pub fn set_up_to(&mut self, universe_size: usize) {
/// Sets all elements up to `domain_size`
pub fn set_up_to(&mut self, domain_size: usize) {
for b in &mut self.bits {
*b = !0;
}
self.trim_to(universe_size);
self.trim_to(domain_size);
}

/// Clear all elements above `universe_size`.
fn trim_to(&mut self, universe_size: usize) {
/// Clear all elements above `domain_size`.
fn trim_to(&mut self, domain_size: usize) {
// `trim_block` is the first block where some bits have
// to be cleared.
let trim_block = universe_size / BITS_PER_WORD;
let trim_block = domain_size / BITS_PER_WORD;

// all the blocks above it have to be completely cleared.
if trim_block < self.bits.len() {
for b in &mut self.bits[trim_block+1..] {
*b = 0;
}

// at that block, the `universe_size % BITS_PER_WORD` lsbs
// at that block, the `domain_size % BITS_PER_WORD` LSBs
// should remain.
let remaining_bits = universe_size % BITS_PER_WORD;
let remaining_bits = domain_size % BITS_PER_WORD;
let mask = (1<<remaining_bits)-1;
self.bits[trim_block] &= mask;
}
Expand Down Expand Up @@ -293,8 +293,8 @@ impl<T: Idx> SparseIdxSet<T> {
}
}

fn to_dense(&self, universe_size: usize) -> IdxSet<T> {
let mut dense = IdxSet::new_empty(universe_size);
fn to_dense(&self, domain_size: usize) -> IdxSet<T> {
let mut dense = IdxSet::new_empty(domain_size);
for elem in self.0.iter() {
dense.add(elem);
}
Expand Down Expand Up @@ -323,28 +323,24 @@ impl<'a, T: Idx> Iterator for SparseIter<'a, T> {
/// Like IdxSet, but with a hybrid representation: sparse when there are few
/// elements in the set, but dense when there are many. It's especially
/// efficient for sets that typically have a small number of elements, but a
/// large `universe_size`, and are cleared frequently.
/// large `domain_size`, and are cleared frequently.
#[derive(Clone, Debug)]
pub enum HybridIdxSet<T: Idx> {
Sparse(SparseIdxSet<T>, usize),
Dense(IdxSet<T>, usize),
}

impl<T: Idx> HybridIdxSet<T> {
pub fn new_empty(universe_size: usize) -> Self {
HybridIdxSet::Sparse(SparseIdxSet::new(), universe_size)
pub fn new_empty(domain_size: usize) -> Self {
HybridIdxSet::Sparse(SparseIdxSet::new(), domain_size)
}

fn universe_size(&mut self) -> usize {
match *self {
pub fn clear(&mut self) {
let domain_size = match *self {
HybridIdxSet::Sparse(_, size) => size,
HybridIdxSet::Dense(_, size) => size,
}
}

pub fn clear(&mut self) {
let universe_size = self.universe_size();
*self = HybridIdxSet::new_empty(universe_size);
};
*self = HybridIdxSet::new_empty(domain_size);
}

/// Returns true iff set `self` contains `elem`.
Expand Down Expand Up @@ -374,11 +370,11 @@ impl<T: Idx> HybridIdxSet<T> {
// appease the borrow checker.
let dummy = HybridIdxSet::Sparse(SparseIdxSet::new(), 0);
match mem::replace(self, dummy) {
HybridIdxSet::Sparse(sparse, universe_size) => {
let mut dense = sparse.to_dense(universe_size);
HybridIdxSet::Sparse(sparse, domain_size) => {
let mut dense = sparse.to_dense(domain_size);
let changed = dense.add(elem);
assert!(changed);
mem::replace(self, HybridIdxSet::Dense(dense, universe_size));
mem::replace(self, HybridIdxSet::Dense(dense, domain_size));
changed
}
_ => panic!("impossible"),
Expand All @@ -401,7 +397,7 @@ impl<T: Idx> HybridIdxSet<T> {
/// Converts to a dense set, consuming itself in the process.
pub fn to_dense(self) -> IdxSet<T> {
match self {
HybridIdxSet::Sparse(sparse, universe_size) => sparse.to_dense(universe_size),
HybridIdxSet::Sparse(sparse, domain_size) => sparse.to_dense(domain_size),
HybridIdxSet::Dense(dense, _) => dense,
}
}
Expand Down

0 comments on commit 180052d

Please sign in to comment.