1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays kernel math sequences sequences.private growable
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> ] [ >fixnum ] bi vector boa ;
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> ;
41 INSTANCE: vector growable
43 : 1vector ( x -- vector ) 1array >vector ;
45 : ?push ( elt seq/f -- seq )
46 [ 1 <vector> ] unless* [ push ] keep ;