Add struct-arrays
parent
1cf1b5fb3c
commit
c6f214f60d
|
@ -0,0 +1 @@
|
||||||
|
Slava Pestov
|
|
@ -0,0 +1,19 @@
|
||||||
|
IN: struct-arrays.tests
|
||||||
|
USING: struct-arrays tools.test kernel math sequences
|
||||||
|
alien.syntax alien.c-types ;
|
||||||
|
|
||||||
|
C-STRUCT: test-struct
|
||||||
|
{ "int" "x" }
|
||||||
|
{ "int" "y" } ;
|
||||||
|
|
||||||
|
: make-point ( x y -- struct )
|
||||||
|
"test-struct" <c-object>
|
||||||
|
[ set-test-struct-y ] keep
|
||||||
|
[ set-test-struct-x ] keep ;
|
||||||
|
|
||||||
|
[ 5/4 ] [
|
||||||
|
2 "test-struct" <struct-array>
|
||||||
|
1 2 make-point over set-first
|
||||||
|
3 4 make-point over set-second
|
||||||
|
0 [ [ test-struct-x ] [ test-struct-y ] bi / + ] reduce
|
||||||
|
] unit-test
|
|
@ -0,0 +1,37 @@
|
||||||
|
! Copyright (C) 2008 Slava Pestov.
|
||||||
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
|
USING: accessors alien alien.c-types byte-arrays kernel libc
|
||||||
|
math sequences sequences.private ;
|
||||||
|
IN: struct-arrays
|
||||||
|
|
||||||
|
TUPLE: struct-array
|
||||||
|
{ underlying c-ptr read-only }
|
||||||
|
{ length array-capacity read-only }
|
||||||
|
{ element-size array-capacity read-only } ;
|
||||||
|
|
||||||
|
M: struct-array length length>> ;
|
||||||
|
|
||||||
|
M: struct-array nth-unsafe
|
||||||
|
[ element-size>> * ] [ underlying>> ] bi <displaced-alien> ;
|
||||||
|
|
||||||
|
M: struct-array set-nth-unsafe
|
||||||
|
[ nth-unsafe swap ] [ element-size>> ] bi memcpy ;
|
||||||
|
|
||||||
|
M: struct-array new-sequence
|
||||||
|
element-size>> [ * <byte-array> ] 2keep struct-array boa ; inline
|
||||||
|
|
||||||
|
: <struct-array> ( length c-type -- struct-array )
|
||||||
|
heap-size [ * <byte-array> ] 2keep struct-array boa ; inline
|
||||||
|
|
||||||
|
ERROR: bad-byte-array-length byte-array ;
|
||||||
|
|
||||||
|
: byte-array>struct-array ( byte-array c-type -- struct-array )
|
||||||
|
heap-size [
|
||||||
|
[ dup length ] dip /mod 0 =
|
||||||
|
[ drop bad-byte-array-length ] unless
|
||||||
|
] keep struct-array boa ; inline
|
||||||
|
|
||||||
|
: <direct-struct-array> ( alien length c-type -- struct-array )
|
||||||
|
struct-array boa ; inline
|
||||||
|
|
||||||
|
INSTANCE: struct-array sequence
|
|
@ -0,0 +1 @@
|
||||||
|
Arrays of C structs and unions
|
|
@ -0,0 +1 @@
|
||||||
|
collections
|
Loading…
Reference in New Issue