// file: tr_lxn_node_4.cc
//

// isip include files
//
#include "train_lex_node.h"
#include "train_lex_node_constants.h"

// method: clear_cc
//
// arguments: none
//
// return: a logical_1 to indicate status
//
// this method recursively cleans up all the memory in the lexical
// node
//
logical_1 Train_Lex_node::clear_cc() {
  
  // get the memory manager
  //
  Train_Memory_manager* manager = Train_Link_list::get_manager_cc();

  // set the child nodes list to null
  //
  Train_Link_list* list = child_d;
  child_d = (Train_Link_list*)NULL;
  
  // reset the tree pointer
  //
  tree_d = (void_p)NULL;

  // reset the scalars
  //
  phone_d = TRAIN_LXN_NULL_PHONE;
  max_lm_score_d = TRAIN_LXN_LM_SCORE;
  status_d = ISIP_TRUE;
  
  // delete the word list
  //
  if (word_list_d != (Train_Link_list*)NULL) {
    delete word_list_d;
    word_list_d = (Train_Link_list*)NULL;
  }

  // delete the score list
  //
  if (score_list_d != (float_8*)NULL) {
    delete [] score_list_d;
    score_list_d = (float_8*)NULL;
  }
  num_words_d = (int_4)0;

  // return this node to the manager
  //
  manager->delete_cc(this);
  
  // now clean up the child lex nodes
  //
  if (list != (Train_Link_list*)NULL) {

    // dummy variable
    //
    Train_Lex_node* lex = (Train_Lex_node*)NULL;
    
    // loop over all children of the current node
    //
    Train_Link_node* pd = (Train_Link_node*)NULL;
    for (Train_Link_node* node = list->get_head_cc();
	 node != (Train_Link_node*)NULL; node = pd) {
      
      // advance link pointer
      //
      pd = node->get_next_cc();
      
      // get the lex node at this place
      //
      lex = (Train_Lex_node*)(node->get_item_cc());

      // if this is an active lex node clear it
      //
      if (lex->status_d == ISIP_FALSE) {
	lex->clear_cc();
      }
    } // end for loop

    // delete the link list
    //
    delete list;
    list = (Train_Link_list*)NULL;
  }

  // exit gracefully
  //
  return ISIP_TRUE;
}
