// file: ngrl_srch_0.cc
//

// isip include files
//
#include "ngram_list.h"
#include "ngram_list_constants.h"

// method: search_node_cc
//
// arguments:
//  Ngram_node** start: (input) the start point for search
//  Ngram_node** end: (input) the end point for search
//  Word* word: (input) the word in the desired ngram node
//
// return: an Ngram_node** with the result of the search
//
// this method searches for the ngram node containing the given word
// in the specified range of the list
//
Ngram_node** Ngram_list::search_node_cc(Ngram_node** start_a,
					Ngram_node** end_a, Word* word_a) {

  // output ngram node
  //
  Ngram_node** ngnode = (Ngram_node**)NULL;
  Ngram_node** node = (Ngram_node**)NULL;
  Ngram_node** start = start_a;
  Ngram_node** end = end_a;
  Word* word = (Word*)NULL;
  int_4 num = (int_4)0;

  // run a binary search if this is a non-empty list
  //
  while ((start <= end) && (start != (Ngram_node**)NULL)) {

    // get the midpoint
    //
    num = end - start;
    node = start + num/2;

    // check for the target word
    //
    word = (*node)->get_word_cc();
      
    // branch off based on word pointer
    //
    if ((int_4)word_a < (int_4)word) {
      end = node - 1;
    }
    else if ((int_4)word_a > (int_4)word) {
      start = node + 1;
    }
    else {
      ngnode = node;
      break;
    }
  }
  
  // return ngram node and exit gracefully
  //
  return ngnode;
}
