Skip to content

Instantly share code, notes, and snippets.

@nulldatamap
Last active December 21, 2015 09:39
Show Gist options
  • Save nulldatamap/6287061 to your computer and use it in GitHub Desktop.
Save nulldatamap/6287061 to your computer and use it in GitHub Desktop.
int slot_table( Table self, String key , int setting )
{
int rslot = 0;
// iv = 0, as default if no chaining is going on
int ihash = 0;
// The hash is now in bounds
int mhash = 0;
// Last link in the chain
int last = 0;
while( 1 )
{
ihash = hash_string( key , last );
mhash = ihash % self.capasity;
// If the slot is empty:
if( ! match_string( self.pairs[mhash].key , key ) )
{
if( ! setting && self.pairs[mhash].key.length == 0 )
throw( ERR_KEY_NOT_FOUND );
// The slot is taken by another key
last = self.pairs[mhash].hash;
}else break;
}
rslot = ihash;
return rslot;
}
int set_table( Table self , String key , void * val )
{
int ihash = slot_table( self , key , 1 );
int slot = ihash % self.capasity;
if( self.pairs[slot].key.length == 0 )
{
self.size += 1;
self.pairs[slot].key = key;
self.pairs[slot].hash = ihash;
self.pairs[slot].value = val;
}else
{
self.pairs[slot].value = val;
}
if( self.size * 3 >= self.capasity )
{
expand_table( self );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment