25#include "match_string.h"
38 return *((
long double *)data + index);
49 return *((
double *)data + index);
60 return *((
float *)data + index);
71 return *((int64_t *)data + index);
82 return *((uint64_t *)data + index);
93 return *((int32_t *)data + index);
104 return *((uint32_t *)data + index);
115 return *((
short *)data + index);
126 return *((
unsigned short *)data + index);
137 return atof(*((
char **)data + index));
148 return *((
char *)data + index);
161 SDDS_SetError(
"NULL data pointer passed (SDDS_ConvertToLongDouble)");
166 return ((
long double)*((
short *)data + index));
168 return ((
long double)*((
unsigned short *)data + index));
170 return ((
long double)*((int32_t *)data + index));
172 return ((
long double)*((uint32_t *)data + index));
174 return ((
long double)*((int64_t *)data + index));
176 return ((
long double)*((uint64_t *)data + index));
178 return ((
long double)*((
float *)data + index));
180 return ((
long double)*((
double *)data + index));
182 return (*((
long double *)data + index));
184 return ((
long double)*((
unsigned char *)data + index));
186 SDDS_SetError(
"Invalid data type seen (SDDS_ConvertToLongDouble)");
201 SDDS_SetError(
"NULL data pointer passed (SDDS_ConvertToDouble)");
206 return ((
double)*((
short *)data + index));
208 return ((
double)*((
unsigned short *)data + index));
210 return ((
double)*((int32_t *)data + index));
212 return ((
double)*((uint32_t *)data + index));
214 return ((
double)*((int64_t *)data + index));
216 return ((
double)*((uint64_t *)data + index));
218 return ((
double)*((
float *)data + index));
220 return (*((
double *)data + index));
222 return ((
double)*((
long double *)data + index));
224 return ((
double)*((
unsigned char *)data + index));
226 SDDS_SetError(
"Invalid data type seen (SDDS_ConvertToDouble)");
241 SDDS_SetError(
"NULL data pointer passed (SDDS_ConvertToLong64)");
246 return ((int64_t) * ((
long double *)data + index));
248 return ((int64_t) * ((
double *)data + index));
250 return ((int64_t) * ((
float *)data + index));
252 return ((int64_t) * ((
short *)data + index));
254 return ((int64_t) * ((
unsigned short *)data + index));
256 return ((int64_t) * ((int32_t *)data + index));
258 return ((int64_t) * ((uint32_t *)data + index));
260 return (*((int64_t *)data + index));
262 return ((int64_t) * ((uint64_t *)data + index));
264 return ((int64_t) * ((
unsigned char *)data + index));
266 SDDS_SetError(
"Invalid data type seen (SDDS_ConvertToLong64)");
281 SDDS_SetError(
"NULL data pointer passed (SDDS_ConvertToLong)");
286 return ((int32_t) * ((
long double *)data + index));
288 return ((int32_t) * ((
double *)data + index));
290 return ((int32_t) * ((
float *)data + index));
292 return ((int32_t) * ((
short *)data + index));
294 return ((int32_t) * ((
unsigned short *)data + index));
296 return (*((int32_t *)data + index));
298 return ((int32_t) * ((uint32_t *)data + index));
300 return ((int32_t) * ((int64_t *)data + index));
302 return ((int32_t) * ((uint64_t *)data + index));
304 return ((int32_t) * ((
unsigned char *)data + index));
306 SDDS_SetError(
"Invalid data type seen (SDDS_ConvertToLong)");
311#if defined(RPN_SUPPORT)
313static double (*
const SDDS_ConvertTypeToDouble[
SDDS_NUM_TYPES + 1])(
void *data, int64_t index) =
329static MDB_THREAD_LOCAL int64_t table_number_mem = -1;
330static MDB_THREAD_LOCAL int64_t i_page_mem = -1;
331static MDB_THREAD_LOCAL int64_t n_rows_mem = -1;
332static MDB_THREAD_LOCAL int64_t i_row_mem = -1;
344 return (rpn_create_mem((
char *)name, is_string));
362 if ((memnum = is_memory(&dummy, &dummy1, &is_string, name)) >= 0) {
366 if ((memnum = rpn_create_mem(name, is_string)) >= 0)
367 rpn_store((
double)rpn_createarray(1), NULL, memnum);
380int32_t SDDS_ComputeParameter(
SDDS_DATASET *SDDS_dataset, int32_t parameter,
char *equation) {
388 layout = &SDDS_dataset->layout;
389 if (parameter < 0 || parameter >= layout->n_parameters)
393 SDDS_SetError(
"Unable to compute defined parameter--no equation for named parameter (SDDS_ComputeParameter)");
397 if (!SDDS_StoreParametersInRpnMemories(SDDS_dataset))
399 if (!SDDS_StoreColumnsInRpnArrays(SDDS_dataset))
402 value = rpn(equation);
403 rpn_store(value, NULL, layout->parameter_definition[parameter].memory_number);
404 if (rpn_check_error()) {
405 SDDS_SetError(
"Unable to compute rpn expression--rpn error (SDDS_ComputeParameter)");
409 fprintf(stderr,
"computed parameter value %s with equation %s: %e\n", layout->parameter_definition[parameter].name, equation, value);
411 switch (layout->parameter_definition[parameter].type) {
413 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (
char)value, -1);
416 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (
short)value, -1);
419 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (
unsigned short)value, -1);
422 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (int32_t)value, -1);
425 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (uint32_t)value, -1);
428 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (int64_t)value, -1);
431 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (uint64_t)value, -1);
434 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (
float)value, -1);
437 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (
double)value, -1);
440 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (
long double)value, -1);
459int32_t SDDS_ComputeColumn(
SDDS_DATASET *SDDS_dataset, int32_t column,
char *equation) {
468 layout = &SDDS_dataset->layout;
469 if (column < 0 || column >= layout->n_columns)
472 if (!SDDS_StoreParametersInRpnMemories(SDDS_dataset))
474 if (!SDDS_StoreColumnsInRpnArrays(SDDS_dataset))
477 if (table_number_mem == -1) {
478 table_number_mem = rpn_create_mem(
"table_number", 0);
479 i_page_mem = rpn_create_mem(
"i_page", 0);
480 n_rows_mem = rpn_create_mem(
"n_rows", 0);
481 i_row_mem = rpn_create_mem(
"i_row", 0);
484 rpn_store((
double)SDDS_dataset->page_number, NULL, table_number_mem);
485 rpn_store((
double)SDDS_dataset->page_number, NULL, i_page_mem);
486 rpn_store((
double)SDDS_dataset->n_rows, NULL, n_rows_mem);
488 fprintf(stderr,
"computing %s using equation %s\n", layout->column_definition[column].name, equation);
491 for (j = 0; j < SDDS_dataset->n_rows; j++) {
493 if (!SDDS_StoreRowInRpnMemories(SDDS_dataset, j))
495 rpn_store((
double)j, NULL, i_row_mem);
496 value = rpn(equation);
497 rpn_store(value, NULL, layout->column_definition[column].memory_number);
498 if (rpn_check_error()) {
499 SDDS_SetError(
"Unable to compute rpn expression--rpn error (SDDS_ComputeDefinedColumn)");
503 fprintf(stderr,
"computed row value: %s = %e\n", layout->column_definition[column].name, value);
505 switch (layout->column_definition[column].type) {
507 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (
char)value, -1);
510 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (
short)value, -1);
513 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (
unsigned short)value, -1);
516 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (int32_t)value, -1);
519 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (uint32_t)value, -1);
522 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (int64_t)value, -1);
525 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (uint64_t)value, -1);
528 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (
float)value, -1);
531 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (
double)value, -1);
534 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (
long double)value, -1);
553int32_t SDDS_FilterRowsWithRpnTest(
SDDS_DATASET *SDDS_dataset,
char *rpn_test) {
558 static MDB_THREAD_LOCAL int64_t table_number_mem = -1, n_rows_mem = -1, i_page_mem = -1;
565 layout = &SDDS_dataset->layout;
567 if (table_number_mem == -1) {
568 table_number_mem = rpn_create_mem(
"table_number", 0);
569 i_page_mem = rpn_create_mem(
"page_number", 0);
570 n_rows_mem = rpn_create_mem(
"n_rows", 0);
571 i_row_mem = rpn_create_mem(
"i_row", 0);
574 rpn_store((
double)SDDS_dataset->page_number, NULL, table_number_mem);
575 rpn_store((
double)SDDS_dataset->page_number, NULL, i_page_mem);
576 rpn_store((
double)SDDS_dataset->n_rows, NULL, n_rows_mem);
578 for (i = 0; i < layout->n_columns; i++) {
579 if (layout->column_definition[i].memory_number < 0) {
580 SDDS_SetError(
"Unable to compute equations--column lacks rpn memory number (SDDS_FilterRowsWithRpnTest)");
585 n_columns = layout->n_columns;
587 for (j = 0; j < SDDS_dataset->n_rows; j++) {
589 rpn_store((
double)j, NULL, i_row_mem);
591 coldef = layout->column_definition;
592 for (i = 0; i < n_columns; i++, coldef++) {
594 rpn_quick_store((*SDDS_ConvertTypeToDouble[coldef->type])(SDDS_dataset->data[i], j), NULL, coldef->memory_number);
596 rpn_quick_store(0, ((
char **)SDDS_dataset->data[i])[j], coldef->memory_number);
600 if (rpn_check_error()) {
601 SDDS_SetError(
"Unable to compute rpn expression--rpn error (SDDS_FilterRowsWithRpnTest)");
604 if (!pop_log(&accept)) {
609 SDDS_dataset->row_flag[j] = 0;
625int32_t SDDS_StoreParametersInRpnMemories(
SDDS_DATASET *SDDS_dataset) {
633 layout = &SDDS_dataset->layout;
637 fprintf(stderr,
"storing parameters: ");
639 for (i = 0; i < layout->n_parameters; i++) {
640 if (layout->parameter_definition[i].memory_number < 0) {
641 SDDS_SetError(
"Unable to compute equations--parameter lacks rpn memory number (SDDS_StoreParametersInRpnMemories");
644 if (layout->parameter_definition[i].type !=
SDDS_STRING) {
645 rpn_quick_store((*SDDS_ConvertTypeToDouble[layout->parameter_definition[i].type])(SDDS_dataset->parameter[i], 0), NULL, layout->parameter_definition[i].memory_number);
647 fprintf(stderr,
"%s = %le ", layout->parameter_definition[i].name, rpn_recall(layout->parameter_definition[i].memory_number));
650 rpn_quick_store(0, ((
char **)SDDS_dataset->parameter[i])[0], layout->parameter_definition[i].memory_number);
652 fprintf(stderr,
"%s = %s ", layout->parameter_definition[i].name, rpn_str_recall(layout->parameter_definition[i].memory_number));
658 if (rpn_check_error()) {
659 SDDS_SetError(
"Unable to compute rpn expression--rpn error (SDDS_StoreParametersInRpnMemories)");
679int32_t SDDS_StoreRowInRpnMemories(
SDDS_DATASET *SDDS_dataset, int64_t row) {
685 columns = SDDS_dataset->layout.n_columns;
687 coldef = SDDS_dataset->layout.column_definition;
688 for (i = 0; i < columns; i++, coldef++) {
689 if (coldef->memory_number < 0) {
690 SDDS_SetError(
"Unable to compute equations--column lacks rpn memory number (SDDS_StoreRowInRpnMemories)");
695 coldef = SDDS_dataset->layout.column_definition;
696 for (i = 0; i < columns; i++, coldef++) {
698 rpn_quick_store((*SDDS_ConvertTypeToDouble[coldef->type])(SDDS_dataset->data[i], row), NULL, coldef->memory_number);
700 rpn_quick_store(0, ((
char **)SDDS_dataset->data[i])[row], coldef->memory_number);
716int32_t SDDS_StoreColumnsInRpnArrays(
SDDS_DATASET *SDDS_dataset) {
724 uint64_t *ulong64Ptr;
726 unsigned short *ushortPtr;
727 long double *ldoublePtr;
736 layout = &SDDS_dataset->layout;
738 for (i = 0; i < layout->n_columns; i++) {
739 if (layout->column_definition[i].type !=
SDDS_STRING) {
740 if (layout->column_definition[i].pointer_number < 0) {
741 SDDS_SetError(
"Unable to compute equations--column lacks rpn pointer number (SDDS_StoreColumnsInRpnArrays)");
744 if (!rpn_resizearray((int32_t)rpn_recall(layout->column_definition[i].pointer_number), SDDS_dataset->n_rows)) {
745 SDDS_SetError(
"Unable to compute equations--couldn't resize rpn arrays (SDDS_StoreColumnsInRpnArrays)");
748 if (!(arraydata = rpn_getarraypointer(layout->column_definition[i].pointer_number, &arraysize)) || arraysize != SDDS_dataset->n_rows) {
749 SDDS_SetError(
"Unable to compute equations--couldn't retrieve rpn arrays (SDDS_StoreColumnsInRpnArrays)");
752 switch (layout->column_definition[i].type) {
754 ldoublePtr = (
long double *)(SDDS_dataset->data[i]);
755 for (j = 0; j < SDDS_dataset->n_rows; j++)
756 arraydata[j] = ldoublePtr[j];
759 memcpy((
char *)arraydata, (
char *)(SDDS_dataset->data[i]), SDDS_dataset->n_rows *
sizeof(
double));
761 doublePtr = (
double *)(SDDS_dataset->data[i]);
762 for (j = 0; j < SDDS_dataset->n_rows; j++)
763 arraydata[j] = doublePtr[j];
767 floatPtr = (
float *)(SDDS_dataset->data[i]);
768 for (j = 0; j < SDDS_dataset->n_rows; j++)
769 arraydata[j] = floatPtr[j];
772 long64Ptr = (int64_t *)(SDDS_dataset->data[i]);
773 for (j = 0; j < SDDS_dataset->n_rows; j++)
774 arraydata[j] = long64Ptr[j];
777 ulong64Ptr = (uint64_t *)(SDDS_dataset->data[i]);
778 for (j = 0; j < SDDS_dataset->n_rows; j++)
779 arraydata[j] = ulong64Ptr[j];
782 longPtr = (int32_t *)(SDDS_dataset->data[i]);
783 for (j = 0; j < SDDS_dataset->n_rows; j++)
784 arraydata[j] = longPtr[j];
787 ulongPtr = (uint32_t *)(SDDS_dataset->data[i]);
788 for (j = 0; j < SDDS_dataset->n_rows; j++)
789 arraydata[j] = ulongPtr[j];
792 shortPtr = (
short *)(SDDS_dataset->data[i]);
793 for (j = 0; j < SDDS_dataset->n_rows; j++)
794 arraydata[j] = shortPtr[j];
797 ushortPtr = (
unsigned short *)(SDDS_dataset->data[i]);
798 for (j = 0; j < SDDS_dataset->n_rows; j++)
799 arraydata[j] = ushortPtr[j];
802 charPtr = (
char *)(SDDS_dataset->data[i]);
803 for (j = 0; j < SDDS_dataset->n_rows; j++)
804 arraydata[j] = charPtr[j];
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_SetRowValues(SDDS_DATASET *SDDS_dataset, int32_t mode, int64_t row,...)
int32_t SDDS_SetParameters(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
Internal definitions and function declarations for SDDS with LZMA support.
int64_t SDDS_ConvertToLong64(int32_t type, void *data, int64_t index)
Converts a value to a 64-bit integer based on its type.
double SDDS_ConvertToDouble(int32_t type, void *data, int64_t index)
Converts a value to double based on its type.
int64_t SDDS_CreateRpnMemory(const char *name, short is_string)
Stub function for creating RPN memory when RPN_SUPPORT is not enabled.
long double SDDS_ConvertToLongDouble(int32_t type, void *data, int64_t index)
Converts a value to long double based on its type.
double SDDS_ConvertCharToDouble(void *data, int64_t index)
Converts a character to double.
double SDDS_ConvertULongToDouble(void *data, int64_t index)
Converts an unsigned 32-bit integer to double.
double SDDS_ConvertFloatToDouble(void *data, int64_t index)
Converts a float value to double.
double SDDS_ConvertLong64ToDouble(void *data, int64_t index)
Converts a 64-bit integer to double.
double SDDS_ConvertDoubleToDouble(void *data, int64_t index)
Converts a double value to double (identity function).
double SDDS_ConvertShortToDouble(void *data, int64_t index)
Converts a short integer to double.
double SDDS_ConvertLongToDouble(void *data, int64_t index)
Converts a 32-bit integer to double.
double SDDS_ConvertUShortToDouble(void *data, int64_t index)
Converts an unsigned short integer to double.
int64_t SDDS_CreateRpnArray(char *name)
Stub function for creating RPN arrays when RPN_SUPPORT is not enabled.
int32_t SDDS_ConvertToLong(int32_t type, void *data, int64_t index)
Converts a value to a 32-bit integer based on its type.
double SDDS_ConvertStringToDouble(void *data, int64_t index)
Converts a string to double using atof.
double SDDS_ConvertULong64ToDouble(void *data, int64_t index)
Converts an unsigned 64-bit integer to double.
double SDDS_ConvertLongDoubleToDouble(void *data, int64_t index)
Converts a long double value to double.
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
int32_t SDDS_CheckDataset(SDDS_DATASET *SDDS_dataset, const char *caller)
Validates the SDDS dataset pointer.
int32_t SDDS_NumberOfErrors()
Retrieves the number of errors recorded by SDDS library routines.
#define SDDS_NUM_TYPES
Total number of defined SDDS data types.
#define SDDS_ULONG
Identifier for the unsigned 32-bit integer data type.
#define SDDS_FLOAT
Identifier for the float data type.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_ULONG64
Identifier for the unsigned 64-bit integer data type.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
#define SDDS_SHORT
Identifier for the signed short integer data type.
#define SDDS_CHARACTER
Identifier for the character data type.
#define SDDS_USHORT
Identifier for the unsigned short integer data type.
#define SDDS_DOUBLE
Identifier for the double data type.
#define SDDS_LONGDOUBLE
Identifier for the long double data type.
#define SDDS_LONG64
Identifier for the signed 64-bit integer data type.