// file: inst_tok_2.cc
//
 
// isip include files
//
#include "instance.h"
#include "instance_constants.h"
 
// method: insert_state_cc
//
// arguments:
//  Trace* trace: (input) the trace to be inserted
//  int_4 state: (input) the state index of the input trace
//  logical_1 flag: (input) indicator for alignment mode
//  float_4& score: (output) the best path score
//  
// return: a logical_1 indicating status
//
// this method inserts the given trace in the specified state token
// trace list
//
logical_1 Instance::insert_trace_cc(Trace* trace_a, int_4 state_a,
				    logical_1 flag_a, float_4& score_a) {

  // dummy variables
  //
  logical_1 flg= ISIP_FALSE;
  Token* tok = (Token*)NULL;

  // allocate token list if one doesn't exist yet
  //
  if (token_list_d == (Link_list*)NULL) {
    token_list_d = new Link_list();
  }
  
  // loop over all states i.e. tokens for this phone
  //
  for (Link_node* nd = token_list_d->get_head_cc(); nd != (Link_node*)NULL;
       nd = nd->get_next_cc()) {

    // get the token
    //
    tok = (Token*)(nd->get_item_cc());
    
    // if a token for this state exists
    //
    if (tok->get_state_cc() == state_a) {
      flg= ISIP_TRUE;
      break;
    }
  }
	      
  // otherwise create a token for this state
  //
  if (flg== ISIP_FALSE) {
    tok = new Token(state_a);
    token_list_d->insert_cc(tok);
  }

  // insert the trace in this token
  //
  flg= tok->insert_trace_cc(trace_a, flag_a, score_a);

  // exit gracefully
  //
  return flg;
}
