From ece682864636e038b5071e3b82616d349b010889 Mon Sep 17 00:00:00 2001 From: geemo Date: Mon, 21 Aug 2023 22:52:06 -0600 Subject: [PATCH] avoid underflow on zero length vector --- .../operation_pool/src/bron_kerbosch.rs | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/beacon_node/operation_pool/src/bron_kerbosch.rs b/beacon_node/operation_pool/src/bron_kerbosch.rs index 65cd624b388..6467ac9d004 100644 --- a/beacon_node/operation_pool/src/bron_kerbosch.rs +++ b/beacon_node/operation_pool/src/bron_kerbosch.rs @@ -5,26 +5,29 @@ pub fn bron_kerbosch bool>( vertices: &Vec, is_compatible: F, ) -> Vec> { - // build neighbourhoods and degeneracy ordering, also move to index-based reasoning - let neighbourhoods = compute_neigbourhoods(vertices, is_compatible); - let ordering = degeneracy_order(vertices.len(), &neighbourhoods); - // create empty vector to store cliques let mut cliques: Vec> = vec![]; - let mut publish_clique = |c| cliques.push(c); - - for i in 0..ordering.len() { - let vi = ordering[i]; - let p = (i + 1..ordering.len()) - .filter(|j| neighbourhoods[vi].contains(&ordering[*j])) - .map(|j| ordering[j]) - .collect(); - let r = vec![vi]; - let x = (0..i) - .filter(|j| neighbourhoods[vi].contains(&ordering[*j])) - .map(|j| ordering[j]) - .collect(); - bron_kerbosch_aux(r, p, x, &neighbourhoods, &mut publish_clique) + + if vertices.len() > 0 { + // build neighbourhoods and degeneracy ordering, also move to index-based reasoning + let neighbourhoods = compute_neigbourhoods(vertices, is_compatible); + let ordering = degeneracy_order(vertices.len(), &neighbourhoods); + + let mut publish_clique = |c| cliques.push(c); + + for i in 0..ordering.len() { + let vi = ordering[i]; + let p = (i + 1..ordering.len()) + .filter(|j| neighbourhoods[vi].contains(&ordering[*j])) + .map(|j| ordering[j]) + .collect(); + let r = vec![vi]; + let x = (0..i) + .filter(|j| neighbourhoods[vi].contains(&ordering[*j])) + .map(|j| ordering[j]) + .collect(); + bron_kerbosch_aux(r, p, x, &neighbourhoods, &mut publish_clique) + } } cliques