#include <cassert>
#include <iostream>
using namespace std;

#include <stringhash.h>



stringhash::stringhash(uintc cyclelength_, uintc loops_)
  : hashfn(loops_), cyclelength(cyclelength_) {}


uintc stringhash::operator ()(stringc & key) const
{
  char * i;
  char * iend;
  assert(cyclelength!=0);
  assert(key.empty()==false);
  uintc keylen = key.length();
if (keylen>=cyclelength)
{
cout << "error: keylen=" << keylen << " cyclelength=" << cyclelength << endl;
cout << "error:  key=" << key.c_str() << endl;
}
  assert(keylen<cyclelength);
  uint hi;

  uint v[cyclelength];
  uint * vend = v + cyclelength;
  uint * pv;

  i = (char *)key.c_str();
  iend = i + keylen;

  pv = v;
  for ( ; i != iend; )
  {
    *pv = *i;
    ++pv;
    ++i;
  }

  for ( ; pv != vend; ++pv )
    *pv = *(pv-keylen);


  // A rotating hash.
  hi = 0;
  for ( pv=v; pv!=vend; ++pv )
    hi ^= ( *pv + (hi << 6 ) + (hi >> 2) );

  return hashfn(hi);
}


void stringhash::construct( uintc cyclelength_, uintc loops_ )
{
  cyclelength=cyclelength_;
  hashfn.construct(loops_);
}




