// file: ht_trans_1.cc
//

// isip include files
//
#include "hmm_train.h"
#include "hmm_train_constants.h"

// method: count_arc_cc;
//
// argument:
// Train_Trace* tr : (input) the current trace
// Train_Trace* pre_tr : (input) the previous trace
// int_4*** trans : (output) the array used to count the arc for phone-internal
//                           states
// int_4* trans_map : (input) the mapping from the model index to the
//                            transition index
// int_4* mod_map : (input) the mapping from the phone index to the model index
// Train_Model** models : (input) the models array
//
// return: a logical_1 flag indicating success
//
// this method checks the input pair of trace to if they belong to the same
// phone and update the arcs if necessary
//
logical_1 count_arc_cc(Train_Trace* tr_a, Train_Trace* pre_tr_a, int_4*** trans_a,
		       int_4* trans_map_a, int_4* mod_map_a,
		       Train_Model** models_a) {

  // local variables
  //
  int_4 ph_ind;  
  int_4 pre_ph_ind;
  int_4 st_ind;
  int_4 pre_st_ind;
  int_4 trans_ind;
  int_4 pre_trans_ind;
  int_4 mod_ind;
  int_4 num_states;
  
  // check the phone index
  //
  ph_ind = tr_a->get_phone_ind_cc();
  pre_ph_ind = pre_tr_a->get_phone_ind_cc();
  mod_ind = mod_map_a[ph_ind];
  num_states = models_a[mod_ind]->get_num_states_cc();
  
  if (tr_a->get_lex_node_cc() != pre_tr_a->get_lex_node_cc()) {

    // update the 0-1 transition of new phone and 3-4 transition of prev
    // phone
    //
    trans_ind = trans_map_a[mod_map_a[ph_ind]];
    pre_trans_ind = trans_map_a[mod_map_a[pre_ph_ind]];
    trans_a[trans_ind][0][1] += (int_4)1;
    pre_st_ind = (int_4)pre_tr_a->get_state_ind_cc();
    trans_a[pre_trans_ind][pre_st_ind][num_states-1] += (int_4)1;
    
    // exit ungracefully
    //
    return ISIP_FALSE;
  }
  
  else {
    
    // get the state index from the traces
    //
    st_ind = (int_4)tr_a->get_state_ind_cc();
    pre_st_ind = (int_4)pre_tr_a->get_state_ind_cc();
    
    // update the count of arc
    //
    trans_ind = trans_map_a[mod_map_a[ph_ind]];
    trans_a[trans_ind][pre_st_ind][st_ind]++;
  }
  
  // exit gracefully
  //
  return ISIP_TRUE;
}

