2026-06-11 22:39:26 -07:00
|
|
|
// SPDX-FileCopyrightText: 2026 Erin Catto
|
2026-05-10 13:36:52 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
2026-06-11 22:39:26 -07:00
|
|
|
#include "core.h"
|
2026-05-10 13:36:52 -07:00
|
|
|
#include "ctz.h"
|
2026-06-11 22:39:26 -07:00
|
|
|
#include "platform.h"
|
2026-05-10 13:36:52 -07:00
|
|
|
#include "table.h"
|
|
|
|
|
#include "test_macros.h"
|
|
|
|
|
|
2026-06-11 22:39:26 -07:00
|
|
|
#include "box3d/base.h"
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
#define SET_SPAN 317
|
|
|
|
|
#define ITEM_COUNT ( ( SET_SPAN * SET_SPAN - SET_SPAN ) / 2 )
|
|
|
|
|
|
|
|
|
|
int TableTest( void )
|
|
|
|
|
{
|
2026-06-11 22:39:26 -07:00
|
|
|
int power = b3BoundingPowerOf2( 3008 );
|
2026-05-10 13:36:52 -07:00
|
|
|
ENSURE( power == 12 );
|
|
|
|
|
|
2026-06-11 22:39:26 -07:00
|
|
|
int nextPowerOf2 = b3RoundUpPowerOf2( 3008 );
|
2026-05-10 13:36:52 -07:00
|
|
|
ENSURE( nextPowerOf2 == ( 1 << power ) );
|
|
|
|
|
|
|
|
|
|
const int32_t N = SET_SPAN;
|
|
|
|
|
const uint32_t itemCount = ITEM_COUNT;
|
|
|
|
|
bool removed[ITEM_COUNT] = { 0 };
|
|
|
|
|
|
|
|
|
|
for ( int32_t iter = 0; iter < 1; ++iter )
|
|
|
|
|
{
|
2026-06-11 22:39:26 -07:00
|
|
|
b3HashSet set = b3CreateSet( 16 );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
// Fill set
|
|
|
|
|
for ( int32_t i = 0; i < N; ++i )
|
|
|
|
|
{
|
|
|
|
|
for ( int32_t j = i + 1; j < N; ++j )
|
|
|
|
|
{
|
2026-06-11 22:39:26 -07:00
|
|
|
uint64_t key = b3ShapePairKey( i, j, 0 );
|
|
|
|
|
b3AddKey( &set, key );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ENSURE( set.count == itemCount );
|
|
|
|
|
|
|
|
|
|
// Remove a portion of the set
|
|
|
|
|
int32_t k = 0;
|
|
|
|
|
uint32_t removeCount = 0;
|
|
|
|
|
for ( int32_t i = 0; i < N; ++i )
|
|
|
|
|
{
|
|
|
|
|
for ( int32_t j = i + 1; j < N; ++j )
|
|
|
|
|
{
|
|
|
|
|
if ( j == i + 1 )
|
|
|
|
|
{
|
2026-06-11 22:39:26 -07:00
|
|
|
uint64_t key = b3ShapePairKey( i, j, 0 );
|
|
|
|
|
b3RemoveKey( &set, key );
|
2026-05-10 13:36:52 -07:00
|
|
|
removed[k++] = true;
|
|
|
|
|
removeCount += 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
removed[k++] = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ENSURE( set.count == ( itemCount - removeCount ) );
|
|
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2026-06-11 22:39:26 -07:00
|
|
|
extern b3AtomicInt b3_probeCount;
|
|
|
|
|
b3AtomicStoreInt(&b3_probeCount, 0);
|
2026-05-10 13:36:52 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Test key search
|
|
|
|
|
// ~5ns per search on an AMD 7950x
|
2026-06-11 22:39:26 -07:00
|
|
|
uint64_t ticks = b3GetTicks();
|
2026-05-10 13:36:52 -07:00
|
|
|
|
|
|
|
|
k = 0;
|
|
|
|
|
for ( int32_t i = 0; i < N; ++i )
|
|
|
|
|
{
|
|
|
|
|
for ( int32_t j = i + 1; j < N; ++j )
|
|
|
|
|
{
|
2026-06-11 22:39:26 -07:00
|
|
|
uint64_t key = b3ShapePairKey( j, i, 0 );
|
|
|
|
|
ENSURE( b3ContainsKey( &set, key ) || removed[k] );
|
2026-05-10 13:36:52 -07:00
|
|
|
k += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-11 22:39:26 -07:00
|
|
|
float ms = b3GetMilliseconds( ticks );
|
|
|
|
|
printf( "set: count = %d, b3ContainsKey = %.5f ms, ave = %.5f us\n", itemCount, ms, 1000.0f * ms / itemCount );
|
2026-05-10 13:36:52 -07:00
|
|
|
|
2026-06-11 22:39:26 -07:00
|
|
|
#ifndef NDEBUG
|
|
|
|
|
int probeCount = b3AtomicLoadInt( &b3_probeCount );
|
|
|
|
|
float aveProbeCount = (float)probeCount / (float)itemCount;
|
|
|
|
|
printf( "item count = %d, probe count = %d, ave probe count %.2f\n", itemCount, probeCount, aveProbeCount );
|
2026-05-10 13:36:52 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Remove all keys from set
|
|
|
|
|
for ( int32_t i = 0; i < N; ++i )
|
|
|
|
|
{
|
|
|
|
|
for ( int32_t j = i + 1; j < N; ++j )
|
|
|
|
|
{
|
2026-06-11 22:39:26 -07:00
|
|
|
uint64_t key = b3ShapePairKey( i, j, 0 );
|
|
|
|
|
b3RemoveKey( &set, key );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ENSURE( set.count == 0 );
|
|
|
|
|
|
2026-06-11 22:39:26 -07:00
|
|
|
b3DestroySet( &set );
|
2026-05-10 13:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|