2007-11-02 15:41:19 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								! Copyright 2007 Ryan Murphy 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								! See http://factorcode.org/license.txt for BSD license. 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2007-12-20 00:34:30 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								USING:  arrays  kernel  math  namespaces  tools.test 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								heaps heaps.private ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-02 15:41:19 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								IN:  temporary 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2008-02-06 14:47:19 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								[ <min-heap> heap-pop ] must-fail
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[ <max-heap> heap-pop ] must-fail
							 
						 
					
						
							
								
									
										
										
										
											2007-11-02 15:41:19 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[ t  ] [ <min-heap> heap-empty? ] unit-test
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:35:44 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								[ f  ] [ <min-heap> 1  t  pick  heap-push heap-empty? ] unit-test
							 
						 
					
						
							
								
									
										
										
										
											2007-11-02 15:41:19 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								[ t  ] [ <max-heap> heap-empty? ] unit-test
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:35:44 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								[ f  ] [ <max-heap> 1  t  pick  heap-push heap-empty? ] unit-test
							 
						 
					
						
							
								
									
										
										
										
											2007-11-02 15:41:19 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								! Binary Min Heap 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ 1  2  3  4  5  6  } [ 0  left 0  right 1  left 1  right 2  left 2  right ] unit-test
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:10:26 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ t  } [ { 5  t  } { 3  t  } T{ min-heap } heap-compare ] unit-test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ f  } [ { 5  t  } { 3  t  } T{ max-heap } heap-compare ] unit-test
							 
						 
					
						
							
								
									
										
										
										
											2007-11-02 15:41:19 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:10:26 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								[ T{ min-heap T{ heap f  V{ { -6  t  } { -4  t  } { 2  t  } { 1  t  } { 5  t  } { 3  t  } { 2  t  } { 4  t  } { 3  t  } { 7  t  } { 6  t  } { 8  t  } { 3  t  } { 4  t  } { 4  t  } { 6  t  } { 5  t  } { 5  t  } } } } ]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[ <min-heap> { { 3  t  } { 5  t  } { 4  t  } { 6  t  } { 7  t  } { 8  t  } { 2  t  } { 4  t  } { 3  t  } { 5  t  } { 6  t  } { 1  t  } { 3  t  } { 2  t  } { 4  t  } { 5  t  } { -6  t  } { -4  t  } } over  heap-push-all ] unit-test
							 
						 
					
						
							
								
									
										
										
										
											2007-11-02 15:41:19 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:10:26 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								[ T{ min-heap T{ heap f  V{ { 5  t  } { 6  t  } { 6  t  } { 7  t  } { 8  t  } } } } ] [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    <min-heap> { { 3  t  } { 5  t  } { 4  t  } { 6  t  } { 5  t  } { 7  t  } { 6  t  } { 8  t  } } over  heap-push-all
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 02:42:37 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    3  [ dup  heap-pop* ] times
 
							 
						 
					
						
							
								
									
										
										
										
											2007-11-02 15:41:19 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								] unit-test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:48:22 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								[ t  2  ] [ <min-heap> t  300  pick  heap-push t  200  pick  heap-push t  400  pick  heap-push t  3  pick  heap-push t  2  pick  heap-push heap-pop ] unit-test
							 
						 
					
						
							
								
									
										
										
										
											2007-11-02 15:41:19 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:48:22 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								[ t  1  ] [ <min-heap> t  300  pick  heap-push t  200  pick  heap-push t  400  pick  heap-push t  3  pick  heap-push t  2  pick  heap-push t  1  pick  heap-push heap-pop ] unit-test
							 
						 
					
						
							
								
									
										
										
										
											2007-11-02 15:41:19 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:48:22 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								[ t  400  ] [ <max-heap> t  300  pick  heap-push t  200  pick  heap-push t  400  pick  heap-push t  3  pick  heap-push t  2  pick  heap-push t  1  pick  heap-push heap-pop ] unit-test
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:10:26 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[ 0  ] [ <max-heap> heap-length ] unit-test
							 
						 
					
						
							
								
									
										
										
										
											2007-11-05 12:48:22 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								[ 1  ] [ <max-heap> t  1  pick  heap-push heap-length ] unit-test
							 
						 
					
						
							
								
									
										
										
										
											2007-12-20 00:34:30 -05:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[ { { 1  2  } { 3  4  } { 5  6  } } ] [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    T{ min-heap T{ heap f  V{ { 2  1  } { 4  3  } { 6  5  } } } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    [ [ 10  <  nip  ] [ 2array  , ] heap-pop-while ] { } make
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								] unit-test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[ { { 1  2  } } ] [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    T{ min-heap T{ heap f  V{ { 2  1  } { 4  3  } { 6  5  } } } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    [ [ 3  <  nip  ] [ 2array  , ] heap-pop-while ] { } make
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								] unit-test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[ { } ] [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    T{ min-heap T{ heap f  V{ { 2  1  } { 4  3  } { 6  5  } } } }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    [ [ 1  <  nip  ] [ 2array  , ] heap-pop-while ] { } make
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								] unit-test