// file: lxn_node_0.cc
//

// isip include files
//
#include "lex_node.h"
#include "lex_node_constants.h"

// method: make_node_cc
//
// arguments:
//  int_4 phone : (input) the monophone index
//
// return: a Lex_node* pointing to the lexical tree node with this
// phone
//
// this method searches all the child nodes of the current node for
// the specified phone and returns the match. if no such node exists,
// a new child is added to the list and returned
//
Lex_node* Lex_node::make_node_cc(int_4 phone_a) {

  // dummy variables
  //
  logical_1 flag = ISIP_FALSE;
  Lex_node* node = (Lex_node*)NULL;

  // make sure the link list of children exists
  //
  if (child_d == (Link_list*)NULL) {
    child_d = new Link_list();
  }

  // loop over all the child lex nodes
  //
  for (Link_node* nd = child_d->get_head_cc(); nd != (Link_node*)NULL;
       nd = nd->get_next_cc()) {

    // get the lexical node in this node
    //
    node = (Lex_node*)(nd->get_item_cc());

    // stop if the phone here matches the input phone
    //
    if (node->get_phone_cc() == phone_a) {
      flag = ISIP_TRUE;
      break;
    }
  }

  // if there is no match create a new node and put it in the list
  //
  if (flag == ISIP_FALSE) {

    // memory manager
    //
    Memory_manager* manager = Link_list::get_manager_cc();

    // create a new node
    //
    node = manager->new_lex_cc();
    node->phone_d = phone_a;
    child_d->insert_cc(node);
  }
  
  // return child node and exit gracefully
  //
  return node;
}
