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 >byte-array dup length byte-vector boa ;
\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 ; inline
\r
26 M: byte-vector equal?
\r
27 over byte-vector? [ sequence= ] [ 2drop f ] if ;
\r
29 M: byte-vector contract 2drop ; inline
\r
32 #! If we have an byte-array, we're done.
\r
33 #! If we have a byte-vector, and it's at full capacity,
\r
34 #! we're done. Otherwise, call resize-byte-array, which is a
\r
35 #! relatively fast primitive.
\r
36 drop dup byte-array? [
\r
38 [ length ] [ underlying>> ] bi
\r
40 [ nip ] [ resize-byte-array ] if
\r
41 ] [ >byte-array ] if
\r
44 M: byte-array new-resizable drop <byte-vector> ; inline
\r
46 M: byte-vector new-resizable drop <byte-vector> ; inline
\r
48 INSTANCE: byte-vector growable
\r