]> gitweb.factorcode.org Git - factor.git/blob - basis/specialized-arrays/specialized-arrays.factor
711354d8034970a2120dd6780b8b6bccafa7b29b
[factor.git] / basis / specialized-arrays / specialized-arrays.factor
1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.data alien.parser
4 assocs byte-arrays classes compiler.units functors kernel lexer
5 libc math math.vectors math.vectors.private
6 math.vectors.specialization namespaces
7 parser prettyprint.custom sequences sequences.private strings
8 summary vocabs vocabs.loader vocabs.parser vocabs.generated
9 words fry combinators make ;
10 IN: specialized-arrays
11
12 MIXIN: specialized-array
13
14 INSTANCE: specialized-array sequence
15
16 GENERIC: direct-array-syntax ( obj -- word )
17
18 ERROR: bad-byte-array-length byte-array type ;
19
20 M: bad-byte-array-length summary
21     drop "Byte array length doesn't divide type width" ;
22
23 : (underlying) ( n c-type -- array )
24     heap-size * (byte-array) ; inline
25
26 : <underlying> ( n type -- array )
27     heap-size * <byte-array> ; inline
28
29 <PRIVATE
30
31 FUNCTOR: define-array ( T -- )
32
33 A            DEFINES-CLASS ${T}-array
34 S            DEFINES-CLASS ${T}-sequence
35 <A>          DEFINES <${A}>
36 (A)          DEFINES (${A})
37 <direct-A>   DEFINES <direct-${A}>
38 malloc-A     DEFINES malloc-${A}
39 >A           DEFINES >${A}
40 byte-array>A DEFINES byte-array>${A}
41
42 A{           DEFINES ${A}{
43 A@           DEFINES ${A}@
44
45 NTH          [ T dup c-type-getter-boxer array-accessor ]
46 SET-NTH      [ T dup c-setter array-accessor ]
47
48 WHERE
49
50 MIXIN: S
51
52 TUPLE: A
53 { underlying c-ptr read-only }
54 { length array-capacity read-only } ;
55
56 : <direct-A> ( alien len -- specialized-array ) A boa ; inline
57
58 : <A> ( n -- specialized-array )
59     [ \ T <underlying> ] keep <direct-A> ; inline
60
61 : (A) ( n -- specialized-array )
62     [ \ T (underlying) ] keep <direct-A> ; inline
63
64 : malloc-A ( len -- specialized-array )
65     [ \ T heap-size calloc ] keep <direct-A> ; inline
66
67 : byte-array>A ( byte-array -- specialized-array )
68     >c-ptr dup length \ T heap-size /mod 0 =
69     [ drop \ T bad-byte-array-length ] unless
70     <direct-A> ; inline
71
72 M: A new-underlying drop byte-array>A ;
73
74 M: A clone [ underlying>> clone ] [ length>> ] bi <direct-A> ; inline
75
76 M: A length length>> ; inline
77
78 M: A nth-unsafe underlying>> NTH call ; inline
79
80 M: A set-nth-unsafe underlying>> SET-NTH call ; inline
81
82 : >A ( seq -- specialized-array ) A new clone-like ;
83
84 M: A like drop dup A instance? [ >A ] unless ; inline
85
86 M: A new-sequence drop (A) ; inline
87
88 M: A equal? over A instance? [ sequence= ] [ 2drop f ] if ;
89
90 M: A resize
91     [
92         [ \ T heap-size * ] [ underlying>> ] bi*
93         resize-byte-array
94     ] [ drop ] 2bi
95     <direct-A> ; inline
96
97 M: A byte-length length \ T heap-size * ; inline
98
99 M: A element-type drop \ T ; inline
100
101 M: A direct-array-syntax drop \ A@ ;
102
103 M: A pprint-delims drop \ A{ \ } ;
104
105 M: A >pprint-sequence ;
106
107 SYNTAX: A{ \ } [ >A ] parse-literal ;
108 SYNTAX: A@ scan-object scan-object <direct-A> suffix! ;
109
110 INSTANCE: A specialized-array
111
112 A T c-type-boxed-class f specialize-vector-words
113
114 ;FUNCTOR
115
116 GENERIC: (underlying-type) ( c-type -- c-type' )
117
118 M: string (underlying-type) c-types get at ;
119 M: word (underlying-type) "c-type" word-prop ;
120
121 : underlying-type ( c-type -- c-type' )
122     dup (underlying-type) {
123         { [ dup not ] [ drop no-c-type ] }
124         { [ dup c-type-name? ] [ nip underlying-type ] }
125         [ drop ]
126     } cond ;
127
128 : specialized-array-vocab ( c-type -- vocab )
129     [
130         "specialized-arrays.instances." %
131         [ vocabulary>> % "." % ]
132         [ name>> % ]
133         bi
134     ] "" make ;
135
136 PRIVATE>
137
138 : define-array-vocab ( type -- vocab )
139     underlying-type
140     [ specialized-array-vocab ] [ '[ _ define-array ] ] bi
141     generate-vocab ;
142
143 M: c-type-name require-c-array define-array-vocab drop ;
144
145 ERROR: specialized-array-vocab-not-loaded c-type ;
146
147 M: c-type-name c-array-constructor
148     underlying-type
149     dup [ name>> "<" "-array>" surround ] [ specialized-array-vocab ] bi lookup
150     [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
151
152 M: c-type-name c-(array)-constructor
153     underlying-type
154     dup [ name>> "(" "-array)" surround ] [ specialized-array-vocab ] bi lookup
155     [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
156
157 M: c-type-name c-direct-array-constructor
158     underlying-type
159     dup [ name>> "<direct-" "-array>" surround ] [ specialized-array-vocab ] bi lookup
160     [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
161
162 SYNTAX: SPECIALIZED-ARRAYS:
163     ";" parse-tokens [ parse-c-type define-array-vocab use-vocab ] each ;
164
165 SYNTAX: SPECIALIZED-ARRAY:
166     scan-c-type define-array-vocab use-vocab ;
167
168 "prettyprint" vocab [
169     "specialized-arrays.prettyprint" require
170 ] when
171
172 "mirrors" vocab [
173     "specialized-arrays.mirrors" require
174 ] when