<commit_before>/* Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include "myisamdef.h"

#ifdef HAVE_RTREE_KEYS

#include "rt_index.h"
#include "rt_key.h"
#include "rt_mbr.h"

typedef struct
{
  double square;
  int n_node;
  uchar *key;
  double *coords;
} SplitStruct;

inline static double *reserve_coords(double **d_buffer, int n_dim)
{
  double *coords = *d_buffer;
  (*d_buffer) += n_dim * 2;
  return coords;
}

static void mbr_join(double *a, const double *b, int n_dim)
{
  double *end = a + n_dim * 2;
  do
  {
    if (a[0] > b[0])
      a[0] = b[0];

    if (a[1] < b[1])
      a[1] = b[1];

    a += 2;
    b += 2;
  }while (a != end);
}

/*
Counts the square of mbr which is a join of a and b
*/
static double mbr_join_square(const double *a, const double *b, int n_dim)
{
  const double *end = a + n_dim * 2;
  double square = 1.0;
  do
  {
    square *= 
      ((a[1] < b[1]) ? b[1] : a[1]) - ((a[0] > b[0]) ? b[0] : a[0]);

    a += 2;
    b += 2;
  }while (a != end);

   /* Check for infinity or NaN */
  if (my_isinf(square) || isnan(square))
    square = DBL_MAX;

  return square;
}

static double count_square(const double *a, int n_dim)
{
  const double *end = a + n_dim * 2;
  double square = 1.0;
  do
  {
    square *= a[1] - a[0];
    a += 2;
  }while (a != end);
  return square;
}

inline static void copy_coords(double *dst, const double *src, int n_dim)
{
  memcpy(dst, src, sizeof(double) * (n_dim * 2));
}

/* 
Select two nodes to collect group upon
*/
static void pick_seeds(SplitStruct *node, int n_entries, 
     SplitStruct **seed_a, SplitStruct **seed_b, int n_dim)
{
  SplitStruct *cur1;
  SplitStruct *lim1 = node + (n_entries - 1);
  SplitStruct *cur2;
  SplitStruct *lim2 = node + n_entries;

  double max_d = -DBL_MAX;
  double d;

  *seed_a = node;
  *seed_b = node + 1;

  for (cur1 = node; cur1 < lim1; ++cur1)
  {
    for (cur2=cur1 + 1; cur2 < lim2; ++cur2)
    {
      
      d = mbr_join_square(cur1->coords, cur2->coords, n_dim) - cur1->square - 
          cur2->square;
      if (d > max_d)
      {
        max_d = d;
        *seed_a = cur1;
        *seed_b = cur2;
      }
    }
  }
}<commit_msg>Revert "Bug #19573096: LOADING CORRUPTED GEOMETRY DATA INTO A"<commit_after>/* Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include "myisamdef.h"

#ifdef HAVE_RTREE_KEYS

#include "rt_index.h"
#include "rt_key.h"
#include "rt_mbr.h"

typedef struct
{
  double square;
  int n_node;
  uchar *key;
  double *coords;
} SplitStruct;

inline static double *reserve_coords(double **d_buffer, int n_dim)
{
  double *coords = *d_buffer;
  (*d_buffer) += n_dim * 2;
  return coords;
}

static void mbr_join(double *a, const double *b, int n_dim)
{
  double *end = a + n_dim * 2;
  do
  {
    if (a[0] > b[0])
      a[0] = b[0];

    if (a[1] < b[1])
      a[1] = b[1];

    a += 2;
    b += 2;
  }while (a != end);
}

/*
Counts the square of mbr which is a join of a and b
*/
static double mbr_join_square(const double *a, const double *b, int n_dim)
{
  const double *end = a + n_dim * 2;
  double square = 1.0;
  do
  {
    square *= 
      ((a[1] < b[1]) ? b[1] : a[1]) - ((a[0] > b[0]) ? b[0] : a[0]);

    a += 2;
    b += 2;
  }while (a != end);

  return square;
}

static double count_square(const double *a, int n_dim)
{
  const double *end = a + n_dim * 2;
  double square = 1.0;
  do
  {
    square *= a[1] - a[0];
    a += 2;
  }while (a != end);
  return square;
}

inline static void copy_coords(double *dst, const double *src, int n_dim)
{
  memcpy(dst, src, sizeof(double) * (n_dim * 2));
}

/* 
Select two nodes to collect group upon
*/
static void pick_seeds(SplitStruct *node, int n_entries, 
     SplitStruct **seed_a, SplitStruct **seed_b, int n_dim)
{
  SplitStruct *cur1;
  SplitStruct *lim1 = node + (n_entries - 1);
  SplitStruct *cur2;
  SplitStruct *lim2 = node + n_entries;

  double max_d = -DBL_MAX;
  double d;

  for (cur1 = node; cur1 < lim1; ++cur1)
  {
    for (cur2=cur1 + 1; cur2 < lim2; ++cur2)
    {
      
      d = mbr_join_square(cur1->coords, cur2->coords, n_dim) - cur1->square - 
          cur2->square;
      if (d > max_d)
      {
        max_d = d;
        *seed_a = cur1;
        *seed_b = cur2;
      }
    }
  }
}
{'fraction_non_alphanumeric': 0.12377560106856635, 'fraction_numerical': 0.027604630454140695, 'mean_word_length': 2.7691275167785236, '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': '7526045', 'n_tokens_mistral': 2184, 'n_tokens_neox': 2067, 'n_words': 707}