'\"
'\" Copyright 1991-1997 by Lucent Technologies, Inc.
'\"
'\" Permission to use, copy, modify, and distribute this software and its
'\" documentation for any purpose and without fee is hereby granted, provided
'\" that the above copyright notice appear in all copies and that both that the
'\" copyright notice and warranty disclaimer appear in supporting documentation,
'\" and that the names of Lucent Technologies any of their entities not be used
'\" in advertising or publicity pertaining to distribution of the software
'\" without specific, written prior permission.
'\"
'\" Lucent Technologies disclaims all warranties with regard to this software,
'\" including all implied warranties of merchantability and fitness.  In no event
'\" shall Lucent Technologies be liable for any special, indirect or
'\" consequential damages or any damages whatsoever resulting from loss of use,
'\" data or profits, whether in an action of contract, negligence or other
'\" tortuous action, arising out of or in connection with the use or performance
'\" of this software.  
'\"
'\" Vector command created by George Howlett.
'\"
.so man.macros
.TH Blt_Vector 3 BLT_VERSION BLT "Blt Library Procedures"
.BS
'\" Note:  do not modify the .SH NAME line immediately below!
.SH NAME
Blt_Vector \- Vector data object.

.SH C LANGUAGE API
You can create, modify, and destroy vectors from C code, using 
library routines.  
You need to include the header file \fBblt.h\fR. It contains the
definition of the structure \fBBlt_Vector\fR, which represents the
vector.  It appears below.
.sp
.CS
\fRtypedef struct {
    double *\fIvalueArr\fR; 
    int \fInumValues\fR;    
    int \fIarraySize\fR;    
    double \fImin\fR, \fImax\fR;  
} \fBBlt_Vector\fR;
.CE
.sp
The field \fIvalueArr\fR points to memory holding the vector
components.  The components are stored in a double precision array,
whose size size is represented by \fIarraySize\fR.  \fINumValues\fR is
the length of vector.  The size of the array is always equal to or
larger than the length of the vector.  \fIMin\fR and \fImax\fR are
minimum and maximum component values.
.SH LIBRARY ROUTINES
The following routines are available from C to manage vectors.
Vectors are identified by the vector name.
.PP
\fBBlt_CreateVector\fR 
.RS .25i
.TP 1i
Synopsis:
.CS 
int \fBBlt_CreateVector\fR (\fIinterp\fR, \fIvecName\fR, \fIlength\fR, \fIvecPtrPtr\fR)
.RS 1.25i
Tcl_Interp *\fIinterp\fR;
char *\fIvecName\fR;
int \fIlength\fR;
Blt_Vector **\fIvecPtrPtr\fR;
.RE
.CE
.TP
Description:
Creates a new vector \fIvecName\fR\fR with a length of \fIlength\fR.
\fBBlt_CreateVector\fR creates both a new Tcl command and array 
variable \fIvecName\fR.  Neither a command nor variable named 
\fIvecName\fR can already exist.  A pointer to the vector is 
placed into \fIvecPtrPtr\fR.
.TP
Results:
Returns \fBTCL_OK\fR if the vector is successfully created.  If
\fIlength\fR is negative, a Tcl variable or command \fIvecName\fR
already exists, or memory cannot be allocated for the vector, then
\fBTCL_ERROR\fR is returned and \fIinterp->result\fR will contain an
error message.
.RE
.sp
.PP
\fBBlt_DeleteVectorByName\fR 
.RS .25i
.TP 1i
Synopsis:
.CS
int \fBBlt_DeleteVectorByName\fR (\fIinterp\fR, \fIvecName\fR)
.RS 1.25i
Tcl_Interp *\fIinterp\fR;
char *\fIvecName\fR;
.RE
.CE
.TP 1i
Description:
Removes the vector \fIvecName\fR.  \fIVecName\fR is the name of a vector
which must already exist.  Both the Tcl command and array variable
\fIvecName\fR are destroyed.  All clients of the vector will be notified
immediately that the vector has been destroyed.
.TP
Results:
Returns \fBTCL_OK\fR if the vector is successfully deleted.  If
\fIvecName\fR is not the name a vector, then \fBTCL_ERROR\fR is returned
and \fIinterp->result\fR will contain an error message.
.RE
.sp
.PP
\fBBlt_DeleteVector\fR 
.RS .25i
.TP 1i
Synopsis:
.CS
int \fBBlt_DeleteVector\fR (\fIvecPtr\fR)
.RS 1.25i
Blt_Vector *\fIvecPtr\fR;
.RE
.CE
.TP 1i
Description:
Removes the vector pointed to by \fIvecPtr\fR.  \fIVecPtr\fR is a
pointer to a vector, typically set by \fBBlt_GetVector\fR or
\fBBlt_CreateVector\fR.  Both the Tcl command and array variable of
the vector are destroyed.  All clients of the vector will be notified
immediately that the vector has been destroyed.
.TP
Results:
Returns \fBTCL_OK\fR if the vector is successfully deleted.  If
\fIvecName\fR is not the name a vector, then \fBTCL_ERROR\fR is returned
and \fIinterp->result\fR will contain an error message.
.RE
.sp
.PP
\fBBlt_GetVector\fR 
.RS .25i
.TP 1i
Synopsis:
.CS
int \fBBlt_GetVector\fR (\fIinterp\fR, \fIvecName\fR, \fIvecPtrPtr\fR)
.RS 1.25i
Tcl_Interp *\fIinterp\fR;
char *\fIvecName\fR;
Blt_Vector **\fIvecPtrPtr\fR;
.RE
.CE
.TP 1i
Description:
Retrieves the vector \fIvecName\fR.  \fIVecName\fR is the name of a
vector which must already exist.  \fIVecPtrPtr\fR will point be set to
the address of the vector.
.TP
Results:
Returns \fBTCL_OK\fR if the vector is successfully retrieved.  If
\fIvecName\fR is not the name of a vector, then \fBTCL_ERROR\fR is
returned and \fIinterp->result\fR will contain an error message.
.RE
.sp
.PP
\fBBlt_ResetVector\fR 
.PP
.RS .25i
.TP 1i
Synopsis:
.CS
int \fBBlt_ResetVector\fR (\fIvecPtr\fR, \fIdataArr\fR, 
	\fInumValues\fR, \fIarraySize\fR, \fIfreeProc\fR)
