PageRenderTime 207ms CodeModel.GetById 94ms app.highlight 4ms RepoModel.GetById 70ms app.codeStats 0ms

/extra/sequences/inserters/inserters.factor

http://github.com/abeaumont/factor
Unknown | 44 lines | 35 code | 9 blank | 0 comment | 0 complexity | 2e4e863fc4d72e5ac05e035fb23a5732 MD5 | raw file
 1! (c)2010 Joe Groff bsd license
 2USING: accessors fry growable kernel locals math sequences ;
 3IN: sequences.inserters
 4
 5TUPLE: offset-growable { underlying read-only } { offset read-only } ;
 6C: <offset-growable> offset-growable
 7INSTANCE: offset-growable virtual-sequence
 8M: offset-growable length
 9    [ underlying>> length ] [ offset>> ] bi - ; inline
10M: offset-growable virtual-exemplar
11    underlying>> ; inline
12M: offset-growable virtual@
13    [ offset>> + ] [ underlying>> ] bi ; inline
14M: offset-growable set-length
15    [ offset>> + ] [ underlying>> ] bi set-length ; inline
16
17MIXIN: inserter
18M: inserter like
19    nip underlying>> ; inline
20M: inserter new-resizable
21    [ drop 0 ] dip new-sequence ; inline
22M: inserter length
23    drop 0 ; inline
24
25TUPLE: appender { underlying read-only } ;
26C: <appender> appender
27
28INSTANCE: appender inserter
29
30M:: appender new-sequence ( len inserter -- sequence )
31    inserter underlying>> :> underlying
32    underlying length :> old-length
33    old-length len + :> new-length
34    new-length underlying set-length
35    underlying old-length <offset-growable> ; inline
36
37TUPLE: replacer { underlying read-only } ;
38C: <replacer> replacer
39
40INSTANCE: replacer inserter
41
42M: replacer new-sequence
43    underlying>> [ set-length ] keep ; inline
44