1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
4 MIXIN: growable ! for bootstrap
5 USING: accessors kernel layouts math math.private sequences
11 M: growable length length>> ; inline
12 M: growable nth-unsafe underlying>> nth-unsafe ; inline
13 M: growable set-nth-unsafe underlying>> set-nth-unsafe ; inline
17 : push-unsafe ( elt seq -- )
18 [ length integer>fixnum-strict ] keep
19 [ set-nth-unsafe ] [ [ 1 fixnum+fast ] dip length<< ] 2bi ; inline
21 : push-all-unsafe ( from to src dst -- )
22 [ over - swap ] 2dip pickd [ length integer>fixnum-strict ] keep
23 [ [ fixnum+fast ] dip length<< ] 2keep <copy> (copy) drop ; inline
27 : capacity ( seq -- n ) underlying>> length ; inline
29 : expand ( len seq -- )
30 [ resize ] change-underlying drop ; inline
32 GENERIC: contract ( len seq -- )
36 [ [ 0 ] 2dip set-nth-unsafe ] curry
37 (each-integer) ; inline
39 M: growable set-length
44 2dup capacity > [ 2dup expand ] when
48 : new-size ( old -- new )
49 integer>fixnum-strict 1 fixnum+fast 2 fixnum*fast
50 dup 0 < [ drop most-positive-fixnum ] when ; inline
52 : ensure ( n seq -- n seq )
55 2dup capacity >= [ over new-size over expand ] when
56 [ integer>fixnum-strict ] dip
57 over 1 fixnum+fast >>length
59 [ integer>fixnum-strict ] dip
62 M: growable set-nth ensure set-nth-unsafe ; inline
64 M: growable clone (clone) [ clone ] change-underlying ; inline
68 2dup capacity > [ over new-size over expand ] when
79 M: growable new-resizable new-sequence 0 over set-length ; inline
81 INSTANCE: growable sequence