.RS 1.25i
Blt_Vector *\fIvecPtr\fR;
double *\fIdataArr\fR;
int *\fInumValues\fR;
int *\fIarraySize\fR;
Tcl_FreeProc *\fIfreeProc\fR;
.RE
.CE
.TP
Description: 
Resets the components of the vector pointed to by \fIvecPtr\fR.
Calling \fBBlt_ResetVector\fR will trigger the vector to dispatch
notifications to its clients. \fIDataArr\fR is the array of doubles
which represents the vector data. \fINumValues\fR is the number of
elements in the array. \fIArraySize\fR is the actual size of the array
(the array may be bigger than the number of values stored in
it). \fIFreeProc\fP indicates how the storage for the vector component
array (\fIdataArr\fR) was allocated.  It is used to determine how to
reallocate memory when the vector is resized or destroyed.  It must be
\fBTCL_DYNAMIC\fR, \fBTCL_STATIC\fR, \fBTCL_VOLATILE\fR, or a pointer
to a function to free the memory allocated for the vector array. If
\fIfreeProc\fR is \fBTCL_VOLATILE\fR, it indicates that \fIdataArr\fR
must be copied and saved.  If \fIfreeProc\fR is \fBTCL_DYNAMIC\fR, it
indicates that \fIdataArr\fR was dynamically allocated and that Tcl
should free \fIdataArr\fR if necessary.  \fBStatic\fR indicates that
nothing should be done to release storage for \fIdataArr\fR.
.TP
Results:
Returns \fBTCL_OK\fR if the vector is successfully resized.  If
\fInewSize\fR is negative, a vector \fIvecName\fR does not exist, or
memory cannot be allocated for the vector, then \fBTCL_ERROR\fR is
returned and \fIinterp->result\fR will contain an error message.
.RE
.sp
.PP
\fBBlt_ResizeVector\fR 
.RS .25i
.TP 1i
Synopsis:
.CS
int \fBBlt_ResizeVector\fR (\fIvecPtr\fR, \fInewSize\fR)
.RS 1.25i
Blt_Vector *\fIvecPtr\fR;
int \fInewSize\fR;
.RE
.CE
.TP
Description:
Resets the length of the vector pointed to by \fIvecPtr\fR to
\fInewSize\fR.  If \fInewSize\fR is smaller than the current size of
the vector, it is truncated.  If \fInewSize\fR is greater, the vector
is extended and the new components are initialized to \fB0.0\fR.
Calling \fBBlt_ResetVector\fR will trigger the vector to dispatch
notifications.
.TP
Results:
Returns \fBTCL_OK\fR if the vector is successfully resized.  If
\fInewSize\fR is negative or memory can not be allocated for the vector, 
then \fBTCL_ERROR\fR is returned and \fIinterp->result\fR will contain 
an error message.
.sp
.PP
\fBBlt_VectorExists\fR 
.RS .25i
.TP 1i
Synopsis:
.CS
int \fBBlt_VectorExists\fR (\fIinterp\fR, \fIvecName\fR)
.RS 1.25i
Tcl_Interp *\fIinterp\fR;
char *\fIvecName\fR;
.RE
.CE
.TP
Description:
Indicates if a vector named \fIvecName\fR exists in \fIinterp\fR.
.TP
Results:
Returns \fB1\fR if a vector \fIvecName\fR exists and \fB0\fR otherwise.
.RE
.sp
.PP
If your application needs to be notified when a vector changes, it can
allocate a unique \fIclient identifier\fR for itself.  Using this
identifier, you can then register a call-back to be made whenever the
vector is updated or destroyed.  By default, the call-backs are made at
the next idle point.  This can be changed to occur at the time the
vector is modified.  An application can allocate more than one
identifier for any vector.  When the client application is done with
the vector, it should free the identifier.
.PP
The call-back routine must of the following type.
.CS
.RS
.sp
typedef void (\fBBlt_VectorChangedProc\fR) (Tcl_Interp *\fIinterp\fR, 
.RS .25i
ClientData \fIclientData\fR, Blt_VectorNotify \fInotify\fR);
.RE
.sp
.RE
.CE
.fi
\fIClientData\fR is passed to this routine whenever it is called.  You
can use this to pass information to the call-back.  The \fInotify\fR 
argument indicates whether the vector has been updated of destroyed. It
is an enumerated type.
.CS
.RS
.sp
typedef enum {
    \fBBLT_VECTOR_NOTIFY_UPDATE\fR=1,
    \fBBLT_VECTOR_NOTIFY_DESTROY\fR=2
} \fBBlt_VectorNotify\fR;
.sp
.RE
.CE
.PP
\fBBlt_AllocVectorId\fR
.RS .25i
.TP 1i
Synopsis:
.CS
Blt_VectorId \fBBlt_AllocVectorId\fR (\fIinterp\fR, \fIvecName\fR)
.RS 1.25i
Tcl_Interp *\fIinterp\fR;
char *\fIvecName\fR;
.RE
.CE
.TP
Description:
Allocates an client identifier for with the vector \fIvecName\fR.
This identifier can be used to specify a call-back which is triggered
when the vector is updated or destroyed.
.TP
Results:
Returns a client identifier if successful.  If \fIvecName\fR is not
the name of a vector, then \fBNULL\fR is returned and
\fIinterp->result\fR will contain an error message.
.RE
.sp
.PP
\fBBlt_GetVectorById\fR 
.RS .25i
.TP 1i
Synopsis:
.CS
int \fBBlt_GetVector\fR (\fIinterp\fR, \fIclientId\fR, \fIvecPtrPtr\fR)
.RS 1.25i
Tcl_Interp *\fIinterp\fR;
Blt_VectorId \fIclientId\fR;
Blt_Vector **\fIvecPtrPtr\fR;
.RE
.CE
.TP 1i
Description:
Retrieves the vector used by \fIclientId\fR.  \fIClientId\fR is a valid
vector client identifier allocated by \fBBlt_AllocVectorId\fR.
\fIVecPtrPtr\fR will point be set to the address of the vector.
.TP
Results:
Returns \fBTCL_OK\fR if the vector is successfully retrieved.  
.RE
.sp
.PP
\fBBlt_SetVectorChangedProc\fR
.RS .25i
.TP 1i
Synopsis:
.CS
void \fBBlt_SetVectorChangedProc\fR (\fIclientId\fR, \fIproc\fR, \fIclientData\fR);
.RS 1.25i
Blt_VectorId \fIclientId\fR;
Blt_VectorChangedProc *\fIproc\fR;
ClientData *\fIclientData\fR;
.RE
.CE
.TP
Description: 
Specifies a call-back routine to be called whenever the vector
associated with \fIclientId\fR is updated or deleted.  \fIProc\fR is a
pointer to call-back routine and must be of the type
\fBBlt_VectorChangedProc\fR.  \fIClientData\fR is a one-word value to
be passed to the routine when it is invoked. If \fIproc\fR is
\fBNULL\fR, then the client is not notified.
.TP
Results:
The designated call-back procedure will be invoked when the vector is 
updated or destroyed.
.RE
.sp
.PP
\fBBlt_FreeVectorId\fR
.RS .25i
.TP 1i
Synopsis:
.CS
void \fBBlt_FreeVectorId\fR (\fIclientId\fR);
.RS 1.25i
Blt_VectorId \fIclientId\fR;
.RE
.CE
.TP
Description: 
Frees the client identifier.  Memory allocated for the identifier 
is released.  The client will no longer be notified when the
vector is modified.
.TP
Results:
The designated call-back procedure will be no longer be invoked when
the vector is updated or destroyed.
.RE
.sp
.PP
\fBBlt_NameOfVectorId\fR
.RS .25i
.TP 1i
Synopsis:
.CS
char *\fBBlt_NameOfVectorId\fR (\fIclientId\fR);
.RS 1.25i
Blt_VectorId \fIclientId\fR;
.RE
.CE
.TP
Description: 
Retrieves the name of the vector associated with the client identifier
\fIclientId\fR.  
.TP
Results:
Returns the name of the vector associated with \fIclientId\fR.  If
\fIclientId\fR is not an identifier or the vector has been destroyed, 
\fBNULL\fR is returned.
.RE
.sp
.PP
\fBBlt_InstallIndexProc\fR
.RS .25i
.TP 1i
Synopsis:
.CS
void \fBBlt_InstallIndexProc\fR (\fIindexName\fR, \fIprocPtr\fR)
.RS 1.25i
char *\fIindexName\fR;
Blt_VectorIndexProc *\fIprocPtr\fR;
.RE
.CE
.TP
Description: 
Registers a function to be called to retrieved the index \fIindexName\fR
from the vector's array variable.  
.sp
typedef double Blt_VectorIndexProc(Vector *vecPtr);
.sp
The function will be passed a pointer to the vector.  The function must
return a double representing the value at the index.
.TP
Results:
The new index is installed into the vector.
.RE
.RE
.SH C API EXAMPLE
The following example opens a file of binary data and stores it in an
array of doubles. The array size is computed from the size of the
file. If the vector "data" exists, calling \fBBlt_VectorExists\fR,
\fBBlt_GetVector\fR is called to get the pointer to the vector.
Otherwise the routine \fBBlt_CreateVector\fR is called to create a new
vector and returns a pointer to it. Just like the Tcl interface, both
a new Tcl command and array variable are created when a new vector is
created. It doesn't make any difference what the initial size of the
vector is since it will be reset shortly. The vector is updated when
\fBlt_ResetVector\fR is called.  Blt_ResetVector makes the changes
visible to the Tcl interface and other vector clients (such as a graph
widget).
.sp
.CS
#include <tcl.h>
#include <blt.h>				
...
Blt_Vector *vecPtr;
double *newArr;
FILE *f;
struct stat statBuf;
int numBytes, numValues;

