<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow

import type { IndexIntoCategoryList, CategoryList } from '../types/profile';

type TranslationMapForCategories = Map<
  IndexIntoCategoryList,
  IndexIntoCategoryList
>;
export function mergeCategories(
  categoriesPerThread: CategoryList[]
): {|
  categories: CategoryList,
  translationMaps: TranslationMapForCategories[],
|} {
  const newCategories = [];
  const translationMaps = [];

  categoriesPerThread.forEach(categories => {
    const translationMap = new Map();
    translationMaps.push(translationMap);
    const insertedCategories: Map<string, IndexIntoCategoryList> = new Map();

    categories.forEach((category, i) => {
      const { name } = category;
      const insertedCategoryIndex = insertedCategories.get(name);
      if (insertedCategoryIndex !== undefined) {
        translationMap.set(i, insertedCategoryIndex);
        return;
      }

      translationMap.set(i, newCategories.length);
      insertedCategories.set(name, newCategories.length);
      newCategories.push(category);
    });
  });

  return { categories: newCategories, translationMaps };
}

export function adjustCategories(
  categories: $ReadOnlyArray<IndexIntoCategoryList | null>,
  translationMap: TranslationMapForCategories
): Array<IndexIntoCategoryList | null> {
  return categories.map(category => {
    if (category === null) {
      return null;
    }
    const result = translationMap.get(category);
    if (result === undefined) {
      throw new Error(
        `Category with index ${category} wasn't found in the translation map.`
      );
    }
    return result;
  });
}
<commit_msg>Add some comments and adjust the error message<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow

/*
 * This file contains all functions that are needed to achieve profiles
 * comparison: how to merge profiles, how to diff them, etc.
 */

import { stripIndent } from 'common-tags';
import type { IndexIntoCategoryList, CategoryList } from '../types/profile';

type TranslationMapForCategories = Map<
  IndexIntoCategoryList,
  IndexIntoCategoryList
>;

/**
 * Merges several categories lists into one, resolving duplicates if necessary.
 * It returns a translation map that can be used in `adjustCategories` later.
 */
export function mergeCategories(
  categoriesPerThread: CategoryList[]
): {|
  categories: CategoryList,
  translationMaps: TranslationMapForCategories[],
|} {
  const newCategories = [];
  const translationMaps = [];

  categoriesPerThread.forEach(categories => {
    const translationMap = new Map();
    translationMaps.push(translationMap);
    const insertedCategories: Map<string, IndexIntoCategoryList> = new Map();

    categories.forEach((category, i) => {
      const { name } = category;
      const insertedCategoryIndex = insertedCategories.get(name);
      if (insertedCategoryIndex !== undefined) {
        translationMap.set(i, insertedCategoryIndex);
        return;
      }

      translationMap.set(i, newCategories.length);
      insertedCategories.set(name, newCategories.length);
      newCategories.push(category);
    });
  });

  return { categories: newCategories, translationMaps };
}

/**
 * Adjusts the category indices in a category list using a translation map.
 */
export function adjustCategories(
  categories: $ReadOnlyArray<IndexIntoCategoryList | null>,
  translationMap: TranslationMapForCategories
): Array<IndexIntoCategoryList | null> {
  return categories.map(category => {
    if (category === null) {
      return null;
    }
    const result = translationMap.get(category);
    if (result === undefined) {
      throw new Error(
        stripIndent`
          Category with index ${category} hasn't been found in the translation map.
          This shouldn't happen and indicates a bug in perf-html's code.
        `
      );
    }
    return result;
  });
}

{'fraction_non_alphanumeric': 0.10348093772199858, 'fraction_numerical': 0.001894387875917594, 'mean_word_length': 3.672566371681416, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4099785', 'n_tokens_mistral': 1149, 'n_tokens_neox': 1096, 'n_words': 344}