<commit_before>#include "intermediate_result.hpp"

#include "../indexer/feature_utils.hpp"
#include "../indexer/mercator.hpp"

#include "../geometry/angles.hpp"
#include "../geometry/distance_on_sphere.hpp"

#include "../base/string_utils.hpp"
#include "../base/logging.hpp"


namespace search
{
namespace impl
{

IntermediateResult::IntermediateResult(m2::RectD const & viewportRect,
                                       FeatureType const & f,
                                       string const & displayName,
                                       string const & regionName)
  : m_str(displayName), m_region(regionName),
    m_rect(feature::GetFeatureViewport(f)),
    m_resultType(RESULT_FEATURE)
{
  FeatureType::GetTypesFn types;
  f.ForEachTypeRef(types);
  ASSERT_GREATER(types.m_size, 0, ());
  m_type = types.m_types[0];

  m_distance = ResultDistance(viewportRect.Center(), m_rect.Center());
  m_direction = ResultDirection(viewportRect.Center(), m_rect.Center());
  m_searchRank = feature::GetSearchRank(f);
}

IntermediateResult::IntermediateResult(m2::RectD const & viewportRect, string const & regionName,
                                       double lat, double lon, double precision)
  : m_str("(" + strings::to_string(lat) + ", " + strings::to_string(lon) + ")"),
    m_region(regionName),
    m_rect(MercatorBounds::LonToX(lon - precision), MercatorBounds::LatToY(lat - precision),
           MercatorBounds::LonToX(lon + precision), MercatorBounds::LatToY(lat + precision)),
    m_type(0), m_resultType(RESULT_LATLON), m_searchRank(0)
{
  m_distance = ResultDistance(viewportRect.Center(), m_rect.Center());
  m_direction = ResultDirection(viewportRect.Center(), m_rect.Center());
}

IntermediateResult::IntermediateResult(string const & name, int penalty)
  : m_str(name), m_completionString(name + " "),
    m_distance(0), m_direction(0),
    m_resultType(RESULT_CATEGORY),
    m_searchRank(0)
{
}

bool IntermediateResult::LessOrderF::operator()
          (IntermediateResult const & r1, IntermediateResult const & r2) const
{
  if (r1.m_resultType != r2.m_resultType)
    return (r1.m_resultType < r2.m_resultType);

  if (r1.m_searchRank != r2.m_searchRank)
    return (r1.m_searchRank > r2.m_searchRank);

  return (r1.m_distance < r2.m_distance);
}

Result IntermediateResult::GenerateFinalResult() const
{
  switch (m_resultType)
  {
  case RESULT_FEATURE:
    return Result(m_str
              #ifdef DEBUG
                  + ' ' + strings::to_string(static_cast<int>(m_searchRank))
              #endif
                  , m_region, m_type, m_rect, m_distance, m_direction);
  case RESULT_LATLON:
    return Result(m_str, m_region, 0, m_rect, m_distance, m_direction);
  case RESULT_CATEGORY:
    return Result(m_str, m_completionString);
  default:
    ASSERT(false, ());
    return Result(m_str, m_completionString);
  }
}

double IntermediateResult::ResultDistance(m2::PointD const & a, m2::PointD const & b)
{
  return ms::DistanceOnEarth(MercatorBounds::YToLat(a.y), MercatorBounds::XToLon(a.x),
                             MercatorBounds::YToLat(b.y), MercatorBounds::XToLon(b.x));
}

double IntermediateResult::ResultDirection(m2::PointD const & a, m2::PointD const & b)
{
  return ang::AngleTo(a, b);
}

bool IntermediateResult::StrictEqualF::operator()(IntermediateResult const & r) const
{
  if (m_r.m_resultType == r.m_resultType && m_r.m_resultType == RESULT_FEATURE)
  {
    if (m_r.m_str == r.m_str && m_r.m_region == r.m_region && m_r.m_type == r.m_type)
    {
      return fabs(m_r.m_distance - r.m_distance) < 500.0;
    }
  }

  return false;
}

namespace
{
  uint8_t FirstLevelIndex(uint32_t t)
  {
    uint8_t v;
    CHECK(ftype::GetValue(t, 0, v), (t));
    return v;
  }

  class IsLinearChecker
  {
    static size_t const m_count = 1;
    uint8_t m_index[m_count];

