Merge branch 'master' of git://spitspat.com/git/factor

db4
Daniel Ehrenberg 2008-01-04 19:22:21 -06:00
commit 7692300311
7 changed files with 29 additions and 10 deletions

View File

@ -6,7 +6,6 @@ IN: heaps
MIXIN: priority-queue
GENERIC: heap-push ( value key heap -- )
GENERIC: heap-push-all ( assoc heap -- )
GENERIC: heap-peek ( heap -- value key )
GENERIC: heap-pop* ( heap -- )
GENERIC: heap-pop ( heap -- value key )
@ -107,7 +106,7 @@ M: priority-queue heap-push ( value key heap -- )
[ heap-data ] keep
up-heap ;
M: priority-queue heap-push-all ( assoc heap -- )
: heap-push-all ( assoc heap -- )
[ swapd heap-push ] curry assoc-each ;
M: priority-queue heap-peek ( heap -- value key )

View File

@ -3,7 +3,7 @@
IN: io.files
USING: io.backend io.files.private io hashtables kernel math
memory namespaces sequences strings assocs arrays definitions
system combinators splitting ;
system combinators splitting sbufs ;
HOOK: <file-reader> io-backend ( path -- stream )
@ -157,3 +157,8 @@ HOOK: binary-roots io-backend ( -- seq )
PRIVATE>
: walk-dir ( path -- seq ) [ (walk-dir) ] { } make ;
: file-lines ( path -- seq ) <file-reader> lines ;
: file-contents ( path -- str )
dup <file-reader> swap file-length <sbuf> [ stream-copy ] keep >string ;

View File

@ -1,7 +1,7 @@
! Copyright (C) 2003, 2007 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: hashtables generic kernel math namespaces sequences strings
continuations assocs io.files io.styles sbufs ;
continuations assocs io.styles sbufs ;
IN: io
GENERIC: stream-close ( stream -- )
@ -90,6 +90,3 @@ SYMBOL: stdio
: contents ( stream -- str )
2048 <sbuf> [ stream-copy ] keep >string ;
: file-contents ( path -- str )
dup <file-reader> swap file-length <sbuf> [ stream-copy ] keep >string ;

View File

@ -44,3 +44,12 @@ T{
T{ max-heap T{ heap f V{ { 1 2 } { 0 1 } } } }
} heap-pop
] unit-test
[
T{
assoc-heap
f
H{ { 1 2 } { 3 4 } }
T{ min-heap T{ heap f V{ { 2 1 } { 4 3 } } } }
}
] [ H{ { 1 2 } { 3 4 } } H{ } clone <assoc-min-heap> [ heap-push-all ] keep ] unit-test

View File

@ -40,9 +40,6 @@ M: assoc-heap heap-peek ( assoc-heap -- value key )
M: assoc-heap heap-push ( value key assoc-heap -- )
set-at ;
M: assoc-heap heap-push-all ( assoc assoc-heap -- )
swap [ rot set-at ] curry* each ;
M: assoc-heap heap-pop ( assoc-heap -- value key )
dup assoc-heap-heap heap-pop swap
rot dupd assoc-heap-assoc delete-at ;

View File

@ -46,3 +46,10 @@ math.functions tools.test strings ;
[ { { 0 0 } { 1 0 } { 0 1 } { 1 1 } } ] [ 2 2 exact-strings ] unit-test
[ t ] [ "ab" 4 strings [ >string ] map "abab" swap member? ] unit-test
[ { { } { 1 } { 2 } { 1 2 } } ] [ { 1 2 } power-set ] unit-test
[ f ] [ { } ?first ] unit-test
[ f ] [ { } ?fourth ] unit-test
[ 1 ] [ { 1 2 3 } ?first ] unit-test
[ 2 ] [ { 1 2 3 } ?second ] unit-test
[ 3 ] [ { 1 2 3 } ?third ] unit-test
[ f ] [ { 1 2 3 } ?fourth ] unit-test

View File

@ -126,3 +126,8 @@ PRIVATE>
: human-sort ( seq -- newseq )
[ dup [ digit? ] [ string>number ] cut-all ] { } map>assoc
sort-values keys ;
: ?first ( seq -- first/f ) 0 swap ?nth ; inline
: ?second ( seq -- second/f ) 1 swap ?nth ; inline
: ?third ( seq -- third/f ) 2 swap ?nth ; inline
: ?fourth ( seq -- fourth/f ) 3 swap ?nth ; inline