circular-while, fix of by 1 error; add tests

db4
Jon Harper 2013-07-29 22:28:11 +02:00 committed by John Benediktsson
parent b5700cd3f5
commit 3a48ab4688
2 changed files with 22 additions and 2 deletions

View File

@ -43,6 +43,26 @@ IN: circular.tests
] with each >array ] with each >array
] unit-test ] unit-test
[ V{ 1 2 3 } ] [
{ 1 2 3 } <circular> V{ } [
[ push f ] curry circular-while
] keep
] unit-test
CONSTANT: test-sequence1 { t f f f }
[ V{ 1 2 3 1 } ] [
{ 1 2 3 } <circular> V{ } [
[ [ push ] [ length 1 - test-sequence1 nth ] bi ] curry circular-while
] keep
] unit-test
CONSTANT: test-sequence2 { t f t t f f t t t f f f }
[ V{ 1 2 3 1 2 3 1 2 3 1 2 3 } ] [
{ 1 2 3 } <circular> V{ } [
[ [ push ] [ length 1 - test-sequence2 nth ] bi ] curry circular-while
] keep
] unit-test
[ V{ 1 2 3 1 2 } ] [ [ V{ 1 2 3 1 2 } ] [
{ 1 2 3 } <circular> V{ } [ { 1 2 3 } <circular> V{ } [
[ [ push ] [ length 5 < ] bi ] curry circular-while* [ [ push ] [ length 5 < ] bi ] curry circular-while*

View File

@ -60,14 +60,14 @@ TUPLE: circular-iterator
{ circular read-only } { n integer } { last-start integer } ; { circular read-only } { n integer } { last-start integer } ;
: <circular-iterator> ( circular -- obj ) : <circular-iterator> ( circular -- obj )
0 0 circular-iterator boa ; inline 0 -1 circular-iterator boa ; inline
<PRIVATE <PRIVATE
: (circular-while) ( ... iterator quot: ( ... obj -- ... ? ) -- ... ) : (circular-while) ( ... iterator quot: ( ... obj -- ... ? ) -- ... )
[ [ [ n>> ] [ circular>> ] bi nth ] dip call ] 2keep [ [ [ n>> ] [ circular>> ] bi nth ] dip call ] 2keep
rot [ [ dup n>> >>last-start ] dip ] when rot [ [ dup n>> >>last-start ] dip ] when
over [ n>> ] [ [ last-start>> ] [ circular>> length ] bi + 1 - ] bi = [ over [ n>> ] [ [ last-start>> ] [ circular>> length ] bi + ] bi = [
2drop 2drop
] [ ] [
[ [ 1 + ] change-n ] dip (circular-while) [ [ 1 + ] change-n ] dip (circular-while)