///////////////////////////////////////////////////////////////////////////////
//                                                                           //
//  Copyright (C) 1995-1998 by the Board of Trustees of Leland Stanford      //
//  Junior University.  See LICENSE for details.                             //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////


#include <string.h>
#include "misc.h"


///////////////////////////////////////////////////////////////////////////////
//                                                                           //
// Function: copy_string						     //
// Description: Copy a string                                                //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////
char *copy_string (const char *s)
{
  return (s == NULL) ? (char *)NULL : strcpy(new char[strlen(s) + 1], s);
}


///////////////////////////////////////////////////////////////////////////////
//                                                                           //
// Function: int_to_string						     //
// Description: Convert an int to a string                                   //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////
char *int_to_string (int i)
{
  static char st[20];
  int d=0;
  for (int j=i;j;d++,j/=10);
  assert_msg(d <= 19, ("Overflow"));
  for (st[d--] = '\0';i;st[d--] = '0' + i % 10, i /= 10);
  return st;
}


///////////////////////////////////////////////////////////////////////////////
//                                                                           //
// Function: stringhash							     //
// Description: Hash a string                                                //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////
int stringhash(const char *s)
{
  int h = int(*s);
  while (*s != '\000') { h += 127 + int(*s++); }
  return h;
}


///////////////////////////////////////////////////////////////////////////////
//                                                                           //
// Function: stringhashcase						     //
// Description: Hash independent of case                                     //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////
int stringhashcase(const char *s)
{
  int h = int(*s);
  int hh;
  while (*s != '\000') {
    hh = (*s >= 'A' && *s <= 'Z') ? *s + ('a' - 'A') : *s;
    h += 127 + hh;
    s++;
  }
  return h;
}


///////////////////////////////////////////////////////////////////////////////
//                                                                           //
// Function: ihash							     //
// Description: Hash an integer                                              //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////
int ihash(const int i)
{
  return i>0 ? i : -i;
}


///////////////////////////////////////////////////////////////////////////////
//                                                                           //
// Function: imatch							     //
// Description: Match two integers                                           //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////
int imatch(const int x, const int y)
{
  return (x==y);
}
