refactoring and slight optimization for index assignment

This commit is contained in:
elvis
2025-07-27 19:55:22 +02:00
parent 4b866b63da
commit c5c4e868c0

View File

@ -3,7 +3,9 @@ use std::collections::hash_map::Entry;
use std::collections::{BTreeSet, HashMap, HashSet}; use std::collections::{BTreeSet, HashMap, HashSet};
use std::rc::Rc; use std::rc::Rc;
use petgraph::visit::{ EdgeCount, EdgeRef, GraphBase, IntoEdgeReferences, IntoEdges, IntoNeighborsDirected, IntoNodeReferences, NodeCount }; use petgraph::visit::{ EdgeCount, EdgeRef, GraphBase, IntoEdgeReferences,
IntoEdges, IntoNeighborsDirected, IntoNodeReferences,
NodeCount };
use petgraph::Direction::{Incoming, Outgoing}; use petgraph::Direction::{Incoming, Outgoing};
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -289,8 +291,8 @@ impl<const N: usize, T> NextId<(T, GraphIdType), NodeType> for NodeState<N> {
} }
} }
type MyNodeTranslator<From, const N: usize> = Translator<(From, GraphIdType), NodeType, NodeState<N>>; type MyNodeTranslator<From, const N: usize> =
Translator<(From, GraphIdType), NodeType, NodeState<N>>;
type EdgeIdType = u32; type EdgeIdType = u32;
@ -323,13 +325,11 @@ impl<T> NextId<T, EdgeType> for EdgeState {
type MyEdgeTranslator<From> = Translator<From, EdgeType, EdgeState>; type MyEdgeTranslator<From> = Translator<From, EdgeType, EdgeState>;
type Block = Vec<NodeType>; type Block = Vec<NodeType>;
type BackEdges = HashMap<NodeType, Vec<NodeType>>; type BackEdges = HashMap<NodeType, Vec<NodeType>>;
type NodeToBlock = HashMap<NodeType, Rc<RefCell<SimpleBlock>>>; type NodeToBlock = HashMap<NodeType, Rc<RefCell<SimpleBlock>>>;
type CompoundPartition = Vec<Rc<CompoundBlock>>; type CompoundPartition = Vec<Rc<CompoundBlock>>;
type FineBlockPointer = Rc<RefCell<SimpleBlock>>; type SimpleBlockPointer = Rc<RefCell<SimpleBlock>>;
type CompoundBlockPointer = Rc<CompoundBlock>; type CompoundBlockPointer = Rc<CompoundBlock>;
type BackEdgesGrouped = HashMap<Block, BackEdgesGroup>; type BackEdgesGrouped = HashMap<Block, BackEdgesGroup>;
@ -369,7 +369,7 @@ trait HasBlock {
fn block(&self) -> Block; fn block(&self) -> Block;
} }
impl HasBlock for FineBlockPointer { impl HasBlock for SimpleBlockPointer {
fn block(&self) -> Block { fn block(&self) -> Block {
(**self).borrow().block.clone() (**self).borrow().block.clone()
} }
@ -384,7 +384,7 @@ impl HasBlock for CompoundBlock {
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
fn initialization<const N: usize, G>( fn initialization<const N: usize, G>(
graphs: &[&G; N] graphs: &[&G; N]
) -> ( (FineBlockPointer, FineBlockPointer), ) -> ( (SimpleBlockPointer, SimpleBlockPointer),
CompoundPartition, CompoundPartition,
NodeToBlock, NodeToBlock,
MyNodeTranslator<G::NodeId, N> ) MyNodeTranslator<G::NodeId, N> )
@ -561,7 +561,7 @@ fn split_blocks_with_grouped_backedges(
mut backedges_grouped: BackEdgesGrouped, mut backedges_grouped: BackEdgesGrouped,
node_to_block: &mut NodeToBlock, node_to_block: &mut NodeToBlock,
) -> ( ) -> (
(Vec<FineBlockPointer>, Vec<FineBlockPointer>), (Vec<SimpleBlockPointer>, Vec<SimpleBlockPointer>),
Vec<CompoundBlockPointer>, Vec<CompoundBlockPointer>,
) { ) {
let mut all_new_simple_blocks: Vec<Rc<RefCell<SimpleBlock>>> = vec![]; let mut all_new_simple_blocks: Vec<Rc<RefCell<SimpleBlock>>> = vec![];
@ -871,15 +871,23 @@ where
// we convert the problem into the equivalent with only one label // we convert the problem into the equivalent with only one label
let converter_edges: MyEdgeTranslator<G::EdgeWeight> = { let converter_edges: MyEdgeTranslator<G::EdgeWeight> = {
let mut tmp = Translator::new(); let mut labels: HashMap<G::EdgeWeight, u32> = HashMap::new();
for edge in graph_a.edge_references() { for edge in graph_a.edge_references() {
let _ = tmp.encode(edge.weight().clone()); *labels.entry(edge.weight().clone()).or_default() += 1;
} }
for edge in graph_b.edge_references() { for edge in graph_b.edge_references() {
let _ = tmp.encode(edge.weight().clone()); *labels.entry(edge.weight().clone()).or_default() += 1;
} }
// slight optimization: we reorder the edges such that edges with the
// most occurrences have smaller index
let mut labels: Vec<(G::EdgeWeight, u32)> = labels.into_iter().collect();
labels.sort_by(|a, b| b.1.cmp(&a.1));
let mut tmp = Translator::new();
for (label, _) in labels.into_iter() {
let _ = tmp.encode(label);
}
tmp tmp
}; };