commit 8f434fb680c23b615ca83c5a4aa6be837025ad5f Author: unknown Date: Sat Dec 4 15:09:31 2010 -0800 growing-circular wouldn't insert well, especially after rotated diff --git a/basis/circular/circular-tests.factor b/basis/circular/circular-tests.factor index cda26df..2956a08 100644 --- a/basis/circular/circular-tests.factor +++ b/basis/circular/circular-tests.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2005, 2006 Alex Chapman, Daniel Ehrenberg ! See http;//factorcode.org/license.txt for BSD license -USING: arrays kernel tools.test sequences sequences.private +USING: arrays kernel tools.test sequences sequences.private combinators circular strings ; IN: circular.tests @@ -42,3 +42,89 @@ IN: circular.tests swap growing-circular-push ] with each >array ] unit-test + +[ { 15 1963 12 } ] +[ 8 +{ + [ 12 swap growing-circular-push ] + [ 15 swap growing-circular-push ] + [ 1963 swap growing-circular-push ] + [ rotate-circular ] + [ ] +} cleave >array +] unit-test + + +[ { 15 1963 12 "(December)" } ] +[ 8 +{ + [ 12 swap growing-circular-push ] + [ 15 swap growing-circular-push ] + [ 1963 swap growing-circular-push ] + [ rotate-circular ] + [ "(December)" swap growing-circular-push ] + [ ] +} cleave >array +] unit-test + + +[ { 1963 12 "(December)" 15 } ] +[ 8 +{ + [ 12 swap growing-circular-push ] + [ 15 swap growing-circular-push ] + [ 1963 swap growing-circular-push ] + [ rotate-circular ] + [ "(December)" swap growing-circular-push ] + [ rotate-circular ] + [ ] +} cleave >array +] unit-test + +{ T{ growing-circular + { seq { 12 15 1963 0 0 0 0 0 } } + { start 0 } + { length 3 } +} } + +[ 8 +{ + [ 12 swap growing-circular-push ] + [ 15 swap growing-circular-push ] + [ 1963 swap growing-circular-push ] + [ ] +} cleave +] unit-test + +{ T{ growing-circular + { seq { 12 15 1963 0 0 0 0 0 } } + { start 1 } + { length 3 } +} } + +[ 8 +{ + [ 12 swap growing-circular-push ] + [ 15 swap growing-circular-push ] + [ 1963 swap growing-circular-push ] + [ rotate-circular ] + [ ] +} cleave +] unit-test + +{ T{ growing-circular + { seq { 12 "end-of-current-loop?" 15 1963 0 0 0 0 } } + { start 2 } + { length 4 } +} } + +[ 8 +{ + [ 12 swap growing-circular-push ] + [ 15 swap growing-circular-push ] + [ 1963 swap growing-circular-push ] + [ rotate-circular ] + [ "end-of-current-loop?" swap growing-circular-push ] + [ ] +} cleave +] unit-test diff --git a/basis/circular/circular.factor b/basis/circular/circular.factor index db60bb1..d321033 100644 --- a/basis/circular/circular.factor +++ b/basis/circular/circular.factor @@ -1,10 +1,11 @@ ! Copyright (C) 2005, 2006 Alex Chapman, Daniel Ehrenberg ! See http;//factorcode.org/license.txt for BSD license -USING: kernel sequences math sequences.private strings +USING: kernel sequences math sequences.private strings accessors locals fry ; IN: circular -TUPLE: circular { seq read-only } { start integer } ; +! TUPLE: circular { seq read-only } { start integer } ; +TUPLE: circular { seq } { start integer } ; : ( seq -- circular ) 0 circular boa ; inline @@ -13,7 +14,7 @@ TUPLE: circular { seq read-only } { start integer } ; : circular-wrap ( n circular -- n circular ) [ start>> + ] keep - [ seq>> length rem ] keep ; inline + [ length rem ] keep ; inline PRIVATE> @@ -25,7 +26,7 @@ M: circular virtual-exemplar seq>> ; inline : change-circular-start ( n circular -- ) #! change start to (start + n) mod length - circular-wrap start<< ; inline + circular-wrap start<< ; inline : rotate-circular ( circular -- ) [ 1 ] dip change-circular-start ; inline @@ -46,12 +47,24 @@ M: growing-circular length length>> ; inline : full? ( circular -- ? ) [ length ] [ seq>> length ] bi = ; inline + +:: insert-last-growing ( elt circular -- ) +! 0 circular virtual@ + circular [ length ] [ ] bi virtual@ + swap cut but-last + elt prefix append + ! [ circular set-nth ] each-index ! runs afoul of virtual seq numbering? + [ circular ] dip >>seq + [ 1 + ] change-start drop + ; PRIVATE> -: growing-circular-push ( elt circular -- ) +: growing-circular-push ( elt circular -- ) dup full? [ circular-push ] - [ [ 1 + ] change-length set-last ] if ; + [ [ 1 + ] change-length [ ] [ start>> 0 = ] bi + [ set-last ] [ insert-last-growing ] if + ] if ; : ( capacity -- growing-circular ) { } new-sequence 0 0 growing-circular boa ; inline