From 4d2940c9d733f93b3502ac729dec983f1a8b3460 Mon Sep 17 00:00:00 2001 From: Sergio Valverde Date: Mon, 4 Jul 2022 12:34:32 -0600 Subject: [PATCH] Add size hint to the subgraph isomorphism iterator --- src/algo/isomorphism.rs | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/algo/isomorphism.rs b/src/algo/isomorphism.rs index e9bc9d881..d554d8479 100644 --- a/src/algo/isomorphism.rs +++ b/src/algo/isomorphism.rs @@ -1,3 +1,5 @@ +use std::convert::TryFrom; + use crate::data::DataMap; use crate::visit::EdgeCount; use crate::visit::EdgeRef; @@ -755,6 +757,47 @@ mod matching { &mut self.stack, ) } + + fn size_hint(&self) -> (usize, Option) { + // To calculate the upper bound of results we use n! where n is the + // number of nodes in graph 1. n! values fit into a 64-bit usize up + // to n = 20, so we don't estimate an upper limit for n > 20. + let n = self.st.0.graph.node_count(); + const MAX_N: usize = 20; + + if n > MAX_N { + return (0, None); + } + + // We hardcode n! values into an array that accounts for architectures + // with smaller usizes to get our upper bound. + let upper_bounds: [Option; MAX_N + 1] = [ + 1u64, + 1, + 2, + 6, + 24, + 120, + 720, + 5040, + 40320, + 362880, + 3628800, + 39916800, + 479001600, + 6227020800, + 87178291200, + 1307674368000, + 20922789888000, + 355687428096000, + 6402373705728000, + 121645100408832000, + 2432902008176640000, + ] + .map(|n| usize::try_from(n).ok()); + + (0, upper_bounds[n]) + } } }