1 ! Copyright (C) 2005, 2006 Alex Chapman, Daniel Ehrenberg
2 ! See http;//factorcode.org/license.txt for BSD license
3 USING: kernel sequences math sequences.private strings
7 ! a circular sequence wraps another sequence, but begins at an
8 ! arbitrary element in the underlying sequence.
9 TUPLE: circular seq start ;
11 : <circular> ( seq -- circular )
15 : circular-wrap ( n circular -- n circular )
17 [ seq>> length rem ] keep ; inline
20 M: circular length seq>> length ;
22 M: circular virtual@ circular-wrap seq>> ;
24 M: circular virtual-seq seq>> ;
26 : change-circular-start ( n circular -- )
27 #! change start to (start + n) mod length
28 circular-wrap (>>start) ;
30 : push-circular ( elt circular -- )
31 [ set-first ] [ 1 swap change-circular-start ] bi ;
33 : <circular-string> ( n -- circular )
34 0 <string> <circular> ;
36 INSTANCE: circular virtual-sequence
38 TUPLE: growing-circular < circular length ;
40 M: growing-circular length length>> ;
43 : full? ( circular -- ? )
44 [ length ] [ seq>> length ] bi = ;
46 : set-peek ( elt seq -- )
47 [ length 1- ] keep set-nth ;
50 : push-growing-circular ( elt circular -- )
51 dup full? [ push-circular ]
52 [ [ 1+ ] change-length set-peek ] if ;
54 : <growing-circular> ( capacity -- growing-circular )
55 { } new-sequence 0 0 growing-circular boa ;