]> gitweb.factorcode.org Git - factor.git/blob - contrib/gap-buffer/circular.factor
1aa1a672b200a5bf4770843c8c716de4830c8b84
[factor.git] / contrib / gap-buffer / circular.factor
1 USING: kernel sequences math generic sequences-internals ;
2 IN: circular
3
4 ! a circular sequence wraps another sequence, but begins at an arbitrary
5 ! element in the underlying sequence.
6 TUPLE: circular start ;
7
8 C: circular ( seq circular -- circular )
9     0 over set-circular-start [ set-delegate ] keep ;
10
11 : circular@ ( n circular -- n seq )
12     [ tuck circular-start + swap length mod ] keep delegate ;
13
14 M: circular nth ( n seq -- elt ) bounds-check circular@ nth ;
15
16 M: circular nth-unsafe ( n seq -- elt ) circular@ nth-unsafe ;
17
18 M: circular set-nth ( elt n seq -- ) bounds-check circular@ set-nth ;
19
20 M: circular set-nth-unsafe ( elt n seq -- ) circular@ set-nth-unsafe ;
21
22 : change-circular-start ( n circular -- )
23     #! change start to (start + n) mod length
24     [ circular@ drop ] keep set-circular-start ;
25