]> gitweb.factorcode.org Git - factor.git/blob - basis/specialized-arrays/specialized-arrays.factor
fe2a93844cf9ccf8f034fd8e78b7c2810bd160dc
[factor.git] / basis / specialized-arrays / specialized-arrays.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.data alien.parser
4 assocs byte-arrays classes compiler.units functors kernel lexer
5 libc math math.vectors math.vectors.private namespaces
6 parser prettyprint.custom sequences sequences.private strings
7 summary vocabs vocabs.loader vocabs.parser vocabs.generated
8 words fry combinators make ;
9 IN: specialized-arrays
10
11 MIXIN: specialized-array
12
13 INSTANCE: specialized-array sequence
14
15 GENERIC: direct-array-syntax ( obj -- word )
16
17 ERROR: bad-byte-array-length byte-array type ;
18
19 M: bad-byte-array-length summary
20     drop "Byte array length doesn't divide type width" ;
21
22 ERROR: not-a-byte-array alien ;
23
24 M: not-a-byte-array summary
25     drop "Not a byte array" ;
26
27 : (underlying) ( n c-type -- array )
28     heap-size * (byte-array) ; inline
29
30 : <underlying> ( n type -- array )
31     heap-size * <byte-array> ; inline
32
33 <PRIVATE
34
35 FUNCTOR: define-array ( T -- )
36
37 A            DEFINES-CLASS ${T}-array
38 <A>          DEFINES <${A}>
39 (A)          DEFINES (${A})
40 <direct-A>   DEFINES <direct-${A}>
41 malloc-A     DEFINES malloc-${A}
42 >A           DEFINES >${A}
43 byte-array>A DEFINES byte-array>${A}
44
45 A{           DEFINES ${A}{
46 A@           DEFINES ${A}@
47
48 NTH          [ T dup c-type-getter-boxer array-accessor ]
49 SET-NTH      [ T dup c-setter array-accessor ]
50
51 WHERE
52
53 TUPLE: A
54 { underlying c-ptr read-only }
55 { length array-capacity read-only } ; final
56
57 : <direct-A> ( alien len -- specialized-array ) A boa ; inline
58
59 : <A> ( n -- specialized-array )
60     [ \ T <underlying> ] keep <direct-A> ; inline
61
62 : (A) ( n -- specialized-array )
63     [ \ T (underlying) ] keep <direct-A> ; inline
64
65 : malloc-A ( len -- specialized-array )
66     [ \ T heap-size calloc ] keep <direct-A> ; inline
67
68 : byte-array>A ( byte-array -- specialized-array )
69     >c-ptr dup byte-array? [
70         dup length \ T heap-size /mod 0 =
71         [ <direct-A> ]
72         [ drop \ T bad-byte-array-length ] if
73     ] [ not-a-byte-array ] if ; inline
74
75 M: A clone [ underlying>> clone ] [ length>> ] bi <direct-A> ; inline
76
77 M: A length length>> ; inline
78
79 M: A nth-unsafe underlying>> NTH call ; inline
80
81 M: A set-nth-unsafe underlying>> SET-NTH call ; inline
82
83 : >A ( seq -- specialized-array ) A new clone-like ;
84
85 M: A like drop dup A instance? [ >A ] unless ; inline
86
87 M: A new-sequence drop (A) ; inline
88
89 M: A equal? over A instance? [ sequence= ] [ 2drop f ] if ;
90
91 M: A resize
92     [
93         [ \ T heap-size * ] [ underlying>> ] bi*
94         resize-byte-array
95     ] [ drop ] 2bi
96     <direct-A> ; inline
97
98 M: A byte-length length \ T heap-size * ; inline
99
100 M: A direct-array-syntax drop \ A@ ;
101
102 M: A pprint-delims drop \ A{ \ } ;
103
104 M: A >pprint-sequence ;
105
106 SYNTAX: A{ \ } [ >A ] parse-literal ;
107 SYNTAX: A@ scan-object scan-object <direct-A> suffix! ;
108
109 INSTANCE: A specialized-array
110
111 M: A vs+ [ + \ T c-type-clamp ] 2map ; inline
112 M: A vs- [ - \ T c-type-clamp ] 2map ; inline
113 M: A vs* [ * \ T c-type-clamp ] 2map ; inline
114
115 M: A v*high [ * \ T heap-size neg shift ] 2map ; inline
116
117 ;FUNCTOR
118
119 : (underlying-type) ( word -- c-type ) "c-type" word-prop ; inline
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