]> gitweb.factorcode.org Git - factor.git/blob - core/byte-vectors/byte-vectors.factor
Fix comments to be ! not #!.
[factor.git] / core / byte-vectors / byte-vectors.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors byte-arrays growable kernel math sequences
4 sequences.private ;
5 IN: byte-vectors
6
7 TUPLE: byte-vector
8 { underlying byte-array }
9 { length array-capacity } ;
10
11 : <byte-vector> ( n -- byte-vector )
12     (byte-array) 0 byte-vector boa ; inline
13
14 : >byte-vector ( seq -- byte-vector )
15     >byte-array dup length byte-vector boa ;
16
17 M: byte-vector like
18     drop dup byte-vector? [
19         dup byte-array?
20         [ dup length byte-vector boa ] [ >byte-vector ] if
21     ] unless ; inline
22
23 M: byte-vector new-sequence
24     drop [ (byte-array) ] [ >fixnum ] bi byte-vector boa ; inline
25
26 M: byte-vector equal?
27     over byte-vector? [ sequence= ] [ 2drop f ] if ;
28
29 M: byte-vector contract 2drop ; inline
30
31 M: byte-array like
32     ! If we have an byte-array, we're done.
33     ! If we have a byte-vector, and it's at full capacity,
34     ! we're done. Otherwise, call resize-byte-array, which is a
35     ! relatively fast primitive.
36     drop dup byte-array? [
37         dup byte-vector? [
38             [ length ] [ underlying>> ] bi
39             2dup length eq?
40             [ nip ] [ resize-byte-array ] if
41         ] [ >byte-array ] if
42     ] unless ; inline
43
44 M: byte-array new-resizable drop <byte-vector> ; inline
45
46 M: byte-vector new-resizable drop <byte-vector> ; inline
47
48 INSTANCE: byte-vector growable