aboutsummaryrefslogtreecommitdiff
path: root/src/tests/heap.cpp
blob: e35ea2ab15a0a6c6c69b4ba1fe8f631c295c571a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdlib.h>
#include <stdio.h>

#include "bu/heap.h"

typedef struct num
{
	num( int iNum, int iOrder ) : iNum( iNum ), iOrder( iOrder )
	{
	}

	num( const num &src ) : iNum( src.iNum ), iOrder( src.iOrder )
	{
	}

	int iNum;
	int iOrder;

	bool operator<( const num &oth ) const
	{
		if( iNum == oth.iNum )
			return iOrder < oth.iOrder;
		return iNum < oth.iNum;
	}
	bool operator>( const num &oth ) const
	{
		return iNum > oth.iNum;
	}
} num;

int main()
{
	Bu::Heap<num> hNum;

	for( int j = 0; j < 30; j++ )
	{
		int r = rand()%10;
		printf("Pushing: %d, top: ", r );
		hNum.enqueue( num( r, j ) );
		printf("%d\n", hNum.peek().iNum );
	}

	while( !hNum.isEmpty() )
	{
		printf("(%d:%d) ", hNum.peek().iOrder, hNum.peek().iNum );
		hNum.dequeue();
	}
	printf("\n");

	return 0;
}