| 
									
										
										
										
											2010-02-19 18:05:12 -05:00
										 |  |  | ! Copyright (C) 2008, 2010 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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | : (underlying) ( n c-type -- array )
 | 
					
						
							|  |  |  |     heap-size * (byte-array) ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <underlying> ( n type -- array )
 | 
					
						
							|  |  |  |     heap-size * <byte-array> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-25 14:49:27 -04:00
										 |  |  | GENERIC: underlying-type ( c-type -- c-type' )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: c-type-word underlying-type | 
					
						
							|  |  |  |     dup "c-type" word-prop { | 
					
						
							|  |  |  |         { [ dup not ] [ drop no-c-type ] } | 
					
						
							|  |  |  |         { [ dup pointer? ] [ 2drop void* ] } | 
					
						
							|  |  |  |         { [ dup c-type-word? ] [ nip underlying-type ] } | 
					
						
							|  |  |  |         [ drop ] | 
					
						
							|  |  |  |     } cond ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: pointer underlying-type | 
					
						
							|  |  |  |     drop void* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-08 18:00:11 -04:00
										 |  |  | GENERIC: nth-c-ptr ( n seq -- displaced-alien )
 | 
					
						
							|  |  |  | GENERIC: direct-like ( alien len exemplar -- seq )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-13 18:11:32 -04:00
										 |  |  | M: byte-array nth-c-ptr <displaced-alien> ; inline
 | 
					
						
							|  |  |  | M: byte-array direct-like drop uchar <c-direct-array> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | FUNCTOR: define-array ( T -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-18 23:09:34 -04:00
										 |  |  | A          DEFINES-CLASS ${T}-array | 
					
						
							|  |  |  | <A>        DEFINES <${A}> | 
					
						
							|  |  |  | (A)        DEFINES (${A}) | 
					
						
							|  |  |  | <direct-A> DEFINES <direct-${A}> | 
					
						
							|  |  |  | A{         DEFINES ${A}{ | 
					
						
							|  |  |  | A@         DEFINES ${A}@ | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | WHERE | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: A | 
					
						
							|  |  |  | { underlying c-ptr read-only } | 
					
						
							| 
									
										
										
										
											2010-02-17 08:39:40 -05:00
										 |  |  | { length array-capacity read-only } ; final
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : <direct-A> ( alien len -- specialized-array ) A boa ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-08 18:00:11 -04:00
										 |  |  | M: A direct-like drop <direct-A> ; 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
										 |  |  | 
 | 
					
						
							|  |  |  | M: A clone [ underlying>> clone ] [ length>> ] bi <direct-A> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: A length length>> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-22 01:25:10 -04:00
										 |  |  | M: A nth-unsafe underlying>> \ T alien-element ; inline
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-08 18:00:11 -04:00
										 |  |  | M: A nth-c-ptr underlying>> \ T array-accessor drop swap <displaced-alien> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-22 01:25:10 -04:00
										 |  |  | M: A set-nth-unsafe underlying>> \ T set-alien-element ; inline
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-25 14:49:27 -04:00
										 |  |  | M: A like drop dup A instance? [ \ T >c-array ] unless ; inline
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-24 10:50:31 -05:00
										 |  |  | M: A element-size drop \ T heap-size ; inline
 | 
					
						
							| 
									
										
										
										
											2009-09-21 00:16:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-25 14:49:27 -04:00
										 |  |  | M: A underlying-type drop \ T ;
 | 
					
						
							| 
									
										
										
										
											2009-09-10 15:46:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-25 14:49:27 -04:00
										 |  |  | SYNTAX: A{ \ } [ \ T >c-array ] parse-literal ;
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-10 19:49:59 -04:00
										 |  |  | : direct-slice-unsafe ( from to seq -- seq' )
 | 
					
						
							| 
									
										
										
										
											2010-06-08 18:00:11 -04:00
										 |  |  |     [ nip nth-c-ptr ] | 
					
						
							|  |  |  |     [ drop swap - ] | 
					
						
							|  |  |  |     [ 2nip ] 3tri direct-like ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-10 19:49:59 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : direct-slice ( from to seq -- seq' )
 | 
					
						
							|  |  |  |     check-slice direct-slice-unsafe ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-08 18:00:11 -04:00
										 |  |  | : direct-head ( seq n -- seq' ) (head) direct-slice ; inline
 | 
					
						
							|  |  |  | : direct-tail ( seq n -- seq' ) (tail) direct-slice ; inline
 | 
					
						
							|  |  |  | : direct-head* ( seq n -- seq' ) from-end direct-head ; inline
 | 
					
						
							|  |  |  | : direct-tail* ( seq n -- seq' ) from-end direct-tail ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | ERROR: specialized-array-vocab-not-loaded c-type ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-21 19:27:36 -05:00
										 |  |  | M: c-type-word c-array-constructor | 
					
						
							| 
									
										
										
										
											2009-10-19 05:41:53 -04:00
										 |  |  |     underlying-type | 
					
						
							| 
									
										
										
										
											2011-11-06 18:57:24 -05:00
										 |  |  |     dup [ name>> "<" "-array>" surround ] [ specialized-array-vocab ] bi lookup-word | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  |     [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
 | 
					
						
							| 
									
										
										
										
											2010-02-24 02:18:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-21 22:23:47 -05:00
										 |  |  | M: pointer c-array-constructor drop void* c-array-constructor ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-21 19:27:36 -05:00
										 |  |  | M: c-type-word c-(array)-constructor | 
					
						
							| 
									
										
										
										
											2009-10-19 05:41:53 -04:00
										 |  |  |     underlying-type | 
					
						
							| 
									
										
										
										
											2011-11-06 18:57:24 -05:00
										 |  |  |     dup [ name>> "(" "-array)" surround ] [ specialized-array-vocab ] bi lookup-word | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  |     [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
 | 
					
						
							| 
									
										
										
										
											2010-02-24 02:18:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-21 22:23:47 -05:00
										 |  |  | M: pointer c-(array)-constructor drop void* c-(array)-constructor ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-21 19:27:36 -05:00
										 |  |  | M: c-type-word c-direct-array-constructor | 
					
						
							| 
									
										
										
										
											2009-10-19 05:41:53 -04:00
										 |  |  |     underlying-type | 
					
						
							| 
									
										
										
										
											2011-11-06 18:57:24 -05:00
										 |  |  |     dup [ name>> "<direct-" "-array>" surround ] [ specialized-array-vocab ] bi lookup-word | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  |     [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
 | 
					
						
							| 
									
										
										
										
											2010-02-24 02:18:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-21 22:23:47 -05:00
										 |  |  | M: pointer c-direct-array-constructor drop void* c-direct-array-constructor ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-25 14:49:27 -04:00
										 |  |  | M: c-type-word c-array-type | 
					
						
							|  |  |  |     underlying-type | 
					
						
							| 
									
										
										
										
											2011-11-06 18:57:24 -05:00
										 |  |  |     dup [ name>> "-array" append ] [ specialized-array-vocab ] bi lookup-word | 
					
						
							| 
									
										
										
										
											2011-09-25 14:49:27 -04:00
										 |  |  |     [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: pointer c-array-type drop void* c-array-type ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: c-type-word c-array-type? | 
					
						
							|  |  |  |     underlying-type | 
					
						
							| 
									
										
										
										
											2011-11-06 18:57:24 -05:00
										 |  |  |     dup [ name>> "-array?" append ] [ specialized-array-vocab ] bi lookup-word | 
					
						
							| 
									
										
										
										
											2011-09-25 14:49:27 -04:00
										 |  |  |     [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: pointer c-array-type? drop void* c-array-type? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-09 11:43:37 -04:00
										 |  |  | SYNTAX: SPECIALIZED-ARRAYS: | 
					
						
							| 
									
										
										
										
											2010-03-01 01:06:47 -05:00
										 |  |  |     ";" [ parse-c-type define-array-vocab use-vocab ] each-token ;
 | 
					
						
							| 
									
										
										
										
											2009-10-09 11:43:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-18 15:29:24 -04:00
										 |  |  | { "specialized-arrays" "prettyprint" } "specialized-arrays.prettyprint" require-when | 
					
						
							| 
									
										
										
										
											2009-10-23 05:24:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-18 15:29:24 -04:00
										 |  |  | { "specialized-arrays" "mirrors" } "specialized-arrays.mirrors" require-when | 
					
						
							| 
									
										
										
										
											2011-10-13 18:11:32 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | uchar define-array-vocab drop
 |