// file: ngrl_node_4.cc
//

// isip include files
//
#include "ngram_list.h"
#include "ngram_list_constants.h"

// method: adjust_num_cc
//
// arguments:
//  int_4 num: (input) the final number of nodes in the array
//
// return:  a logical_1 indicating success
//
// this method sets the size of the nodes array to the specified value
// and frees excess memory / reallocates memory if needed [Note: if
// the array size increases then the ngram nodes need to be re-tooled
// to point to the new next and prev node pointers, so this is not
// recommended after the ngram has been read]
//
logical_1 Ngram_list::adjust_num_cc(int_4 num_a) {

  // if the new number of nodes is smaller
  //
  if (num_a < num_d) {

    // delete excess memory at the end of the nodes array
    //
    nodes_d += num_a;
    delete [] nodes_d;
    nodes_d -= num_a;
  }

  // otherwise if the number of nodes has increased
  //
  else if (num_a > num_d) {
    
    // create array
    //
    Ngram_node** nodes = new Ngram_node*[num_a];
    
    // initialize by copying the current nodes
    //
    for (int_4 i = 0; i < num_d; i++) {
      if (nodes_d != (Ngram_node**)NULL) {
	nodes[i] = nodes_d[i];
      }
      else {
	nodes[i] = (Ngram_node*)NULL;
      }
    }
    
    // initialize the new nodes
    //
    for (int_4 i = num_d; i < num_a; i++) {
      nodes[i] = (Ngram_node*)NULL;
    }
    
    // reset the old array and replace it with the new array
    //
    if (nodes_d != (Ngram_node**)NULL) {
      delete [] nodes_d;
    }
    nodes_d = nodes;
    
  } // end else
  
  // set number of nodes
  //
  num_d = num_a;

  // exit gracefully
  //
  return ISIP_TRUE;
}
