| 
									
										
										
										
											2010-05-29 04:12:49 -04:00
										 |  |  | ! Copyright (C) 2008, 2010 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							|  |  |  | USING: accessors kernel math math.ranges math.order math.parser | 
					
						
							|  |  |  | io locals sequences ;
 | 
					
						
							| 
									
										
										
										
											2008-04-20 06:15:46 -04:00
										 |  |  | IN: benchmark.binary-trees | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: tree-node item left right ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C: <tree-node> tree-node | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : bottom-up-tree ( item depth -- tree )
 | 
					
						
							|  |  |  |     dup 0 > [ | 
					
						
							|  |  |  |         1 -
 | 
					
						
							|  |  |  |         [ drop ] | 
					
						
							| 
									
										
										
										
											2008-12-17 20:28:07 -05:00
										 |  |  |         [ [ 2 * 1 - ] dip bottom-up-tree ] | 
					
						
							|  |  |  |         [ [ 2 *     ] dip bottom-up-tree ] 2tri
 | 
					
						
							| 
									
										
										
										
											2008-04-20 06:15:46 -04:00
										 |  |  |     ] [ | 
					
						
							|  |  |  |         drop f f
 | 
					
						
							| 
									
										
										
										
											2008-09-03 04:39:49 -04:00
										 |  |  |     ] if <tree-node> ; inline recursive
 | 
					
						
							| 
									
										
										
										
											2008-04-20 06:15:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | GENERIC: item-check ( node -- n )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: tree-node item-check | 
					
						
							|  |  |  |     [ item>> ] [ left>> ] [ right>> ] tri [ item-check ] bi@ - + ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: f item-check drop 0 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-22 20:08:45 -05:00
										 |  |  | CONSTANT: min-depth 4
 | 
					
						
							| 
									
										
										
										
											2008-04-20 06:15:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : stretch-tree ( max-depth -- )
 | 
					
						
							|  |  |  |     1 + 0 over bottom-up-tree item-check | 
					
						
							| 
									
										
										
										
											2010-05-29 04:12:49 -04:00
										 |  |  |     [ "stretch tree of depth " write number>string write ] | 
					
						
							|  |  |  |     [ "\t check: " write number>string print ] bi* ; inline
 | 
					
						
							| 
									
										
										
										
											2008-04-20 06:15:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | :: long-lived-tree ( max-depth -- )
 | 
					
						
							|  |  |  |     0 max-depth bottom-up-tree | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     min-depth max-depth 2 <range> [| depth | | 
					
						
							|  |  |  |         max-depth depth - min-depth + 2^ [ | 
					
						
							|  |  |  |             [1,b] 0 [ | 
					
						
							| 
									
										
										
										
											2008-04-20 07:28:18 -04:00
										 |  |  |                 dup neg
 | 
					
						
							|  |  |  |                 [ depth bottom-up-tree item-check + ] bi@
 | 
					
						
							| 
									
										
										
										
											2008-04-20 06:15:46 -04:00
										 |  |  |             ] reduce
 | 
					
						
							|  |  |  |         ] | 
					
						
							| 
									
										
										
										
											2010-05-29 04:12:49 -04:00
										 |  |  |         [ 2 * number>string write ] bi
 | 
					
						
							|  |  |  |         "\t trees of depth " write depth number>string write
 | 
					
						
							|  |  |  |         "\t check: " write number>string print
 | 
					
						
							| 
									
										
										
										
											2008-04-20 06:15:46 -04:00
										 |  |  |     ] each
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-29 04:12:49 -04:00
										 |  |  |     "long lived tree of depth " write max-depth number>string write
 | 
					
						
							|  |  |  |     "\t check: " write item-check number>string print ; inline
 | 
					
						
							| 
									
										
										
										
											2008-04-20 07:28:18 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : binary-trees ( n -- )
 | 
					
						
							| 
									
										
										
										
											2008-09-03 04:39:49 -04:00
										 |  |  |     min-depth 2 + max [ stretch-tree ] [ long-lived-tree ] bi ; inline
 | 
					
						
							| 
									
										
										
										
											2008-04-20 07:28:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-19 20:35:47 -04:00
										 |  |  | : binary-trees-benchmark ( -- )
 | 
					
						
							| 
									
										
										
										
											2008-04-20 07:28:18 -04:00
										 |  |  |     16 binary-trees ;
 | 
					
						
							| 
									
										
										
										
											2008-05-07 22:37:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-19 20:35:47 -04:00
										 |  |  | MAIN: binary-trees-benchmark |