]> gitweb.factorcode.org Git - factor.git/blob - basis/struct-arrays/struct-arrays.factor
change math.floats.env tests not to use any libm functions, which don't reliably...
[factor.git] / basis / struct-arrays / struct-arrays.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.structs byte-arrays
4 classes classes.struct kernel libc math parser sequences
5 sequences.private words fry memoize compiler.units ;
6 IN: struct-arrays
7
8 TUPLE: struct-array
9 { underlying c-ptr read-only }
10 { length array-capacity read-only }
11 { element-size array-capacity read-only }
12 { class read-only }
13 { ctor read-only } ;
14
15 <PRIVATE
16
17 : (nth-ptr) ( i struct-array -- alien )
18     [ element-size>> * >fixnum ] [ underlying>> ] bi <displaced-alien> ; inline
19
20 : (struct-element-constructor) ( struct-class -- word )
21     [
22         "struct-array-ctor" f <word>
23         [ swap '[ _ memory>struct ] (( alien -- object )) define-inline ] keep
24     ] with-compilation-unit ;
25
26 ! Foldable memo word. This is an optimization; by precompiling a
27 ! constructor for array elements, we avoid memory>struct's slow path.
28 MEMO: struct-element-constructor ( struct-class -- word )
29     (struct-element-constructor) ; foldable
30
31 PRIVATE>
32
33 M: struct-array length length>> ; inline
34
35 M: struct-array byte-length [ length>> ] [ element-size>> ] bi * ; inline
36
37 M: struct-array nth-unsafe
38     [ (nth-ptr) ] [ ctor>> ] bi execute( alien -- object ) ; inline
39
40 M: struct-array set-nth-unsafe
41     [ (nth-ptr) swap ] [ element-size>> ] bi memcpy ; inline
42
43 ERROR: not-a-struct-class struct-class ;
44
45 : <direct-struct-array> ( alien length struct-class -- struct-array )
46     dup struct-class? [ not-a-struct-class ] unless
47     [ heap-size ] [ ] [ struct-element-constructor ]
48     tri struct-array boa ; inline
49
50 M: struct-array new-sequence
51     [ element-size>> * (byte-array) ] [ class>> ] 2bi
52     <direct-struct-array> ; inline
53
54 M: struct-array resize ( n seq -- newseq )
55     [ [ element-size>> * ] [ underlying>> ] bi resize ] [ class>> ] 2bi
56     <direct-struct-array> ; inline
57
58 : <struct-array> ( length struct-class -- struct-array )
59     [ heap-size * <byte-array> ] 2keep <direct-struct-array> ; inline
60
61 ERROR: bad-byte-array-length byte-array ;
62
63 : byte-array>struct-array ( byte-array c-type -- struct-array )
64     [
65         heap-size
66         [ dup length ] dip /mod 0 =
67         [ drop bad-byte-array-length ] unless
68     ] keep <direct-struct-array> ; inline
69
70 : struct-array-on ( struct length -- struct-array )
71     [ [ >c-ptr ] [ class ] bi ] dip swap <direct-struct-array> ; inline    
72
73 : malloc-struct-array ( length c-type -- struct-array )
74     [ heap-size calloc ] 2keep <direct-struct-array> ; inline
75
76 INSTANCE: struct-array sequence
77
78 M: struct-type <c-array> ( len c-type -- array )
79     dup c-array-constructor
80     [ execute( len -- array ) ]
81     [ <struct-array> ] ?if ; inline
82
83 M: struct-type <c-direct-array> ( alien len c-type -- array )
84     dup c-direct-array-constructor
85     [ execute( alien len -- array ) ]
86     [ <direct-struct-array> ] ?if ; inline
87
88 : >struct-array ( sequence class -- struct-array )
89     [ dup length ] dip <struct-array>
90     [ 0 swap copy ] keep ; inline
91
92 SYNTAX: struct-array{
93     \ } scan-word [ >struct-array ] curry parse-literal ;
94
95 SYNTAX: struct-array@
96     scan-word [ scan-object scan-object ] dip <direct-struct-array> parsed ;
97
98 USING: vocabs vocabs.loader ;
99
100 "prettyprint" vocab [ "struct-arrays.prettyprint" require ] when