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
|
2014-11-29 23:02:51 -05:00
|
|
|
byte-arrays classes combinators fry functors kernel lexer locals
|
|
|
|
|
make math math.vectors parser prettyprint.custom sequences
|
|
|
|
|
sequences.private vocabs.generated vocabs.loader vocabs.parser
|
2017-12-02 11:41:57 -05:00
|
|
|
words math.parser arrays functors2 ;
|
2008-11-14 21:18:16 -05:00
|
|
|
IN: specialized-arrays
|
2009-09-04 23:01:55 -04:00
|
|
|
|
2017-11-25 01:56:08 -05:00
|
|
|
MIXIN: specialized-array-mixin
|
2017-11-24 22:46:16 -05:00
|
|
|
MIXIN: specialized-array2
|
2009-09-09 23:33:34 -04:00
|
|
|
|
2017-11-25 01:56:08 -05:00
|
|
|
INSTANCE: specialized-array-mixin sequence
|
2017-11-24 22:46:16 -05:00
|
|
|
INSTANCE: specialized-array2 sequence
|
2009-09-04 23:01:55 -04:00
|
|
|
|
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 ;
|
|
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
M: pointer underlying-type drop void* ;
|
2011-09-25 14:49:27 -04:00
|
|
|
|
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
|
|
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
PRIVATE>
|
2009-09-09 23:33:34 -04:00
|
|
|
|
2017-12-02 11:41:57 -05:00
|
|
|
VARIABLES-FUNCTOR: specialized-array ( T: existing-word -- ) {
|
2017-11-24 22:46:16 -05:00
|
|
|
{ "A" "${T}-array" }
|
|
|
|
|
{ "<A>" "<${A}>" }
|
|
|
|
|
{ "(A)" "(${A})" }
|
|
|
|
|
{ "<direct-A>" "<direct-${A}>" }
|
|
|
|
|
} [[
|
|
|
|
|
USING: accessors alien alien.c-types alien.data byte-arrays
|
|
|
|
|
classes kernel math math.vectors parser prettyprint.custom
|
|
|
|
|
sequences sequences.private specialized-arrays
|
|
|
|
|
specialized-arrays.private ;
|
|
|
|
|
|
|
|
|
|
<<
|
|
|
|
|
TUPLE: ${A}
|
2009-09-09 23:33:34 -04:00
|
|
|
{ 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
|
|
|
|
2017-11-25 01:56:08 -05:00
|
|
|
INSTANCE: ${A} specialized-array-mixin
|
2017-11-24 22:46:16 -05:00
|
|
|
|
|
|
|
|
: ${<direct-A>} ( alien len -- specialized-array ) ${A} boa ; inline
|
2009-09-09 23:33:34 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
: ${<A>} ( n -- specialized-array )
|
|
|
|
|
[ \ ${T} <underlying> ] keep ${<direct-A>} ; inline
|
2010-06-08 18:00:11 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
: ${(A)} ( n -- specialized-array )
|
|
|
|
|
[ \ ${T} (underlying) ] keep ${<direct-A>} ; inline
|
|
|
|
|
>>
|
2009-09-09 23:33:34 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
SYNTAX: ${A}{ \ } [ \ ${T} >c-array ] parse-literal ;
|
2009-09-09 23:33:34 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
M: ${A} direct-like drop ${<direct-A>} ; inline
|
2009-09-09 23:33:34 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
M: ${A} clone [ underlying>> clone ] [ length>> ] bi ${<direct-A>} ; inline
|
2009-09-09 23:33:34 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
M: ${A} length length>> ; inline
|
2009-09-09 23:33:34 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
M: ${A} nth-unsafe underlying>> \ ${T} alien-element ; inline
|
2010-06-08 18:00:11 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
M: ${A} nth-c-ptr underlying>> \ ${T} array-accessor drop swap <displaced-alien> ; inline
|
2009-09-09 23:33:34 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
M: ${A} set-nth-unsafe underlying>> \ ${T} set-alien-element ; inline
|
2009-09-09 23:33:34 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
M: ${A} like drop dup ${A} instance? [ \ ${T} >c-array ] unless ; inline
|
2009-09-09 23:33:34 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
M: ${A} new-sequence drop ${(A)} ; inline
|
2009-09-09 23:33:34 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
M: ${A} equal? over ${A} instance? [ sequence= ] [ 2drop f ] if ;
|
|
|
|
|
|
|
|
|
|
M: ${A} resize
|
2009-09-09 23:33:34 -04:00
|
|
|
[
|
2017-11-24 22:46:16 -05:00
|
|
|
[ \ ${T} heap-size * ] [ underlying>> ] bi*
|
2009-09-09 23:33:34 -04:00
|
|
|
resize-byte-array
|
|
|
|
|
] [ drop ] 2bi
|
2017-11-24 22:46:16 -05:00
|
|
|
${<direct-A>} ; inline
|
2009-09-10 15:46:26 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
M: ${A} element-size drop \ ${T} heap-size ; inline
|
2009-09-10 15:46:26 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
M: ${A} underlying-type drop \ ${T} ;
|
2009-09-09 23:33:34 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
M: ${A} pprint-delims drop \ ${A}{ \ } ;
|
2009-09-09 23:33:34 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
M: ${A} >pprint-sequence ;
|
2009-09-09 23:33:34 -04:00
|
|
|
|
2017-11-24 22:46:16 -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
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
M: ${A} v*high [ * \ ${T} heap-size neg shift ] 2map ; inline
|
|
|
|
|
]]
|
2009-09-09 23:33:34 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
<PRIVATE
|
2009-09-09 23:33:34 -04:00
|
|
|
: specialized-array-vocab ( c-type -- vocab )
|
2009-10-19 05:41:53 -04:00
|
|
|
[
|
2017-11-24 22:46:16 -05:00
|
|
|
"specialized-arrays:functors:specialized-array:" %
|
|
|
|
|
! [ vocabulary>> % "." % ]
|
|
|
|
|
! [ name>> % ":" % ]
|
|
|
|
|
[ drop ]
|
|
|
|
|
[ 1array hashcode number>string % ] bi
|
2009-10-19 05:41:53 -04:00
|
|
|
] "" make ;
|
2009-09-09 23:33:34 -04:00
|
|
|
|
2014-11-29 23:02:51 -05:00
|
|
|
:: direct-slice-unsafe ( from to seq -- seq' )
|
|
|
|
|
from seq nth-c-ptr to from - seq 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-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
|
2015-08-13 19:13:05 -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
|
2015-08-13 19:13:05 -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
|
2015-08-13 19:13:05 -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
|
2015-08-13 19:13:05 -04:00
|
|
|
[ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
|
2011-09-25 14:49:27 -04:00
|
|
|
|
|
|
|
|
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
|
2015-08-13 19:13:05 -04:00
|
|
|
[ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
|
2011-09-25 14:49:27 -04:00
|
|
|
|
|
|
|
|
M: pointer c-array-type? drop void* c-array-type? ;
|
|
|
|
|
|
2017-08-26 15:05:26 -04:00
|
|
|
SYNTAX: \SPECIALIZED-ARRAYS:
|
2017-11-24 22:46:16 -05:00
|
|
|
";" [ parse-c-type define-specialized-array ] each-token ;
|
2009-09-09 23:33:34 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
! { "specialized-arrays" "prettyprint" } "specialized-arrays.prettyprint" require-when
|
2009-10-23 05:24:20 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
! { "specialized-arrays" "mirrors" } "specialized-arrays.mirrors" require-when
|
2011-10-13 18:11:32 -04:00
|
|
|
|
2017-11-24 22:46:16 -05:00
|
|
|
! uchar define-specialized-array
|