1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel kernel.private math math.private
4 sequences sequences.private ;
12 M: growable length length>> ;
13 M: growable nth-unsafe underlying>> nth-unsafe ;
14 M: growable set-nth-unsafe underlying>> set-nth-unsafe ;
16 : capacity ( seq -- n ) underlying>> length ; inline
18 : expand ( len seq -- )
19 [ resize ] change-underlying drop ; inline
21 : contract ( len seq -- )
23 [ [ 0 ] 2dip set-nth-unsafe ] curry
24 (each-integer) ; inline
26 : growable-check ( n seq -- n seq )
27 over 0 < [ bounds-error ] when ; inline
29 M: growable set-length ( n seq -- )
34 2dup capacity > [ 2dup expand ] when
38 : new-size ( old -- new ) 1+ 3 * ; inline
40 : ensure ( n seq -- n seq )
43 2dup capacity >= [ over new-size over expand ] when
45 over 1 fixnum+fast over (>>length)
50 M: growable set-nth ensure set-nth-unsafe ;
52 M: growable clone (clone) [ clone ] change-underlying ;
54 M: growable lengthen ( n seq -- )
56 2dup capacity > [ over new-size over expand ] when
60 M: growable shorten ( n seq -- )
67 INSTANCE: growable sequence