Skip to content

Commit 9f11461

Browse files
committed
main: add a fuction to report statistics of a given hashtable
Signed-off-by: Masatake YAMATO <[email protected]>
1 parent 1606e5a commit 9f11461

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

main/htable.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,42 @@ extern bool hashTableForeachItemOnChain (hashTable *htable, const void *ke
383383
return true;
384384
}
385385

386+
extern void hashTablePrintStatistics(hashTable *htable)
387+
{
388+
if (htable->size == 0 || htable->count == 0)
389+
fprintf(stderr, "size: %u, count: %u, average: 0\n",
390+
htable->size, htable->count);
391+
392+
double sum = 0.0;
393+
for (size_t i = 0; i < htable->size; i++)
394+
{
395+
hentry *e = htable->table[i];
396+
while (e)
397+
{
398+
sum += 1.0;
399+
e = e->next;
400+
}
401+
}
402+
double average = sum / (double)htable->size;
403+
404+
double variance = 0.0;
405+
for (size_t i = 0; i < htable->size; i++)
406+
{
407+
double sum0 = 0;
408+
hentry *e = htable->table[i];
409+
while (e)
410+
{
411+
sum0 += 1.0;
412+
e = e->next;
413+
}
414+
double d = sum0 - average;
415+
variance += (d * d);
416+
}
417+
variance = variance / (double)htable->size;
418+
fprintf(stderr, "size: %u, count: %u, average: %lf, s.d.: sqrt(%lf)\n",
419+
htable->size, htable->count, average, variance);
420+
}
421+
386422
extern unsigned int hashTableCountItem (hashTable *htable)
387423
{
388424
return htable->count;

main/htable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ extern bool hashTableForeachItemOnChain (hashTable *htable, const void *ke
8787

8888
extern unsigned int hashTableCountItem (hashTable *htable);
8989

90+
extern void hashTablePrintStatistics(hashTable *htable);
91+
9092
#define HT_PTR_TO_INT(P) ((int)(intptr_t)(P))
9193
#define HT_INT_TO_PTR(P) ((void*)(intptr_t)(P))
9294
#define HT_PTR_TO_UINT(P) ((unsigned int)(uintptr_t)(P))

0 commit comments

Comments
 (0)