// file: tr_tr_path_4.cc
//

// isip include files
//
#include "train_trace.h"
#include "train_trace_constants.h"

// method: compare_cc
//
// arguments:
//  Train_Trace* trace : (input) the other trace
//
// return: a Train_Trace* containing the better trace
//
// this method checks if two traces can be compared, and if they are
// at the same level and can be compared, it returns which of them is
// better
//
Train_Trace* Train_Trace::compare_cc(Train_Trace* trace_a) {

  // dummy variables
  //
  Train_Trace* better_path = (Train_Trace*)NULL;
  logical_1 flag = ISIP_FALSE;
  int_4 lxp = (int_4)0;
  int_4 lxp1 = (int_4)0;

  // make sure the two traces are at the same level
  //
  if (level_d != trace_a->level_d) {
    return better_path;
  }

  // if these are state level traces
  //
  if (level_d == TRAIN_TRACE_STATE_LEVEL) {
  
    // make sure these traces pertain to the same position in the
    // linguistic search space
    //
    if (lat_node_d ==  trace_a->lat_node_d) {
      
      // compare only if the lexical nodes are the same
      //
      if (lex_node_d == trace_a->lex_node_d) {
	flag = ISIP_TRUE;
      }

      // otherwise check if this is end of word
      //
      else if (lat_node_d->get_word_cc()->get_index_cc()
	       != TRAIN_WRD_SENT_END_WORD) {
	
	// get the phone in the current lexical node
	//
	lxp = lex_node_d->get_phone_cc();
	lxp1 = trace_a->lex_node_d->get_phone_cc();
	
	// if this is the end of word can compare traces directly
	//
	if ((lxp == TRAIN_LXN_STOP_PHONE) || (lxp == TRAIN_LXN_SP_PHONE)) {
	  if (lxp1 == lxp) {
	    flag = ISIP_TRUE;
	  }
	}
      } // end else
    } // end if same lattice node
  } // end if state level

  // if these are phone-level traces they have the same triphone
  //
  if (level_d == TRAIN_TRACE_PHONE_LEVEL) {

    // make sure it is the same lattice node
    //
    if (lat_node_d == trace_a->lat_node_d) {
      
      // compare only if the lexical nodes are the same
      //
      if (lex_node_d == trace_a->lex_node_d) {
	flag = ISIP_TRUE;
      }
      
      // otherwise check if this is a word end triphone
      //
      else if (lat_node_d->get_word_cc()->get_index_cc()
	       != TRAIN_WRD_SENT_END_WORD) {
	
	// get the monophone in the current lexical node
	//
	lxp = lex_node_d->get_phone_cc();
	lxp1 = trace_a->lex_node_d->get_phone_cc();
	
	// if this is an end of word triphone can compare
	//
	if ((lxp == TRAIN_LXN_STOP_PHONE) || (lxp == TRAIN_LXN_SP_PHONE)) {
	  if ((lxp1 == TRAIN_LXN_STOP_PHONE) || (lxp1 == TRAIN_LXN_SP_PHONE)) {
	    flag = ISIP_TRUE;
	  }
	}
      } // end else
    } // end if same lattice node    
  } // end if phone level
    
  // if these are word-level traces need only to check triphone
  //
  if (level_d == TRAIN_TRACE_WORD_LEVEL) {

    // compare the triphone index
    //
    if (phone_index_d == trace_a->phone_index_d) {
      flag = ISIP_TRUE;
    }    
  } // end if word level
  
  // now compare the two traces by score if allowed
  //
  if (flag == ISIP_TRUE) {
    if (score_d > trace_a->score_d) {
      better_path = this;
    }
    else {
      better_path = trace_a;
    }
  } // end if flag is true

  // exit gracefully
  //
  return better_path;
}
