From a4c22303b1044eee5ccbf0766895a879a8f4810e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 19 Feb 2008 21:51:48 +0000 Subject: 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. --- src/tests/heap.cpp | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) (limited to 'src/tests/heap.cpp') 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 @@ #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 > hInt; + Bu::Heap hNum; - for( int j = 0; j < 15; j++ ) + for( int j = 0; j < 30; j++ ) { int r = rand()%10; printf("Pushing: %d, top: ", r ); - hInt.push( r ); - printf("%d\n", hInt.peek() ); + hNum.enqueue( num( r, j ) ); + printf("%d\n", hNum.peek().iNum ); } - for( int j = 0; j < 15; j++ ) + while( !hNum.isEmpty() ) { - printf("%d ", hInt.peek() ); - hInt.pop(); + printf("(%d:%d) ", hNum.peek().iOrder, hNum.peek().iNum ); + hNum.dequeue(); } printf("\n"); - -// hInt.print(); return 0; } -- cgit v1.2.3