// file: ht_nbest_1.cc
//
// isip include files
//
#include "hmm_train.h"
#include "hmm_train_constants.h"

// method: backtrace_cc
//
// arguments:
//  Train_Link_node* marker : (input) trace position marker
//  int_4 num_nbest : (input) number of best hypotheses requested
//  int_4& num_hyp : (output) number of best hypotheses returned
//  Train_Trace** array : (output) best hypotheses array
//
// return: a logical_1 flag indicating success
//
// this method finds the n-best sentence-end hypotheses
//
logical_1 backtrace_cc(Train_Link_node* marker_a, int_4 num_nbest_a,
		       int_4& num_hyp_a, Train_Trace** array_a) {

  // dummy variables
  //
  Train_Trace* trace = (Train_Trace*)NULL;
  
  // initialize number of hypotheses
  //
  num_hyp_a = (int_4)0;

  // loop over all the active traces in the sentence end list
  //
  for (Train_Link_node* node = marker_a; node != (Train_Link_node*)NULL;
       node = node->get_next_cc()) {
    
    // get the current trace
    //
    trace = (Train_Trace*)(node->get_item_cc());
    
    // sort the n-best array
    //
    for (int_4 j = 0; j < num_nbest_a; j++) {
      
      // if there is a space left in the array insert here
      //
      if (array_a[j] == (Train_Trace*)NULL) {
	array_a[j] = trace;
	if (num_hyp_a < num_nbest_a) {
	  num_hyp_a++;
	}
	break;
      }
      
      // if the score in the input trace is larger, insert here
      //
      if (array_a[j]->get_score_cc() < trace->get_score_cc()) {
	for (int_4 k = num_nbest_a - 1; k > j; k--) {
	  array_a[k] = array_a[k-1];
	}
	if (num_hyp_a < num_nbest_a) {
	  num_hyp_a++;
	}
	array_a[j] = trace;
	break;
      }
    } // end for j
  } // end for link nodes loop
  
  // return gracefully
  //
  return ISIP_TRUE;
}




