📄 test_yc_hashtable.c
字号:
#ifdef _MSC_VER
#pragma warning(disable:4127)
#pragma warning(disable:4244)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../young/youngc.h"
#ifdef __cplusplus
using namespace youngc;
#define EXTERN extern "C"
#else
#define EXTERN extern
#endif
static size_t last_chain = 0;
void destroy_float( void* ptr )
{
printf( "~~~~destroy float: %f\n", *((float*)ptr) );
}
void print_float( void* ptr )
{
printf( "%f, ", *((float*)ptr) );
}
EXTERN void hstb_foreach( hashtable* self,
void (*op_node)(size_t, const hashtable_node*) );
void print_node( size_t chain, const hashtable_node* node )
{
if( 0 == chain )
printf( "chain[%u]: ", chain );
else if( chain > last_chain )
printf( "\nchain[%u]: ", chain );
last_chain = chain;
if( node )
printf( "%f, ", *( (float*)((ylib_byte_t*)node + sizeof(hashtable_node)) ) );
}
void print_sequence( hashtable* self )
{
hstb_iterator begin, end;
begin = hstb_begin( self );
end = hstb_end( self );
while( false == hstb_itr_equal(begin, end) )
{
print_float( hstb_itr_deref(begin) );
hstb_itr_inc( &begin );
}
printf( "\n\n\n" );
}
void print_table( hashtable* self )
{
printf( "print chains:\n" );
last_chain = 0;
hstb_foreach( self, print_node );
printf( "\n\n\n" );
}
void test_hashtable(void)
{
hashtable ht;
hstb_init( &ht, 0, sizeof(float), NULL, destroy_float,
NULL, cmp_float, hash_float, pool_alloc, pool_free );
while( 1 )
{
int choice = 0;
printf( "\n\n" );
printf( " 1: test hstb_init_copy\n" );
printf( " 2: test hstb_assign_copy\n" );
printf( " 3: test hstb_init_move\n" );
printf( " 4: test hstb_assign_move\n" );
printf( " 5: test hstb_rehash\n" );
printf( " 6: test hstb_count\n" );
printf( " 7: test hstb_find\n" );
printf( " 8: test hstb_equal_range\n" );
printf( " 9: test hstb_erase_pos\n" );
printf( " 10: test hstb_erase_range\n" );
printf( " 11: test hstb_erase_key\n" );
printf( " 12: test hstb_insert_unique\n" );
printf( " 13: test hstb_insert_unique_pos\n" );
printf( " 14: test hstb_insert_equal\n" );
printf( " 15: test hstb_insert_equal_pos\n" );
printf( " 16: test hstb_replace_key_unique_pos\n" );
printf( " 17: test hstb_replace_key_unique\n" );
printf( " 18: test hstb_replace_key_equal_pos\n" );
printf( " 19: test hstb_replace_key_equal\n" );
printf( " 20: test hstb_begin\n" );
printf( " 21: test hstb_size\n" );
printf( " 22: test hstb_max_size\n" );
printf( " 23: test hstb_max_chain_count\n" );
printf( " 24: test hstb_chain_count\n" );
printf( " 25: test hstb_chain_size\n" );
printf( " 26: test hstb_key_chain\n" );
printf( " 27: test hstb_load_factor\n" );
printf( " 28: test hstb_get_max_load_factor\n" );
printf( " 29: test hstb_set_max_load_factor\n" );
printf( " 30: print hash table\n" );
printf( " 31: print sequence\n" );
printf( " 0: exit\n\n\n" );
scanf( "%d", &choice );
switch( choice )
{
case 0:
goto EXIT;
case 1:
{
hashtable ht1;
hstb_init_copy( &ht1, &ht );
printf( "src:\n" );
print_table( &ht );
printf( "dst:\n" );
print_table( &ht1 );
hstb_destroy( &ht );
hstb_init_copy( &ht, &ht1 );
hstb_destroy( &ht1 );
break;
}
case 2:
{
hashtable ht1;
hstb_init( &ht1, 0, sizeof(float), NULL,
destroy_float, NULL, cmp_float, hash_float,
pool_alloc, pool_free );
hstb_assign_copy( &ht1, &ht );
printf( "src:\n" );
print_table( &ht );
printf( "dst:\n" );
print_table( &ht1 );
hstb_destroy( &ht );
hstb_assign_copy( &ht, &ht1 );
hstb_destroy( &ht1 );
break;
}
case 3:
{
hashtable ht1;
hstb_init_move( &ht1, &ht );
printf( "src:\n" );
print_table( &ht );
printf( "dst:\n" );
print_table( &ht1 );
hstb_destroy( &ht1 );
break;
}
case 4:
{
hashtable ht1;
hstb_init( &ht1, 0, sizeof(float), NULL,
destroy_float, NULL, cmp_float, hash_float,
pool_alloc, pool_free );
hstb_assign_move( &ht1, &ht );
printf( "src:\n" );
print_table( &ht );
printf( "dst:\n" );
print_table( &ht1 );
hstb_destroy( &ht1 );
break;
}
case 5:
{
size_t size = 0;
printf( "please input reserve size:\n" );
scanf( "%u", &size );
hstb_rehash( &ht, size );
break;
}
case 6:
{
float key = 0;
printf( "please input find float key:\n" );
scanf( "%f", &key );
printf( "count of %f: %u\n", key, hstb_count(&ht, &key) );
break;
}
case 7:
{
float key = 0;
hstb_iterator itr;
printf( "please input find float key:\n" );
scanf( "%f", &key );
itr = hstb_find( &ht, &key );
printf( "find node = %p\n", itr.node );
printf( "table = %p\n", itr.table );
break;
}
case 8:
{
float key = 0;
hstb_pair_iterator pitr;
printf( "please input find float key:\n" );
scanf( "%f", &key );
pitr = hstb_equal_range( &ht, &key );
printf( "first.node = %p\n", pitr.first.node );
printf( "first.table = %p\n", pitr.first.table );
printf( "last.node = %p\n", pitr.second.node );
printf( "last.table = %p\n", pitr.second.table );
break;
}
case 9:
{
float key = 0;
printf( "please input erase float key:\n" );
scanf( "%f", &key );
hstb_erase_pos( &ht, hstb_find(&ht, &key) );
break;
}
case 10:
{
float key1 = 0, key2 = 0;
printf( "please input first erase float key:\n" );
scanf( "%f", &key1 );
printf( "please input last erase float key:\n" );
scanf( "%f", &key2 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -