// file: ht_tree_0.cc
//
// function to update the active status of lexical trees
//

// isip include files
//
#include "hmm_train.h"
#include "hmm_train_constants.h"

// method: update_trees_cc
//
// arguments:
//  Train_Link_list* treelist : (input) list of lexical trees
//
// return: a logical_1 with status
//
// this method loops through all lexical trees and deletes the
// inactive trees, and resets the active flags of the rest
//
logical_1 update_trees_cc(Train_Link_list* treelist_a) {

  // dummy variables
  //
  Train_Lex_tree* tree = (Train_Lex_tree*)NULL;
  Train_Lattice_node* latn = (Train_Lattice_node*)NULL;
  Train_Link_node* lnd = (Train_Link_node*)NULL;
  Train_Hash_cell* hcell = (Train_Hash_cell*)NULL;

  // loop over all active lexical trees
  //
  for (Train_Link_node* nd = treelist_a->get_head_cc(); nd != (Train_Link_node*)NULL;
       nd = lnd) {

    // advance node pointer
    //
    lnd = nd->get_next_cc();

    // get the lex tree
    //
    tree = (Train_Lex_tree*)(nd->get_item_cc());
    
    // if this is marked inactive
    //
    if (tree->get_status_cc() == ISIP_FALSE) {

      // reset the pointer in the lattice node
      //
      hcell = tree->get_history_cc();
      latn = (Train_Lattice_node*)(hcell->get_item_cc());
      latn->set_lex_tree_cc((Train_Lex_tree*)NULL);

      // delete the tree
      //
      delete tree;

      // remove it from the system
      //
      treelist_a->remove_cc(nd);
    }
    
    // otherwise mark it as inactive so that it can be activated
    // only if necessary
    //
    else {
      tree->set_status_cc(ISIP_FALSE);
    }
  }

  // exit gracefully
  //
  return ISIP_TRUE;
}
