Algorithm Alley
by John Boyer

Listing One
char *lex_growbuffer(char *Buffer, long *pBufferSize)
{
char    *TempP;
long    NewSize;
    if (*pBufferSize==0) NewSize = 16;
    else NewSize = *pBufferSize * 2;
    TempP = (char *) realloc(Buffer, NewSize+1); 
    if (TempP != NULL) *pBufferSize = NewSize;
    return TempP;
}
  ... /* Snippet to add a character to the lex buffer */

   if (BufferLen == BufferSize)
        Buffer = lex_growbuffer(Buffer, &BufferSize);
    if (Buffer == NULL) return NULL;
    Buffer[BufferLen++] = NewChar;
    Buffer[BufferLen] = '\0';
    return Buffer;
  ... 
/* After string is read, reduce to exact size */
    Buffer = (char *) realloc(Buffer, BufferLen+1);


Listing Two
typedef unsigned long ulong;
typedef unsigned char uchar;
ulong HashJB(uchar *KeyStr, ulong N)
{
ulong Hash=0, I=0;
uchar Ch;
    for (;Ch=*KeyStr++; I^=1)
        if (I)
            Hash *= Ch;
        else    Hash += Ch;
    Hash += ((Hash&0xFFFF0000)>>16);
    Hash += ((Hash&0x0000FF00)>>8);
    return Hash & (N-1); 
}

1


