#include <v_lib.h>

class compobj_c
{
public:
	int val;
	compobj_c(int val)	{this->val=val;}
	friend inline int compar(compobj_c &ref1,compobj_c &ref2);
};	/* compobj_c */

inline int compar(compobj_c &ref1,compobj_c &ref2)
{
	if(ref1.val == ref2.val)
		return 0;
	else
    	return (ref1.val < ref2.val) ? -1 : 1;
}

int V_CALL
comp(void *param,void *p1,void *p2)
{
	param=NULL;
	return compar(*((compobj_c*)p1),*((compobj_c*)p2));
}

int V_CALL
comp2(void *param,void *p1,void *p2)
{
	return -1;
}

int rtl_compar(const void *p1,const void *p2)
{
	return compar(*((compobj_c*)p1),*((compobj_c*)p2));
}

const MAX=30;

void dump_objects(vp_stack_t *objs)
{
	u32_t i,len;
	compobj_c **obj=(compobj_c**)vpsGetStorage(objs);
	len=vpsCount(objs);
	if(len<=100)
		for(i=0;i<len;i++)
			printf("OBJ %u, VAL %d\n",i,obj[i]->val);
}

int main()
{
	u32_t start,i;
	vp_stack_t objs, *st=&objs;
	srand(timeTickCount());
	logStartConsoleLog();
	vpsInit(st);
	for(i=0;i<MAX;i++)
		vpsPush(st,new compobj_c(rand()));
	dump_objects(st);
	start=timeTickCount();
	vpsSort(st,comp,NULL);
	start=timeTickCount()-start;
	printf("%u objects, %u milliseconds.\n",MAX,start);
	dump_objects(st);
	for(i=0;i<MAX;i++)
		delete (compobj_c*)st->buf[i];
	vpsClear(st);
	
	/*
	for(i=0;i<MAX;i++)
		vpsPush(st,new compobj_c(rand()));
	dump_objects(st);
	start=timeTickCount();
	qsort(vpsGetStorage(st),vpsCount(st),sizeof(void*),rtl_compar);
	start=timeTickCount()-start;
	printf("RTL qsort: %u objects, %u milliseconds.\n",MAX,start);
	dump_objects(st);
	for(i=0;i<MAX;i++)
		delete (compobj_c*)st->buf[i];
	vpsClear(st);
	*/
	
	return 0;
}
