]> gitweb.factorcode.org Git - factor.git/blob - basis/specialized-vectors/specialized-vectors.factor
specialized-vectors: implement binary-object protocol for specialized-vectors
[factor.git] / basis / specialized-vectors / specialized-vectors.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.parser assocs
4 compiler.units functors growable kernel lexer math namespaces
5 parser prettyprint.custom sequences specialized-arrays
6 specialized-arrays.private strings vocabs vocabs.parser
7 vocabs.generated fry make ;
8 FROM: sequences.private => nth-unsafe ;
9 QUALIFIED: vectors.functor
10 IN: specialized-vectors
11
12 <PRIVATE
13
14 FUNCTOR: define-vector ( T -- )
15
16 V   DEFINES-CLASS ${T}-vector
17
18 A   IS      ${T}-array
19 <A> IS      <${A}>
20
21 >V  DEFERS >${V}
22 V{  DEFINES ${V}{
23
24 WHERE
25
26 V A <A> vectors.functor:define-vector
27
28 M: V contract 2drop ; inline
29
30 M: V element-size drop \ T heap-size ; inline
31
32 M: V pprint-delims drop \ V{ \ } ;
33
34 M: V >pprint-sequence ;
35
36 M: V pprint* pprint-object ;
37
38 M: V >c-ptr underlying>> underlying>> ; inline
39 M: V byte-length [ length ] [ element-size ] bi * ; inline
40
41 SYNTAX: V{ \ } [ >V ] parse-literal ;
42
43 INSTANCE: V growable
44
45 ;FUNCTOR
46
47 : specialized-vector-vocab ( c-type -- vocab )
48     [
49         "specialized-vectors.instances." %
50         [ vocabulary>> % "." % ]
51         [ name>> % ]
52         bi
53     ] "" make ;
54
55 PRIVATE>
56
57 : push-new ( vector -- new )
58     [ length ] keep ensure nth-unsafe ; inline
59
60 : define-vector-vocab ( type -- vocab )
61     underlying-type
62     [ specialized-vector-vocab ] [ '[ _ define-vector ] ] bi
63     generate-vocab ;
64
65 SYNTAX: SPECIALIZED-VECTORS:
66     ";" [
67         parse-c-type
68         [ define-array-vocab use-vocab ]
69         [ define-vector-vocab use-vocab ] bi
70     ] each-token ;
71
72 SYNTAX: SPECIALIZED-VECTOR:
73     scan-c-type
74     [ define-array-vocab use-vocab ]
75     [ define-vector-vocab use-vocab ] bi ;