f = fopen("binary.dat", "r");
fstat(fileno(f), &statBuf);
numBytes = (int)statBuf.st_size;

/* Allocate an array big enough to hold all the data */
newArr = (double *)malloc(numBytes);
numValues = numBytes / sizeof(double);
fread((void *)newArr, numValues, sizeof(double), f);
fclose(f);

if (Blt_VectorExists(interp, "data"))  {
    if (Blt_GetVector(interp, "data", &vecPtr) != TCL_OK) {
	return TCL_ERROR;
    }
} else {
   if (Blt_CreateVector(interp, "data", 0, &vecPtr) != TCL_OK) {
	return TCL_ERROR;
   }
}
/* 
 * Reset the vector. Clients will be notified when Tk is idle. 
 * TCL_DYNAMIC tells the vector to free the memory allocated 
 * if it needs to reallocate or destroy the vector.
 */
if (Blt_ResetVector(vecPtr, newArr, numValues, numValues, 
	TCL_DYNAMIC) != TCL_OK) {
    return TCL_ERROR;
}
.CE
.SH "INCOMPATIBILITIES"
In previous versions, if the array variable isn't global 
(i.e. local to a Tcl procedure), the vector is automatically 
destroyed when the procedure returns.
.CS
proc doit {} {
    # Temporary vector x
    vector x(10)
    set x(9) 2.0
      ...
}
.CE
.PP
This has changed.  Variables are not automatically destroyed when
their variable is unset.  You can restore the old behavior by
setting the "-watchunset" switch.
.CE
.SH KEYWORDS
vector, graph, Blt_Vector
