// file: hist_add_1.cc
//

// isip include files
//
#include "history.h"
#include "history_constants.h"

// method: add_score_cc
//
// arguments:
//  int_4 idx: (input) index position to insert score
//  float_4 pscore: (input) path score for this history
//  int_4 max_num: (input) maximum number of histories allowed
//
// return: a logical_1 indicating status
//
// this method inserts the input score in the history score array
//
logical_1 History::add_score_cc(int_4 idx_a, float_4 pscore_a,
				int_4 max_num_a) {
  
  // define local variables
  //
  int_4 len = max_num_a;
  if (hist_num_d < max_num_a) {
    len = hist_num_d + 1;
  }
  
  // allocate memory for the new list
  //
  float_4* sclist = new float_4[len];

  // loop over old score list and copy
  //
  if (prevsc_d != (float_4*)NULL) {
    for (int_4 i = 0; i < idx_a; i++) {
      sclist[i] = prevsc_d[i];
    }
  }

  // insert the new score
  //
  sclist[idx_a] = pscore_a;

  // copy the remaining scores
  //
  for (int_4 i = idx_a + 1; i < len; i++) {
    sclist[i] = prevsc_d[i - 1];
  }

  // free old array and copy new one
  //
  if (prevsc_d != (float_4*)NULL) {
    delete [] prevsc_d;
  }
  prevsc_d = sclist;

  // update the score of this history node
  //
  score_d = prevsc_d[0];
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
