]> gitweb.factorcode.org Git - factor.git/blob - core/vectors/vectors.factor
Faster conversion of sbufs, vectors and byte-vectors to their corresponding fixed...
[factor.git] / core / vectors / vectors.factor
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
4 accessors ;
5 IN: vectors
6
7 TUPLE: vector
8 { underlying array }
9 { length array-capacity } ;
10
11 : <vector> ( n -- vector ) f <array> 0 vector boa ; inline
12
13 : >vector ( seq -- vector ) V{ } clone-like ;
14
15 M: vector like
16     drop dup vector? [
17         dup array? [ dup length vector boa ] [ >vector ] if
18     ] unless ;
19
20 M: vector new-sequence
21     drop [ f <array> ] [ >fixnum ] bi vector boa ;
22
23 M: vector equal?
24     over vector? [ sequence= ] [ 2drop f ] if ;
25
26 M: array like
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
30     #! fast primitive.
31     drop dup array? [
32         dup vector? [
33             [ length ] [ underlying>> ] bi
34             2dup length eq?
35             [ nip ] [ resize-array ] if
36         ] [ >array ] if
37     ] unless ;
38
39 M: sequence new-resizable drop <vector> ;
40
41 INSTANCE: vector growable
42
43 : 1vector ( x -- vector ) 1array >vector ;
44
45 : ?push ( elt seq/f -- seq )
46     [ 1 <vector> ] unless* [ push ] keep ;