// file: $USFS/class/image_eval/v1.0/imgev_lookup_0.cc
//

// system include files
//
#include <stdlib.h>

// isip include files
//
#include "image_eval.h"

#include <integral_constants.h>
#include "image_eval_constants.h"

// method: lookup_cc
//
// arguments:
//  int_4 &pos_a: (output) indicating position in array where a match
//  with the reference data is found
//  char_1* str_a: (input) string to match
//  char_1** fnames_a: (input) buffer of filenames
//  int_4 num_items: (input) number of items in the buffer
//
// return:
//  logical_1 to indicate status
//
// This method uses binary search to look up the given string
// in a given buffer.  If the string is found, then it returns
// the position in the buffer where the string is found.
// If the string is not found, it returns a -1.
//
//
int_4 Image_eval::lookup_cc(char_1* str_a, char_1** fnames_a,
			    int_4 num_items_a, int_4* index_a) {

  // initialize upper and lower limits
  //
  int_4 upper = num_items_a-1;
  int_4 lower = 0;

  int_4 mid;

  // go find a match
  //
  while (lower <= upper) {
    mid = (lower+upper) >> 1;

    // item is less than mid element
    //
    if (strcmp((char*)str_a, (char*)fnames_a[index_a[mid]]) < 0) {
      upper = mid - 1;
    }

    // item is more than mid element
    //
    else if (strcmp((char*)str_a, (char*)fnames_a[index_a[mid]]) > 0) {
      lower = mid + 1;
    }

    // found a match
    //
    else {
      return mid;
    }
  }

  return IMGEV_NOT_FOUND;
  
}


