aboutsummaryrefslogtreecommitdiff
path: root/src/tests/heap.cpp
blob: 3576f25ded6cc7f4fc2f008c3b159c810c9941ef (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (C) 2007-2010 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

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

#include "bu/formatter.h"
#include "bu/heap.h"
#include "bu/string.h"
#include "bu/file.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;

void printHeap( Bu::Heap<Bu::String> &h, int j )
{
//	return;
	Bu::String sFName;
	sFName.format("graph-step-%02d.dot", j );
	Bu::File fOut( sFName, Bu::File::WriteNew );
	Bu::Formatter f( fOut );
	f << "Graph step: " << j << ", total size: " << h.getSize() << f.nl;
	for( Bu::Heap<Bu::String>::iterator i = h.begin(); i; i++ )
	{
		f << *i << f.nl;
	}
	f << f.nl;
}

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");
*/
	Bu::Heap<Bu::String> hStr;
	int j = 0;

	hStr.enqueue("George");
	printHeap( hStr, j++ );
	hStr.enqueue("George");
	printHeap( hStr, j++ );
	hStr.enqueue("Sam");
	printHeap( hStr, j++ );
	hStr.enqueue("Abby");
	printHeap( hStr, j++ );
	hStr.enqueue("Zorro");
	printHeap( hStr, j++ );
	hStr.enqueue("Brianna");
	printHeap( hStr, j++ );
	hStr.enqueue("Kate");
	printHeap( hStr, j++ );
	hStr.enqueue("Soggy");
	printHeap( hStr, j++ );

	while( !hStr.isEmpty() )
	{
		printf("\"%s\" ", hStr.dequeue().getStr() );
		printHeap( hStr, j++ );
	}
	printf("\n");

	Bu::List<Bu::String> lStr;

	lStr.insertSorted("George");
	lStr.insertSorted("George");
	lStr.insertSorted("Sam");
	lStr.insertSorted("Abby");
	lStr.insertSorted("Zorro");
	lStr.insertSorted("Brianna");
	lStr.insertSorted("Kate");
	lStr.insertSorted("Soggy");
	for( Bu::List<Bu::String>::iterator i = lStr.begin(); i; i++ )
	{
		printf("\"%s\" ", (*i).getStr() );
	}
	printf("\n");

	return 0;
}