// file: tr_lxn_node_2.cc
//

// isip include files
//
#include "train_lex_node.h"
#include "train_lex_node_constants.h"

// method: add_word_cc
//
// arguments:
//  Train_Lattice_node* latnode : (input) the lattice node of the word to be added
//  float_8 score : (input) the LM score of the word
//
// return: a logical_1 indicating success
//
// this method adds the input word to the current word list and
// updates the max LM score at the node if required
//
logical_1 Train_Lex_node::add_word_cc(Train_Lattice_node* latnode_a, float_8 score_a) {

  // check if the word list exists
  //
  if (word_list_d == (Train_Link_list*)NULL) {
    word_list_d = new Train_Link_list();
  }

  // add the word to the list
  //
  word_list_d->insert_cc(latnode_a);

  // store the incremented number of words and create scratch memory
  //
  int_4 num = num_words_d + (int_4)1;
  float_8* score = new float_8[num];
  
  // add the score to the score list and free old memory
  //
  if (num_words_d > (int_4)0) {
    memcpy(score, score_list_d, num_words_d * sizeof(float_8));
    delete [] score_list_d;
  }
  score[num_words_d] = score_a;

  // copy new memory
  //
  score_list_d = score;
  score = (float_8*)NULL;
  num_words_d = num;
  
  // update the maximum score
  //
  if (max_lm_score_d < score_a) {
    max_lm_score_d = score_a;
  }
    
  // exit gracefully
  //
  return ISIP_TRUE;
}
