my small hashlib - using pythons hash-functions

Mathias Panzenboeck e0427417 at student.tuwien.ac.at
Sat Nov 25 14:57:24 EST 2006


Hi.

I wrote a small hashlib for C. Because I'm new to hashes I looked at pythons implementation and 
reused *some* of the code... or more the mathematical "hash-function", not really the code.

In particular I looked at pythons hash and lookup functions, so I came up with this (see the code 
underneath).

So, can this code be considered as derived and do I have to put my code under the GPL? I'd like to 
publish it under something less restrictive, like a BSD style license. But if GPL is the only way, 
then GPL it is. :)

	-panzi


#define PERTURB_SHIFT 5

static ENTRY_T * lookup(CONTAINER_T * table, hash_const_str_t key,
  int (*loop_cond)(ENTRY_T * ptr, hash_const_str_t key)) {
	register ENTRY_T *   entries = table->entries;
	register hash_size_t size    = table->size;
	register hash_size_t pos     = hash(key) % size;
	register hash_size_t perturb = pos;
	
	register ENTRY_T * ptr = entries + pos;

	while(loop_cond(ptr,key)) {
		pos       = ((pos << 2) + pos + perturb + 1) % size;
		perturb >>= PERTURB_SHIFT;
				
		ptr = entries + pos;
		
#ifdef HASH_COUNT_COLLISIONS
		++ table->collisions;
#endif
	}
	
	return ptr;
}

hash_t hash(register hash_const_str_t str) {
/* python 2.5's hash function: */
	register hash_size_t len;
	register hash_t      h;
	
	assert(str);
	
	len = strlen(str);
	h   = *str << 7;
	
	while (*str)
		h = (1000003 * h) ^ *str ++;
	h ^= len;
	
	return h;
}



More information about the Python-list mailing list