<commit_before>#ifndef NWRCDE_COMMON_H
#define NWRCDE_COMMON_H

#include "fastlib/fastlib.h"

class NWRCdeCommon {

 public:

  /** @brief Shuffles a vector according to a given permutation.
   *
   *  @param v The vector to be shuffled.
   *  @param permutation The permutation.
   */
  static void ShuffleAccordingToPermutation
  (Vector &v, const ArrayList<index_t> &permutation) {
    
    Vector v_tmp;
    v_tmp.Init(v.length());
    for(index_t i = 0; i < v_tmp.length(); i++) {
      v_tmp[i] = v[permutation[i]];
    }
    v.CopyValues(v_tmp);
  }

  template<typename Tree1, typename Tree2>
  static void Heuristic
  (Tree1 *nd, Tree2 *nd1, Tree2 *nd2, double probability, 
   Tree2 **partner1, double *probability1, 
   Tree2 **partner2, double *probability2) {
    
    double d1 = nd->bound().MinDistanceSq(nd1->bound());
    double d2 = nd->bound().MinDistanceSq(nd2->bound());
    
    // Prioritized traversal based on the squared distance bounds.
    if(d1 <= d2) {
      *partner1 = nd1;
      *probability1 = sqrt(probability);
      *partner2 = nd2;
      *probability2 = sqrt(probability);
    }
    else {
      *partner1 = nd2;
      *probability1 = sqrt(probability);
      *partner2 = nd1;
      *probability2 = sqrt(probability);
    }
  }

  template<typename TGlobal, typename QueryTree, typename ReferenceTree,
	   typename TQueryResult, typename TDelta>
  static void ApplySeriesExpansion(const TGlobal &globals, const Matrix &qset,
				   QueryTree *qnode, ReferenceTree *rnode, 
				   TQueryResult &query_results,
				   const TDelta &delta) {

    switch(delta.nwr_numerator.approx_type) {
      case TDelta::FAR_TO_LOCAL:
	rnode->stat().nwr_numerator_farfield_expansion.TranslateToLocal
	  (qnode->stat().nwr_numerator_local_expansion, 
	   delta.nwr_numerator.order_farfield_to_local);
	query_results.num_far_to_local_prunes++;
	break;
      case TDelta::DIRECT_FARFIELD:
	for(index_t q = qnode->begin(); q < qnode->end(); q++) {
	  query_results.nwr_numerator_sum_e[q] += 
	    rnode->stat().nwr_numerator_farfield_expansion.EvaluateField
	    (qset, q, delta.nwr_numerator.order_farfield);
	}
	query_results.num_direct_far_prunes++;
	break;
      case TDelta::DIRECT_LOCAL:
	qnode->stat().nwr_numerator_local_expansion.AccumulateCoeffs
	  (globals.rset, globals.nwr_numerator_weights, rnode->begin(), 
	   rnode->end(), delta.nwr_numerator.order_local);
	query_results.num_direct_local_prunes++;
	break;
      default:
	break;
    }
    switch(delta.nwr_denominator.approx_type) {
      case TDelta::FAR_TO_LOCAL:
	rnode->stat().nwr_denominator_farfield_expansion.TranslateToLocal
	  (qnode->stat().nwr_denominator_local_expansion, 
	   delta.nwr_denominator.order_farfield_to_local);
	query_results.num_far_to_local_prunes++;
	break;
      case TDelta::DIRECT_FARFIELD:
	for(index_t q = qnode->begin(); q < qnode->end(); q++) {
	  query_results.nwr_denominator_sum_e[q] += 
	    rnode->stat().nwr_denominator_farfield_expansion.EvaluateField
	    (qset, q, delta.nwr_denominator.order_farfield);
	}
	query_results.num_direct_far_prunes++;
	break;
      case TDelta::DIRECT_LOCAL:
	qnode->stat().nwr_denominator_local_expansion.AccumulateCoeffs
	  (globals.rset, globals.nwr_denominator_weights, rnode->begin(),
	   rnode->end(), delta.nwr_numerator.order_local);
	query_results.num_direct_local_prunes++;
	break;
      default:
	break;
    }

    // Lastly, we need to add on the rest of the delta contributions.
    qnode->stat().postponed.ApplyDelta(delta);
  }

  template<typename TGlobal, typename QueryTree, typename ReferenceTree,
	   typename TQueryResult, typename TQuerySummary, typename TDelta,<commit_msg>Critical bug fix.<commit_after>#ifndef NWRCDE_COMMON_H
#define NWRCDE_COMMON_H

#include "fastlib/fastlib.h"

class NWRCdeCommon {

 public:

