Uplift rustc_mir_transform::coverage::counters::union_find to rustc_data_structures.

This commit is contained in:
Camille Gillot
2025-08-22 02:22:51 +00:00
parent 040a98af70
commit 689171d38e
5 changed files with 7 additions and 8 deletions

View File

@@ -77,6 +77,7 @@ pub mod thinvec;
pub mod thousands; pub mod thousands;
pub mod transitive_relation; pub mod transitive_relation;
pub mod unhash; pub mod unhash;
pub mod union_find;
pub mod unord; pub mod unord;
pub mod vec_cache; pub mod vec_cache;
pub mod work_queue; pub mod work_queue;

View File

@@ -9,7 +9,7 @@ mod tests;
/// Simple implementation of a union-find data structure, i.e. a disjoint-set /// Simple implementation of a union-find data structure, i.e. a disjoint-set
/// forest. /// forest.
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct UnionFind<Key: Idx> { pub struct UnionFind<Key: Idx> {
table: IndexVec<Key, UnionFindEntry<Key>>, table: IndexVec<Key, UnionFindEntry<Key>>,
} }
@@ -28,7 +28,7 @@ struct UnionFindEntry<Key> {
impl<Key: Idx> UnionFind<Key> { impl<Key: Idx> UnionFind<Key> {
/// Creates a new disjoint-set forest containing the keys `0..num_keys`. /// Creates a new disjoint-set forest containing the keys `0..num_keys`.
/// Initially, every key is part of its own one-element set. /// Initially, every key is part of its own one-element set.
pub(crate) fn new(num_keys: usize) -> Self { pub fn new(num_keys: usize) -> Self {
// Initially, every key is the root of its own set, so its parent is itself. // Initially, every key is the root of its own set, so its parent is itself.
Self { table: IndexVec::from_fn_n(|key| UnionFindEntry { parent: key, rank: 0 }, num_keys) } Self { table: IndexVec::from_fn_n(|key| UnionFindEntry { parent: key, rank: 0 }, num_keys) }
} }
@@ -38,7 +38,7 @@ impl<Key: Idx> UnionFind<Key> {
/// ///
/// Also updates internal data structures to make subsequent `find` /// Also updates internal data structures to make subsequent `find`
/// operations faster. /// operations faster.
pub(crate) fn find(&mut self, key: Key) -> Key { pub fn find(&mut self, key: Key) -> Key {
// Loop until we find a key that is its own parent. // Loop until we find a key that is its own parent.
let mut curr = key; let mut curr = key;
while let parent = self.table[curr].parent while let parent = self.table[curr].parent
@@ -60,7 +60,7 @@ impl<Key: Idx> UnionFind<Key> {
/// Merges the set containing `a` and the set containing `b` into one set. /// Merges the set containing `a` and the set containing `b` into one set.
/// ///
/// Returns the common root of both keys, after the merge. /// Returns the common root of both keys, after the merge.
pub(crate) fn unify(&mut self, a: Key, b: Key) -> Key { pub fn unify(&mut self, a: Key, b: Key) -> Key {
let mut a = self.find(a); let mut a = self.find(a);
let mut b = self.find(b); let mut b = self.find(b);
@@ -90,7 +90,7 @@ impl<Key: Idx> UnionFind<Key> {
/// Takes a "snapshot" of the current state of this disjoint-set forest, in /// Takes a "snapshot" of the current state of this disjoint-set forest, in
/// the form of a vector that directly maps each key to its current root. /// the form of a vector that directly maps each key to its current root.
pub(crate) fn snapshot(&mut self) -> IndexVec<Key, Key> { pub fn snapshot(&mut self) -> IndexVec<Key, Key> {
self.table.indices().map(|key| self.find(key)).collect() self.table.indices().map(|key| self.find(key)).collect()
} }
} }

View File

@@ -16,7 +16,6 @@ use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph};
mod balanced_flow; mod balanced_flow;
pub(crate) mod node_flow; pub(crate) mod node_flow;
mod union_find;
/// Struct containing the results of [`prepare_bcb_counters_data`]. /// Struct containing the results of [`prepare_bcb_counters_data`].
pub(crate) struct BcbCountersData { pub(crate) struct BcbCountersData {

View File

@@ -7,13 +7,12 @@
//! (Knuth & Stevenson, 1973). //! (Knuth & Stevenson, 1973).
use rustc_data_structures::graph; use rustc_data_structures::graph;
use rustc_data_structures::union_find::UnionFind;
use rustc_index::bit_set::DenseBitSet; use rustc_index::bit_set::DenseBitSet;
use rustc_index::{Idx, IndexSlice, IndexVec}; use rustc_index::{Idx, IndexSlice, IndexVec};
pub(crate) use rustc_middle::mir::coverage::NodeFlowData; pub(crate) use rustc_middle::mir::coverage::NodeFlowData;
use rustc_middle::mir::coverage::Op; use rustc_middle::mir::coverage::Op;
use crate::coverage::counters::union_find::UnionFind;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;