aboutsummaryrefslogtreecommitdiff
path: root/src/tests/heap.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2008-02-19 21:51:48 +0000
committerMike Buland <eichlan@xagasoft.com>2008-02-19 21:51:48 +0000
commita4c22303b1044eee5ccbf0766895a879a8f4810e (patch)
treebe6563dfca81f57e4883a55c4517f5878c40dd4e /src/tests/heap.cpp
parent62287ea894a1587553f017d0f6a09d4c87ad3b1f (diff)
downloadlibbu++-a4c22303b1044eee5ccbf0766895a879a8f4810e.tar.gz
libbu++-a4c22303b1044eee5ccbf0766895a879a8f4810e.tar.bz2
libbu++-a4c22303b1044eee5ccbf0766895a879a8f4810e.tar.xz
libbu++-a4c22303b1044eee5ccbf0766895a879a8f4810e.zip
Oops. I made the Bu::Heap API look like a stack, not a queue, that's been
fixed, and the Bu::ItoHeap is working and tested. Note that when multiple items have the same sort order, they will come out in random order.
Diffstat (limited to '')
-rw-r--r--src/tests/heap.cpp41
1 files changed, 32 insertions, 9 deletions
diff --git a/src/tests/heap.cpp b/src/tests/heap.cpp
index 3217715..e35ea2a 100644
--- a/src/tests/heap.cpp
+++ b/src/tests/heap.cpp
@@ -3,26 +3,49 @@
3 3
4#include "bu/heap.h" 4#include "bu/heap.h"
5 5
6typedef struct num
7{
8 num( int iNum, int iOrder ) : iNum( iNum ), iOrder( iOrder )
9 {
10 }
11
12 num( const num &src ) : iNum( src.iNum ), iOrder( src.iOrder )
13 {
14 }
15
16 int iNum;
17 int iOrder;
18
19 bool operator<( const num &oth ) const
20 {
21 if( iNum == oth.iNum )
22 return iOrder < oth.iOrder;
23 return iNum < oth.iNum;
24 }
25 bool operator>( const num &oth ) const
26 {
27 return iNum > oth.iNum;
28 }
29} num;
30
6int main() 31int main()
7{ 32{
8 Bu::Heap<int, Bu::__basicGTCmp<int> > hInt; 33 Bu::Heap<num> hNum;
9 34
10 for( int j = 0; j < 15; j++ ) 35 for( int j = 0; j < 30; j++ )
11 { 36 {
12 int r = rand()%10; 37 int r = rand()%10;
13 printf("Pushing: %d, top: ", r ); 38 printf("Pushing: %d, top: ", r );
14 hInt.push( r ); 39 hNum.enqueue( num( r, j ) );
15 printf("%d\n", hInt.peek() ); 40 printf("%d\n", hNum.peek().iNum );
16 } 41 }
17 42
18 for( int j = 0; j < 15; j++ ) 43 while( !hNum.isEmpty() )
19 { 44 {
20 printf("%d ", hInt.peek() ); 45 printf("(%d:%d) ", hNum.peek().iOrder, hNum.peek().iNum );
21 hInt.pop(); 46 hNum.dequeue();
22 } 47 }
23 printf("\n"); 48 printf("\n");
24
25// hInt.print();
26 49
27 return 0; 50 return 0;
28} 51}