// file: dec_path_1.cc
//
 
// isip include files
//
#include "decoder.h"
#include "decoder_constants.h"
 
// method: find_nbest_cc
//
// arguments: none
//
// return: a logical_1 indicating status
//
// this method finds the best word-end hypotheses at the current frame
//
logical_1 Decoder::find_nbest_cc() {

  // dummy variables
  //
  float_4 score = (float_4)0;
  Trace* trace = (Trace*)NULL;
  Link_list* trlist = (Link_list*)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;
  }
      
  // loop over all the active word trace lists
  //
  for (int_4 i = 0; i < num_active_words_d; i++) {

    // make sure the list exists
    //
    trlist = word_trlist_d[active_words_d[i]];
    if (trlist != (Link_list*)NULL) {
      
      // loop over all active traces
      //
      for (Link_node* node = trlist->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 node
    } // end if link list is not null
  } // end for i
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
