#include "table.h"
#include "core.h"
#include "ctz.h"
#include <stdatomic.h>
#include <stdbool.h>
#include <string.h>
#if B2_DEBUG
_Atomic int g_probeCount;
#endif
b2HashSet b2CreateSet( int32_t capacity )
{
b2HashSet set = { 0 };
if ( capacity > 16 )
{
set.capacity = b2RoundUpPowerOf2( capacity );
}
else
{
set.capacity = 16;
}
set.count = 0;
set.items = b2Alloc( capacity * sizeof( b2SetItem ) );
memset( set.items, 0, capacity * sizeof( b2SetItem ) );
return set;
}
void b2DestroySet( b2HashSet* set )
{
b2Free( set->items, set->capacity * sizeof( b2SetItem ) );
set->items = NULL;
set->count = 0;
set->capacity = 0;
}
void b2ClearSet( b2HashSet* set )
{
set->count = 0;
memset( set->items, 0, set->capacity * sizeof( b2SetItem ) );
}
static inline uint32_t b2KeyHash( uint64_t key )
{
uint64_t h = key;
h ^= h >> 33;
h *= 0xff51afd7ed558ccdL;
h ^= h >> 33;
h *= 0xc4ceb9fe1a85ec53L;
h ^= h >> 33;
return (uint32_t)h;
}
int32_t b2FindSlot( const b2HashSet* set, uint64_t key, uint32_t hash )
{
uint32_t capacity = set->capacity;
int32_t index = hash & ( capacity - 1 );
const b2SetItem* items = set->items;
while ( items[index].hash != 0 && items[index].key != key )
{
#if B2_DEBUG
atomic_fetch_add( &g_probeCount, 1 );
#endif
index = ( index + 1 ) & ( capacity - 1 );
}
return index;
}
static void b2AddKeyHaveCapacity( b2HashSet* set, uint64_t key, uint32_t hash )
{
int32_t index = b2FindSlot( set, key, hash );
b2SetItem* items = set->items;
B2_ASSERT( items[index].hash == 0 );
items[index].key = key;
items[index].hash = hash;
set->count += 1;
}
static void b2GrowTable( b2HashSet* set )
{
uint32_t oldCount = set->count;
B2_MAYBE_UNUSED( oldCount );
uint32_t oldCapacity = set->capacity;
b2SetItem* oldItems = set->items;
set->count = 0;
set->capacity = 2 * oldCapacity;
set->items = b2Alloc( set->capacity * sizeof( b2SetItem ) );
memset( set->items, 0, set->capacity * sizeof( b2SetItem ) );
for ( uint32_t i = 0; i < oldCapacity; ++i )
{
b2SetItem* item = oldItems + i;
if ( item->hash == 0 )
{
continue;
}
b2AddKeyHaveCapacity( set, item->key, item->hash );
}
B2_ASSERT( set->count == oldCount );
b2Free( oldItems, oldCapacity * sizeof( b2SetItem ) );
}
bool b2ContainsKey( const b2HashSet* set, uint64_t key )
{
B2_ASSERT( key != 0 );
uint32_t hash = b2KeyHash( key );
int32_t index = b2FindSlot( set, key, hash );
return set->items[index].key == key;
}
int b2GetHashSetBytes( b2HashSet* set )
{
return set->capacity * (int)sizeof( b2SetItem );
}
bool b2AddKey( b2HashSet* set, uint64_t key )
{
B2_ASSERT( key != 0 );
uint32_t hash = b2KeyHash( key );
B2_ASSERT( hash != 0 );
int32_t index = b2FindSlot( set, key, hash );
if ( set->items[index].hash != 0 )
{
B2_ASSERT( set->items[index].hash == hash && set->items[index].key == key );
return true;
}
if ( 2 * set->count >= set->capacity )
{
b2GrowTable( set );
}
b2AddKeyHaveCapacity( set, key, hash );
return false;
}
bool b2RemoveKey( b2HashSet* set, uint64_t key )
{
uint32_t hash = b2KeyHash( key );
int32_t i = b2FindSlot( set, key, hash );
b2SetItem* items = set->items;
if ( items[i].hash == 0 )
{
return false;
}
items[i].key = 0;
items[i].hash = 0;
B2_ASSERT( set->count > 0 );
set->count -= 1;
int32_t j = i;
uint32_t capacity = set->capacity;
for ( ;; )
{
j = ( j + 1 ) & ( capacity - 1 );
if ( items[j].hash == 0 )
{
break;
}
int32_t k = items[j].hash & ( capacity - 1 );
if ( i <= j )
{
if ( i < k && k <= j )
{
continue;
}
}
else
{
if ( i < k || k <= j )
{
continue;
}
}
items[i] = items[j];
items[j].key = 0;
items[j].hash = 0;
i = j;
}
return true;
}