Repository: tesseract-ocr/tesseract
File: src/classify/trainingsampleset.cpp (C++)

Code before:
      int max_s1 = 0;
      int max_s2 = 0;
      fcinfo.canonical_sample = fcinfo.samples[0];
      fcinfo.canonical_dist = 0.0f;
      for (int i = 0; i < fcinfo.samples.size(); ++i) {
        int s1 = fcinfo.samples[i];
        const GenericVector<int>& features1 = samples_[s1]->indexed_features();
        f_table.Set(features1, features1.size(), true);
        double max_dist = 0.0;
        // Run the full squared-order search for similar samples. It is still
        // reasonably fast because f_table.FeatureDistance is fast, but we
        // may have to reconsider if we start playing with too many samples
        // of a single char/font.
        for (int j = 0; j < fcinfo.samples.size(); ++j) {
          int s2 = fcinfo.samples[j];
          if (samples_[s2]->class_id() != c  ||
              samples_[s2]->font_id() != font_id ||
              s2 == s1)
            continue;
          GenericVector<int> features2 = samples_[s2]->indexed_features();
          double dist = f_table.FeatureDistance(features2);
          if (dist > max_dist) {
            max_dist = dist;
            if (dist > max_max_dist) {
              max_max_dist = dist;
              max_s1 = s1;
              max_s2 = s2;
            }
          }
        }
        // Using Set(..., false) is far faster than re initializing, due to
        // the sparseness of the feature space.
        f_table.Set(features1, features1.size(), false);
        samples_[s1]->set_max_dist(max_dist);
        ++samples_found;
        if (max_dist < min_max_dist) {
          fcinfo.canonical_sample = s1;
          fcinfo.canonical_dist = max_dist;
        }
        UpdateRange(max_dist, &min_max_dist, &max_max_dist);
      }
      if (max_max_dist > global_worst_dist) {
        // Keep a record of the worst pair over all characters/fonts too.
        global_worst_dist = max_max_dist;
        worst_s1 = max_s1;
        worst_s2 = max_s2;
      }
      if (debug) {
        tprintf("Found %d samples of class %d=%s, font %d, "
                "dist range [%g, %g], worst pair= %s, %s\n",
                samples_found, c, unicharset_.debug_str(c).string(),

Review comment (bug):
That fixes a bug, thank you. Could you create a separate pull request for this fix?

Code after:
      int max_s1 = 0;
      int max_s2 = 0;
      fcinfo.canonical_sample = fcinfo.samples[0];
      fcinfo.canonical_dist = 0.0f;
      for (int i = 0; i < fcinfo.samples.size(); ++i) {
        int s1 = fcinfo.samples[i];
        const GenericVector<int>& features1 = samples_[s1]->indexed_features();
        f_table.Set(features1, features1.size(), true);
        double max_dist = 0.0;
        // Run the full squared-order search for similar samples. It is still
        // reasonably fast because f_table.FeatureDistance is fast, but we
        // may have to reconsider if we start playing with too many samples
        // of a single char/font.
        for (int j = 0; j < fcinfo.samples.size(); ++j) {
          int s2 = fcinfo.samples[j];
          if (samples_[s2]->class_id() != c  ||
              samples_[s2]->font_id() != font_id ||
              s2 == s1)
            continue;
          GenericVector<int> features2 = samples_[s2]->indexed_features();
          double dist = f_table.FeatureDistance(features2);
          if (dist > max_dist) {
            max_dist = dist;
            if (dist > max_max_dist) {
              max_s1 = s1;
              max_s2 = s2;
            }
          }
        }
        // Using Set(..., false) is far faster than re initializing, due to
        // the sparseness of the feature space.
        f_table.Set(features1, features1.size(), false);
        samples_[s1]->set_max_dist(max_dist);
        ++samples_found;
        if (max_dist < min_max_dist) {
          fcinfo.canonical_sample = s1;
          fcinfo.canonical_dist = max_dist;
        }
        UpdateRange(max_dist, &min_max_dist, &max_max_dist);
      }
      if (max_max_dist > global_worst_dist) {
        // Keep a record of the worst pair over all characters/fonts too.
        global_worst_dist = max_max_dist;
        worst_s1 = max_s1;
        worst_s2 = max_s2;
      }
      if (debug) {
        tprintf("Found %d samples of class %d=%s, font %d, "
                "dist range [%g, %g], worst pair= %s, %s\n",
                samples_found, c, unicharset_.debug_str(c).string(),
                font_index, min_max_dist, max_max_dist,