]> gitweb.factorcode.org Git - factor.git/blob - extra/circular/circular.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / extra / circular / circular.factor
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
4 accessors ;
5 IN: circular
6
7 ! a circular sequence wraps another sequence, but begins at an
8 ! arbitrary element in the underlying sequence.
9 TUPLE: circular seq start ;
10
11 : <circular> ( seq -- circular )
12     0 circular boa ;
13
14 : circular-wrap ( n circular -- n circular )
15     [ start>> + ] keep
16     [ seq>> length rem ] keep ; inline
17
18 M: circular length seq>> length ;
19
20 M: circular virtual@ circular-wrap seq>> ;
21
22 M: circular virtual-seq seq>> ;
23
24 : change-circular-start ( n circular -- )
25     #! change start to (start + n) mod length
26     circular-wrap (>>start) ;
27
28 : push-circular ( elt circular -- )
29     [ set-first ] [ 1 swap change-circular-start ] bi ;
30
31 : <circular-string> ( n -- circular )
32     0 <string> <circular> ;
33
34 INSTANCE: circular virtual-sequence
35
36 TUPLE: growing-circular < circular length ;
37
38 M: growing-circular length length>> ;
39
40 : full? ( circular -- ? )
41     [ length ] [ seq>> length ] bi = ;
42
43 : set-peek ( elt seq -- )
44     [ length 1- ] keep set-nth ;
45
46 : push-growing-circular ( elt circular -- )
47     dup full? [ push-circular ]
48     [ [ 1+ ] change-length set-peek ] if ;
49
50 : <growing-circular> ( capacity -- growing-circular )
51     { } new-sequence 0 0 growing-circular boa ;