// file: mod_hmm_1.cc
//

// isip include files
//
#include "model.h"
#include "model_constants.h"

static int_4* tmp_next = (int_4*)NULL;
static int_4 tmp_next_size = 0;

// method: get_next_states_cc
//
// arguments:
//  int_4 state : (input) the state index for source state
//  int_4& num_trans : (output) number of possible transitions
//  int_4*& trans_states : (output) array of possible next state indices
//  float_4*& trans_scores : (output) array of transition scores
//  int_4&  trans_states_size: (input/output) current size of the input vectors
//  
//
// return: logical_1 indicating suuccess
//
// this methods checks all possible transitions from the specified
// state and returns the number of transitions along with the
// coresponding destination state indices and the transition scores
//
logical_1 Model::get_next_states_cc(int_4 state_ind_a, int_4& num_trans_a,
				    int_4*& trans_states_a,
				    float_4*& trans_scores_a,
				    int_4& trans_states_size_a) {
  
  // check the validity of the input state index
  //
  if (state_ind_a > num_states_d - (int_4)1) {
    return ISIP_FALSE;
  }

  if (tmp_next_size < num_states_d) {
    if (tmp_next != (int_4*)NULL) {
      delete [] tmp_next;
    }
    tmp_next = new int_4[num_states_d];
    tmp_next_size = num_states_d;
  }

  // initialize the number of transitions
  //
  num_trans_a = (int_4)0;
  int_4 ind = state_ind_a * num_states_d;

  // now check all transitions from the current state
  //
  for (int_4 i = 0; i < num_states_d; i++) {

    // if this transition exists
    //
    if (trans_prob_d[ind + i] > MODEL_NOTRANS_SCORE) {

      // record this transition and increment transition count
      //
      tmp_next[num_trans_a++] = i;
    }
  }

  // make sure some transitions exist
  //
  if (num_trans_a == 0) {
    return ISIP_FALSE;
  }

  // now copy this to the output array
  //
  if (trans_states_size_a < num_trans_a) {
    if (trans_states_a != (int_4*)NULL) {
      delete [] trans_states_a;
    }
    trans_states_a = new int_4[num_trans_a];

    if (trans_scores_a != (float_4*)NULL) {
      delete [] trans_scores_a;
    }
    trans_scores_a = new float_4[num_trans_a];
    trans_states_size_a = num_trans_a;
  }

  // copy the states and the scores
  //
  for (int_4 i = 0; i < num_trans_a; i++) {
    trans_states_a[i] = tmp_next[i];
    trans_scores_a[i] = trans_prob_d[ind + trans_states_a[i]];
  }
 
  // exit gracefully
  //
  return ISIP_TRUE;
}
