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

Code before:
//
///////////////////////////////////////////////////////////////////////

#include "shapetable.h"

#include "bitvector.h"
#include "fontinfo.h"
#include "intfeaturespace.h"
#include "unicharset.h"
#include "unicity_table.h"

#include <algorithm>

namespace tesseract {

// Helper function to get the index of the first result with the required
// unichar_id. If the results are sorted by rating, this will also be the
// best result with the required unichar_id.
// Returns -1 if the unichar_id is not found
int ShapeRating::FirstResultWithUnichar(const std::vector<ShapeRating> &results,
                                        const ShapeTable &shape_table, UNICHAR_ID unichar_id) {
  for (unsigned r = 0; r < results.size(); ++r) {
    const auto shape_id = results[r].shape_id;
    const Shape &shape = shape_table.GetShape(shape_id);
    if (shape.ContainsUnichar(unichar_id)) {
      return r;
    }
  }
  return -1;
}

// Helper function to get the index of the first result with the required
// unichar_id. If the results are sorted by rating, this will also be the
// best result with the required unichar_id.
// Returns -1 if the unichar_id is not found
int UnicharRating::FirstResultWithUnichar(const std::vector<UnicharRating> &results,
                                          UNICHAR_ID unichar_id) {
  for (unsigned r = 0; r < results.size(); ++r) {
    if (results[r].unichar_id == unichar_id) {
      return r;
    }
  }
  return -1;
}

// Writes to the given file. Returns false in case of error.
bool UnicharAndFonts::Serialize(FILE *fp) const {
  return tesseract::Serialize(fp, &unichar_id) && tesseract::Serialize(fp, font_ids);
}

// Reads from the given file. Returns false in case of error.

Review comment (suggestion):
`if (shape_table.GetShape(result.shape_id).ContainsUnichar(unichar_id)) {`

also can be put inside `std::ranges::find_if()`

Code after:
//
///////////////////////////////////////////////////////////////////////

#include "shapetable.h"

#include "bitvector.h"
#include "fontinfo.h"
#include "intfeaturespace.h"
#include "unicharset.h"
#include "unicity_table.h"

#include <algorithm>

namespace tesseract {

// Helper function to get the index of the first result with the required
// unichar_id. If the results are sorted by rating, this will also be the
// best result with the required unichar_id.
// Returns -1 if the unichar_id is not found
int ShapeRating::FirstResultWithUnichar(const std::vector<ShapeRating> &results,
                                        const ShapeTable &shape_table, UNICHAR_ID unichar_id) {
  size_t r = 0;
  for (const auto &result : results) {
    const auto shape_id = result.shape_id;
    const Shape &shape = shape_table.GetShape(shape_id);
    if (shape.ContainsUnichar(unichar_id)) {
      return r;
    }
    ++r;
  }
  return -1;
}

// Helper function to get the index of the first result with the required
// unichar_id. If the results are sorted by rating, this will also be the
// best result with the required unichar_id.
// Returns -1 if the unichar_id is not found
int UnicharRating::FirstResultWithUnichar(const std::vector<UnicharRating> &results,
                                          UNICHAR_ID unichar_id) {
  size_t r = 0;
  for (const auto &result : results) {
    if (result.unichar_id == unichar_id) {
      return r;
    }
    ++r;
  }
  return -1;
}

// Writes to the given file. Returns false in case of error.
bool UnicharAndFonts::Serialize(FILE *fp) const {