2015-12-28 20:24:35 +01:00
|
|
|
#include "ngspice/ngspice.h"
|
|
|
|
|
#include "ngspice/dvec.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct dvec *
|
2015-12-28 20:26:58 +01:00
|
|
|
dvec_alloc(char *name, int type, short flags, int length, void *storage)
|
2015-12-28 20:24:35 +01:00
|
|
|
{
|
|
|
|
|
struct dvec *rv = TMALLOC(struct dvec, 1);
|
|
|
|
|
|
2019-05-01 16:35:59 +02:00
|
|
|
/* If the allocation failed, return NULL as a failure flag.
|
|
|
|
|
* As of 2019-03, TMALLOC will not return on failure, so this check is
|
|
|
|
|
* redundant, but it may be useful if it is decided to allow the
|
|
|
|
|
* allocation functions to return NULL on failure and handle recovery
|
|
|
|
|
* by the calling functions */
|
|
|
|
|
if (!rv) {
|
2015-12-28 20:25:22 +01:00
|
|
|
return NULL;
|
2019-05-01 16:35:59 +02:00
|
|
|
}
|
2015-12-28 20:25:22 +01:00
|
|
|
|
2019-05-01 16:35:59 +02:00
|
|
|
/* Set all fields to 0 */
|
2015-12-28 20:25:22 +01:00
|
|
|
ZERO(rv, struct dvec);
|
|
|
|
|
|
2019-05-01 16:35:59 +02:00
|
|
|
/* Set information on the vector from parameters. Note that storage for
|
|
|
|
|
* the name string belongs to the dvec when this function returns. */
|
2015-12-28 20:26:58 +01:00
|
|
|
rv->v_name = name;
|
|
|
|
|
rv->v_type = type;
|
|
|
|
|
rv->v_flags = flags;
|
|
|
|
|
rv->v_length = length;
|
2016-01-07 08:54:14 +01:00
|
|
|
rv->v_alloc_length = length;
|
2015-12-28 20:26:58 +01:00
|
|
|
|
2019-05-01 16:35:59 +02:00
|
|
|
if (length == 0) { /* Redundant due to ZERO() call above */
|
2015-12-28 20:26:58 +01:00
|
|
|
rv->v_realdata = NULL;
|
|
|
|
|
rv->v_compdata = NULL;
|
2019-05-01 16:35:59 +02:00
|
|
|
}
|
|
|
|
|
else if (flags & VF_REAL) {
|
|
|
|
|
/* Vector consists of real data. Use the supplied storage if given
|
|
|
|
|
* or allocate if not */
|
2015-12-28 20:26:58 +01:00
|
|
|
rv->v_realdata = storage
|
|
|
|
|
? (double *) storage
|
|
|
|
|
: TMALLOC(double, length);
|
|
|
|
|
rv->v_compdata = NULL;
|
2019-05-01 16:35:59 +02:00
|
|
|
}
|
|
|
|
|
else if (flags & VF_COMPLEX) {
|
|
|
|
|
/* Vector holds complex data. Perform actions as for real data */
|
2015-12-28 20:26:58 +01:00
|
|
|
rv->v_realdata = NULL;
|
|
|
|
|
rv->v_compdata = storage
|
|
|
|
|
? (ngcomplex_t *) storage
|
|
|
|
|
: TMALLOC(ngcomplex_t, length);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 16:35:59 +02:00
|
|
|
/* Set remaining fields to none/unknown. Again not required due to
|
|
|
|
|
* the ZERO() call */
|
2015-12-28 20:26:11 +01:00
|
|
|
rv->v_plot = NULL;
|
2015-12-28 20:26:34 +01:00
|
|
|
rv->v_scale = NULL;
|
2019-05-01 16:35:59 +02:00
|
|
|
rv->v_numdims = 0; /* Really "unknown" */
|
2015-12-28 20:25:44 +01:00
|
|
|
|
2015-12-28 20:24:35 +01:00
|
|
|
return rv;
|
|
|
|
|
}
|
2015-12-28 20:28:28 +01:00
|
|
|
|
|
|
|
|
|
2015-12-28 20:27:52 +01:00
|
|
|
void
|
|
|
|
|
dvec_realloc(struct dvec *v, int length, void *storage)
|
|
|
|
|
{
|
|
|
|
|
if (isreal(v)) {
|
|
|
|
|
if (storage) {
|
|
|
|
|
tfree(v->v_realdata);
|
|
|
|
|
v->v_realdata = (double *) storage;
|
|
|
|
|
} else {
|
|
|
|
|
v->v_realdata = TREALLOC(double, v->v_realdata, length);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (storage) {
|
|
|
|
|
tfree(v->v_compdata);
|
|
|
|
|
v->v_compdata = (ngcomplex_t *) storage;
|
|
|
|
|
} else {
|
|
|
|
|
v->v_compdata = TREALLOC(ngcomplex_t, v->v_compdata, length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v->v_length = length;
|
2016-01-07 08:54:14 +01:00
|
|
|
v->v_alloc_length = length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
dvec_extend(struct dvec *v, int length)
|
|
|
|
|
{
|
|
|
|
|
if (isreal(v))
|
|
|
|
|
v->v_realdata = TREALLOC(double, v->v_realdata, length);
|
|
|
|
|
else
|
|
|
|
|
v->v_compdata = TREALLOC(ngcomplex_t, v->v_compdata, length);
|
|
|
|
|
|
|
|
|
|
v->v_alloc_length = length;
|
2015-12-28 20:27:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-12-28 20:28:39 +01:00
|
|
|
void
|
|
|
|
|
dvec_trunc(struct dvec *v, int length)
|
|
|
|
|
{
|
|
|
|
|
v->v_length = length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-12-28 20:28:28 +01:00
|
|
|
void
|
|
|
|
|
dvec_free(struct dvec *v)
|
|
|
|
|
{
|
|
|
|
|
if (v->v_name)
|
|
|
|
|
tfree(v->v_name);
|
|
|
|
|
if (v->v_realdata)
|
|
|
|
|
tfree(v->v_realdata);
|
|
|
|
|
if (v->v_compdata)
|
|
|
|
|
tfree(v->v_compdata);
|
|
|
|
|
tfree(v);
|
|
|
|
|
}
|