  /** @brief Shuffles a vector according to a given permutation.
   *
   *  @param v The vector to be shuffled.
   *  @param permutation The permutation.
   */
  static void ShuffleAccordingToPermutation
  (Vector &v, const ArrayList<index_t> &permutation) {
    
    Vector v_tmp;
    v_tmp.Init(v.length());
    for(index_t i = 0; i < v_tmp.length(); i++) {
      v_tmp[i] = v[permutation[i]];
    }
    v.CopyValues(v_tmp);
  }

  template<typename Tree1, typename Tree2>
  static void Heuristic
  (Tree1 *nd, Tree2 *nd1, Tree2 *nd2, double probability, 
   Tree2 **partner1, double *probability1, 
   Tree2 **partner2, double *probability2) {
    
    double d1 = nd->bound().MinDistanceSq(nd1->bound());
    double d2 = nd->bound().MinDistanceSq(nd2->bound());
    
    // Prioritized traversal based on the squared distance bounds.
    if(d1 <= d2) {
      *partner1 = nd1;
      *probability1 = sqrt(probability);
      *partner2 = nd2;
      *probability2 = sqrt(probability);
    }
    else {
      *partner1 = nd2;
      *probability1 = sqrt(probability);
      *partner2 = nd1;
      *probability2 = sqrt(probability);
    }
  }

  template<typename TGlobal, typename QueryTree, typename ReferenceTree,
	   typename TQueryResult, typename TDelta>
  static void ApplySeriesExpansion(const TGlobal &globals, const Matrix &qset,
				   QueryTree *qnode, ReferenceTree *rnode, 
				   TQueryResult &query_results,
				   const TDelta &delta) {

    switch(delta.nwr_numerator.approx_type) {
      case TDelta::FAR_TO_LOCAL:
	DEBUG_ASSERT(delta.nwr_numerator.order_farfield_to_local >= 0);
	rnode->stat().nwr_numerator_farfield_expansion.TranslateToLocal
	  (qnode->stat().nwr_numerator_local_expansion, 
	   delta.nwr_numerator.order_farfield_to_local);
	query_results.num_far_to_local_prunes++;
	break;
      case TDelta::DIRECT_FARFIELD:
	DEBUG_ASSERT(delta.nwr_numerator.order_farfield >= 0);
	for(index_t q = qnode->begin(); q < qnode->end(); q++) {
	  query_results.nwr_numerator_sum_e[q] += 
	    rnode->stat().nwr_numerator_farfield_expansion.EvaluateField
	    (qset, q, delta.nwr_numerator.order_farfield);
	}
	query_results.num_direct_far_prunes++;
	break;
      case TDelta::DIRECT_LOCAL:
	DEBUG_ASSERT(delta.nwr_numerator.order_local >= 0);
	qnode->stat().nwr_numerator_local_expansion.AccumulateCoeffs
	  (globals.rset, globals.nwr_numerator_weights, rnode->begin(), 
	   rnode->end(), delta.nwr_numerator.order_local);
	query_results.num_direct_local_prunes++;
	break;
      default:
	break;
    }
    switch(delta.nwr_denominator.approx_type) {
      case TDelta::FAR_TO_LOCAL:
	DEBUG_ASSERT(delta.nwr_denominator.order_farfield_to_local >= 0);
	rnode->stat().nwr_denominator_farfield_expansion.TranslateToLocal
	  (qnode->stat().nwr_denominator_local_expansion, 
	   delta.nwr_denominator.order_farfield_to_local);
	query_results.num_far_to_local_prunes++;
	break;
      case TDelta::DIRECT_FARFIELD:
	DEBUG_ASSERT(delta.nwr_denominator.order_farfield >= 0);
	for(index_t q = qnode->begin(); q < qnode->end(); q++) {
	  query_results.nwr_denominator_sum_e[q] += 
	    rnode->stat().nwr_denominator_farfield_expansion.EvaluateField
	    (qset, q, delta.nwr_denominator.order_farfield);
	}
	query_results.num_direct_far_prunes++;
	break;
      case TDelta::DIRECT_LOCAL:
	DEBUG_ASSERT(delta.nwr_denominator.order_local >= 0);
	qnode->stat().nwr_denominator_local_expansion.AccumulateCoeffs
	  (globals.rset, globals.nwr_denominator_weights, rnode->begin(),
	   rnode->end(), delta.nwr_denominator.order_local);
	query_results.num_direct_local_prunes++;
	break;
      default:
	break;
    }

    // Lastly, we need to add on the rest of the delta contributions.
    qnode->stat().postponed.ApplyDelta(delta);
  }

  template<typename TGlobal, typename QueryTree, typename ReferenceTree,
	   typename TQueryResult, typename TQuerySummary, typename TDelta,
{'fraction_non_alphanumeric': 0.11535453597497393, 'fraction_numerical': 0.009124087591240875, 'mean_word_length': 4.226839237057221, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 57, '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': '4773055', 'n_tokens_mistral': 2873, 'n_tokens_neox': 2720, 'n_words': 467}