All pastes #993471 Raw Edit

Untitled

public text v1 · immutable
#993471 ·published 2008-04-22 17:23 UTC
rendered paste body
unsigned int integerCast(long int value)
{
	int newValue;

	// cast it to an int
	newValue = (int)value;

	// calculate absolute value (so we can use it as an array index)
	newValue = abs(newValue);

	// return it, also compressing it, to minimize memory use for the array
	return (newValue % NMAX);
}

unsigned long compSumHash(long int value)
{
	// vars to hold the currently worked on component
	// and the accumulated hash
	unsigned short int thisComponent, summedComponents;
	const int bytesIn16Bits = 2;
	const int bitsInByte = 8;
	char key[20];
	char *ptr;
	int i, j;
	int bufLen;
	int padding;

	sprintf(key, "%x", value);

	ptr = key;

	// get number of chars in our key
	bufLen = strlen(key);
	// work out if string is even or odd number of bytes in length.
	// if it's odd, we need to add padding
	padding = bufLen % 2;

	// zero our working vars
	summedComponents = thisComponent = 0;

	// if we have an even number of chars in the string, just use
	// the non-zero chars to calculate the result.
	// If, on the other hand, there are an odd number of chars, then
	// we also use the terminating null.
	//    printf("Component Sum loop start\n");
	for (i=0; i<(bufLen+padding)/bytesIn16Bits; i++)
	{
		thisComponent = 0;
		for (j=0; j<bytesIn16Bits; j++)
		{
			thisComponent <<= bitsInByte;
			//            printf("%c, ", *ptr);
			thisComponent |= *ptr++;
		}
		//        printf("\n%d\n", thisComponent);
		summedComponents += thisComponent;
	}

	// again compressing the result
	return (summedComponents % NMAX);
}