1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays growable kernel math sequences
9 { length array-capacity } ;
11 : <vector> ( n -- vector ) 0 <array> 0 vector boa ; inline
13 : >vector ( seq -- vector ) V{ } clone-like ;
17 dup array? [ dup length vector boa ] [ >vector ] if
20 M: vector new-sequence
21 drop [ f <array> ] [ integer>fixnum ] bi vector boa ; inline
24 over vector? [ sequence= ] [ 2drop f ] if ;
27 ! If we have an array, we're done.
28 ! If we have a vector, and it's at full capacity, we're done.
29 ! Otherwise, call resize-array, which is a relatively
33 [ length ] [ underlying>> ] bi
35 [ nip ] [ resize-array ] if
39 M: sequence new-resizable drop <vector> ; inline
41 INSTANCE: vector growable
43 : 1vector ( x -- vector ) 1array 1 vector boa ; inline
45 : ?push ( elt seq/f -- seq )
46 [ [ push ] keep ] [ 1vector ] if* ;