// file: $isip/class/io/SofSymbolTable/sofs_06.cc
// version: $Id: sofs_06.cc 4955 2000-09-22 20:48:39Z zhao $
//

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

// method: getIndex
//
// arguments:
//  const SysString& name: (input) the name of the symbol
//
// return: a long index
//
// this method finds the index of name in the list
//
long SofSymbolTable::getIndex(const SysString& name_a) const {
  
  // make sure it is a valid name
  //
  if (!checkName(name_a)) {
    return NO_SYMB;
  }
					     
  // check for special cases
  //
  if (num_syms_d <= (long)0) {
    return NO_SYMB;
  }

  // iterate through the list
  //
  for (long i = 0; i <= table_size_d; i++) {

    // this is a match if the strings are equal and the reference
    // count is greater than zero.
    //
    if ((ref_count_d[i] > 0) && (name_a.eq(table_d[i]))) {
      return i;
    }
  }
  
  // exit (un)gracefully - no match was found
  //
  return NO_SYMB;
}

// method: getSymbol
//
// arguments:
//  SysString& name: (output) name of this index
//  long index: (input) index into the symbol table
//
// return: a boolean value indicating status
//
// this method finds the symbol of the given index in the list
//
boolean SofSymbolTable::getSymbol(SysString& name_a, long index_a) const {
  
  // check for special cases
  //
  if ((index_a < 0) || (index_a > table_size_d)) {
    Error::handle(name(), L"getSymbol", ERR_SYMB, __FILE__, __LINE__);
    name_a.clear();
    return false;
  }
  
  if (ref_count_d[index_a] <= 0) {
    name_a.clear();
    return false;
  }
  
  // assign the string
  //
  name_a.assign(table_d[index_a]);
    
  // exit gracefully 
  //
  return true;
}

// method: getRefCount
//
// arguments:
//  const SysString& name: (input) object name
//
// return: a long count
// 
// find the number of symbols matching this name exist in the table
//
long SofSymbolTable::getRefCount(const SysString& name_a) const {

  // do a linear search on the index
  //
  long i = getIndex(name_a);

  // get the count
  //
  if (i >= 0) {
    return ref_count_d[i];
  }

  // exit (un)gracefully
  //
  return (long)0;
}

// method: getRefCount
//
// arguments:
//  long index: (input) object index
//
// return: a long count
// 
// find the number of symbols matching this index exist in the table
//
long SofSymbolTable::getRefCount(long index_a) const {
  
  // get the count
  //
  if ((index_a >= 0) && (index_a <= table_size_d)) {
    return ref_count_d[index_a];
  }

  // exit (un)gracefully
  //
  return (long)0;
}
