]> gitweb.factorcode.org Git - factor.git/blob - basis/specialized-arrays/specialized-arrays-docs.factor
b476a4707251c5c6f50821831f1782b41b2b02b1
[factor.git] / basis / specialized-arrays / specialized-arrays-docs.factor
1 USING: help.markup help.syntax byte-arrays alien math sequences ;
2 IN: specialized-arrays
3
4 HELP: SPECIALIZED-ARRAY:
5 { $syntax "SPECIALIZED-ARRAY: type" }
6 { $values { "type" "a C type" } }
7 { $description "Brings a specialized array for holding values of " { $snippet "type" } " into the vocabulary search path. The generated words are documented in " { $link "specialized-array-words" } "." } ;
8
9 HELP: SPECIALIZED-ARRAYS:
10 { $syntax "SPECIALIZED-ARRAYS: type type type ... ;" }
11 { $values { "type" "a C type" } }
12 { $description "Brings a set of specialized arrays for holding values of each " { $snippet "type" } " into the vocabulary search path. The generated words are documented in " { $link "specialized-array-words" } "." } ;
13
14 { POSTPONE: SPECIALIZED-ARRAY: POSTPONE: SPECIALIZED-ARRAYS: } related-words
15
16 HELP: direct-slice
17 { $values { "from" integer } { "to" integer } { "seq" "a specialized array" } { "seq'" "a new specialized array" } }
18 { $description "Constructs a new specialized array of the same type as " { $snippet "seq" } " sharing the same underlying memory as the subsequence of " { $snippet "seq" } " from elements " { $snippet "from" } " up to but not including " { $snippet "to" } ". Like " { $link slice } ", raises an error if " { $snippet "from" } " or " { $snippet "to" } " is out of bounds." } ;
19
20 HELP: direct-head
21 { $values { "seq" "a specialized array" } { "n" integer } { "seq'" "a new specialized array" } }
22 { $description "Constructs a new specialized array of the same type as " { $snippet "seq" } " sharing the same underlying memory as the first " { $snippet "n" } " elements of " { $snippet "seq" } ". Like " { $link head } ", raises an error if " { $snippet "n" } " is out of bounds." } ;
23
24 HELP: direct-tail
25 { $values { "seq" "a specialized array" } { "n" integer } { "seq'" "a new specialized array" } }
26 { $description "Constructs a new specialized array of the same type as " { $snippet "seq" } " sharing the same underlying memory as " { $snippet "seq" } " without the first " { $snippet "n" } " elements. Like " { $link tail } ", raises an error if " { $snippet "n" } " is out of bounds." } ;
27
28 HELP: direct-head*
29 { $values { "seq" "a specialized array" } { "n" integer } { "seq'" "a new specialized array" } }
30 { $description "Constructs a new specialized array of the same type as " { $snippet "seq" } " sharing the same underlying memory as " { $snippet "seq" } " without the last " { $snippet "n" } " elements. Like " { $link head* } ", raises an error if " { $snippet "n" } " is out of bounds." } ;
31
32 HELP: direct-tail*
33 { $values { "seq" "a specialized array" } { "n" integer } { "seq'" "a new specialized array" } }
34 { $description "Constructs a new specialized array of the same type as " { $snippet "seq" } " sharing the same underlying memory as the last " { $snippet "n" } " elements of " { $snippet "seq" } ". Like " { $link tail* } ", raises an error if " { $snippet "n" } " is out of bounds." } ;
35
36 { direct-slice direct-head direct-tail direct-head* direct-tail* } related-words
37
38 ARTICLE: "specialized-array-words" "Specialized array words"
39 "The " { $link POSTPONE: SPECIALIZED-ARRAY: } " and " { $link POSTPONE: SPECIALIZED-ARRAYS: } " parsing words generate specialized array types if they haven't been generated already and add the following words to the vocabulary search path, where " { $snippet "T" } " is the C type in question:"
40 { $table
41     { { $snippet "T-array" } { "The class of arrays with elements of type " { $snippet "T" } } }
42     { { $snippet "<T-array>" } { "Constructor for arrays with elements of type " { $snippet "T" } "; stack effect " { $snippet "( len -- array )" } } }
43     { { $snippet "(T-array)" } { "Constructor for arrays with elements of type " { $snippet "T" } ", where the initial contents are uninitialized; stack effect " { $snippet "( len -- array )" } } }
44     { { $snippet "malloc-T-array" } { "Constructor for arrays with elements of type " { $snippet "T" } " backed by newly-allocated unmanaged memory; stack effect " { $snippet "( alien len -- array )" } } }
45     { { $snippet "<direct-T-array>" } { "Constructor for arrays with elements of type " { $snippet "T" } " backed by raw memory; stack effect " { $snippet "( alien len -- array )" } } }
46     { { $snippet "T-array-cast" } { "Converts a byte array into a specialized array by interpreting the bytes in as machine-specific values. Code which uses this word is unportable" } }
47     { { $snippet ">T-array" } { "Converts a sequence into a specialized array of type " { $snippet "T" } "; stack effect " { $snippet "( seq -- array )" } } }
48     { { $snippet "T-array{" } { "Literal syntax, consists of a series of values terminated by " { $snippet "}" } } }
49 }
50 "Behind the scenes, these words are placed in a vocabulary named " { $snippet "specialized-arrays.instances.T" } ", however this vocabulary should not be placed in a " { $link POSTPONE: USING: } " form directly. Instead, always use " { $link POSTPONE: SPECIALIZED-ARRAY: } " or " { $link POSTPONE: SPECIALIZED-ARRAYS: } ". This ensures that the vocabulary can get generated the first time it is needed."
51 $nl
52 "Additionally, special versions of the standard " { $link <slice> } ", " { $link head } ", and " { $link tail } " sequence operations are provided for specialized arrays to create a new specialized array object sharing storage with a subsequence of an existing array:"
53 { $subsections
54     direct-slice
55     direct-head
56     direct-tail
57     direct-head*
58     direct-tail*
59 } ;
60
61 ARTICLE: "specialized-array-c" "Passing specialized arrays to C functions"
62 "If a C function is declared as taking a parameter with a pointer or an array type (for example, " { $snippet "float*" } " or " { $snippet "int[3]" } "), instances of the relevant specialized array can be passed in."
63 $nl
64 "C type specifiers for array types are documented in " { $link "c-types-specs" } "."
65 $nl
66 "Here is an example; as is common with C functions, the array length is passed in separately, since C does not offer a runtime facility to determine the array length of a base pointer:"
67 { $code
68     "USING: alien.syntax specialized-arrays ;"
69     "SPECIALIZED-ARRAY: int"
70     "FUNCTION: void process_data ( int* data, int len ) ;"
71     "int-array{ 10 20 30 } dup length process_data"
72 }
73 "Literal specialized arrays, as well as specialized arrays created with " { $snippet "<T-array>" } " and " { $snippet ">T-array" } " are backed by a " { $link byte-array } " in the Factor heap, and can move as a result of garbage collection. If this is unsuitable, the array can be allocated in unmanaged memory instead."
74 $nl
75 "In the following example, it is presumed that the C library holds on to a pointer to the array's data after the " { $snippet "init_with_data()" } " call returns; this is one situation where unmanaged memory has to be used instead. Note the use of destructors to ensure the memory is deallocated after the block ends:"
76 { $code
77     "USING: alien.syntax specialized-arrays ;"
78     "SPECIALIZED-ARRAY: float"
79     "FUNCTION: void init_with_data ( float* data, int len ) ;"
80     "FUNCTION: float compute_result ( ) ;"
81     "["
82     "    100 malloc-float-array &free"
83     "    dup length init_with_data"
84     "    compute_result"
85     "] with-destructors"
86 }
87 "Finally, sometimes a C library returns a pointer to an array in unmanaged memory, together with a length. In this case, a specialized array can be constructed to view this memory using " { $snippet "<direct-T-array>" } ":"
88 { $code
89     "USING: alien.c-types classes.struct ;"
90     ""
91     "STRUCT: device_info"
92     "    { id int }"
93     "    { name char* } ;"
94     ""
95     "FUNCTION: void get_device_info ( int* length ) ;"
96     ""
97     "0 <int> [ get_device_info ] keep <direct-int-array> ."
98 }
99 "For a full discussion of Factor heap allocation versus unmanaged memory allocation, see " { $link "byte-arrays-gc" } "."
100 $nl
101 "Each specialized array has a " { $slot "underlying" } " slot holding a " { $link byte-array } " with the raw data. Passing a specialized array as a parameter to a C function call will automatically extract the underlying data. To get at the underlying data directly, call the " { $link >c-ptr } " word on a specialized array." ;
102
103 ARTICLE: "specialized-array-math" "Vector arithmetic with specialized arrays"
104 "Each specialized array with a numeric type generates specialized versions of the " { $link "math-vectors" } " words. The compiler substitutes calls for these words if it can statically determine input types. The " { $snippet "optimized." } " word in the " { $vocab-link "compiler.tree.debugger" } " vocabulary can be used to determine if this optimization is being performed for a particular piece of code." ;
105
106 ARTICLE: "specialized-array-examples" "Specialized array examples"
107 "Let's import specialized float arrays:"
108 { $code "USING: specialized-arrays math.constants math.functions ;" "SPECIALIZED-ARRAY: float" }
109 "Creating a float array with 3 elements:"
110 { $code "1.0 [ sin ] [ cos ] [ tan ] tri float-array{ } 3sequence ." }
111 "Create a float array and sum the elements:"
112 { $code
113     "1000 iota [ 1000 /f pi * sin ] float-array{ } map-as"
114     "0.0 [ + ] reduce ."
115 } ;
116
117 ARTICLE: "specialized-arrays" "Specialized arrays"
118 "The " { $vocab-link "specialized-arrays" } " vocabulary implements fixed-length sequence types for storing machine values in a space-efficient manner without boxing."
119 $nl
120 "A specialized array type needs to be generated for each element type. This is done with parsing words:"
121 { $subsections
122     POSTPONE: SPECIALIZED-ARRAY:
123     POSTPONE: SPECIALIZED-ARRAYS:
124 }
125 "This parsing word adds new words to the search path, documented in the next section."
126 { $subsections
127     "specialized-array-words"
128     "specialized-array-c"
129     "specialized-array-math"
130     "specialized-array-examples"
131 }
132 "The " { $vocab-link "specialized-vectors" } " vocabulary provides a resizable version of this abstraction." ;
133
134 ABOUT: "specialized-arrays"