// file: tr_lat_index_1.cc
//

// isip include files
//
#include "train_lattice.h"
#include "train_lattice_constants.h"

// method: update_index_cc
//
// arguments:
//  Train_Lattice_node* latnode: (input) the start point Train_Lattice node
//  int_4*& map: (output) the map from old to new indices
//  int_4& idx: (output) the index value assigned last
//
// return: a logical_1 indicating status
//
// this method recursively re-assigns the node index of the Train_Lattice
// nodes to reflect the start to end progression of the Train_Lattice
//
logical_1 Train_Lattice::update_index_cc(Train_Lattice_node* latnode_a, int_4* map_a,
				   int_4& idx_a) {

  // get the current node index
  //
  int_4 ndx = latnode_a->get_node_index_cc();
  
  // if this Train_Lattice node has been already visited return
  //
  if (map_a[ndx] != (int_4)-1) {
    return ISIP_TRUE;
  }
  
  // update the map
  //
  map_a[ndx] = idx_a++;
  
  // loop over all the next nodes
  //
  Train_Lattice_node* latn = (Train_Lattice_node*)NULL;
  Train_Link_list* next_list = latnode_a->get_next_nodes_cc();
  if (next_list != (Train_Link_list*)NULL) {
    for (Train_Link_node* lnd = next_list->get_head_cc(); lnd != (Train_Link_node*)NULL;
	 lnd = lnd->get_next_cc()) {
      
      // get the Train_Lattice node here
      //
      latn = (Train_Lattice_node*)(lnd->get_item_cc());
      
      // start with the start node of the Train_Lattice
      //
      update_index_cc(latn, map_a, idx_a);
    }
  }
    
  // exit gracefully
  //
  return ISIP_TRUE;
}