  public:
    IsLinearChecker()
    {
      char const * arr[m_count] = { "highway" };

      ClassifObject const * c = classif().GetRoot();
      for (size_t i = 0; i < m_count; ++i)
        m_index[i] = static_cast<uint8_t>(c->BinaryFind(arr[i]).GetIndex());<commit_msg>[search] Minor fix.<commit_after>#include "intermediate_result.hpp"

#include "../indexer/feature_utils.hpp"
#include "../indexer/mercator.hpp"

#include "../geometry/angles.hpp"
#include "../geometry/distance_on_sphere.hpp"

#include "../base/string_utils.hpp"
#include "../base/logging.hpp"


namespace search
{
namespace impl
{

IntermediateResult::IntermediateResult(m2::RectD const & viewportRect,
                                       FeatureType const & f,
                                       string const & displayName,
                                       string const & regionName)
  : m_str(displayName), m_region(regionName),
    m_rect(feature::GetFeatureViewport(f)),
    m_resultType(RESULT_FEATURE)
{
  FeatureType::GetTypesFn types;
  f.ForEachTypeRef(types);
  ASSERT_GREATER(types.m_size, 0, ());
  m_type = types.m_types[0];

  m_distance = ResultDistance(viewportRect.Center(), m_rect.Center());
  m_direction = ResultDirection(viewportRect.Center(), m_rect.Center());
  m_searchRank = feature::GetSearchRank(f);
}

IntermediateResult::IntermediateResult(m2::RectD const & viewportRect, string const & regionName,
                                       double lat, double lon, double precision)
  : m_str("(" + strings::to_string(lat) + ", " + strings::to_string(lon) + ")"),
    m_region(regionName),
    m_rect(MercatorBounds::LonToX(lon - precision), MercatorBounds::LatToY(lat - precision),
           MercatorBounds::LonToX(lon + precision), MercatorBounds::LatToY(lat + precision)),
    m_type(0), m_resultType(RESULT_LATLON), m_searchRank(0)
{
  m_distance = ResultDistance(viewportRect.Center(), m_rect.Center());
  m_direction = ResultDirection(viewportRect.Center(), m_rect.Center());
}

IntermediateResult::IntermediateResult(string const & name, int penalty)
  : m_str(name), m_completionString(name + " "),
    m_distance(0), m_direction(0),
    m_resultType(RESULT_CATEGORY),
    m_searchRank(0)
{
}

bool IntermediateResult::LessOrderF::operator()
          (IntermediateResult const & r1, IntermediateResult const & r2) const
{
  if (r1.m_resultType != r2.m_resultType)
    return (r1.m_resultType < r2.m_resultType);

  if (r1.m_searchRank != r2.m_searchRank)
    return (r1.m_searchRank > r2.m_searchRank);

  return (r1.m_distance < r2.m_distance);
}

Result IntermediateResult::GenerateFinalResult() const
{
  switch (m_resultType)
  {
  case RESULT_FEATURE:
    return Result(m_str
              #ifdef DEBUG
                  + ' ' + strings::to_string(static_cast<int>(m_searchRank))
              #endif
                  , m_region, m_type, m_rect, m_distance, m_direction);
  case RESULT_LATLON:
    return Result(m_str, m_region, 0, m_rect, m_distance, m_direction);
  case RESULT_CATEGORY:
    return Result(m_str, m_completionString);
  default:
    ASSERT(false, ());
    return Result(m_str, m_completionString);
  }
}

double IntermediateResult::ResultDistance(m2::PointD const & a, m2::PointD const & b)
{
  return ms::DistanceOnEarth(MercatorBounds::YToLat(a.y), MercatorBounds::XToLon(a.x),
                             MercatorBounds::YToLat(b.y), MercatorBounds::XToLon(b.x));
}

double IntermediateResult::ResultDirection(m2::PointD const & a, m2::PointD const & b)
{
  return ang::AngleTo(a, b);
}

bool IntermediateResult::StrictEqualF::operator()(IntermediateResult const & r) const
{
  if (m_r.m_resultType == r.m_resultType && m_r.m_resultType == RESULT_FEATURE)
  {
    if (m_r.m_str == r.m_str && m_r.m_region == r.m_region && m_r.m_type == r.m_type)
    {
      return fabs(m_r.m_distance - r.m_distance) < 500.0;
    }
  }

  return false;
}

namespace
{
  uint8_t FirstLevelIndex(uint32_t t)
  {
    uint8_t v;
    VERIFY ( ftype::GetValue(t, 0, v), (t) );
    return v;
  }

  class IsLinearChecker
  {
    static size_t const m_count = 1;
    uint8_t m_index[m_count];

  public:
    IsLinearChecker()
    {
      char const * arr[m_count] = { "highway" };

      ClassifObject const * c = classif().GetRoot();
      for (size_t i = 0; i < m_count; ++i)
        m_index[i] = static_cast<uint8_t>(c->BinaryFind(arr[i]).GetIndex());
{'fraction_non_alphanumeric': 0.13395904436860068, 'fraction_numerical': 0.009507557289127255, 'mean_word_length': 3.5356550580431176, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '902465', 'n_tokens_mistral': 2873, 'n_tokens_neox': 2827, 'n_words': 480}