combine method

StringStatistics? combine(
  1. StringStatistics? stats
)

Implementation

StringStatistics? combine(StringStatistics? stats) {
  final newCharFrequencies = <String, int>{};
  newCharFrequencies.addAll(charFrequencies);
  for (final key in stats!.charFrequencies.keys) {
    newCharFrequencies.update(key, (val) => val + stats.charFrequencies[key]!,
        ifAbsent: () => stats.charFrequencies[key]!);
  }

  final newWordFrequencies = <String, int>{};
  newWordFrequencies.addAll(wordFrequencies);
  for (final key in stats.wordFrequencies.keys) {
    newWordFrequencies.update(key, (val) => val + stats.wordFrequencies[key]!,
        ifAbsent: () => stats.wordFrequencies[key]!);
  }

  final newWordPositions = <String, Set<Position>>{};
  wordPositions!.forEach((val, set) => newWordPositions.putIfAbsent(val, () => Set.from(set)));
  stats.wordPositions!
      .forEach((val, set) => newWordPositions.update(val, (old) => set.union(old), ifAbsent: () => Set.from(set)));

  return StringStatistics(
      charCount! + stats.charCount!,
      wordCount! + stats.wordCount!,
      lineCount! + stats.lineCount!,
      emptyLineCount! + stats.emptyLineCount!,
      nonEmptyLineCount! + stats.nonEmptyLineCount!,
      newCharFrequencies,
      newWordFrequencies,
      newWordPositions);
}