unwind method

List<EstimatedGuessesMixin> unwind(
  1. int passwordLength
)

helper: step backwards through optimal.m starting at the end, constructing the final optimal match sequence.

Implementation

List<EstimatedGuessesMixin> unwind(int passwordLength) {
  final List<EstimatedGuessesMixin> optimalMatchSequence = [];
  int k = passwordLength - 1;
  // find the final best sequence length and score
  int sequenceLength = 0; // TODO was: optimal.m[k].length;

  double g = double.infinity;
  // This is to mimic javascript behavior when accessing an array by negative
  // key which returns undefined - opposite to dart which throws an exception.
  final Map<int, double>? temp = k.isNegative ? null : optimal.g[k];

  if (temp != null) {
    temp.forEach((candidateSequenceLength, candidateMetricMatch) {
      if (candidateMetricMatch < g) {
        sequenceLength = candidateSequenceLength;
        g = candidateMetricMatch;
      }
    });
  }
  while (k >= 0) {
    // TODO check null safety
    final EstimatedGuessesMixin match = optimal.m[k][sequenceLength]!;
    optimalMatchSequence.insert(0, match);
    k = match.i - 1;
    sequenceLength--;
  }

  return optimalMatchSequence;
}