<commit_before>#include "./cost_function_calculator.h"
#include <thrust/transform_reduce.h>
#include <limits>
#include <tuple>

struct EvalResult
{
  __host__ __device__ EvalResult()
  {
  }

  __host__ __device__ EvalResult(int x, int y, float cost)
    : x(x), y(y), cost(cost)
  {
  }

  int x, y;

  float cost;

  bool operator<(const EvalResult &other)
  {
    return thrust::min<float>(this->cost, other.cost);
  }
};

__host__ __device__ bool operator<(const EvalResult &a, const EvalResult &b)
{
  return (a.cost < b.cost);
}

struct CostEvaluator : public thrust::unary_function<int, EvalResult>
{
  __host__ __device__ CostEvaluator(int width, int height)
    : width(width), height(height)
  {
  }

  int width;
  int height;

  int halfLabelWidth;
  int halfLabelHeight;

  float anchorX;
  float anchorY;

  cudaTextureObject_t constraints;
  const float *occupancy;

  const float constraintViolationCost = 1e100f;

  __device__ float lineLength(int x, int y) const
  {
    float diffX = x - anchorX;
    float diffY = y - anchorY;

    return sqrt(diffX * diffX + diffY * diffY);
  }

  __device__ float favorHorizontalOrVerticalLines(int x, int y) const
  {
    float diffX = x - anchorX;
    float diffY = y - anchorY;

    float length = sqrt(diffX * diffX + diffY * diffY);
    diffX = diffX / length;
    diffY = diffY / length;

    return fabs(diffX) + fabs(diffY);
  }

  __device__ float occupancyForLabelArea(int x, int y) const
  {
    int startX = max(x - halfLabelWidth - 1, 0);
    int startY = max(y - halfLabelWidth - 1, 0);
    int endX = min(x + halfLabelWidth, width - 1);
    int endY = min(y + halfLabelWidth, height - 1);

    float lowerRight = occupancy[endY * width + endX];
    float lowerLeft = occupancy[endY * width + startX];
    float upperLeft = occupancy[startY * width + startX];
    float upperRight = occupancy[startY * width + endX];
    float sum = lowerRight + upperLeft - lowerLeft - upperRight;

    // this can be the case through inaccuracies
    if (sum < 0.0f)
      return 0.0f;

    return sum / ((endX - startX) * (endY - startY));
  }

  __device__ EvalResult operator()(const int &index) const
  {
    int x = index % width;
    int y = index / width;

    unsigned char constraintValue =
        tex2D<unsigned char>(constraints, x + 0.5f, y + 0.5f);
    if (constraintValue)
    {
      EvalResult result(x, y, constraintViolationCost);
      return result;
    }

    float distanceToAnchor = lineLength(x, y);

    float cost = occupancyForLabelArea(x, y) + 1e-3f * distanceToAnchor +
                 1e-1f * favorHorizontalOrVerticalLines(x, y);
    EvalResult result(x, y, cost);

    return result;
  }
};

template <typename T>
struct MinimumCostOperator : public thrust::binary_function<T, T, T>
{
  __host__ __device__ T operator()(const T &x, const T &y) const
  {
    T result;

    result = x < y ? x : y;
    return result;
  }
};
<commit_msg>Ignore false linting errors in cu file.<commit_after>#include "./cost_function_calculator.h"
#include <thrust/transform_reduce.h>
#include <limits>
#include <tuple>

struct EvalResult
{
  __host__ __device__ EvalResult()
  {
  }

  __host__ __device__ EvalResult(int x, int y, float cost)
    : x(x), y(y), cost(cost)
  {
  }

  int x, y;

  float cost;

  bool operator<(const EvalResult &other)
  {
    return thrust::min<float>(this->cost, other.cost);
  }
};

__host__ __device__ bool operator<(const EvalResult &a, const EvalResult &b)
{
  return (a.cost < b.cost);
}

struct CostEvaluator : public thrust::unary_function<int, EvalResult>
{
  __host__ __device__ CostEvaluator(int width, int height)
    : width(width), height(height)
  {
  }

  int width;
  int height;

  int halfLabelWidth;
  int halfLabelHeight;

  float anchorX;
  float anchorY;

  cudaTextureObject_t constraints;
  const float *occupancy;

  const float constraintViolationCost = 1e100f;

  __device__ float lineLength(int x, int y) const
  {
    float diffX = x - anchorX;
    float diffY = y - anchorY;

    return sqrt(diffX * diffX + diffY * diffY);
  }

  __device__ float favorHorizontalOrVerticalLines(int x, int y) const
  {
    float diffX = x - anchorX;
    float diffY = y - anchorY;

    float length = sqrt(diffX * diffX + diffY * diffY);
    diffX = diffX / length;
    diffY = diffY / length;

    return fabs(diffX) + fabs(diffY);
  }

  __device__ float occupancyForLabelArea(int x, int y) const
  {
    int startX = max(x - halfLabelWidth - 1, 0);
    int startY = max(y - halfLabelWidth - 1, 0);
    int endX = min(x + halfLabelWidth, width - 1);   // NOLINT
    int endY = min(y + halfLabelWidth, height - 1);  // NOLINT

    float lowerRight = occupancy[endY * width + endX];
    float lowerLeft = occupancy[endY * width + startX];
    float upperLeft = occupancy[startY * width + startX];
    float upperRight = occupancy[startY * width + endX];
    float sum = lowerRight + upperLeft - lowerLeft - upperRight;

    // this can be the case through inaccuracies
    if (sum < 0.0f)
      return 0.0f;

    return sum / ((endX - startX) * (endY - startY));
  }

  __device__ EvalResult operator()(const int &index) const
  {
    int x = index % width;
    int y = index / width;

    unsigned char constraintValue =
        tex2D<unsigned char>(constraints, x + 0.5f, y + 0.5f);
    if (constraintValue)
    {
      EvalResult result(x, y, constraintViolationCost);
      return result;
    }

    float distanceToAnchor = lineLength(x, y);

    float cost = occupancyForLabelArea(x, y) + 1e-3f * distanceToAnchor +
                 1e-1f * favorHorizontalOrVerticalLines(x, y);
    EvalResult result(x, y, cost);

    return result;
  }
};

template <typename T>
struct MinimumCostOperator : public thrust::binary_function<T, T, T>  // NOLINT
{
  __host__ __device__ T operator()(const T &x, const T &y) const
  {
    T result;

    result = x < y ? x : y;
    return result;
  }
};

{'fraction_non_alphanumeric': 0.11052809749492214, 'fraction_numerical': 0.007786052809749492, 'mean_word_length': 3.0252043596730247, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 21, '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': '120523', 'n_tokens_mistral': 2084, 'n_tokens_neox': 1987, 'n_words': 579}