// file: dec_path_0.cc
//
 
// isip include files
//
#include "decoder.h"
#include "decoder_constants.h"
 
// method: backtrace_cc
//
// arguments: none
//
// return: a logical_1 indicating status
//
// this method finds the sentence-end hypotheses
//
logical_1 Decoder::backtrace_cc() {
  
  // dummy variables
  //
  float_4 score = (float_4)0;
  Trace* trace = (Trace*)NULL;
  
  // initialize number of hypotheses
  //
  num_hyps_d = (int_4)0;

  // reset the n-best array
  //
  for (int_4 ff = 0; ff < num_nbest_d; ff++) {
    nbest_d[ff] = (Trace*)NULL;
  }

  // make sure a sentence-end list exists
  //
  if (word_trlist_d[DEC_SENT_END_WORD] == (Link_list*)NULL) {
    return ISIP_FALSE;
  }
  
  // loop over all the active traces in the sentence end list
  //
  for (Link_node* node = word_trlist_d[DEC_SENT_END_WORD]->get_head_cc();
       node != (Link_node*)NULL; node = node->get_next_cc()) {
    
    // get the current trace
    //
    trace = (Trace*)(node->get_item_cc());
    score = trace->get_score_cc();
    
    // sort the n-best array
    //
    for (int_4 j = 0; j < num_nbest_d; j++) {
      
      // if there is a space left in the array insert here
      //
      if (nbest_d[j] == (Trace*)NULL) {
	nbest_d[j] = trace;
	if (num_hyps_d < num_nbest_d) {
	  num_hyps_d++;
	}
	break;
      }
      
      // if the score in the input trace is larger, insert here
      //
      if (nbest_d[j]->get_score_cc() < score) {
	for (int_4 k = num_nbest_d - 1; k > j; k--) {
	  nbest_d[k] = nbest_d[k-1];
	}
	if (num_hyps_d < num_nbest_d) {
	  num_hyps_d++;
	}
	nbest_d[j] = trace;
	break;
      }
    } // end for j
  } // end for link nodes loop
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
