#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "ght_hash_table.h"
#include "naht.h"

int     int_compare (void *va, void *vb)
{
    /*
    ** To use naht, you will need to provide
    ** some function that compares 2 items,
    ** and returns 0 if they are not the same.
    */

    unsigned long a , b;

    a = *(unsigned long *) va;
    b = *(unsigned long *) vb;

    return (a == b);
}

void    int_discard (void *va)
{
    /*
    ** This function will be called whenever
    ** an item will be forcibly be removed
    ** from storage to make room for a new
    ** item. If you want to save your items,
    ** this callback would be the place to
    ** do it.
    */

}


int main (int argc, char **argv)
{
	ght_hash_table_t	*t;
	nahtT				*n;
	unsigned long		a, b, i, j;
	void				*vp;
	struct timeval		start, end;
	float				ght_time, n_time;


	for (j=10; j<2e6; j = j*1.5)
{
	n = naht_init (sizeof (unsigned long), 5.0, int_compare, int_discard);
	t = ght_create (n->buckets);
	gettimeofday (&start, NULL);
	for (i=0; i<j; i++)
	{
		b = random();
		vp = ght_get (t, sizeof (unsigned long), (void *) &b);
		if (!vp)
		{
			ght_insert (t, (void *) &b, sizeof (unsigned long), (void *) &b);
			vp = ght_get (t, sizeof (unsigned long), (void *) &b);
		}
		a = *(unsigned long *)vp;
		if (a != b) 
		{
			printf ("ght failed\n");
			return(0);
		}
	}
	gettimeofday (&end, NULL);
	ght_time = (end.tv_sec + end.tv_usec / 1e6) - (start.tv_sec + start.tv_usec / 1e6);	
	
	gettimeofday (&start, NULL);
	for (i=0; i<j; i++)
	{
		b = random();
		vp = naht_find (n, b, (void *) &b);
		if (!vp)
		{
			naht_insert (n, b, (void *) &b);
			vp = naht_find (n, b, (void *) &b);
		}
		a = *(unsigned long *)vp;
		if (a != b) 
		{
			printf ("naht failed\n");
			return(0);
		}
	}
	gettimeofday (&end, NULL);
	n_time = (end.tv_sec + end.tv_usec / 1e6) - (start.tv_sec + start.tv_usec / 1e6);	
	printf ("%ld, %6.4f, %6.4f\n", j, ght_time, n_time);
	naht_free (n);
	ght_finalize (t);
}
	return (0);
}

