From ded88583efc35b2604e4554c6e9f6cedcedae16c Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 20 Dec 2007 00:06:36 -0600 Subject: [PATCH 1/3] fixed heap-push-all --- core/heaps/heaps.factor | 3 +-- extra/assoc-heaps/assoc-heaps-tests.factor | 9 +++++++++ extra/assoc-heaps/assoc-heaps.factor | 3 --- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/core/heaps/heaps.factor b/core/heaps/heaps.factor index f01b436e90..cd00dc0db3 100644 --- a/core/heaps/heaps.factor +++ b/core/heaps/heaps.factor @@ -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 ) diff --git a/extra/assoc-heaps/assoc-heaps-tests.factor b/extra/assoc-heaps/assoc-heaps-tests.factor index 656e7fc15c..24a7730847 100644 --- a/extra/assoc-heaps/assoc-heaps-tests.factor +++ b/extra/assoc-heaps/assoc-heaps-tests.factor @@ -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 [ heap-push-all ] keep ] unit-test diff --git a/extra/assoc-heaps/assoc-heaps.factor b/extra/assoc-heaps/assoc-heaps.factor index 0c44950923..552845b00e 100644 --- a/extra/assoc-heaps/assoc-heaps.factor +++ b/extra/assoc-heaps/assoc-heaps.factor @@ -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 ; From c6ce216e0367b5bb61acca288d1651b2f8be2a11 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Thu, 27 Dec 2007 16:38:54 -0600 Subject: [PATCH 2/3] Add ?first .. ?fourth to sequences.lib --- extra/sequences/lib/lib-tests.factor | 7 +++++++ extra/sequences/lib/lib.factor | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/extra/sequences/lib/lib-tests.factor b/extra/sequences/lib/lib-tests.factor index 72cf9ad9c4..717f463c45 100644 --- a/extra/sequences/lib/lib-tests.factor +++ b/extra/sequences/lib/lib-tests.factor @@ -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 diff --git a/extra/sequences/lib/lib.factor b/extra/sequences/lib/lib.factor index ea6fdd141b..442b5f317d 100644 --- a/extra/sequences/lib/lib.factor +++ b/extra/sequences/lib/lib.factor @@ -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 From ace3419a8a60db5be97a5973813d586ee6740aaa Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Fri, 4 Jan 2008 18:56:04 -0600 Subject: [PATCH 3/3] Remove circular dependency in io add file-lines --- core/io/files/files.factor | 7 ++++++- core/io/io.factor | 5 +---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/core/io/files/files.factor b/core/io/files/files.factor index 3a01cc7d82..350ea1dfa6 100755 --- a/core/io/files/files.factor +++ b/core/io/files/files.factor @@ -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: 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 ) lines ; + +: file-contents ( path -- str ) + dup swap file-length [ stream-copy ] keep >string ; diff --git a/core/io/io.factor b/core/io/io.factor index 9c5cf782e7..0336ffda78 100755 --- a/core/io/io.factor +++ b/core/io/io.factor @@ -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 [ stream-copy ] keep >string ; - -: file-contents ( path -- str ) - dup swap file-length [ stream-copy ] keep >string ;