| 
									
										
										
										
											2009-09-04 23:01:55 -04:00
										 |  |  | ! Copyright (C) 2008, 2009 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2008-11-14 21:18:16 -05:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2009-09-21 00:16:02 -04:00
										 |  |  | USING: accessors alien alien.c-types alien.data alien.parser | 
					
						
							|  |  |  | assocs byte-arrays classes compiler.units functors kernel lexer | 
					
						
							| 
									
										
										
										
											2009-11-11 17:08:40 -05:00
										 |  |  | libc math math.vectors math.vectors.private namespaces | 
					
						
							| 
									
										
										
										
											2009-09-21 00:16:02 -04:00
										 |  |  | parser prettyprint.custom sequences sequences.private strings | 
					
						
							|  |  |  | summary vocabs vocabs.loader vocabs.parser vocabs.generated | 
					
						
							| 
									
										
										
										
											2009-10-19 05:41:53 -04:00
										 |  |  | words fry combinators make ;
 | 
					
						
							| 
									
										
										
										
											2008-11-14 21:18:16 -05:00
										 |  |  | IN: specialized-arrays | 
					
						
							| 
									
										
										
										
											2009-09-04 23:01:55 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | MIXIN: specialized-array | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-04 23:01:55 -04:00
										 |  |  | INSTANCE: specialized-array sequence | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: direct-array-syntax ( obj -- word )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | ERROR: bad-byte-array-length byte-array type ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: bad-byte-array-length summary | 
					
						
							|  |  |  |     drop "Byte array length doesn't divide type width" ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (underlying) ( n c-type -- array )
 | 
					
						
							|  |  |  |     heap-size * (byte-array) ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <underlying> ( n type -- array )
 | 
					
						
							|  |  |  |     heap-size * <byte-array> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | FUNCTOR: define-array ( T -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A            DEFINES-CLASS ${T}-array | 
					
						
							|  |  |  | <A>          DEFINES <${A}> | 
					
						
							|  |  |  | (A)          DEFINES (${A}) | 
					
						
							|  |  |  | <direct-A>   DEFINES <direct-${A}> | 
					
						
							|  |  |  | malloc-A     DEFINES malloc-${A} | 
					
						
							|  |  |  | >A           DEFINES >${A} | 
					
						
							|  |  |  | byte-array>A DEFINES byte-array>${A} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A{           DEFINES ${A}{ | 
					
						
							|  |  |  | A@           DEFINES ${A}@ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | NTH          [ T dup c-type-getter-boxer array-accessor ] | 
					
						
							|  |  |  | SET-NTH      [ T dup c-setter array-accessor ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | WHERE | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: A | 
					
						
							|  |  |  | { underlying c-ptr read-only } | 
					
						
							|  |  |  | { length array-capacity read-only } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <direct-A> ( alien len -- specialized-array ) A boa ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-02 00:12:53 -04:00
										 |  |  | : <A> ( n -- specialized-array )
 | 
					
						
							|  |  |  |     [ \ T <underlying> ] keep <direct-A> ; inline
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-02 00:12:53 -04:00
										 |  |  | : (A) ( n -- specialized-array )
 | 
					
						
							|  |  |  |     [ \ T (underlying) ] keep <direct-A> ; inline
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-02 00:12:53 -04:00
										 |  |  | : malloc-A ( len -- specialized-array )
 | 
					
						
							|  |  |  |     [ \ T heap-size calloc ] keep <direct-A> ; inline
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : byte-array>A ( byte-array -- specialized-array )
 | 
					
						
							| 
									
										
										
										
											2009-10-02 00:12:53 -04:00
										 |  |  |     >c-ptr dup length \ T heap-size /mod 0 =
 | 
					
						
							|  |  |  |     [ drop \ T bad-byte-array-length ] unless
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  |     <direct-A> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: A clone [ underlying>> clone ] [ length>> ] bi <direct-A> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: A length length>> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: A nth-unsafe underlying>> NTH call ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: A set-nth-unsafe underlying>> SET-NTH call ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : >A ( seq -- specialized-array ) A new clone-like ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: A like drop dup A instance? [ >A ] unless ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: A new-sequence drop (A) ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: A equal? over A instance? [ sequence= ] [ 2drop f ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: A resize | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2009-09-21 00:16:02 -04:00
										 |  |  |         [ \ T heap-size * ] [ underlying>> ] bi*
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  |         resize-byte-array | 
					
						
							|  |  |  |     ] [ drop ] 2bi
 | 
					
						
							|  |  |  |     <direct-A> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-21 00:16:02 -04:00
										 |  |  | M: A byte-length length \ T heap-size * ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-10 15:46:26 -04:00
										 |  |  | M: A direct-array-syntax drop \ A@ ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | M: A pprint-delims drop \ A{ \ } ;
 | 
					
						
							| 
									
										
										
										
											2009-09-10 15:46:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | M: A >pprint-sequence ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYNTAX: A{ \ } [ >A ] parse-literal ;
 | 
					
						
							| 
									
										
										
										
											2009-10-28 14:38:27 -04:00
										 |  |  | SYNTAX: A@ scan-object scan-object <direct-A> suffix! ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | INSTANCE: A specialized-array | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-18 23:48:10 -05:00
										 |  |  | M: A vs+ [ + \ T c-type-clamp ] 2map ; inline
 | 
					
						
							|  |  |  | M: A vs- [ - \ T c-type-clamp ] 2map ; inline
 | 
					
						
							|  |  |  | M: A vs* [ * \ T c-type-clamp ] 2map ; inline
 | 
					
						
							| 
									
										
										
										
											2009-12-04 15:43:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-18 23:48:10 -05:00
										 |  |  | M: A v*high [ * \ T heap-size neg shift ] 2map ; inline
 | 
					
						
							| 
									
										
										
										
											2009-12-05 14:32:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | ;FUNCTOR | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-15 22:43:18 -04:00
										 |  |  | GENERIC: (underlying-type) ( c-type -- c-type' )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: string (underlying-type) c-types get at ;
 | 
					
						
							|  |  |  | M: word (underlying-type) "c-type" word-prop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | : underlying-type ( c-type -- c-type' )
 | 
					
						
							| 
									
										
										
										
											2009-09-15 22:43:18 -04:00
										 |  |  |     dup (underlying-type) { | 
					
						
							| 
									
										
										
										
											2009-09-10 15:46:26 -04:00
										 |  |  |         { [ dup not ] [ drop no-c-type ] } | 
					
						
							| 
									
										
										
										
											2009-09-15 22:43:18 -04:00
										 |  |  |         { [ dup c-type-name? ] [ nip underlying-type ] } | 
					
						
							| 
									
										
										
										
											2009-09-10 15:46:26 -04:00
										 |  |  |         [ drop ] | 
					
						
							|  |  |  |     } cond ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : specialized-array-vocab ( c-type -- vocab )
 | 
					
						
							| 
									
										
										
										
											2009-10-19 05:41:53 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |         "specialized-arrays.instances." % | 
					
						
							|  |  |  |         [ vocabulary>> % "." % ] | 
					
						
							|  |  |  |         [ name>> % ] | 
					
						
							|  |  |  |         bi
 | 
					
						
							|  |  |  |     ] "" make ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-10 15:46:26 -04:00
										 |  |  | : define-array-vocab ( type -- vocab )
 | 
					
						
							| 
									
										
										
										
											2009-09-21 00:16:02 -04:00
										 |  |  |     underlying-type | 
					
						
							| 
									
										
										
										
											2009-09-10 15:46:26 -04:00
										 |  |  |     [ specialized-array-vocab ] [ '[ _ define-array ] ] bi
 | 
					
						
							|  |  |  |     generate-vocab ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-15 22:43:18 -04:00
										 |  |  | M: c-type-name require-c-array define-array-vocab drop ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ERROR: specialized-array-vocab-not-loaded c-type ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-15 22:43:18 -04:00
										 |  |  | M: c-type-name c-array-constructor | 
					
						
							| 
									
										
										
										
											2009-10-19 05:41:53 -04:00
										 |  |  |     underlying-type | 
					
						
							|  |  |  |     dup [ name>> "<" "-array>" surround ] [ specialized-array-vocab ] bi lookup | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  |     [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-15 22:43:18 -04:00
										 |  |  | M: c-type-name c-(array)-constructor | 
					
						
							| 
									
										
										
										
											2009-10-19 05:41:53 -04:00
										 |  |  |     underlying-type | 
					
						
							|  |  |  |     dup [ name>> "(" "-array)" surround ] [ specialized-array-vocab ] bi lookup | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  |     [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-15 22:43:18 -04:00
										 |  |  | M: c-type-name c-direct-array-constructor | 
					
						
							| 
									
										
										
										
											2009-10-19 05:41:53 -04:00
										 |  |  |     underlying-type | 
					
						
							|  |  |  |     dup [ name>> "<direct-" "-array>" surround ] [ specialized-array-vocab ] bi lookup | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  |     [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-09 11:43:37 -04:00
										 |  |  | SYNTAX: SPECIALIZED-ARRAYS: | 
					
						
							|  |  |  |     ";" parse-tokens [ parse-c-type define-array-vocab use-vocab ] each ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | SYNTAX: SPECIALIZED-ARRAY: | 
					
						
							| 
									
										
										
										
											2009-09-16 21:54:22 -04:00
										 |  |  |     scan-c-type define-array-vocab use-vocab ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-04 23:01:55 -04:00
										 |  |  | "prettyprint" vocab [ | 
					
						
							|  |  |  |     "specialized-arrays.prettyprint" require | 
					
						
							|  |  |  | ] when
 | 
					
						
							| 
									
										
										
										
											2009-10-23 05:24:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | "mirrors" vocab [ | 
					
						
							|  |  |  |     "specialized-arrays.mirrors" require | 
					
						
							|  |  |  | ] when
 |