This commit is contained in:
wborgeaud 2021-07-02 14:26:49 +02:00
parent fc0f8a78ce
commit d93cf693ba

View File

@ -17,17 +17,6 @@ pub struct ForestNode<T: Debug + Copy + Eq + PartialEq> {
index: usize, index: usize,
} }
impl<T: Debug + Copy + Eq + PartialEq> ForestNode<T> {
pub fn new(t: T, parent: usize, size: usize, index: usize) -> Self {
Self {
t,
parent,
size,
index,
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct TargetPartition<T: Debug + Copy + Eq + PartialEq + Hash> { pub struct TargetPartition<T: Debug + Copy + Eq + PartialEq + Hash> {
forest: Vec<ForestNode<T>>, forest: Vec<ForestNode<T>>,
@ -44,21 +33,15 @@ impl<T: Debug + Copy + Eq + PartialEq + Hash> Default for TargetPartition<T> {
} }
impl<T: Debug + Copy + Eq + PartialEq + Hash> TargetPartition<T> { impl<T: Debug + Copy + Eq + PartialEq + Hash> TargetPartition<T> {
pub fn get(&self, t: T) -> ForestNode<T> {
self.forest[self.indices[&t]]
}
pub fn get_mut(&mut self, t: T) -> &mut ForestNode<T> {
&mut self.forest[self.indices[&t]]
}
// pub fn get_partition(&self, target: Target) -> &[Target] {
// &self.partitions[self.indices[&target]]
// }
/// Add a new partition with a single member. /// Add a new partition with a single member.
pub fn add(&mut self, t: T) { pub fn add(&mut self, t: T) {
let index = self.forest.len(); let index = self.forest.len();
self.forest.push(ForestNode::new(t, index, 1, index)); self.forest.push(ForestNode {
t,
parent: index,
size: 1,
index,
});
self.indices.insert(t, index); self.indices.insert(t, index);
} }
@ -75,10 +58,10 @@ impl<T: Debug + Copy + Eq + PartialEq + Hash> TargetPartition<T> {
/// Merge the two partitions containing the two given targets. Does nothing if the targets are /// Merge the two partitions containing the two given targets. Does nothing if the targets are
/// already members of the same partition. /// already members of the same partition.
pub fn merge(&mut self, tx: T, ty: T) { pub fn merge(&mut self, tx: T, ty: T) {
let mut x = self.get(tx); let index_x = self.indices[&tx];
let mut y = self.get(ty); let index_y = self.indices[&ty];
let index_x = x.index; let mut x = self.forest[index_x];
let index_y = y.index; let mut y = self.forest[index_y];
x = self.forest[x.parent]; x = self.forest[x.parent];
y = self.forest[y.parent]; y = self.forest[y.parent];
@ -108,6 +91,7 @@ impl TargetPartition<Target> {
} }
let mut indices = HashMap::new(); let mut indices = HashMap::new();
// // Here we keep just the Wire targets, filtering out everything else.
let partition = partition let partition = partition
.into_values() .into_values()
.map(|v| { .map(|v| {
@ -126,31 +110,6 @@ impl TargetPartition<Target> {
}); });
WirePartitions { partition, indices } WirePartitions { partition, indices }
// // Here we keep just the Wire targets, filtering out everything else.
// let mut partitions = Vec::new();
// let mut indices = HashMap::new();
//
// for old_partition in &self.partitions {
// let mut new_partition = Vec::new();
// for target in old_partition {
// if let Target::Wire(w) = *target {
// new_partition.push(w);
// }
// }
// partitions.push(new_partition);
// }
//
// for (&target, &index) in &self.indices {
// if let Target::Wire(gi) = target {
// indices.insert(gi, index);
// }
// }
//
// WirePartitions {
// partitions,
// indices,
// }
} }
} }
@ -214,15 +173,3 @@ impl WirePartitions {
sigma sigma
} }
} }
#[test]
fn test_part() {
let mut part = TargetPartition::default();
part.add(1);
part.add(2);
part.add(3);
part.merge(1, 3);
dbg!(part);
}