// file: lxn_clear_0.cc
//

// isip include files
//
#include "lex_node.h"
#include "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 Lex_node::clear_cc() {

  // memory manager
  //
  Memory_manager* manager = Link_list::get_manager_cc();
  
  // reset the scalars
  //
  phone_d = LXN_NULL_PHONE;
  score_d = LXN_LM_SCORE;
  word_end_d = ISIP_FALSE;
  
  // delete the word list
  //
  if (words_d != (Link_list*)NULL) {
    delete words_d;
    words_d = (Link_list*)NULL;
  }
  
  // now clean up the child lex nodes
  //
  if (child_d != (Link_list*)NULL) {
    
    // dummy variable
    //
    Lex_node* lex = (Lex_node*)NULL;
    
    // loop over all children of the current node
    //
    Link_node* pd = (Link_node*)NULL;
    for (Link_node* node = child_d->get_head_cc();
	 node != (Link_node*)NULL; node = pd) {
      
      // advance link pointer
      //
      pd = node->get_next_cc();
      
      // get the lex node at this place
      //
      lex = (Lex_node*)(node->get_item_cc());
      
      // if this is an active lex node clear it
      //
      if (lex->phone_d != LXN_NULL_PHONE) {
	lex->clear_cc();
	manager->delete_cc(lex);
      }

    } // end for loop
    
    // delete the link list
    //
    delete child_d;
    child_d = (Link_list*)NULL;
  }
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
