// file: dec_hist_0.cc
//

// isip include files
//
#include "decoder.h"
#include "decoder_constants.h"

// method: next_histwords_cc
//
// arguments:
//  void_p* histwords: (input) the pointers to old history words
//  void_p curr_node: (input) lattice node or unigram node which includes the
//                    immediate previous word
//
// return: a new history words pointer
//
// this method gets the next history words based on the previous history words
// and the current node
//
void_p* Decoder::next_histwords_cc(void_p* histwords_a, void_p curr_node_a) {

  // initialize the new histwords
  //
  void_p* newhist_words = (void_p*)NULL;
  
  // no history words (this is true for ngram startnode)
  //
  if (curr_node_a == (void_p)NULL) {
    return newhist_words;
  }

  // allocate memory for histwords (the length varies with the decoding mode)
  //  
  int_4 hist_len = History::get_hist_len_cc();

  newhist_words = new void_p[hist_len];
  newhist_words[0] = curr_node_a;
  
  // if lattice rescoring mode or low ngram mode, exit now
  //
  if (ngram_order_d <= 2) {
    
    // exit gracefully
    //
    return newhist_words;
  }

  // we need more history words for lattice_verification /ngram decoding /
  // lattice generation mode and ngram order >= 3
  //
  Word* curword = (Word*)NULL;
  
  if (function_mode_d == DEC_LATTICE_VERIFY_FUNCTION) {
    
    // use part of the old histwords as the N-2 previous words
    //
    if (histwords_a != (void_p*)NULL) {
      
      curword = ((Lattice_node*)histwords_a[0])->get_word_cc();
	
      // make sure this is not the dummy lat start node
      //
      if (curword != (Word*)NULL) {
	newhist_words[1] = (void_p)curword;
      }
      else {
	newhist_words[1] = (void_p)NULL;
      }
	
      // if more history words are needed, move the Word pointers
      //
      for (int_4 i = 1; i < ngram_order_d-2; i++) {
	newhist_words[i+1] = histwords_a[i];
      }
    }
    // use the !NULL dummy word to fill in the history
    //
    else {
      for (int_4 i = 0; i < ngram_order_d-2; i++) {
	newhist_words[i+1] = (void_p)NULL;
      }
    }    
  } // end if LATTICE_VERIFY mode
    
  // ngram decoding and lattice generation mode
  //
  else if (function_mode_d == DEC_NGRAM_DECODE_FUNCTION ||
	   function_mode_d == DEC_LATTICE_GENERATE_FUNCTION) {
    
    // use part of the old histwords as the N-2 previous words
    //
    if (histwords_a != (void_p*)NULL) {
      curword = ((Ngram_node*)histwords_a[0])->get_word_cc();
      
      if (curword != (Word*)NULL) {
	newhist_words[1] = (void_p)curword;
      }
      else {
	newhist_words[1] = (void_p)NULL;
      }
      
      // if more history words are needed, move the Word pointers
      //
      for (int_4 i = 1; i < ngram_order_d-2; i++) {
	newhist_words[i+1] = histwords_a[i];
      }
    }

    // use the !NULL dummy word to fill in the history
    //
    else {
      for (int_4 i = 0; i < ngram_order_d-2; i++) {
	newhist_words[i+1] = (void_p)NULL;
      }
    }
  } // end ngram mode
  
  // exit gracefully
  //
  return newhist_words;
}
