]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/specialized-arrays/specialized-arrays.factor
Slices over specialized arrays can now be passed to C functions, written to binary...
[factor.git] / basis / specialized-arrays / specialized-arrays.factor
old mode 100755 (executable)
new mode 100644 (file)
index 67c5898..b052bec
@@ -1,9 +1,8 @@
-! Copyright (C) 2008, 2009 Slava Pestov.
+! Copyright (C) 2008, 2010 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors alien alien.c-types alien.data alien.parser
 assocs byte-arrays classes compiler.units functors kernel lexer
-libc math math.vectors math.vectors.private
-math.vectors.specialization namespaces
+libc math math.vectors math.vectors.private namespaces
 parser prettyprint.custom sequences sequences.private strings
 summary vocabs vocabs.loader vocabs.parser vocabs.generated
 words fry combinators make ;
@@ -20,6 +19,11 @@ ERROR: bad-byte-array-length byte-array type ;
 M: bad-byte-array-length summary
     drop "Byte array length doesn't divide type width" ;
 
+ERROR: not-a-byte-array alien ;
+
+M: not-a-byte-array summary
+    drop "Not a byte array" ;
+
 : (underlying) ( n c-type -- array )
     heap-size * (byte-array) ; inline
 
@@ -31,7 +35,6 @@ M: bad-byte-array-length summary
 FUNCTOR: define-array ( T -- )
 
 A            DEFINES-CLASS ${T}-array
-S            DEFINES-CLASS ${T}-sequence
 <A>          DEFINES <${A}>
 (A)          DEFINES (${A})
 <direct-A>   DEFINES <direct-${A}>
@@ -47,11 +50,9 @@ SET-NTH      [ T dup c-setter array-accessor ]
 
 WHERE
 
-MIXIN: S
-
 TUPLE: A
 { underlying c-ptr read-only }
-{ length array-capacity read-only } ;
+{ length array-capacity read-only } ; final
 
 : <direct-A> ( alien len -- specialized-array ) A boa ; inline
 
@@ -65,11 +66,11 @@ TUPLE: A
     [ \ T heap-size calloc ] keep <direct-A> ; inline
 
 : byte-array>A ( byte-array -- specialized-array )
-    >c-ptr dup length \ T heap-size /mod 0 =
-    [ drop \ T bad-byte-array-length ] unless
-    <direct-A> ; inline
-
-M: A new-underlying drop byte-array>A ;
+    >c-ptr dup byte-array? [
+        dup length \ T heap-size /mod 0 =
+        [ <direct-A> ]
+        [ drop \ T bad-byte-array-length ] if
+    ] [ not-a-byte-array ] if ; inline
 
 M: A clone [ underlying>> clone ] [ length>> ] bi <direct-A> ; inline
 
@@ -94,9 +95,7 @@ M: A resize
     ] [ drop ] 2bi
     <direct-A> ; inline
 
-M: A byte-length length \ T heap-size * ; inline
-
-M: A element-type drop \ T ; inline
+M: A element-size drop \ T heap-size ; inline
 
 M: A direct-array-syntax drop \ A@ ;
 
@@ -105,26 +104,31 @@ M: A pprint-delims drop \ A{ \ } ;
 M: A >pprint-sequence ;
 
 SYNTAX: A{ \ } [ >A ] parse-literal ;
-SYNTAX: A@ scan-object scan-object <direct-A> parsed ;
+SYNTAX: A@ scan-object scan-object <direct-A> suffix! ;
 
 INSTANCE: A specialized-array
 
-A T c-type-boxed-class f specialize-vector-words
+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
 
-;FUNCTOR
+M: A v*high [ * \ T heap-size neg shift ] 2map ; inline
 
-GENERIC: (underlying-type) ( c-type -- c-type' )
+;FUNCTOR
 
-M: string (underlying-type) c-types get at ;
-M: word (underlying-type) "c-type" word-prop ;
+GENERIC: underlying-type ( c-type -- c-type' )
 
-: underlying-type ( c-type -- c-type' )
-    dup (underlying-type) {
+M: c-type-word underlying-type
+    dup "c-type" word-prop {
         { [ dup not ] [ drop no-c-type ] }
-        { [ dup c-type-name? ] [ nip underlying-type ] }
+        { [ dup pointer? ] [ 2drop void* ] }
+        { [ dup c-type-word? ] [ nip underlying-type ] }
         [ drop ]
     } cond ;
 
+M: pointer underlying-type
+    drop void* ;
+
 : specialized-array-vocab ( c-type -- vocab )
     [
         "specialized-arrays.instances." %
@@ -140,25 +144,29 @@ PRIVATE>
     [ specialized-array-vocab ] [ '[ _ define-array ] ] bi
     generate-vocab ;
 
-M: c-type-name require-c-array define-array-vocab drop ;
-
 ERROR: specialized-array-vocab-not-loaded c-type ;
 
-M: c-type-name c-array-constructor
+M: c-type-word c-array-constructor
     underlying-type
     dup [ name>> "<" "-array>" surround ] [ specialized-array-vocab ] bi lookup
     [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
 
-M: c-type-name c-(array)-constructor
+M: pointer c-array-constructor drop void* c-array-constructor ;
+
+M: c-type-word c-(array)-constructor
     underlying-type
     dup [ name>> "(" "-array)" surround ] [ specialized-array-vocab ] bi lookup
     [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
 
-M: c-type-name c-direct-array-constructor
+M: pointer c-(array)-constructor drop void* c-(array)-constructor ;
+
+M: c-type-word c-direct-array-constructor
     underlying-type
     dup [ name>> "<direct-" "-array>" surround ] [ specialized-array-vocab ] bi lookup
     [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
 
+M: pointer c-direct-array-constructor drop void* c-direct-array-constructor ;
+
 SYNTAX: SPECIALIZED-ARRAYS:
     ";" parse-tokens [ parse-c-type define-array-vocab use-vocab ] each ;
 
@@ -168,3 +176,7 @@ SYNTAX: SPECIALIZED-ARRAY:
 "prettyprint" vocab [
     "specialized-arrays.prettyprint" require
 ] when
+
+"mirrors" vocab [
+    "specialized-arrays.mirrors" require
+] when