1 ! Copyright (C) 2008 Slava Pestov.
\r
2 ! See http://factorcode.org/license.txt for BSD license.
\r
3 USING: arrays kernel kernel.private math sequences
\r
4 sequences.private growable byte-arrays accessors ;
\r
8 { underlying byte-array }
\r
9 { length array-capacity } ;
\r
11 : <byte-vector> ( n -- byte-vector )
\r
12 (byte-array) 0 byte-vector boa ; inline
\r
14 : >byte-vector ( seq -- byte-vector )
\r
15 T{ byte-vector f B{ } 0 } clone-like ;
\r
18 drop dup byte-vector? [
\r
20 [ dup length byte-vector boa ] [ >byte-vector ] if
\r
23 M: byte-vector new-sequence
\r
24 drop [ (byte-array) ] [ >fixnum ] bi byte-vector boa ;
\r
26 M: byte-vector equal?
\r
27 over byte-vector? [ sequence= ] [ 2drop f ] if ;
\r
30 #! If we have an byte-array, we're done.
\r
31 #! If we have a byte-vector, and it's at full capacity,
\r
32 #! we're done. Otherwise, call resize-byte-array, which is a
\r
33 #! relatively fast primitive.
\r
34 drop dup byte-array? [
\r
36 [ length ] [ underlying>> ] bi
\r
38 [ nip ] [ resize-byte-array ] if
\r
39 ] [ >byte-array ] if
\r
42 M: byte-array new-resizable drop <byte-vector> ;
\r
44 INSTANCE: byte-vector growable
\r