// file: dec_prune_3.cc
//

// isip include files
//
#include "decoder.h"
#include "decoder_constants.h"

// method: score_compare_cc
//
// arguments:
//  float_4* score1 : (input) the first score value
//  float_4* score2 : (input) the second score value
//
// return: an int_4 containing result of comparison
//
// this method compares two word scores
//
static int score_compare_cc(const void* score1_a, const void* score2_a) {

  // result value of comparison
  //
  int_4 value = (int_4)0;
  
  // compare the two and set value
  //
  if (*(float_4*)score1_a >= *(float_4*)score2_a) {
    value = (int_4)-1;
  }
  else if (*(float_4*)score1_a < *(float_4*)score2_a) {
    value = (int_4)1;
  }

  // exit gracefully
  //
  return (int)value;
}

// method: word_cutoff_cc
//
// arguments: none
//
// return: a logical_1 to indicate status
//
// this method sorts all the active word instance scores and finds the
// cut-off score for growing words
//
logical_1 Decoder::word_cutoff_cc() {
  
  // initialize the threshold
  //
  wordend_thresh_d = DEC_DEFAULT_SCORE;
    
  // this is required only if there are active words
  //
  if (num_active_words_d > (int_4)0) {
    
    // create the scores array
    //
    float_4* scores = new float_4[num_active_words_d];
    for (int_4 i = 0; i < num_active_words_d; i++) {
      scores[i] = word_scores_d[active_words_d[i]];
      word_scores_d[active_words_d[i]] = DEC_DEFAULT_SCORE;
    }
    
    // sort all the instances by score if needed
    //
    if ((num_active_words_d > num_wordends_d) && (num_wordends_d > (int_4)0)) {
      
      // now sort this array in descending order according to the trace
      // scores
      //
      qsort(scores, num_active_words_d, sizeof(float_4), score_compare_cc);
      
      // get the score at the cut-off instance
      //
      wordend_thresh_d = scores[num_wordends_d];
    }
    
    // free memory
    //
    delete [] scores;
  }
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
