src/stdlib/hashtab.c
/*======================
 * insert_hashtab -- Add new value to hash table
 * return previous value for this key, if any
 *====================*/
- Add new value to hash table
return previous value for this key, if any
- Add new value to hash table return previous value for this key if any
HVALUE
insert_hashtab (HASHTAB tab, CNSTRING key, HVALUE val)
{
	HASHENT entry=0;
	INT hval=0;
	ASSERT(tab);
	ASSERT(tab->magic == hashtab_magic);
	hval = hash(tab, key);
	if (!tab->entries[hval]) {
		entry = create_entry(key, val);
		tab->entries[hval] = entry;
		++tab->count;
		return 0; 
	}
	entry = tab->entries[hval];
	while (TRUE) {
		ASSERT(entry->magic == hashent_magic);
		if (eqstr(key, entry->ekey)) {
			HVALUE old = entry->val;
			entry->val = val;
			return old;
		}
		if (!entry->enext) {
			HASHENT newent = create_entry(key, val);
			entry->enext = newent;
			++tab->count;
			return 0; 
		}
		entry = entry->enext;
	}
}
HVALUE insert_hashtab ( HASHTAB tab , CNSTRING key , HVALUE val ) { HASHENT entry = 0 ; INT hval = 0 ; ASSERT ( tab ) ; ASSERT ( tab -> magic == hashtab_magic ) ; hval = hash ( tab , key ) ; if ( ! tab -> entries [ hval ] ) { entry = create_entry ( key , val ) ; tab -> entries [ hval ] = entry ; ++ tab -> count ; return 0 ; } entry = tab -> entries [ hval ] ; while ( TRUE ) { ASSERT ( entry -> magic == hashent_magic ) ; if ( eqstr ( key , entry -> ekey ) ) { HVALUE old = entry -> val ; entry -> val = val ; return old ; } if ( ! entry -> enext ) { HASHENT newent = create_entry ( key , val ) ; entry -> enext = newent ; ++ tab -> count ; return 0 ; } entry = entry -> enext ; } }
insert_hashtab -- Add new value to hash table
 return previous value for this key, if any
insert_hashtab -- Add new value to hash table return previous value for this key if any
/* find appropriate has chain */ /* table lacks entry for this key, create it */ /* no old value */ /* table already has entry for this key, replace it */ /* table lacks entry for this key, create it */ /* no old value */