| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
<HTML> |
| 6 |
<HEAD> |
| 7 |
<TITLE>vector(n) manual page</TITLE> |
| 8 |
</HEAD> |
| 9 |
<BODY bgcolor=white> |
| 10 |
<A HREF="#toc">Table of Contents</A><P> |
| 11 |
|
| 12 |
<H2><A NAME="sect0" HREF="#toc0">Name</A></H2> |
| 13 |
vector - Vector data type for |
| 14 |
Tcl |
| 15 |
<H2><A NAME="sect1" HREF="#toc1">Synopsis</A></H2> |
| 16 |
<B>vector configure <I>option value ...</I></B> <P> |
| 17 |
<B>vector create <I>vecName </I></B>?<I>vecName</I>...? |
| 18 |
?<I>switches</I>? <P> |
| 19 |
<B>vector destroy <I>vecName </I></B>?<I>vecName</I>...? <P> |
| 20 |
<B>vector expr <I>expression</I></B> <P> |
| 21 |
<B>vector |
| 22 |
names </B>?<I>pattern</I>...? <P> |
| 23 |
<B>vector op</B> <I>operation vecName</I> ?<I>arg</I>?... |
| 24 |
<H2><A NAME="sect2" HREF="#toc2">Description</A></H2> |
| 25 |
The <B>vector</B> |
| 26 |
command creates a vector of floating point values. The vector's components |
| 27 |
can be manipulated in three ways: through a Tcl array variable, a Tcl command, |
| 28 |
or the C API. |
| 29 |
<H2><A NAME="sect3" HREF="#toc3">Introduction</A></H2> |
| 30 |
A vector is simply an ordered set of numbers. |
| 31 |
The components of a vector are real numbers, indexed by counting numbers. |
| 32 |
<P> |
| 33 |
Vectors are common data structures for many applications. For example, |
| 34 |
a graph may use two vectors to represent the X-Y coordinates of the data |
| 35 |
plotted. The graph will automatically be redrawn when the vectors are updated |
| 36 |
or changed. By using vectors, you can separate data analysis from the graph |
| 37 |
widget. This makes it easier, for example, to add data transformations, |
| 38 |
such as splines. It's possible to plot the same data to in multiple graphs, |
| 39 |
where each graph presents a different view or scale of the data. <P> |
| 40 |
You could |
| 41 |
try to use Tcl's associative arrays as vectors. Tcl arrays are easy to use. |
| 42 |
You can access individual elements randomly by specifying the index, or |
| 43 |
the set the entire array by providing a list of index and value pairs for |
| 44 |
each element. The disadvantages of associative arrays as vectors lie in |
| 45 |
the fact they are implemented as hash tables. |
| 46 |
<UL> |
| 47 |
<LI>There's no implied ordering |
| 48 |
to the associative arrays. If you used vectors for plotting, you would |
| 49 |
want to insure the second component comes after the first, an so on. This |
| 50 |
isn't possible since arrays are actually hash tables. For example, you can't |
| 51 |
get a range of values between two indices. Nor can you sort an array. </LI><LI>Arrays |
| 52 |
consume lots of memory when the number of elements becomes large (tens |
| 53 |
of thousands). This is because each element's index and value are stored |
| 54 |
as strings in the hash table. </LI><LI>The C programming interface is unwieldy. Normally |
| 55 |
with vectors, you would like to view the Tcl array as you do a C array, |
| 56 |
as an array of floats or doubles. But with hash tables, you must convert |
| 57 |
both the index and value to and from decimal strings, just to access an |
| 58 |
element in the array. This makes it cumbersome to perform operations on |
| 59 |
the array as a whole. </LI> |
| 60 |
</UL> |
| 61 |
<P> |
| 62 |
The <B>vector</B> command tries to overcome these disadvantages |
| 63 |
while still retaining the ease of use of Tcl arrays. The <B>vector</B> command |
| 64 |
creates both a new Tcl command and associate array which are linked to |
| 65 |
the vector components. You can randomly access vector components though |
| 66 |
the elements of array. Not all indices are generated for the array, so |
| 67 |
printing the array (using the <B>parray</B> procedure) does not print out all |
| 68 |
the component values. You can use the Tcl command to access the array as |
| 69 |
a whole. You can copy, append, or sort vector using its command. If you |
| 70 |
need greater performance, or customized behavior, you can write your own |
| 71 |
C code to manage vectors. |
| 72 |
<H2><A NAME="sect4" HREF="#toc4">Example</A></H2> |
| 73 |
You create vectors using the <B>vector</B> command |
| 74 |
and its <B>create</B> operation. <BR> |
| 75 |
<CODE># Create a new vector. <BR> |
| 76 |
vector create y(50)<BR> |
| 77 |
</CODE>This creates a new vector named y. It has fifty components, by default, |
| 78 |
initialized to 0.0. In addition, both a Tcl command and array variable, |
| 79 |
both named y, are created. You can use either the command or variable |
| 80 |
to query or modify components of the vector. <BR> |
| 81 |
<CODE># Set the first value. <BR> |
| 82 |
set y(0) 9.25<BR> |
| 83 |
puts "y has [y length] components"<BR> |
| 84 |
</CODE>The array y can be used to read or set individual components of the vector. |
| 85 |
Vector components are indexed from zero. The array index must be a number |
| 86 |
less than the number of components. For example, it's an error if you try |
| 87 |
to set the 51st element of y. <BR> |
| 88 |
<CODE># This is an error. The vector only has 50 components.<BR> |
| 89 |
set y(50) 0.02<BR> |
| 90 |
</CODE>You can also specify a range of indices using a colon (:) to separate the |
| 91 |
first and last indices of the range. <BR> |
| 92 |
<CODE># Set the first six components of y <BR> |
| 93 |
set y(0:5) 25.2<BR> |
| 94 |
</CODE>If you don't include an index, then it will default to the first and/or |
| 95 |
last component of the vector. <BR> |
| 96 |
<CODE># Print out all the components of y <BR> |
| 97 |
puts "y = $y(:)"<BR> |
| 98 |
</CODE>There are special non-numeric indices. The index end, specifies the last |
| 99 |
component of the vector. It's an error to use this index if the vector is |
| 100 |
empty (length is zero). The index ++end can be used to extend the vector |
| 101 |
by one component and initialize it to a specific value. You can't read |
| 102 |
from the array using this index, though. <BR> |
| 103 |
<CODE># Extend the vector by one component.<BR> |
| 104 |
set y(++end) 0.02<BR> |
| 105 |
</CODE>The other special indices are min and max. They return the current |
| 106 |
smallest and largest components of the vector. <BR> |
| 107 |
<CODE># Print the bounds of the vector<BR> |
| 108 |
puts "min=$y(min) max=$y(max)"<BR> |
| 109 |
</CODE>To delete components from a vector, simply unset the corresponding array |
| 110 |
element. In the following example, the first component of y is deleted. |
| 111 |
All the remaining components of y will be moved down by one index as |
| 112 |
the length of the vector is reduced by one. <BR> |
| 113 |
<CODE># Delete the first component<BR> |
| 114 |
unset y(0)<BR> |
| 115 |
puts "new first element is $y(0)"<BR> |
| 116 |
</CODE>The vector's Tcl command can also be used to query or set the vector. <BR> |
| 117 |
<CODE># Create and set the components of a new vector<BR> |
| 118 |
vector create x<BR> |
| 119 |
x set { 0.02 0.04 0.06 0.08 0.10 0.12 0.14 0.16 0.18 0.20 }<BR> |
| 120 |
</CODE>Here we've created a vector x without a initial length specification. In |
| 121 |
this case, the length is zero. The <B>set</B> operation resets the vector, extending |
| 122 |
it and setting values for each new component. <P> |
| 123 |
There are several operations |
| 124 |
for vectors. The <B>range</B> operation lists the components of a vector between |
| 125 |
two indices. <BR> |
| 126 |
<CODE># List the components <BR> |
| 127 |
puts "x = [x range 0 end]"<BR> |
| 128 |
</CODE>You can search for a particular value using the <B>search</B> operation. It returns |
| 129 |
a list of indices of the components with the same value. If no component |
| 130 |
has the same value, it returns "". <BR> |
| 131 |
<CODE># Find the index of the biggest component<BR> |
| 132 |
set indices [x search $x(max)]<BR> |
| 133 |
</CODE>Other operations copy, append, or sort vectors. You can append vectors |
| 134 |
or new values onto an existing vector with the <B>append</B> operation. <BR> |
| 135 |
<CODE># Append assorted vectors and values to x<BR> |
| 136 |
x append x2 x3 { 2.3 4.5 } x4<BR> |
| 137 |
</CODE>The <B>sort</B> operation sorts the vector. If any additional vectors are specified, |
| 138 |
they are rearranged in the same order as the vector. For example, you could |
| 139 |
use it to sort data points represented by x and y vectors. <BR> |
| 140 |
<CODE># Sort the data points<BR> |
| 141 |
x sort y<BR> |
| 142 |
</CODE>The vector x is sorted while the components of y are rearranged so |
| 143 |
that the original x,y coordinate pairs are retained. <P> |
| 144 |
The <B>expr</B> operation |
| 145 |
lets you perform arithmetic on vectors. The result is stored in the vector. |
| 146 |
<BR> |
| 147 |
<CODE># Add the two vectors and a scalar<BR> |
| 148 |
x expr { x + y }<BR> |
| 149 |
x expr { x * 2 }<BR> |
| 150 |
</CODE>When a vector is modified, resized, or deleted, it may trigger call-backs |
| 151 |
to notify the clients of the vector. For example, when a vector used in |
| 152 |
the <B>graph</B> widget is updated, the vector automatically notifies the widget |
| 153 |
that it has changed. The graph can then redrawn itself at the next idle |
| 154 |
point. By default, the notification occurs when Tk is next idle. This way |
| 155 |
you can modify the vector many times without incurring the penalty of the |
| 156 |
graph redrawing itself for each change. You can change this behavior using |
| 157 |
the <B>notify</B> operation. <BR> |
| 158 |
<CODE># Make vector x notify after every change<BR> |
| 159 |
x notify always<BR> |
| 160 |
<tt> </tt> <tt> </tt> ...<BR> |
| 161 |
# Never notify<BR> |
| 162 |
x notify never<BR> |
| 163 |
<tt> </tt> <tt> </tt> ...<BR> |
| 164 |
# Force notification now<BR> |
| 165 |
x notify now<BR> |
| 166 |
<P> |
| 167 |
# Set Tcl callback for update of Tktable widget .t.<BR> |
| 168 |
x notify callback {.t conf -padx [.t cget -padx]; .t reread}<BR> |
| 169 |
<P> |
| 170 |
</CODE>To delete a vector, use the <B>vector delete</B> command. Both the vector and |
| 171 |
its corresponding Tcl command are destroyed. <BR> |
| 172 |
<CODE># Remove vector x<BR> |
| 173 |
vector destroy x<BR> |
| 174 |
</CODE>The psuedo vector <B>last</B> can be used at the end of an expression to implement |
| 175 |
running totals. During execution it resolves to the result from the previous |
| 176 |
vector element evaluation. <BR> |
| 177 |
<CODE>vector create A(10)<BR> |
| 178 |
vector create B(10)<BR> |
| 179 |
vector create S(10)<BR> |
| 180 |
vector create T(10)<BR> |
| 181 |
S expr A+B<BR> |
| 182 |
T expr S+last; # Running total<BR> |
| 183 |
|
| 184 |
<H2><A NAME="sect5" HREF="#toc5"></CODE>Syntax</A></H2> |
| 185 |
Vectors are created using the <B>vector create</B> operation. Th <B>create</B> |
| 186 |
operation can be invoked in one of three forms: |
| 187 |
<DL> |
| 188 |
|
| 189 |
<DT><B>vector create <I>vecName</I></B> </DT> |
| 190 |
<DD>This |
| 191 |
creates a new vector <I>vecName</I> which initially has no components. </DD> |
| 192 |
|
| 193 |
<DT><B>vector create |
| 194 |
<I>vecName</I></B>(<I>size</I>) </DT> |
| 195 |
<DD>This second form creates a new vector which will contain |
| 196 |
<I>size</I> number of components. The components will be indexed starting from |
| 197 |
zero (0). The default value for the components is 0.0. </DD> |
| 198 |
|
| 199 |
<DT><B>vector create <I>vecName</I></B>(<I>rows,columns</I>) |
| 200 |
</DT> |
| 201 |
<DD>This form allows creation of a matrix with the specified columns and <I>rows*columns</I> |
| 202 |
elements. See the <I>matrix</I> section for more details. </DD> |
| 203 |
|
| 204 |
<DT><B>vector create <I>vecName</I></B>(<I>first</I>:<I>last</I>) |
| 205 |
</DT> |
| 206 |
<DD>The last form creates a new vector of indexed <I>first</I> through <I>last</I>. <I>First</I> |
| 207 |
and <I>last</I> can be any integer value so long as <I>first</I> is less than <I>last</I>. </DD> |
| 208 |
</DL> |
| 209 |
<P> |
| 210 |
Vector |
| 211 |
names must start with a letter and consist of letters, digits, or underscores. |
| 212 |
<BR> |
| 213 |
<CODE># Error: must start with letter<BR> |
| 214 |
vector create 1abc<BR> |
| 215 |
</CODE>You can automatically generate vector names using the "#auto" vector |
| 216 |
name. The <B>create</B> operation will generate a unique vector name. <BR> |
| 217 |
<CODE>set vec [vector create #auto]<BR> |
| 218 |
puts "$vec has [$vec length] components"<BR> |
| 219 |
|
| 220 |
<H3><A NAME="sect6" HREF="#toc6"></CODE>Vector Indices</A></H3> |
| 221 |
Vectors are indexed by integers. You can access the individual |
| 222 |
vector components via its array variable or Tcl command. The string representing |
| 223 |
the index can be an integer, a numeric expression, a range, or a special |
| 224 |
keyword. <P> |
| 225 |
The index must lie within the current range of the vector, otherwise |
| 226 |
an an error message is returned. Normally the indices of a vector are start |
| 227 |
from 0. But you can use the <B>offset</B> operation to change a vector's indices |
| 228 |
on-the-fly. <BR> |
| 229 |
<CODE>puts $vecName(0)<BR> |
| 230 |
vecName offset -5<BR> |
| 231 |
puts $vecName(-5)<BR> |
| 232 |
</CODE>When <I>matrix numcols</I> is > 1, 2D indexes are supported using ROW,COL form. |
| 233 |
<BR> |
| 234 |
<CODE>vecName matrix numcols 3<BR> |
| 235 |
puts vecName(0,2)<BR> |
| 236 |
</CODE>You can also use numeric expressions as indices. The result of the expression |
| 237 |
must be an integer value. <BR> |
| 238 |
<CODE>set n 21<BR> |
| 239 |
set vecName($n+3) 50.2<BR> |
| 240 |
</CODE>The following special non-numeric indices are available: min, max, end, |
| 241 |
and ++end. <BR> |
| 242 |
<CODE>puts "min = $vecName($min)"<BR> |
| 243 |
set vecName(end) -1.2<BR> |
| 244 |
</CODE>The indices min and max will return the minimum and maximum values |
| 245 |
of the vector. Also available are: prod, sum, and mean. The index |
| 246 |
end returns the value of the last component in the vector. he index end,0 |
| 247 |
returns the value of the last row in column 0 of the vector. The index |
| 248 |
++end is used to append new value onto the vector. It automatically extends |
| 249 |
the vector by numcols and sets its value. <BR> |
| 250 |
<CODE># Append an new component to the end<BR> |
| 251 |
set vecName(++end) 3.2<BR> |
| 252 |
</CODE>A range of indices can be indicated by a colon (:). <BR> |
| 253 |
<CODE># Set the first six components to 1.0<BR> |
| 254 |
set vecName(0:5) 1.0<BR> |
| 255 |
</CODE>If no index is supplied the first or last component is assumed. <BR> |
| 256 |
<CODE># Print the values of all the components<BR> |
| 257 |
puts $vecName(:)<BR> |
| 258 |
|
| 259 |
<H2><A NAME="sect7" HREF="#toc7"></CODE>Vector Operations</A></H2> |
| 260 |
|
| 261 |
<DL> |
| 262 |
|
| 263 |
<DT><B>vector configure <I>? -flush bool -watchunset bool -oldcreate |
| 264 |
bool -maxsize int -novariable bool -nocommand bool?</I></B> </DT> |
| 265 |
<DD>The <B>configure</B> operation |
| 266 |
sets the default options used in creating vectors: these options are global |
| 267 |
to the interpreter. The <I>-maxsize</I> option, when non-zero, limits creation size. |
| 268 |
The <I>-oldcreate</I> enable the creation shortcut: <B>vector vec1 vec2 ...</B>. See the create |
| 269 |
command for details on the others. By default, these are all disabled or |
| 270 |
zero. </DD> |
| 271 |
</DL> |
| 272 |
</blockquote> |
| 273 |
|
| 274 |
<DL> |
| 275 |
|
| 276 |
<DT><B>vector create <I>vecName</I></B>?(<I>size</I>)?... ?<I>switches</I>? </DT> |
| 277 |
<DD>The <B>create</B> operation creates |
| 278 |
a new vector <I>vecName</I>. The <I>size</I> may be an integer, a START:END range or ROW,COL |
| 279 |
(see matrix). This creates both a Tcl command and array variable called |
| 280 |
<I>vecName</I>. The name <I>vecName</I> must be unique, so another Tcl command or array |
| 281 |
variable can not already exist in the current scope. You may access the |
| 282 |
components of the vector using the variable. If you change a value in |
| 283 |
the array, or unset an array element, the vector is updated to reflect |
| 284 |
the changes. When the variable <I>vecName</I> is unset, the vector and its Tcl |
| 285 |
command are also destroyed. <P> |
| 286 |
The vector has optional switches that affect |
| 287 |
how the vector is created. They are as follows: <blockquote></DD> |
| 288 |
|
| 289 |
<DT><B>-variable <I>varName</I></B> </DT> |
| 290 |
<DD>Specifies |
| 291 |
the name of a Tcl variable to be mapped to the vector. If the variable already |
| 292 |
exists, it is first deleted, then recreated. If <I>varName</I> is the empty string, |
| 293 |
then no variable will be mapped. You can always map a variable back to the |
| 294 |
vector using the vector's <B>variable</B> operation. </DD> |
| 295 |
|
| 296 |
<DT><B>-command <I>cmdName</I></B> </DT> |
| 297 |
<DD>Maps a Tcl |
| 298 |
command to the vector. The vector can be accessed using <I>cmdName</I> and one |
| 299 |
of the vector instance operations. A Tcl command by that name cannot already |
| 300 |
exist. If <I>cmdName</I> is the empty string, no command mapping will be made. </DD> |
| 301 |
|
| 302 |
<DT><B>-watchunset |
| 303 |
<I>boolean</I></B> </DT> |
| 304 |
<DD>Indicates that the vector should automatically delete itself if |
| 305 |
the variable associated with the vector is unset. By default, the vector |
| 306 |
will not be deleted. This is different from previous releases. Set <I>boolean</I> |
| 307 |
to "true" to get the old behavior. </DD> |
| 308 |
|
| 309 |
<DT><B>-flush <I>boolean</I></B> </DT> |
| 310 |
<DD>Indicates that the vector |
| 311 |
should automatically flush the cached variable elements which unsets all |
| 312 |
the elements of the Tcl array variable associated with the vector, freeing |
| 313 |
memory associated with the variable. This includes both the hash table |
| 314 |
and the hash keys. The down side is that this effectively flushes the caching |
| 315 |
of vector elements in the array. This means that the subsequent reads of |
| 316 |
the array will require a decimal to string conversion. By default, flushing |
| 317 |
is disabled. </DD> |
| 318 |
</DL> |
| 319 |
</blockquote> |
| 320 |
|
| 321 |
<DL> |
| 322 |
|
| 323 |
<DT><B>vector destroy <I>vecName</I></B> ?<I>vecName...</I>? </DT> |
| 324 |
<DD>Destroy vectors. </DD> |
| 325 |
|
| 326 |
<DT><B>vector expr |
| 327 |
<I>expression</I></B> </DT> |
| 328 |
<DD><blockquote>All binary operators take vectors as operands (remember that |
| 329 |
numbers are treated as one-component vectors).The exact action of binary |
| 330 |
operators depends upon the length of the second operand. If the second |
| 331 |
operand has only one component, then each element of the first vector operand |
| 332 |
is computed by that value. For example, the expression "x * 2" multiples |
| 333 |
all elements of the vector x by 2. If the second operand has more than |
| 334 |
one component, both operands must be the same length. Each pair of corresponding |
| 335 |
elements are computed. So "x + y" adds the the first components of x and |
| 336 |
y together, the second, and so on. <P> |
| 337 |
The valid operators are listed below, |
| 338 |
grouped in decreasing order of precedence: </DD> |
| 339 |
|
| 340 |
<DT><B>- !</B> </DT> |
| 341 |
<DD>Unary minus and logical |
| 342 |
NOT. The unary minus flips the sign of each component in the vector. The |
| 343 |
logical not operator returns a vector of whose values are 0.0 or 1.0. For |
| 344 |
each non-zero component 1.0 is returned, 0.0 otherwise. </DD> |
| 345 |
|
| 346 |
<DT><B>^</B> </DT> |
| 347 |
<DD>Exponentiation. </DD> |
| 348 |
|
| 349 |
<DT><B>* |
| 350 |
/ %</B> </DT> |
| 351 |
<DD>Multiply, divide, remainder. </DD> |
| 352 |
|
| 353 |
<DT><B>+ -</B> </DT> |
| 354 |
<DD>Add and subtract. </DD> |
| 355 |
|
| 356 |
<DT><B><< >></B> </DT> |
| 357 |
<DD>Left and |
| 358 |
right shift. Circularly shifts the values of the vector </DD> |
| 359 |
|
| 360 |
<DT><B>< > <= >=</B> </DT> |
| 361 |
<DD>Boolean |
| 362 |
less, greater, less than or equal, and greater than or equal. Each operator |
| 363 |
returns a vector of ones and zeros. If the condition is true, 1.0 is the |
| 364 |
component value, 0.0 otherwise. </DD> |
| 365 |
|
| 366 |
<DT><B>== !=</B> </DT> |
| 367 |
<DD>Boolean equal and not equal. Each operator |
| 368 |
returns a vector of ones and zeros. If the condition is true, 1.0 is the |
| 369 |
component value, 0.0 otherwise. </DD> |
| 370 |
|
| 371 |
<DT><B>&&</B> </DT> |
| 372 |
<DD>Logical AND. Produces a 1 result if both |
| 373 |
operands are non-zero, 0 otherwise. </DD> |
| 374 |
|
| 375 |
<DT><B>||</B> </DT> |
| 376 |
<DD>Logical OR. Produces a 0 result if both |
| 377 |
operands are zero, 1 otherwise. </DD> |
| 378 |
|
| 379 |
<DT><I>x<B>?<I>y<B>:<I>z</I></B></I></B></I> </DT> |
| 380 |
<DD>If-then-else, as in C. </DD> |
| 381 |
</DL> |
| 382 |
<P> |
| 383 |
<P> |
| 384 |
See the C manual |
| 385 |
for more details on the results produced by each operator. All of the binary |
| 386 |
operators group left-to-right within the same precedence level. <P> |
| 387 |
Several |
| 388 |
mathematical functions are supported for vectors. Each of the following |
| 389 |
functions invokes the math library function of the same name; see the manual |
| 390 |
entries for the library functions for details on what they do. The operation |
| 391 |
is applied to all elements of the vector returning the results. All functions |
| 392 |
take a vector operand. If no vector operand is used in the call, the current |
| 393 |
vector is assumed. eg. <BR> |
| 394 |
<CODE>vector create aVec<BR> |
| 395 |
aVec seq 0 100<BR> |
| 396 |
aVec expr {2*abs(aVec)-1}<BR> |
| 397 |
aVec length 100<BR> |
| 398 |
aVec expr {2*row()}<BR> |
| 399 |
vector expr {2*row()} ; # ERROR!<BR> |
| 400 |
<BR> |
| 401 |
</CODE><CODE><P> |
| 402 |
<B>acos</B><tt> </tt> <tt> </tt> <B>cos</B><tt> </tt> <tt> </tt> <B>hypot</B><tt> </tt> <tt> </tt> <B>sinh</B> <BR> |
| 403 |
<B>asin</B><tt> </tt> <tt> </tt> <B>cosh</B><tt> </tt> <tt> </tt> <B>log</B><tt> </tt> <tt> </tt> <B>sqrt</B> <BR> |
| 404 |
<B>atan</B><tt> </tt> <tt> </tt> <B>exp</B><tt> </tt> <tt> </tt> <B>log10</B><tt> </tt> <tt> </tt> <B>tan</B> <BR> |
| 405 |
<B>ceil</B><tt> </tt> <tt> </tt> <B>floor</B><tt> </tt> <tt> </tt> <B>sin</B><tt> </tt> <tt> </tt> <B>tanh</B> <BR> |
| 406 |
</CODE>Additional functions are: |
| 407 |
<DL> |
| 408 |
|
| 409 |
<DT><B>abs</B> </DT> |
| 410 |
<DD>Returns the absolute value of each component. |
| 411 |
</DD> |
| 412 |
|
| 413 |
<DT><B>random</B> </DT> |
| 414 |
<DD>Returns a vector of non-negative values uniformly distributed between |
| 415 |
[0.0, 1.0) using <I>drand48</I>. The seed comes from the internal clock of the machine |
| 416 |
or may be set manual with the srandom function. </DD> |
| 417 |
|
| 418 |
<DT><B>round</B> </DT> |
| 419 |
<DD>Rounds each component |
| 420 |
of the vector. </DD> |
| 421 |
|
| 422 |
<DT><B>srandom</B> </DT> |
| 423 |
<DD>Initializes the random number generator using <I>srand48</I>. |
| 424 |
The high order 32-bits are set using the integral portion of the first |
| 425 |
vector component. All other components are ignored. The low order 16-bits |
| 426 |
are set to an arbitrary value. </DD> |
| 427 |
</DL> |
| 428 |
<P> |
| 429 |
The following functions return a single |
| 430 |
value. |
| 431 |
<DL> |
| 432 |
|
| 433 |
<DT><B>adev</B> </DT> |
| 434 |
<DD>Returns the average deviation (defined as the sum of the absolute |
| 435 |
values of the differences between component and the mean, divided by the |
| 436 |
length of the vector). </DD> |
| 437 |
|
| 438 |
<DT><B>kurtosis</B> </DT> |
| 439 |
<DD>Returns the degree of peakedness (fourth |
| 440 |
moment) of the vector. </DD> |
| 441 |
|
| 442 |
<DT><B>length</B> </DT> |
| 443 |
<DD>Returns the number of components in the vector. |
| 444 |
</DD> |
| 445 |
|
| 446 |
<DT><B>max</B> </DT> |
| 447 |
<DD>Returns the vector's maximum value. </DD> |
| 448 |
|
| 449 |
<DT><B>mean</B> </DT> |
| 450 |
<DD>Returns the mean value of the |
| 451 |
vector. </DD> |
| 452 |
|
| 453 |
<DT><B>median</B> </DT> |
| 454 |
<DD>Returns the median of the vector. </DD> |
| 455 |
|
| 456 |
<DT><B>min</B> </DT> |
| 457 |
<DD>Returns the vector's |
| 458 |
minimum value. </DD> |
| 459 |
|
| 460 |
<DT><B>q1</B> </DT> |
| 461 |
<DD>Returns the first quartile of the vector. </DD> |
| 462 |
|
| 463 |
<DT><B>q3</B> </DT> |
| 464 |
<DD>Returns the |
| 465 |
third quartile of the vector. </DD> |
| 466 |
|
| 467 |
<DT><B>prod</B> </DT> |
| 468 |
<DD>Returns the product of the components. |
| 469 |
</DD> |
| 470 |
|
| 471 |
<DT><B>sdev</B> </DT> |
| 472 |
<DD>Returns the standard deviation (defined as the square root of the |
| 473 |
variance) of the vector. </DD> |
| 474 |
|
| 475 |
<DT><B>skew</B> </DT> |
| 476 |
<DD>Returns the skewness (or third moment) of |
| 477 |
the vector. This characterizes the degree of asymmetry of the vector about |
| 478 |
the mean. </DD> |
| 479 |
|
| 480 |
<DT><B>sum</B> </DT> |
| 481 |
<DD>Returns the sum of the components. </DD> |
| 482 |
|
| 483 |
<DT><B>var</B> </DT> |
| 484 |
<DD>Returns the variance |
| 485 |
of the vector. The sum of the squared differences between each component |
| 486 |
and the mean is computed. The variance is the sum divided by the length |
| 487 |
of the vector minus 1. </DD> |
| 488 |
</DL> |
| 489 |
<P> |
| 490 |
This last set of functions returns a vector of the |
| 491 |
same length as the argument. |
| 492 |
<DL> |
| 493 |
|
| 494 |
<DT><B>invert</B> </DT> |
| 495 |
<DD>Returns vector with elements in reversed |
| 496 |
order. </DD> |
| 497 |
|
| 498 |
<DT><B>norm</B> </DT> |
| 499 |
<DD>Scales the values of the vector to lie in the range [0.0..1.0]. |
| 500 |
</DD> |
| 501 |
|
| 502 |
<DT><B>row</B> </DT> |
| 503 |
<DD>Psuedo function to get the current row. </DD> |
| 504 |
|
| 505 |
<DT><B>sort</B> </DT> |
| 506 |
<DD>Returns the vector components |
| 507 |
sorted in ascending order. </DD> |
| 508 |
|
| 509 |
<DT><B>shift(nVec,N)</B> </DT> |
| 510 |
<DD>This is the only function taking |
| 511 |
a second arg. It provides a version of <I>nvec</I> shifted by N places. When N is |
| 512 |
a scalar or vector with only one element, shift fills vacant area with |
| 513 |
0. Otherwise the second element of <I>nVec</I> is used for the fill value. One use |
| 514 |
for this is providing running totals. </DD> |
| 515 |
</DL> |
| 516 |
</blockquote> |
| 517 |
|
| 518 |
<DL> |
| 519 |
|
| 520 |
<DT><B>vector names </B>?<I>pattern</I>? </DT> |
| 521 |
<DD>Return names |
| 522 |
of all defined vectors. </DD> |
| 523 |
</DL> |
| 524 |
</blockquote> |
| 525 |
|
| 526 |
<DL> |
| 527 |
|
| 528 |
<DT><B>vector op</B> <I>operation vecName</I> ?<I>arg</I>?... </DT> |
| 529 |
<DD>Invoke instance |
| 530 |
operation. Supported operations are defined in the next section. Op is the |
| 531 |
only way to invoke instance operation sub-commands when -command is defined |
| 532 |
as empty in a vector. It also allows writing vector code that is checkable |
| 533 |
by a syntax checkers. eg. <BR> |
| 534 |
<P> |
| 535 |
<CODE>vector create v1<BR> |
| 536 |
v1 op append {1 2 3}<BR> |
| 537 |
v1 op modify 1 2.1<BR> |
| 538 |
<P> |
| 539 |
</DD> |
| 540 |
</DL> |
| 541 |
</blockquote> |
| 542 |
|
| 543 |
<H2><A NAME="sect8" HREF="#toc8"></CODE>Instance Operations</A></H2> |
| 544 |
You can also use the vector's Tcl command to query or |
| 545 |
modify it. The general form is <BR> |
| 546 |
<P> |
| 547 |
<CODE><I>vecName <I>operation</I></I> ?<I>arg</I>?...<BR> |
| 548 |
</CODE>Note this is equivalent to the form: <BR> |
| 549 |
<P> |
| 550 |
<CODE><B>vector op</B> <I>operation vecName</I> ?<I>arg</I>?...<BR> |
| 551 |
</CODE>Both <I>operation</I> and its arguments determine the exact behavior of the command. |
| 552 |
The operations available for vectors are listed below. |
| 553 |
<DL> |
| 554 |
|
| 555 |
<DT><I>vecName <B>+</B></I> <I>item</I> </DT> |
| 556 |
<DD><I>vecName |
| 557 |
<B>-</B></I> <I>item</I> <I>vecName <B>*</B></I> <I>item</I> <I>vecName <B>/</B></I> <I>item</I> Perform binary op and return result |
| 558 |
as a list. </DD> |
| 559 |
|
| 560 |
<DT><I>vecName <B>append</B></I> <I>item</I> ?<I>item</I>?... </DT> |
| 561 |
<DD>Appends the component values from |
| 562 |
<I>item</I> to <I>vecName</I>. <I>Item</I> can be either the name of a vector or a list of numeric |
| 563 |
values. </DD> |
| 564 |
|
| 565 |
<DT><I>vecName <B>binread</B></I> <I>channel</I> ?<I>length</I>? ?<I>switches</I>? </DT> |
| 566 |
<DD>Reads binary values |
| 567 |
from a Tcl channel. Values are either appended to the end of the vector |
| 568 |
or placed at a given index (using the <B>-at</B> option), overwriting existing |
| 569 |
values. Data is read until EOF is found on the channel or a specified number |
| 570 |
of values <I>length</I> are read (note that this is not necessarily the same |
| 571 |
as the number of bytes). The following switches are supported: <blockquote></DD> |
| 572 |
|
| 573 |
<DT><B>-swap</B> </DT> |
| 574 |
<DD>Swap |
| 575 |
bytes and words. The default endian is the host machine. </DD> |
| 576 |
|
| 577 |
<DT><B>-at <I>index</I></B> </DT> |
| 578 |
<DD>New values |
| 579 |
will start at vector index <I>index</I>. This will overwrite any current values. |
| 580 |
</DD> |
| 581 |
|
| 582 |
<DT><B>-format</B> <I>format</I> </DT> |
| 583 |
<DD>Specifies the format of the data. <I>Format</I> can be one of the |
| 584 |
following: "i1", "i2", "i4", "i8", "u1, "u2", "u4", "u8", "r4", "r8", or |
| 585 |
"r16". The number indicates the number of bytes required for each value. |
| 586 |
The letter indicates the type: "i" for signed, "u" for unsigned, "r" or |
| 587 |
real. The default format is "r16". </DD> |
| 588 |
</DL> |
| 589 |
</blockquote> |
| 590 |
|
| 591 |
<DL> |
| 592 |
|
| 593 |
<DT><I>vecName <B>binwrite</B></I> <I>channel</I> ?<I>length</I>? ?<I>-at |
| 594 |
index</I>? </DT> |
| 595 |
<DD>Like <B>binread</B>, but writes data. </DD> |
| 596 |
|
| 597 |
<DT><I>vecName <B>clear</B></I> </DT> |
| 598 |
<DD>Clears the element |
| 599 |
indices from the array variable associated with <I>vecName</I>. This doesn't affect |
| 600 |
the components of the vector. By default, the number of entries in the |
| 601 |
Tcl array doesn't match the number of components in the vector. This is |
| 602 |
because its too expensive to maintain decimal strings for both the index |
| 603 |
and value for each component. Instead, the index and value are saved only |
| 604 |
when you read or write an element with a new index. This command removes |
| 605 |
the index and value strings from the array. This is useful when the vector |
| 606 |
is large. </DD> |
| 607 |
|
| 608 |
<DT><I>vecName <B>delete</B></I> <I>index</I> ?<I>index</I>?... </DT> |
| 609 |
<DD>Deletes the <I>index</I>th component from |
| 610 |
the vector <I>vecName</I>. <I>Index</I> is the index of the element to be deleted. This |
| 611 |
is the same as unsetting the array variable element <I>index</I>. The vector is |
| 612 |
compacted after all the indices have been deleted. </DD> |
| 613 |
|
| 614 |
<DT><I>vecName <B>dup</B></I> <I>destName</I> |
| 615 |
</DT> |
| 616 |
<DD>Copies <I>vecName</I> to <I>destName</I>. <I>DestName</I> is the name of a destination vector. |
| 617 |
If a vector <I>destName</I> already exists, it is overwritten with the components |
| 618 |
of <I>vecName</I>. Otherwise a new vector is created. </DD> |
| 619 |
|
| 620 |
<DT><I>vecName <B>expr</B></I> <I>expression</I> |
| 621 |
</DT> |
| 622 |
<DD>Computes the expression and resets the values of the vector accordingly. |
| 623 |
Both scalar and vector math operations are allowed. All values in expressions |
| 624 |
are either real numbers or names of vectors. All numbers are treated as |
| 625 |
one component vectors. </DD> |
| 626 |
|
| 627 |
<DT><I>vecName <B>index</B></I> <I>index</I> ?<I>value</I>?... </DT> |
| 628 |
<DD>Get/set individual vector |
| 629 |
values. This provides element updating when <I>-variable</I> is set to empty. </DD> |
| 630 |
|
| 631 |
<DT><I>vecName |
| 632 |
<B>insert</B></I> <I>index</I> <I>item</I> ?<I>item</I>?... </DT> |
| 633 |
<DD>Inserts the component values from <I>item</I> to <I>vecName</I> |
| 634 |
at <I>index</I> <I>Item</I> can be either the name of a vector or a list of numeric values. |
| 635 |
</DD> |
| 636 |
|
| 637 |
<DT><I>vecName <B>length</B></I> ?<I>newSize</I>? </DT> |
| 638 |
<DD>Queries or resets the number of components in |
| 639 |
<I>vecName</I>. <I>NewSize</I> is a number specifying the new size of the vector. If <I>newSize</I> |
| 640 |
is smaller than the current size of <I>vecName</I>, <I>vecName</I> is truncated. If <I>newSize</I> |
| 641 |
is greater, the vector is extended and the new components are initialized |
| 642 |
to 0.0. If no <I>newSize</I> argument is present, the current length of the vector |
| 643 |
is returned. </DD> |
| 644 |
|
| 645 |
<DT><I>vecName <B>matrix <I> ...</I></B></I> </DT> |
| 646 |
<DD>Matrix provides a 2D array view into 1D data. |
| 647 |
It provides indexing operations in ROW,COL form making it suitable for |
| 648 |
use with TkTable. Data storage remains unchanged: vectors are still just |
| 649 |
a single long array. For example, here are two ways to create a 3 column |
| 650 |
by 10 row matrix: <BR> |
| 651 |
<P> |
| 652 |
<CODE>vector create aVec(10,3)<BR> |
| 653 |
vector create bVec(30)<BR> |
| 654 |
bVec matrix numcols 3<BR> |
| 655 |
set aVec(0,0) 99<BR> |
| 656 |
set bVec(29,2) -99<BR> |
| 657 |
aVec append {5 6 7}; # aVec now has 11 rows.<BR> |
| 658 |
aVec append 1 2; # Now aVec has 13 rows!<BR> |
| 659 |
<P> |
| 660 |
</CODE>Note that data is appended only in increments of numcols. Elements 0-2 make |
| 661 |
up the first row, 3-5 the second, etc. Elements will appear only in increments |
| 662 |
of the column size. <blockquote></DD> |
| 663 |
|
| 664 |
<DT><I>vecName <B>matrix copy <I>dstcolumn</I></B></I> <I>srccolumn</I> <I>?srcVec?</I> </DT> |
| 665 |
<DD>Copy |
| 666 |
a column of element values to column <I>dstcolumn</I> from <I>srccolumn</I>. If vector |
| 667 |
<I>srcVec</I> is given, and not the same as <I>vecName</I>, the columns numbers must |
| 668 |
be different. If the <I>srcVec</I> column is longer, <I>vecName</I> will be extended. If |
| 669 |
shorter, remaining destination values are not overwritten. </DD> |
| 670 |
|
| 671 |
<DT><I>vecName <B>matrix |
| 672 |
delete <I>column</I></B></I>. </DT> |
| 673 |
<DD>Delete elements in a column. Note that <B>numcols</B>, which must |
| 674 |
be greater than 1, will be decremented. </DD> |
| 675 |
|
| 676 |
<DT><I>vecName <B>matrix get <I>column</I></B></I> </DT> |
| 677 |
<DD>Get the |
| 678 |
element in a column: this number must be less than <B>numcols</B>. Note that <B>numcols</B> |
| 679 |
must be non-zero. </DD> |
| 680 |
|
| 681 |
<DT><I>vecName <B>matrix insert <I>column</I></B></I> <I>?initvalue?</I> . </DT> |
| 682 |
<DD>Insert a new |
| 683 |
column of elements at column (default 0). The new column is initialized |
| 684 |
with <I>initvalue</I>, or <I>0.0</I> if not specified. Note that <B>numcols</B> will be incremented. |
| 685 |
</DD> |
| 686 |
|
| 687 |
<DT><I>vecName <B>matrix multiply <I>srcVec</I></B></I> ?<I>dstVec</I>? </DT> |
| 688 |
<DD>Perform matrix multiplication |
| 689 |
using <B>srcVec</B>, placing results either in <B>dstVec</B>, or returned as a list. The |
| 690 |
numrows of <I>srcVec</I> must equal numcols in <I>vecName</I>. One application for multiply |
| 691 |
is coordinate transformation. </DD> |
| 692 |
|
| 693 |
<DT><I>vecName <B>matrix numcols <I>?size?</I></B></I> </DT> |
| 694 |
<DD>Get or set the |
| 695 |
number of columns for a vectors data. Values >1 enable array variables to |
| 696 |
accept 2d matrix indexes. For example with a numcols of 10, <B>$vec1(1,2)</B> refers |
| 697 |
to the 13th element in the vector. A vectors size is also constrained to |
| 698 |
multiples of numcols, as is it's offset. By default, numcols is 1. </DD> |
| 699 |
|
| 700 |
<DT><I>vecName |
| 701 |
<B>matrix numrows <I>?size?</I></B></I> </DT> |
| 702 |
<DD>Get or set the length of rows in a columns for a |
| 703 |
vector. By default, this is just the <I>vector length/numcols</I>. Setting this |
| 704 |
value simply provides a convenient way to increase or decrease the vector |
| 705 |
size by multiples of <I>numcols</I>. </DD> |
| 706 |
|
| 707 |
<DT><I>vecName <B>matrix set <I>column</I></B></I> <I>?valuelist?</I> </DT> |
| 708 |
<DD>Set |
| 709 |
value elements in a column: this number must be less than <B>numcols</B>. The |
| 710 |
<I>valuelist</I> is a list values. If this list is shorter than the column, it's |
| 711 |
last value is used for all remaining columns. The column gets set to the |
| 712 |
values of <I>item</I>, or <I>0.0</I> by default. </DD> |
| 713 |
|
| 714 |
<DT><I>vecName <B>matrix shift <I>column</I></B></I> <I>amount</I> ?<I>startoffset</I>? |
| 715 |
</DT> |
| 716 |
<DD>Shifts the values of a column by integer in<I>amount</I>. A negative value shifts |
| 717 |
upward. The <I>startoffset</I> indicates where to start shifting from. </DD> |
| 718 |
|
| 719 |
<DT><I>vecName <B>matrix |
| 720 |
sort <I>column</I></B></I> <I>?-reverse?</I> </DT> |
| 721 |
<DD>Sort the vector by the given column. </DD> |
| 722 |
|
| 723 |
<DT><I>vecName <B>matrix |
| 724 |
transpose</B></I> </DT> |
| 725 |
<DD>Transpose all columns with rows in matrix. Note that this is a |
| 726 |
no-op if <B>numcols</B> is 1. Otherwise, numcols will change to <B>vectorLength/numcols</B>. |
| 727 |
</DD> |
| 728 |
</DL> |
| 729 |
</blockquote> |
| 730 |
|
| 731 |
<DL> |
| 732 |
|
| 733 |
<DT><I>vecName <B>merge</B></I> <I>srcName</I> ?<I>srcName</I>?... </DT> |
| 734 |
<DD>Merges the named vectors into a single |
| 735 |
vector. The resulting vector is formed by merging the components of each |
| 736 |
source vector one index at a time. </DD> |
| 737 |
|
| 738 |
<DT><I>vecName <B>notify</B></I> ?<I>keyword</I>? ?<I>script</I>? </DT> |
| 739 |
<DD>Queries |
| 740 |
or controls how vector clients are notified of changes to the vector. Also |
| 741 |
allows setting a notifier callback. The exact behavior is determined by |
| 742 |
<I>keyword</I>. <blockquote></DD> |
| 743 |
|
| 744 |
<DT>always </DT> |
| 745 |
<DD>Indicates that clients are to be notified immediately |
| 746 |
whenever the vector is updated. </DD> |
| 747 |
|
| 748 |
<DT>never </DT> |
| 749 |
<DD>Indicates that no clients are to |
| 750 |
be notified. </DD> |
| 751 |
|
| 752 |
<DT>whenidle </DT> |
| 753 |
<DD>Indicates that clients are to be notified at the |
| 754 |
next idle point whenever the vector is updated. </DD> |
| 755 |
|
| 756 |
<DT>now </DT> |
| 757 |
<DD>If any client notifications |
| 758 |
is currently pending, they are notified immediately. </DD> |
| 759 |
|
| 760 |
<DT>cancel </DT> |
| 761 |
<DD>Cancels pending |
| 762 |
notifications of clients using the vector. </DD> |
| 763 |
|
| 764 |
<DT>pending </DT> |
| 765 |
<DD>Returns 1 if a client |
| 766 |
notification is pending, and 0 otherwise. </DD> |
| 767 |
|
| 768 |
<DT>callback ?<I>script</I>? </DT> |
| 769 |
<DD>Query or |
| 770 |
set a Tcl callback script that is evaluated when a vector is updated. </DD> |
| 771 |
</DL> |
| 772 |
</blockquote> |
| 773 |
|
| 774 |
<DL> |
| 775 |
|
| 776 |
<DT><I>vecName |
| 777 |
<B>populate</B></I> <I>destName</I> ?<I>density</I>? </DT> |
| 778 |
<DD>Creates a vector <I>destName</I> which is a superset |
| 779 |
of <I>vecName</I>. <I>DestName</I> will include all the components of <I>vecName</I>, in addition |
| 780 |
the interval between each of the original components will contain a <I>density</I> |
| 781 |
number of new components, whose values are evenly distributed between the |
| 782 |
original components values. This is useful for generating abscissas to |
| 783 |
be interpolated along a spline. </DD> |
| 784 |
|
| 785 |
<DT><I>vecName <B>range</B></I> <I>firstIndex</I> ?<I>lastIndex</I>?... </DT> |
| 786 |
<DD>Returns |
| 787 |
a list of numeric values representing the vector components between two |
| 788 |
indices. Both <I>firstIndex</I> and <I>lastIndex</I> are indices representing the range |
| 789 |
of components to be returned. If <I>lastIndex</I> is less than <I>firstIndex</I>, the |
| 790 |
components are listed in reverse order. </DD> |
| 791 |
|
| 792 |
<DT><I>vecName <B>search</B></I> <I>value</I> ?<I>value</I>? </DT> |
| 793 |
<DD>Searches |
| 794 |
for a value or range of values among the components of <I>vecName</I>. If one |
| 795 |
<I>value</I> argument is given, a list of indices of the components which equal |
| 796 |
<I>value</I> is returned. If a second <I>value</I> is also provided, then the indices |
| 797 |
of all components which lie within the range of the two values are returned. |
| 798 |
If no components are found, then "" is returned. </DD> |
| 799 |
|
| 800 |
<DT><I>vecName <B>set</B></I> <I>item</I> </DT> |
| 801 |
<DD>Resets |
| 802 |
the components of the vector to <I>item</I>. <I>Item</I> can be either a list of numeric |
| 803 |
expressions or another vector. </DD> |
| 804 |
|
| 805 |
<DT><I>vecName <B>seq</B></I> <I>start</I> ?<I>finish</I>? ?<I>step</I>? </DT> |
| 806 |
<DD>Generates |
| 807 |
a sequence of values starting with the value <I>start</I>. <I>Finish</I> indicates the |
| 808 |
terminating value of the sequence. The vector is automatically resized |
| 809 |
to contain just the sequence. If three arguments are present, <I>step</I> designates |
| 810 |
the interval. <P> |
| 811 |
With only two arguments (no <I>finish</I> argument), the sequence |
| 812 |
will continue until the vector is filled. With one argument, the interval |
| 813 |
defaults to 1.0. </DD> |
| 814 |
|
| 815 |
<DT><I>vecName <B>sort</B></I> ?<B>-reverse</B>? ?<I>argName</I>?... </DT> |
| 816 |
<DD>Sorts the vector <I>vecName</I> |
| 817 |
in increasing order. If the <B>-reverse</B> flag is present, the vector is sorted |
| 818 |
in decreasing order. If other arguments <I>argName</I> are present, they are the |
| 819 |
names of vectors which will be rearranged in the same manner as <I>vecName</I>. |
| 820 |
Each vector must be the same length as <I>vecName</I>. You could use this to sort |
| 821 |
the x vector of a graph, while still retaining the same x,y coordinate |
| 822 |
pairs in a y vector. </DD> |
| 823 |
|
| 824 |
<DT><I>vecName <B>split</B></I> <I>dstName</I> ?<I>dstName</I>?... </DT> |
| 825 |
<DD>Split the vector into |
| 826 |
a multiple vectors. The resulting N vectors each contain the mod-Nth element |
| 827 |
from source. </DD> |
| 828 |
|
| 829 |
<DT><I>vecName <B>variable</B></I> <I>varName</I> </DT> |
| 830 |
<DD>Maps a Tcl variable to the vector, |
| 831 |
creating another means for accessing the vector. The variable <I>varName</I> |
| 832 |
can't already exist. This overrides any current variable mapping the vector |
| 833 |
may have. </DD> |
| 834 |
</DL> |
| 835 |
</blockquote> |
| 836 |
|
| 837 |
<H2><A NAME="sect9" HREF="#toc9">C Language API</A></H2> |
| 838 |
You can create, modify, and destroy vectors from |
| 839 |
C code, using library routines. You need to include the header file blt.h. |
| 840 |
It contains the definition of the structure <B>Blt_Vector</B>, which represents |
| 841 |
the vector. It appears below. <BR> |
| 842 |
<CODE>typedef struct {<BR> |
| 843 |
double *<I>valueArr</I>; <BR> |
| 844 |
int <I>numValues</I>; <BR> |
| 845 |
int <I>arraySize</I>; <BR> |
| 846 |
double <I>min</I>, <I>max</I>; <BR> |
| 847 |
} <B>Blt_Vector</B>;<BR> |
| 848 |
</CODE>The field <I>valueArr</I> points to memory holding the vector components. The |
| 849 |
components are stored in a double precision array, whose size size is represented |
| 850 |
by <I>arraySize</I>. <I>NumValues</I> is the length of vector. The size of the array |
| 851 |
is always equal to or larger than the length of the vector. <I>Min</I> and <I>max</I> |
| 852 |
are minimum and maximum component values. |
| 853 |
<H2><A NAME="sect10" HREF="#toc10">Library Routines</A></H2> |
| 854 |
The following |
| 855 |
routines are available from C to manage vectors. Vectors are identified |
| 856 |
by the vector name. <P> |
| 857 |
<B>Blt_CreateVector</B> <blockquote> |
| 858 |
<DL> |
| 859 |
|
| 860 |
<DT>Synopsis: </DT> |
| 861 |
<DD><BR> |
| 862 |
<CODE>int <B>Blt_CreateVector</B> (<I>interp</I>, <I>vecName</I>, <I>length</I>, <I>vecPtrPtr</I>)<BR> |
| 863 |
<blockquote>Tcl_Interp *<I>interp</I>;<BR> |
| 864 |
char *<I>vecName</I>;<BR> |
| 865 |
int <I>length</I>;<BR> |
| 866 |
Blt_Vector **<I>vecPtrPtr</I>;<BR> |
| 867 |
</DD> |
| 868 |
</DL> |
| 869 |
</blockquote> |
| 870 |
|
| 871 |
<DL> |
| 872 |
|
| 873 |
<DT></CODE>Description: </DT> |
| 874 |
<DD>Creates a new vector <I>vecName</I> with a length of <I>length</I>. <B>Blt_CreateVector</B> |
| 875 |
creates both a new Tcl command and array variable <I>vecName</I>. Neither a command |
| 876 |
nor variable named <I>vecName</I> can already exist. A pointer to the vector |
| 877 |
is placed into <I>vecPtrPtr</I>. </DD> |
| 878 |
|
| 879 |
<DT>Results: </DT> |
| 880 |
<DD>Returns TCL_OK if the vector is successfully |
| 881 |
created. If <I>length</I> is negative, a Tcl variable or command <I>vecName</I> already |
| 882 |
exists, or memory cannot be allocated for the vector, then TCL_ERROR |
| 883 |
is returned and <I>interp->result</I> will contain an error message. </DD> |
| 884 |
</DL> |
| 885 |
</blockquote> |
| 886 |
<P> |
| 887 |
<P> |
| 888 |
<B>Blt_DeleteVectorByName</B> |
| 889 |
<blockquote> |
| 890 |
<DL> |
| 891 |
|
| 892 |
<DT>Synopsis: </DT> |
| 893 |
<DD><BR> |
| 894 |
<CODE>int <B>Blt_DeleteVectorByName</B> (<I>interp</I>, <I>vecName</I>)<BR> |
| 895 |
<blockquote>Tcl_Interp *<I>interp</I>;<BR> |
| 896 |
char *<I>vecName</I>;<BR> |
| 897 |
</DD> |
| 898 |
</DL> |
| 899 |
</blockquote> |
| 900 |
|
| 901 |
<DL> |
| 902 |
|
| 903 |
<DT></CODE>Description: </DT> |
| 904 |
<DD>Removes the vector <I>vecName</I>. <I>VecName</I> is the name of a vector |
| 905 |
which must already exist. Both the Tcl command and array variable <I>vecName</I> |
| 906 |
are destroyed. All clients of the vector will be notified immediately that |
| 907 |
the vector has been destroyed. </DD> |
| 908 |
|
| 909 |
<DT>Results: </DT> |
| 910 |
<DD>Returns TCL_OK if the vector is |
| 911 |
successfully deleted. If <I>vecName</I> is not the name a vector, then TCL_ERROR |
| 912 |
is returned and <I>interp->result</I> will contain an error message. </DD> |
| 913 |
</DL> |
| 914 |
</blockquote> |
| 915 |
<P> |
| 916 |
<P> |
| 917 |
<B>Blt_DeleteVector</B> |
| 918 |
<blockquote> |
| 919 |
<DL> |
| 920 |
|
| 921 |
<DT>Synopsis: </DT> |
| 922 |
<DD><BR> |
| 923 |
<CODE>int <B>Blt_DeleteVector</B> (<I>vecPtr</I>)<BR> |
| 924 |
<blockquote>Blt_Vector *<I>vecPtr</I>;<BR> |
| 925 |
</DD> |
| 926 |
</DL> |
| 927 |
</blockquote> |
| 928 |
|
| 929 |
<DL> |
| 930 |
|
| 931 |
<DT></CODE>Description: </DT> |
| 932 |
<DD>Removes the vector pointed to by <I>vecPtr</I>. <I>VecPtr</I> is a pointer |
| 933 |
to a vector, typically set by <B>Blt_GetVector</B> or <B>Blt_CreateVector</B>. Both the |
| 934 |
Tcl command and array variable of the vector are destroyed. All clients |
| 935 |
of the vector will be notified immediately that the vector has been destroyed. |
| 936 |
</DD> |
| 937 |
|
| 938 |
<DT>Results: </DT> |
| 939 |
<DD>Returns TCL_OK if the vector is successfully deleted. If <I>vecName</I> |
| 940 |
is not the name a vector, then TCL_ERROR is returned and <I>interp->result</I> |
| 941 |
will contain an error message. </DD> |
| 942 |
</DL> |
| 943 |
</blockquote> |
| 944 |
<P> |
| 945 |
<P> |
| 946 |
<B>Blt_GetVector</B> <blockquote> |
| 947 |
<DL> |
| 948 |
|
| 949 |
<DT>Synopsis: </DT> |
| 950 |
<DD><BR> |
| 951 |
<CODE>int <B>Blt_GetVector</B> (<I>interp</I>, <I>vecName</I>, <I>vecPtrPtr</I>)<BR> |
| 952 |
<blockquote>Tcl_Interp *<I>interp</I>;<BR> |
| 953 |
char *<I>vecName</I>;<BR> |
| 954 |
Blt_Vector **<I>vecPtrPtr</I>;<BR> |
| 955 |
</DD> |
| 956 |
</DL> |
| 957 |
</blockquote> |
| 958 |
|
| 959 |
<DL> |
| 960 |
|
| 961 |
<DT></CODE>Description: </DT> |
| 962 |
<DD>Retrieves the vector <I>vecName</I>. <I>VecName</I> is the name of a vector |
| 963 |
which must already exist. <I>VecPtrPtr</I> will point be set to the address of |
| 964 |
the vector. </DD> |
| 965 |
|
| 966 |
<DT>Results: </DT> |
| 967 |
<DD>Returns TCL_OK if the vector is successfully retrieved. |
| 968 |
If <I>vecName</I> is not the name of a vector, then TCL_ERROR is returned and |
| 969 |
<I>interp->result</I> will contain an error message. </DD> |
| 970 |
</DL> |
| 971 |
</blockquote> |
| 972 |
<P> |
| 973 |
<P> |
| 974 |
<B>Blt_ResetVector</B> <P> |
| 975 |
<blockquote> |
| 976 |
<DL> |
| 977 |
|
| 978 |
<DT>Synopsis: </DT> |
| 979 |
<DD><BR> |
| 980 |
<CODE>int <B>Blt_ResetVector</B> (<I>vecPtr</I>, <I>dataArr</I>, <BR> |
| 981 |
<tt> </tt> <tt> </tt> <I>numValues</I>, <I>arraySize</I>, <I>freeProc</I>)<BR> |
| 982 |
<blockquote>Blt_Vector *<I>vecPtr</I>;<BR> |
| 983 |
double *<I>dataArr</I>;<BR> |
| 984 |
int *<I>numValues</I>;<BR> |
| 985 |
int *<I>arraySize</I>;<BR> |
| 986 |
Tcl_FreeProc *<I>freeProc</I>;<BR> |
| 987 |
</DD> |
| 988 |
</DL> |
| 989 |
</blockquote> |
| 990 |
|
| 991 |
<DL> |
| 992 |
|
| 993 |
<DT></CODE>Description: </DT> |
| 994 |
<DD>Resets the components of the vector pointed to by <I>vecPtr</I>. |
| 995 |
Calling <B>Blt_ResetVector</B> will trigger the vector to dispatch notifications |
| 996 |
to its clients. <I>DataArr</I> is the array of doubles which represents the vector |
| 997 |
data. <I>NumValues</I> is the number of elements in the array. <I>ArraySize</I> is the |
| 998 |
actual size of the array (the array may be bigger than the number of values |
| 999 |
stored in it). <I>FreeProc</I> indicates how the storage for the vector component |
| 1000 |
array (<I>dataArr</I>) was allocated. It is used to determine how to reallocate |
| 1001 |
memory when the vector is resized or destroyed. It must be TCL_DYNAMIC, |
| 1002 |
TCL_STATIC, TCL_VOLATILE, or a pointer to a function to free the memory |
| 1003 |
allocated for the vector array. If <I>freeProc</I> is TCL_VOLATILE, it indicates |
| 1004 |
that <I>dataArr</I> must be copied and saved. If <I>freeProc</I> is TCL_DYNAMIC, it |
| 1005 |
indicates that <I>dataArr</I> was dynamically allocated and that Tcl should free |
| 1006 |
<I>dataArr</I> if necessary. Static indicates that nothing should be done to |
| 1007 |
release storage for <I>dataArr</I>. </DD> |
| 1008 |
|
| 1009 |
<DT>Results: </DT> |
| 1010 |
<DD>Returns TCL_OK if the vector is |
| 1011 |
successfully resized. If <I>newSize</I> is negative, a vector <I>vecName</I> does not |
| 1012 |
exist, or memory cannot be allocated for the vector, then TCL_ERROR is |
| 1013 |
returned and <I>interp->result</I> will contain an error message. </DD> |
| 1014 |
</DL> |
| 1015 |
</blockquote> |
| 1016 |
<P> |
| 1017 |
<P> |
| 1018 |
<B>Blt_ResizeVector</B> |
| 1019 |
<blockquote> |
| 1020 |
<DL> |
| 1021 |
|
| 1022 |
<DT>Synopsis: </DT> |
| 1023 |
<DD><BR> |
| 1024 |
<CODE>int <B>Blt_ResizeVector</B> (<I>vecPtr</I>, <I>newSize</I>)<BR> |
| 1025 |
<blockquote>Blt_Vector *<I>vecPtr</I>;<BR> |
| 1026 |
int <I>newSize</I>;<BR> |
| 1027 |
</DD> |
| 1028 |
</DL> |
| 1029 |
</blockquote> |
| 1030 |
|
| 1031 |
<DL> |
| 1032 |
|
| 1033 |
<DT></CODE>Description: </DT> |
| 1034 |
<DD>Resets the length of the vector pointed to by <I>vecPtr</I> to <I>newSize</I>. |
| 1035 |
If <I>newSize</I> is smaller than the current size of the vector, it is truncated. |
| 1036 |
If <I>newSize</I> is greater, the vector is extended and the new components are |
| 1037 |
initialized to 0.0. Calling <B>Blt_ResetVector</B> will trigger the vector to |
| 1038 |
dispatch notifications. </DD> |
| 1039 |
|
| 1040 |
<DT>Results: </DT> |
| 1041 |
<DD>Returns TCL_OK if the vector is successfully |
| 1042 |
resized. If <I>newSize</I> is negative or memory can not be allocated for the |
| 1043 |
vector, then TCL_ERROR is returned and <I>interp->result</I> will contain an |
| 1044 |
error message. <P> |
| 1045 |
</DD> |
| 1046 |
</DL> |
| 1047 |
<P> |
| 1048 |
<B>Blt_VectorExists</B> <blockquote> |
| 1049 |
<DL> |
| 1050 |
|
| 1051 |
<DT>Synopsis: </DT> |
| 1052 |
<DD><BR> |
| 1053 |
<CODE>int <B>Blt_VectorExists</B> (<I>interp</I>, <I>vecName</I>)<BR> |
| 1054 |
<blockquote>Tcl_Interp *<I>interp</I>;<BR> |
| 1055 |
char *<I>vecName</I>;<BR> |
| 1056 |
</DD> |
| 1057 |
</DL> |
| 1058 |
</blockquote> |
| 1059 |
|
| 1060 |
<DL> |
| 1061 |
|
| 1062 |
<DT></CODE>Description: </DT> |
| 1063 |
<DD>Indicates if a vector named <I>vecName</I> exists in <I>interp</I>. </DD> |
| 1064 |
|
| 1065 |
<DT>Results: |
| 1066 |
</DT> |
| 1067 |
<DD>Returns 1 if a vector <I>vecName</I> exists and 0 otherwise. </DD> |
| 1068 |
</DL> |
| 1069 |
</blockquote> |
| 1070 |
<P> |
| 1071 |
<P> |
| 1072 |
If your application |
| 1073 |
needs to be notified when a vector changes, it can allocate a unique <I>client |
| 1074 |
identifier</I> for itself. Using this identifier, you can then register a call-back |
| 1075 |
to be made whenever the vector is updated or destroyed. By default, the |
| 1076 |
call-backs are made at the next idle point. This can be changed to occur |
| 1077 |
at the time the vector is modified. An application can allocate more than |
| 1078 |
one identifier for any vector. When the client application is done with |
| 1079 |
the vector, it should free the identifier. <P> |
| 1080 |
The call-back routine must of |
| 1081 |
the following type. <BR> |
| 1082 |
<blockquote><BR> |
| 1083 |
<CODE>typedef void (<B>Blt_VectorChangedProc</B>) (Tcl_Interp *<I>interp</I>, <BR> |
| 1084 |
<blockquote>ClientData <I>clientData</I>, Blt_VectorNotify <I>notify</I>);<BR> |
| 1085 |
</blockquote> |
| 1086 |
<BR> |
| 1087 |
</blockquote> |
| 1088 |
</PRE></CODE><I>ClientData</I> is passed to this routine whenever it is called. You can use |
| 1089 |
this to pass information to the call-back. The <I>notify</I> argument indicates |
| 1090 |
whether the vector has been updated of destroyed. It is an enumerated type. |
| 1091 |
<BR> |
| 1092 |
<blockquote><BR> |
| 1093 |
<CODE>typedef enum {<BR> |
| 1094 |
BLT_VECTOR_NOTIFY_UPDATE=1,<BR> |
| 1095 |
BLT_VECTOR_NOTIFY_DESTROY=2<BR> |
| 1096 |
} <B>Blt_VectorNotify</B>;<BR> |
| 1097 |
<BR> |
| 1098 |
</blockquote> |
| 1099 |
<P> |
| 1100 |
</CODE><B>Blt_AllocVectorId</B> <blockquote> |
| 1101 |
<DL> |
| 1102 |
|
| 1103 |
<DT>Synopsis: </DT> |
| 1104 |
<DD><BR> |
| 1105 |
<CODE>Blt_VectorId <B>Blt_AllocVectorId</B> (<I>interp</I>, <I>vecName</I>)<BR> |
| 1106 |
<blockquote>Tcl_Interp *<I>interp</I>;<BR> |
| 1107 |
char *<I>vecName</I>;<BR> |
| 1108 |
</DD> |
| 1109 |
</DL> |
| 1110 |
</blockquote> |
| 1111 |
|
| 1112 |
<DL> |
| 1113 |
|
| 1114 |
<DT></CODE>Description: </DT> |
| 1115 |
<DD>Allocates an client identifier for with the vector <I>vecName</I>. |
| 1116 |
This identifier can be used to specify a call-back which is triggered when |
| 1117 |
the vector is updated or destroyed. </DD> |
| 1118 |
|
| 1119 |
<DT>Results: </DT> |
| 1120 |
<DD>Returns a client identifier |
| 1121 |
if successful. If <I>vecName</I> is not the name of a vector, then NULL is returned |
| 1122 |
and <I>interp->result</I> will contain an error message. </DD> |
| 1123 |
</DL> |
| 1124 |
</blockquote> |
| 1125 |
<P> |
| 1126 |
<P> |
| 1127 |
<B>Blt_GetVectorById</B> <blockquote> |
| 1128 |
<DL> |
| 1129 |
|
| 1130 |
<DT>Synopsis: |
| 1131 |
</DT> |
| 1132 |
<DD><BR> |
| 1133 |
<CODE>int <B>Blt_GetVector</B> (<I>interp</I>, <I>clientId</I>, <I>vecPtrPtr</I>)<BR> |
| 1134 |
<blockquote>Tcl_Interp *<I>interp</I>;<BR> |
| 1135 |
Blt_VectorId <I>clientId</I>;<BR> |
| 1136 |
Blt_Vector **<I>vecPtrPtr</I>;<BR> |
| 1137 |
</DD> |
| 1138 |
</DL> |
| 1139 |
</blockquote> |
| 1140 |
|
| 1141 |
<DL> |
| 1142 |
|
| 1143 |
<DT></CODE>Description: </DT> |
| 1144 |
<DD>Retrieves the vector used by <I>clientId</I>. <I>ClientId</I> is a valid |
| 1145 |
vector client identifier allocated by <B>Blt_AllocVectorId</B>. <I>VecPtrPtr</I> will |
| 1146 |
point be set to the address of the vector. </DD> |
| 1147 |
|
| 1148 |
<DT>Results: </DT> |
| 1149 |
<DD>Returns TCL_OK if |
| 1150 |
the vector is successfully retrieved. </DD> |
| 1151 |
</DL> |
| 1152 |
</blockquote> |
| 1153 |
<P> |
| 1154 |
<P> |
| 1155 |
<B>Blt_SetVectorChangedProc</B> <blockquote> |
| 1156 |
<DL> |
| 1157 |
|
| 1158 |
<DT>Synopsis: |
| 1159 |
</DT> |
| 1160 |
<DD><BR> |
| 1161 |
<CODE>void <B>Blt_SetVectorChangedProc</B> (<I>clientId</I>, <I>proc</I>, <I>clientData</I>);<BR> |
| 1162 |
<blockquote>Blt_VectorId <I>clientId</I>;<BR> |
| 1163 |
Blt_VectorChangedProc *<I>proc</I>;<BR> |
| 1164 |
ClientData *<I>clientData</I>;<BR> |
| 1165 |
</DD> |
| 1166 |
</DL> |
| 1167 |
</blockquote> |
| 1168 |
|
| 1169 |
<DL> |
| 1170 |
|
| 1171 |
<DT></CODE>Description: </DT> |
| 1172 |
<DD>Specifies a call-back routine to be called whenever the vector |
| 1173 |
associated with <I>clientId</I> is updated or deleted. <I>Proc</I> is a pointer to call-back |
| 1174 |
routine and must be of the type <B>Blt_VectorChangedProc</B>. <I>ClientData</I> is a |
| 1175 |
one-word value to be passed to the routine when it is invoked. If <I>proc</I> is |
| 1176 |
NULL, then the client is not notified. </DD> |
| 1177 |
|
| 1178 |
<DT>Results: </DT> |
| 1179 |
<DD>The designated call-back |
| 1180 |
procedure will be invoked when the vector is updated or destroyed. </DD> |
| 1181 |
</DL> |
| 1182 |
</blockquote> |
| 1183 |
<P> |
| 1184 |
<P> |
| 1185 |
<B>Blt_FreeVectorId</B> |
| 1186 |
<blockquote> |
| 1187 |
<DL> |
| 1188 |
|
| 1189 |
<DT>Synopsis: </DT> |
| 1190 |
<DD><BR> |
| 1191 |
<CODE>void <B>Blt_FreeVectorId</B> (<I>clientId</I>);<BR> |
| 1192 |
<blockquote>Blt_VectorId <I>clientId</I>;<BR> |
| 1193 |
</DD> |
| 1194 |
</DL> |
| 1195 |
</blockquote> |
| 1196 |
|
| 1197 |
<DL> |
| 1198 |
|
| 1199 |
<DT></CODE>Description: </DT> |
| 1200 |
<DD>Frees the client identifier. Memory allocated for the identifier |
| 1201 |
is released. The client will no longer be notified when the vector is |
| 1202 |
modified. </DD> |
| 1203 |
|
| 1204 |
<DT>Results: </DT> |
| 1205 |
<DD>The designated call-back procedure will be no longer be |
| 1206 |
invoked when the vector is updated or destroyed. </DD> |
| 1207 |
</DL> |
| 1208 |
</blockquote> |
| 1209 |
<P> |
| 1210 |
<P> |
| 1211 |
<B>Blt_NameOfVectorId</B> <blockquote> |
| 1212 |
<DL> |
| 1213 |
|
| 1214 |
<DT>Synopsis: |
| 1215 |
</DT> |
| 1216 |
<DD><BR> |
| 1217 |
<CODE>char *<B>Blt_NameOfVectorId</B> (<I>clientId</I>);<BR> |
| 1218 |
<blockquote>Blt_VectorId <I>clientId</I>;<BR> |
| 1219 |
</DD> |
| 1220 |
</DL> |
| 1221 |
</blockquote> |
| 1222 |
|
| 1223 |
<DL> |
| 1224 |
|
| 1225 |
<DT></CODE>Description: </DT> |
| 1226 |
<DD>Retrieves the name of the vector associated with the client |
| 1227 |
identifier <I>clientId</I>. </DD> |
| 1228 |
|
| 1229 |
<DT>Results: </DT> |
| 1230 |
<DD>Returns the name of the vector associated |
| 1231 |
with <I>clientId</I>. If <I>clientId</I> is not an identifier or the vector has been |
| 1232 |
destroyed, NULL is returned. </DD> |
| 1233 |
</DL> |
| 1234 |
</blockquote> |
| 1235 |
<P> |
| 1236 |
<P> |
| 1237 |
<B>Blt_InstallIndexProc</B> <blockquote> |
| 1238 |
<DL> |
| 1239 |
|
| 1240 |
<DT>Synopsis: </DT> |
| 1241 |
<DD><BR> |
| 1242 |
<CODE>void <B>Blt_InstallIndexProc</B> (<I>indexName</I>, <I>procPtr</I>)<BR> |
| 1243 |
<blockquote>char *<I>indexName</I>;<BR> |
| 1244 |
Blt_VectorIndexProc *<I>procPtr</I>;<BR> |
| 1245 |
</DD> |
| 1246 |
</DL> |
| 1247 |
</blockquote> |
| 1248 |
|
| 1249 |
<DL> |
| 1250 |
|
| 1251 |
<DT></CODE>Description: </DT> |
| 1252 |
<DD>Registers a function to be called to retrieved the index |
| 1253 |
<I>indexName</I> from the vector's array variable. <P> |
| 1254 |
typedef double Blt_VectorIndexProc(Vector |
| 1255 |
*vecPtr); <P> |
| 1256 |
The function will be passed a pointer to the vector. The function |
| 1257 |
must return a double representing the value at the index. </DD> |
| 1258 |
|
| 1259 |
<DT>Results: </DT> |
| 1260 |
<DD>The new |
| 1261 |
index is installed into the vector. </DD> |
| 1262 |
</DL> |
| 1263 |
</blockquote> |
| 1264 |
</blockquote> |
| 1265 |
|
| 1266 |
<H2><A NAME="sect11" HREF="#toc11">C API Example</A></H2> |
| 1267 |
The following example opens |
| 1268 |
a file of binary data and stores it in an array of doubles. The array size |
| 1269 |
is computed from the size of the file. If the vector "data" exists, calling |
| 1270 |
<B>Blt_VectorExists</B>, <B>Blt_GetVector</B> is called to get the pointer to the vector. |
| 1271 |
Otherwise the routine <B>Blt_CreateVector</B> is called to create a new vector |
| 1272 |
and returns a pointer to it. Just like the Tcl interface, both a new Tcl |
| 1273 |
command and array variable are created when a new vector is created. It |
| 1274 |
doesn't make any difference what the initial size of the vector is since |
| 1275 |
it will be reset shortly. The vector is updated when <B>lt_ResetVector</B> is called. |
| 1276 |
Blt_ResetVector makes the changes visible to the Tcl interface and other |
| 1277 |
vector clients (such as a graph widget). <P> |
| 1278 |
<BR> |
| 1279 |
<CODE>#include <tcl.h><BR> |
| 1280 |
#include <blt.h><tt> </tt> <tt> </tt> <tt> </tt> <tt> </tt> <tt> </tt> <tt> </tt> <tt> </tt> <tt> </tt> <BR> |
| 1281 |
Blt_Vector *vecPtr;<BR> |
| 1282 |
double *newArr;<BR> |
| 1283 |
FILE *f;<BR> |
| 1284 |
struct stat statBuf;<BR> |
| 1285 |
int numBytes, numValues;<BR> |
| 1286 |
<P> |
| 1287 |
f = fopen("binary.dat", "r");<BR> |
| 1288 |
fstat(fileno(f), &statBuf);<BR> |
| 1289 |
numBytes = (int)statBuf.st_size;<BR> |
| 1290 |
<P> |
| 1291 |
/* Allocate an array big enough to hold all the data */<BR> |
| 1292 |
newArr = (double *)m<A HREF="alloc.numBytes">alloc(numBytes)</A> |
| 1293 |
;<BR> |
| 1294 |
numValues = numBytes / sizeof(double);<BR> |
| 1295 |
fread((void *)newArr, numValues, sizeof(double), f);<BR> |
| 1296 |
fclose(f);<BR> |
| 1297 |
<P> |
| 1298 |
if (Blt_VectorExists(interp, "data")) {<BR> |
| 1299 |
if (Blt_GetVector(interp, "data", &vecPtr) != TCL_OK) {<BR> |
| 1300 |
<tt> </tt> <tt> </tt> return TCL_ERROR;<BR> |
| 1301 |
}<BR> |
| 1302 |
} else {<BR> |
| 1303 |
if (Blt_CreateVector(interp, "data", 0, &vecPtr) != TCL_OK) {<BR> |
| 1304 |
<tt> </tt> <tt> </tt> return TCL_ERROR;<BR> |
| 1305 |
}<BR> |
| 1306 |
}<BR> |
| 1307 |
/* <BR> |
| 1308 |
* Reset the vector. Clients will be notified when Tk is idle. <BR> |
| 1309 |
* TCL_DYNAMIC tells the vector to free the memory allocated <BR> |
| 1310 |
* if it needs to reallocate or destroy the vector.<BR> |
| 1311 |
*/<BR> |
| 1312 |
if (Blt_ResetVector(vecPtr, newArr, numValues, numValues, <BR> |
| 1313 |
<tt> </tt> <tt> </tt> TCL_DYNAMIC) != TCL_OK) {<BR> |
| 1314 |
return TCL_ERROR;<BR> |
| 1315 |
}<BR> |
| 1316 |
|
| 1317 |
<H2><A NAME="sect12" HREF="#toc12"></CODE>Incompatibilities</A></H2> |
| 1318 |
In previous versions, if the array variable isn't global |
| 1319 |
(i.e. local to a Tcl procedure), the vector is automatically destroyed |
| 1320 |
when the procedure returns. <BR> |
| 1321 |
<CODE>proc doit {} {<BR> |
| 1322 |
# Temporary vector x<BR> |
| 1323 |
vector x(10)<BR> |
| 1324 |
set <A HREF="x.9">x(9)</A> |
| 1325 |
2.0<BR> |
| 1326 |
...<BR> |
| 1327 |
}<BR> |
| 1328 |
<P> |
| 1329 |
</CODE>This has changed. Variables are not automatically destroyed when their |
| 1330 |
variable is unset. You can restore the old behavior by setting the "-watchunset" |
| 1331 |
switch. |
| 1332 |
<H2><A NAME="sect13" HREF="#toc13"></CODE>Keywords</A></H2> |
| 1333 |
vector, graph, widget <P> |
| 1334 |
|
| 1335 |
<HR><P> |
| 1336 |
<A NAME="toc"><B>Table of Contents</B></A><P> |
| 1337 |
<UL> |
| 1338 |
<LI><A NAME="toc0" HREF="#sect0">Name</A></LI> |
| 1339 |
<LI><A NAME="toc1" HREF="#sect1">Synopsis</A></LI> |
| 1340 |
<LI><A NAME="toc2" HREF="#sect2">Description</A></LI> |
| 1341 |
<LI><A NAME="toc3" HREF="#sect3">Introduction</A></LI> |
| 1342 |
<LI><A NAME="toc4" HREF="#sect4">Example</A></LI> |
| 1343 |
<LI><A NAME="toc5" HREF="#sect5">Syntax</A></LI> |
| 1344 |
<UL> |
| 1345 |
<LI><A NAME="toc6" HREF="#sect6">Vector Indices</A></LI> |
| 1346 |
</UL> |
| 1347 |
<LI><A NAME="toc7" HREF="#sect7">Vector Operations</A></LI> |
| 1348 |
<LI><A NAME="toc8" HREF="#sect8">Instance Operations</A></LI> |
| 1349 |
<LI><A NAME="toc9" HREF="#sect9">C Language API</A></LI> |
| 1350 |
<LI><A NAME="toc10" HREF="#sect10">Library Routines</A></LI> |
| 1351 |
<LI><A NAME="toc11" HREF="#sect11">C API Example</A></LI> |
| 1352 |
<LI><A NAME="toc12" HREF="#sect12">Incompatibilities</A></LI> |
| 1353 |
<LI><A NAME="toc13" HREF="#sect13">Keywords</A></LI> |
| 1354 |
</UL> |
| 1355 |
</BODY></HTML> |