SDDSlib
Loading...
Searching...
No Matches
SDDS_rpn.c
Go to the documentation of this file.
1/**
2 * @file SDDS_rpn.c
3 * @brief Implements Reverse Polish Notation (RPN) operations for SDDS datasets.
4 *
5 * This file provides functions to convert various data types to double and long double,
6 * as well as functions to compute parameters and columns using RPN expressions within
7 * SDDS datasets. It also includes functionality to filter rows based on RPN tests.
8 *
9 * @copyright
10 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
11 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
12 *
13 * @license
14 * This file is distributed under the terms of the Software License Agreement
15 * found in the file LICENSE included with this distribution.
16 *
17 * @authors
18 * M. Borland,
19 * C. Saunders,
20 * R. Soliday
21 * H. Shang
22 */
23
24#include "mdb.h"
25#include "match_string.h"
26#include "SDDS.h"
27#include "SDDS_internal.h"
28#include "rpn.h"
29
30/**
31 * @brief Converts a long double value to double.
32 *
33 * @param data Pointer to the data array.
34 * @param index Index of the element to convert.
35 * @return The converted double value.
36 */
37double SDDS_ConvertLongDoubleToDouble(void *data, int64_t index) {
38 return *((long double *)data + index);
39}
40
41/**
42 * @brief Converts a double value to double (identity function).
43 *
44 * @param data Pointer to the data array.
45 * @param index Index of the element.
46 * @return The double value.
47 */
48double SDDS_ConvertDoubleToDouble(void *data, int64_t index) {
49 return *((double *)data + index);
50}
51
52/**
53 * @brief Converts a float value to double.
54 *
55 * @param data Pointer to the data array.
56 * @param index Index of the element to convert.
57 * @return The converted double value.
58 */
59double SDDS_ConvertFloatToDouble(void *data, int64_t index) {
60 return *((float *)data + index);
61}
62
63/**
64 * @brief Converts a 64-bit integer to double.
65 *
66 * @param data Pointer to the data array.
67 * @param index Index of the element to convert.
68 * @return The converted double value.
69 */
70double SDDS_ConvertLong64ToDouble(void *data, int64_t index) {
71 return *((int64_t *)data + index);
72}
73
74/**
75 * @brief Converts an unsigned 64-bit integer to double.
76 *
77 * @param data Pointer to the data array.
78 * @param index Index of the element to convert.
79 * @return The converted double value.
80 */
81double SDDS_ConvertULong64ToDouble(void *data, int64_t index) {
82 return *((uint64_t *)data + index);
83}
84
85/**
86 * @brief Converts a 32-bit integer to double.
87 *
88 * @param data Pointer to the data array.
89 * @param index Index of the element to convert.
90 * @return The converted double value.
91 */
92double SDDS_ConvertLongToDouble(void *data, int64_t index) {
93 return *((int32_t *)data + index);
94}
95
96/**
97 * @brief Converts an unsigned 32-bit integer to double.
98 *
99 * @param data Pointer to the data array.
100 * @param index Index of the element to convert.
101 * @return The converted double value.
102 */
103double SDDS_ConvertULongToDouble(void *data, int64_t index) {
104 return *((uint32_t *)data + index);
105}
106
107/**
108 * @brief Converts a short integer to double.
109 *
110 * @param data Pointer to the data array.
111 * @param index Index of the element to convert.
112 * @return The converted double value.
113 */
114double SDDS_ConvertShortToDouble(void *data, int64_t index) {
115 return *((short *)data + index);
116}
117
118/**
119 * @brief Converts an unsigned short integer to double.
120 *
121 * @param data Pointer to the data array.
122 * @param index Index of the element to convert.
123 * @return The converted double value.
124 */
125double SDDS_ConvertUShortToDouble(void *data, int64_t index) {
126 return *((unsigned short *)data + index);
127}
128
129/**
130 * @brief Converts a string to double using atof.
131 *
132 * @param data Pointer to the data array.
133 * @param index Index of the element to convert.
134 * @return The converted double value.
135 */
136double SDDS_ConvertStringToDouble(void *data, int64_t index) {
137 return atof(*((char **)data + index));
138}
139
140/**
141 * @brief Converts a character to double.
142 *
143 * @param data Pointer to the data array.
144 * @param index Index of the element to convert.
145 * @return The converted double value.
146 */
147double SDDS_ConvertCharToDouble(void *data, int64_t index) {
148 return *((char *)data + index);
149}
150
151/**
152 * @brief Converts a value to long double based on its type.
153 *
154 * @param type The SDDS data type of the value.
155 * @param data Pointer to the data array.
156 * @param index Index of the element to convert.
157 * @return The converted long double value, or 0.0 on error.
158 */
159long double SDDS_ConvertToLongDouble(int32_t type, void *data, int64_t index) {
160 if (!data) {
161 SDDS_SetError("NULL data pointer passed (SDDS_ConvertToLongDouble)");
162 return (0.0);
163 }
164 switch (type) {
165 case SDDS_SHORT:
166 return ((long double)*((short *)data + index));
167 case SDDS_USHORT:
168 return ((long double)*((unsigned short *)data + index));
169 case SDDS_LONG:
170 return ((long double)*((int32_t *)data + index));
171 case SDDS_ULONG:
172 return ((long double)*((uint32_t *)data + index));
173 case SDDS_LONG64:
174 return ((long double)*((int64_t *)data + index));
175 case SDDS_ULONG64:
176 return ((long double)*((uint64_t *)data + index));
177 case SDDS_FLOAT:
178 return ((long double)*((float *)data + index));
179 case SDDS_DOUBLE:
180 return ((long double)*((double *)data + index));
181 case SDDS_LONGDOUBLE:
182 return (*((long double *)data + index));
183 case SDDS_CHARACTER:
184 return ((long double)*((unsigned char *)data + index));
185 default:
186 SDDS_SetError("Invalid data type seen (SDDS_ConvertToLongDouble)");
187 return (0.0);
188 }
189}
190
191/**
192 * @brief Converts a value to double based on its type.
193 *
194 * @param type The SDDS data type of the value.
195 * @param data Pointer to the data array.
196 * @param index Index of the element to convert.
197 * @return The converted double value, or 0.0 on error.
198 */
199double SDDS_ConvertToDouble(int32_t type, void *data, int64_t index) {
200 if (!data) {
201 SDDS_SetError("NULL data pointer passed (SDDS_ConvertToDouble)");
202 return (0.0);
203 }
204 switch (type) {
205 case SDDS_SHORT:
206 return ((double)*((short *)data + index));
207 case SDDS_USHORT:
208 return ((double)*((unsigned short *)data + index));
209 case SDDS_LONG:
210 return ((double)*((int32_t *)data + index));
211 case SDDS_ULONG:
212 return ((double)*((uint32_t *)data + index));
213 case SDDS_LONG64:
214 return ((double)*((int64_t *)data + index));
215 case SDDS_ULONG64:
216 return ((double)*((uint64_t *)data + index));
217 case SDDS_FLOAT:
218 return ((double)*((float *)data + index));
219 case SDDS_DOUBLE:
220 return (*((double *)data + index));
221 case SDDS_LONGDOUBLE:
222 return ((double)*((long double *)data + index));
223 case SDDS_CHARACTER:
224 return ((double)*((unsigned char *)data + index));
225 default:
226 SDDS_SetError("Invalid data type seen (SDDS_ConvertToDouble)");
227 return (0.0);
228 }
229}
230
231/**
232 * @brief Converts a value to a 64-bit integer based on its type.
233 *
234 * @param type The SDDS data type of the value.
235 * @param data Pointer to the data array.
236 * @param index Index of the element to convert.
237 * @return The converted 64-bit integer, or 0 on error.
238 */
239int64_t SDDS_ConvertToLong64(int32_t type, void *data, int64_t index) {
240 if (!data) {
241 SDDS_SetError("NULL data pointer passed (SDDS_ConvertToLong64)");
242 return (0.0);
243 }
244 switch (type) {
245 case SDDS_LONGDOUBLE:
246 return ((int64_t) * ((long double *)data + index));
247 case SDDS_DOUBLE:
248 return ((int64_t) * ((double *)data + index));
249 case SDDS_FLOAT:
250 return ((int64_t) * ((float *)data + index));
251 case SDDS_SHORT:
252 return ((int64_t) * ((short *)data + index));
253 case SDDS_USHORT:
254 return ((int64_t) * ((unsigned short *)data + index));
255 case SDDS_LONG:
256 return ((int64_t) * ((int32_t *)data + index));
257 case SDDS_ULONG:
258 return ((int64_t) * ((uint32_t *)data + index));
259 case SDDS_LONG64:
260 return (*((int64_t *)data + index));
261 case SDDS_ULONG64:
262 return ((int64_t) * ((uint64_t *)data + index));
263 case SDDS_CHARACTER:
264 return ((int64_t) * ((unsigned char *)data + index));
265 default:
266 SDDS_SetError("Invalid data type seen (SDDS_ConvertToLong64)");
267 return (0.0);
268 }
269}
270
271/**
272 * @brief Converts a value to a 32-bit integer based on its type.
273 *
274 * @param type The SDDS data type of the value.
275 * @param data Pointer to the data array.
276 * @param index Index of the element to convert.
277 * @return The converted 32-bit integer, or 0 on error.
278 */
279int32_t SDDS_ConvertToLong(int32_t type, void *data, int64_t index) {
280 if (!data) {
281 SDDS_SetError("NULL data pointer passed (SDDS_ConvertToLong)");
282 return (0.0);
283 }
284 switch (type) {
285 case SDDS_LONGDOUBLE:
286 return ((int32_t) * ((long double *)data + index));
287 case SDDS_DOUBLE:
288 return ((int32_t) * ((double *)data + index));
289 case SDDS_FLOAT:
290 return ((int32_t) * ((float *)data + index));
291 case SDDS_SHORT:
292 return ((int32_t) * ((short *)data + index));
293 case SDDS_USHORT:
294 return ((int32_t) * ((unsigned short *)data + index));
295 case SDDS_LONG:
296 return (*((int32_t *)data + index));
297 case SDDS_ULONG:
298 return ((int32_t) * ((uint32_t *)data + index));
299 case SDDS_LONG64:
300 return ((int32_t) * ((int64_t *)data + index));
301 case SDDS_ULONG64:
302 return ((int32_t) * ((uint64_t *)data + index));
303 case SDDS_CHARACTER:
304 return ((int32_t) * ((unsigned char *)data + index));
305 default:
306 SDDS_SetError("Invalid data type seen (SDDS_ConvertToLong)");
307 return (0.0);
308 }
309}
310
311#if defined(RPN_SUPPORT)
312
313static double (*SDDS_ConvertTypeToDouble[SDDS_NUM_TYPES + 1])(void *data, int64_t index) =
314 {
315 NULL,
327
328// Static variables to store memory identifiers for RPN operations
329static int64_t table_number_mem = -1;
330static int64_t i_page_mem = -1;
331static int64_t n_rows_mem = -1;
332static int64_t i_row_mem = -1;
333
334/**
335 * @brief Creates an RPN memory block.
336 *
337 * @param name Name of the RPN memory.
338 * @param is_string Flag indicating if the memory is for string data.
339 * @return Identifier of the created RPN memory, or -1 on failure.
340 */
341int64_t SDDS_CreateRpnMemory(const char *name, short is_string) {
342 if (!name)
343 return (-1);
344 return (rpn_create_mem((char *)name, is_string));
345}
346
347/**
348 * @brief Creates an RPN array for a given name.
349 *
350 * @param name Name of the RPN array.
351 * @return Identifier of the created RPN array, or -1 on failure.
352 */
353int64_t SDDS_CreateRpnArray(char *name) {
354 int64_t memnum;
355 double dummy;
356 char *dummy1 = NULL;
357 short is_string = 0;
358
359 if (!name)
360 return (-1);
361 if ((memnum = is_memory(&dummy, &dummy1, &is_string, name)) >= 0)
362 return memnum;
363 if ((memnum = rpn_create_mem(name, is_string)) >= 0)
364 rpn_store((double)rpn_createarray(1), NULL, memnum);
365 return memnum;
366}
367
368/**
369 * @brief Computes a parameter in the SDDS dataset using an RPN equation.
370 *
371 * @param SDDS_dataset Pointer to the SDDS dataset.
372 * @param parameter Index of the parameter to compute.
373 * @param equation RPN equation as a string.
374 * @return 1 on success, 0 on failure.
375 */
376int32_t SDDS_ComputeParameter(SDDS_DATASET *SDDS_dataset, int32_t parameter, char *equation) {
377 SDDS_LAYOUT *layout;
378 double value;
379
380 if (!SDDS_CheckDataset(SDDS_dataset, "SDDS_ComputeParameter"))
381 return (0);
382 layout = &SDDS_dataset->layout;
383 if (parameter < 0 || parameter >= layout->n_parameters)
384 return (0);
385
386 if (!equation) {
387 SDDS_SetError("Unable to compute defined parameter--no equation for named parameter (SDDS_ComputeParameter)");
388 return (0);
389 }
390
391 if (!SDDS_StoreParametersInRpnMemories(SDDS_dataset))
392 return (0);
393 if (!SDDS_StoreColumnsInRpnArrays(SDDS_dataset))
394 return 0;
395
396 value = rpn(equation);
397 rpn_store(value, NULL, layout->parameter_definition[parameter].memory_number);
398 if (rpn_check_error()) {
399 SDDS_SetError("Unable to compute rpn expression--rpn error (SDDS_ComputeParameter)");
400 return (0);
401 }
402# if defined(DEBUG)
403 fprintf(stderr, "computed parameter value %s with equation %s: %e\n", layout->parameter_definition[parameter].name, equation, value);
404# endif
405 switch (layout->parameter_definition[parameter].type) {
406 case SDDS_CHARACTER:
407 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (char)value, -1);
408 break;
409 case SDDS_SHORT:
410 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (short)value, -1);
411 break;
412 case SDDS_USHORT:
413 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (unsigned short)value, -1);
414 break;
415 case SDDS_LONG:
416 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (int32_t)value, -1);
417 break;
418 case SDDS_ULONG:
419 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (uint32_t)value, -1);
420 break;
421 case SDDS_LONG64:
422 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (int64_t)value, -1);
423 break;
424 case SDDS_ULONG64:
425 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (uint64_t)value, -1);
426 break;
427 case SDDS_FLOAT:
428 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (float)value, -1);
429 break;
430 case SDDS_DOUBLE:
431 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (double)value, -1);
432 break;
433 case SDDS_LONGDOUBLE:
434 SDDS_SetParameters(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, parameter, (long double)value, -1);
435 break;
436 }
437
438 return (1);
439}
440
441/**
442 * @brief Computes a column in the SDDS dataset using an RPN equation.
443 *
444 * @param SDDS_dataset Pointer to the SDDS dataset.
445 * @param column Index of the column to compute.
446 * @param equation RPN equation as a string.
447 * @return 1 on success, 0 on failure.
448 */
449int32_t SDDS_ComputeColumn(SDDS_DATASET *SDDS_dataset, int32_t column, char *equation) {
450 int64_t j;
451 SDDS_LAYOUT *layout;
452 double value;
453
454 if (!SDDS_CheckDataset(SDDS_dataset, "SDDS_ComputeColumn"))
455 return (0);
456 layout = &SDDS_dataset->layout;
457 if (column < 0 || column >= layout->n_columns)
458 return (0);
459
460 if (!SDDS_StoreParametersInRpnMemories(SDDS_dataset))
461 return (0);
462 if (!SDDS_StoreColumnsInRpnArrays(SDDS_dataset))
463 return 0;
464
465 if (table_number_mem == -1) {
466 table_number_mem = rpn_create_mem("table_number", 0);
467 i_page_mem = rpn_create_mem("i_page", 0);
468 n_rows_mem = rpn_create_mem("n_rows", 0);
469 i_row_mem = rpn_create_mem("i_row", 0);
470 }
471
472 rpn_store((double)SDDS_dataset->page_number, NULL, table_number_mem);
473 rpn_store((double)SDDS_dataset->page_number, NULL, i_page_mem);
474 rpn_store((double)SDDS_dataset->n_rows, NULL, n_rows_mem);
475# if defined(DEBUG)
476 fprintf(stderr, "computing %s using equation %s\n", layout->column_definition[column].name, equation);
477# endif
478
479 for (j = 0; j < SDDS_dataset->n_rows; j++) {
480 rpn_clear();
481 if (!SDDS_StoreRowInRpnMemories(SDDS_dataset, j))
482 return (0);
483 rpn_store((double)j, NULL, i_row_mem);
484 value = rpn(equation);
485 rpn_store(value, NULL, layout->column_definition[column].memory_number);
486 if (rpn_check_error()) {
487 SDDS_SetError("Unable to compute rpn expression--rpn error (SDDS_ComputeDefinedColumn)");
488 return (0);
489 }
490# if defined(DEBUG)
491 fprintf(stderr, "computed row value: %s = %e\n", layout->column_definition[column].name, value);
492# endif
493 switch (layout->column_definition[column].type) {
494 case SDDS_CHARACTER:
495 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (char)value, -1);
496 break;
497 case SDDS_SHORT:
498 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (short)value, -1);
499 break;
500 case SDDS_USHORT:
501 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (unsigned short)value, -1);
502 break;
503 case SDDS_LONG:
504 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (int32_t)value, -1);
505 break;
506 case SDDS_ULONG:
507 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (uint32_t)value, -1);
508 break;
509 case SDDS_LONG64:
510 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (int64_t)value, -1);
511 break;
512 case SDDS_ULONG64:
513 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (uint64_t)value, -1);
514 break;
515 case SDDS_FLOAT:
516 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (float)value, -1);
517 break;
518 case SDDS_DOUBLE:
519 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (double)value, -1);
520 break;
521 case SDDS_LONGDOUBLE:
522 SDDS_SetRowValues(SDDS_dataset, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, j, column, (long double)value, -1);
523 break;
524 }
525 }
526
527 return (1);
528}
529
530/**
531 * @brief Filters rows in the SDDS dataset based on an RPN test expression.
532 *
533 * @param SDDS_dataset Pointer to the SDDS dataset.
534 * @param rpn_test RPN test expression as a string.
535 * @return 1 on success, 0 on failure.
536 */
537int32_t SDDS_FilterRowsWithRpnTest(SDDS_DATASET *SDDS_dataset, char *rpn_test) {
538 int64_t i, j;
539 int32_t n_columns;
540 SDDS_LAYOUT *layout;
541 int32_t accept;
542 static int64_t table_number_mem = -1, n_rows_mem = -1, i_page_mem = -1;
543 COLUMN_DEFINITION *coldef;
544
545 if (!SDDS_CheckDataset(SDDS_dataset, "SDDS_ComputeRpnEquations"))
546 return (0);
547 layout = &SDDS_dataset->layout;
548
549 if (table_number_mem == -1) {
550 table_number_mem = rpn_create_mem("table_number", 0);
551 i_page_mem = rpn_create_mem("page_number", 0);
552 n_rows_mem = rpn_create_mem("n_rows", 0);
553 i_row_mem = rpn_create_mem("i_row", 0);
554 }
555
556 rpn_store((double)SDDS_dataset->page_number, NULL, table_number_mem);
557 rpn_store((double)SDDS_dataset->page_number, NULL, i_page_mem);
558 rpn_store((double)SDDS_dataset->n_rows, NULL, n_rows_mem);
559
560 for (i = 0; i < layout->n_columns; i++) {
561 if (layout->column_definition[i].memory_number < 0) {
562 SDDS_SetError("Unable to compute equations--column lacks rpn memory number (SDDS_FilterRowsWithRpnTest)");
563 return (0);
564 }
565 }
566
567 n_columns = layout->n_columns;
568
569 for (j = 0; j < SDDS_dataset->n_rows; j++) {
570 rpn_clear();
571 rpn_store((double)j, NULL, i_row_mem);
572 /* store values in memories */
573 coldef = layout->column_definition;
574 for (i = 0; i < n_columns; i++, coldef++) {
575 if (coldef->type != SDDS_STRING) {
576 rpn_quick_store((*SDDS_ConvertTypeToDouble[coldef->type])(SDDS_dataset->data[i], j), NULL, coldef->memory_number);
577 } else {
578 rpn_quick_store(0, ((char **)SDDS_dataset->data[i])[j], coldef->memory_number);
579 }
580 }
581 rpn(rpn_test);
582 if (rpn_check_error()) {
583 SDDS_SetError("Unable to compute rpn expression--rpn error (SDDS_FilterRowsWithRpnTest)");
584 return (0);
585 }
586 if (!pop_log(&accept)) {
587 SDDS_SetError("rpn column-based test expression problem");
588 return (0);
589 }
590 if (!accept)
591 SDDS_dataset->row_flag[j] = 0;
592 }
593 rpn_clear();
594 return (1);
595}
596
597/**
598 * @brief Stores all parameters of the SDDS dataset into RPN memories.
599 *
600 * @param SDDS_dataset Pointer to the SDDS dataset.
601 * @return 1 on success, 0 on failure.
602 */
603int32_t SDDS_StoreParametersInRpnMemories(SDDS_DATASET *SDDS_dataset) {
604 int32_t i;
605 SDDS_LAYOUT *layout;
606
607 if (!SDDS_CheckDataset(SDDS_dataset, "SDDS_StoreParametersInRpnMemories"))
608 return (0);
609 layout = &SDDS_dataset->layout;
610
611 rpn_clear();
612# if defined(DEBUG)
613 fprintf(stderr, "storing parameters: ");
614# endif
615 for (i = 0; i < layout->n_parameters; i++) {
616 if (layout->parameter_definition[i].memory_number < 0) {
617 SDDS_SetError("Unable to compute equations--parameter lacks rpn memory number (SDDS_StoreParametersInRpnMemories");
618 return (0);
619 }
620 if (layout->parameter_definition[i].type != SDDS_STRING) {
621 rpn_quick_store((*SDDS_ConvertTypeToDouble[layout->parameter_definition[i].type])(SDDS_dataset->parameter[i], 0), NULL, layout->parameter_definition[i].memory_number);
622# if defined(DEBUG)
623 fprintf(stderr, "%s = %le ", layout->parameter_definition[i].name, rpn_recall(layout->parameter_definition[i].memory_number));
624# endif
625 } else {
626 rpn_quick_store(0, ((char **)SDDS_dataset->parameter[i])[0], layout->parameter_definition[i].memory_number);
627# if defined(DEBUG)
628 fprintf(stderr, "%s = %s ", layout->parameter_definition[i].name, rpn_str_recall(layout->parameter_definition[i].memory_number));
629# endif
630 }
631 }
633 return (0);
634 if (rpn_check_error()) {
635 SDDS_SetError("Unable to compute rpn expression--rpn error (SDDS_StoreParametersInRpnMemories)");
636 return (0);
637 }
638# if defined(DEBUG)
639 fputc('\n', stderr);
640# endif
641 return (1);
642}
643
644/**
645 * @brief Stores a specific row's column values into RPN memories.
646 *
647 * @param SDDS_dataset Pointer to the SDDS dataset.
648 * @param row Index of the row to store.
649 * @return 1 on success, 0 on failure.
650 */
651int32_t SDDS_StoreRowInRpnMemories(SDDS_DATASET *SDDS_dataset, int64_t row) {
652 int32_t i, columns;
653 COLUMN_DEFINITION *coldef;
654
655 columns = SDDS_dataset->layout.n_columns;
656 if (row == 0) {
657 coldef = SDDS_dataset->layout.column_definition;
658 for (i = 0; i < columns; i++, coldef++) {
659 if (coldef->memory_number < 0) {
660 SDDS_SetError("Unable to compute equations--column lacks rpn memory number (SDDS_StoreRowInRpnMemories)");
661 return (0);
662 }
663 }
664 }
665 coldef = SDDS_dataset->layout.column_definition;
666 for (i = 0; i < columns; i++, coldef++) {
667 if (coldef->type != SDDS_STRING) {
668 rpn_quick_store((*SDDS_ConvertTypeToDouble[coldef->type])(SDDS_dataset->data[i], row), NULL, coldef->memory_number);
669 } else {
670 rpn_quick_store(0, ((char **)SDDS_dataset->data[i])[row], coldef->memory_number);
671 }
672 }
673 return (1);
674}
675
676/**
677 * @brief Stores all column data into RPN arrays for efficient computation.
678 *
679 * @param SDDS_dataset Pointer to the SDDS dataset.
680 * @return 1 on success, 0 on failure.
681 */
682int32_t SDDS_StoreColumnsInRpnArrays(SDDS_DATASET *SDDS_dataset) {
683 int64_t i, j;
684 int32_t arraysize;
685 SDDS_LAYOUT *layout;
686 double *arraydata;
687 int32_t *longPtr;
688 uint32_t *ulongPtr;
689 int64_t *long64Ptr;
690 uint64_t *ulong64Ptr;
691 short *shortPtr;
692 unsigned short *ushortPtr;
693 long double *ldoublePtr;
694 /* double *doublePtr; */
695 float *floatPtr;
696 char *charPtr;
697
698 if (!SDDS_CheckDataset(SDDS_dataset, "SDDS_StoreColumnsRpnArrays"))
699 return (0);
700 layout = &SDDS_dataset->layout;
701 rpn_clear();
702 for (i = 0; i < layout->n_columns; i++) {
703 if (layout->column_definition[i].type != SDDS_STRING) {
704 if (layout->column_definition[i].pointer_number < 0) {
705 SDDS_SetError("Unable to compute equations--column lacks rpn pointer number (SDDS_StoreColumnsInRpnArrays)");
706 return (0);
707 }
708 if (!rpn_resizearray((int32_t)rpn_recall(layout->column_definition[i].pointer_number), SDDS_dataset->n_rows)) {
709 SDDS_SetError("Unable to compute equations--couldn't resize rpn arrays (SDDS_StoreColumnsInRpnArrays)");
710 return 0;
711 }
712 if (!(arraydata = rpn_getarraypointer(layout->column_definition[i].pointer_number, &arraysize)) || arraysize != SDDS_dataset->n_rows) {
713 SDDS_SetError("Unable to compute equations--couldn't retrieve rpn arrays (SDDS_StoreColumnsInRpnArrays)");
714 return 0;
715 }
716 switch (layout->column_definition[i].type) {
717 case SDDS_LONGDOUBLE:
718 ldoublePtr = (long double *)(SDDS_dataset->data[i]);
719 for (j = 0; j < SDDS_dataset->n_rows; j++)
720 arraydata[j] = ldoublePtr[j];
721 break;
722 case SDDS_DOUBLE:
723 memcpy((char *)arraydata, (char *)(SDDS_dataset->data[i]), SDDS_dataset->n_rows * sizeof(double));
724# if 0
725 doublePtr = (double *)(SDDS_dataset->data[i]);
726 for (j = 0; j < SDDS_dataset->n_rows; j++)
727 arraydata[j] = doublePtr[j];
728# endif
729 break;
730 case SDDS_FLOAT:
731 floatPtr = (float *)(SDDS_dataset->data[i]);
732 for (j = 0; j < SDDS_dataset->n_rows; j++)
733 arraydata[j] = floatPtr[j];
734 break;
735 case SDDS_LONG64:
736 long64Ptr = (int64_t *)(SDDS_dataset->data[i]);
737 for (j = 0; j < SDDS_dataset->n_rows; j++)
738 arraydata[j] = long64Ptr[j];
739 break;
740 case SDDS_ULONG64:
741 ulong64Ptr = (uint64_t *)(SDDS_dataset->data[i]);
742 for (j = 0; j < SDDS_dataset->n_rows; j++)
743 arraydata[j] = ulong64Ptr[j];
744 break;
745 case SDDS_LONG:
746 longPtr = (int32_t *)(SDDS_dataset->data[i]);
747 for (j = 0; j < SDDS_dataset->n_rows; j++)
748 arraydata[j] = longPtr[j];
749 break;
750 case SDDS_ULONG:
751 ulongPtr = (uint32_t *)(SDDS_dataset->data[i]);
752 for (j = 0; j < SDDS_dataset->n_rows; j++)
753 arraydata[j] = ulongPtr[j];
754 break;
755 case SDDS_SHORT:
756 shortPtr = (short *)(SDDS_dataset->data[i]);
757 for (j = 0; j < SDDS_dataset->n_rows; j++)
758 arraydata[j] = shortPtr[j];
759 break;
760 case SDDS_USHORT:
761 ushortPtr = (unsigned short *)(SDDS_dataset->data[i]);
762 for (j = 0; j < SDDS_dataset->n_rows; j++)
763 arraydata[j] = ushortPtr[j];
764 break;
765 case SDDS_CHARACTER:
766 charPtr = (char *)(SDDS_dataset->data[i]);
767 for (j = 0; j < SDDS_dataset->n_rows; j++)
768 arraydata[j] = charPtr[j];
769 break;
770 }
771 }
772 }
773 return 1;
774}
775
776#else /* end of RPN_SUPPORT section */
777
778/**
779 * @brief Stub function for creating RPN memory when RPN_SUPPORT is not enabled.
780 *
781 * @param name Name of the RPN memory.
782 * @param is_string Flag indicating if the memory is for string data.
783 * @return Always returns 1.
784 */
785int64_t SDDS_CreateRpnMemory(const char *name, short is_string) {
786 return (1);
787}
788
789/**
790 * @brief Stub function for creating RPN arrays when RPN_SUPPORT is not enabled.
791 *
792 * @param name Name of the RPN array.
793 * @return Always returns 1.
794 */
795int64_t SDDS_CreateRpnArray(char *name) {
796 return (1);
797}
798
799#endif
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.
Definition SDDS_rpn.c:239
double SDDS_ConvertToDouble(int32_t type, void *data, int64_t index)
Converts a value to double based on its type.
Definition SDDS_rpn.c:199
int64_t SDDS_CreateRpnMemory(const char *name, short is_string)
Stub function for creating RPN memory when RPN_SUPPORT is not enabled.
Definition SDDS_rpn.c:785
long double SDDS_ConvertToLongDouble(int32_t type, void *data, int64_t index)
Converts a value to long double based on its type.
Definition SDDS_rpn.c:159
double SDDS_ConvertCharToDouble(void *data, int64_t index)
Converts a character to double.
Definition SDDS_rpn.c:147
double SDDS_ConvertULongToDouble(void *data, int64_t index)
Converts an unsigned 32-bit integer to double.
Definition SDDS_rpn.c:103
double SDDS_ConvertFloatToDouble(void *data, int64_t index)
Converts a float value to double.
Definition SDDS_rpn.c:59
double SDDS_ConvertLong64ToDouble(void *data, int64_t index)
Converts a 64-bit integer to double.
Definition SDDS_rpn.c:70
double SDDS_ConvertDoubleToDouble(void *data, int64_t index)
Converts a double value to double (identity function).
Definition SDDS_rpn.c:48
double SDDS_ConvertShortToDouble(void *data, int64_t index)
Converts a short integer to double.
Definition SDDS_rpn.c:114
double SDDS_ConvertLongToDouble(void *data, int64_t index)
Converts a 32-bit integer to double.
Definition SDDS_rpn.c:92
double SDDS_ConvertUShortToDouble(void *data, int64_t index)
Converts an unsigned short integer to double.
Definition SDDS_rpn.c:125
int64_t SDDS_CreateRpnArray(char *name)
Stub function for creating RPN arrays when RPN_SUPPORT is not enabled.
Definition SDDS_rpn.c:795
int32_t SDDS_ConvertToLong(int32_t type, void *data, int64_t index)
Converts a value to a 32-bit integer based on its type.
Definition SDDS_rpn.c:279
double SDDS_ConvertStringToDouble(void *data, int64_t index)
Converts a string to double using atof.
Definition SDDS_rpn.c:136
double SDDS_ConvertULong64ToDouble(void *data, int64_t index)
Converts an unsigned 64-bit integer to double.
Definition SDDS_rpn.c:81
double SDDS_ConvertLongDoubleToDouble(void *data, int64_t index)
Converts a long double value to double.
Definition SDDS_rpn.c:37
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
Definition SDDS_utils.c:379
int32_t SDDS_CheckDataset(SDDS_DATASET *SDDS_dataset, const char *caller)
Validates the SDDS dataset pointer.
Definition SDDS_utils.c:552
int32_t SDDS_NumberOfErrors()
Retrieves the number of errors recorded by SDDS library routines.
Definition SDDS_utils.c:304
#define SDDS_NUM_TYPES
Total number of defined SDDS data types.
Definition SDDStypes.h:97
#define SDDS_ULONG
Identifier for the unsigned 32-bit integer data type.
Definition SDDStypes.h:67
#define SDDS_FLOAT
Identifier for the float data type.
Definition SDDStypes.h:43
#define SDDS_STRING
Identifier for the string data type.
Definition SDDStypes.h:85
#define SDDS_ULONG64
Identifier for the unsigned 64-bit integer data type.
Definition SDDStypes.h:55
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
Definition SDDStypes.h:61
#define SDDS_SHORT
Identifier for the signed short integer data type.
Definition SDDStypes.h:73
#define SDDS_CHARACTER
Identifier for the character data type.
Definition SDDStypes.h:91
#define SDDS_USHORT
Identifier for the unsigned short integer data type.
Definition SDDStypes.h:79
#define SDDS_DOUBLE
Identifier for the double data type.
Definition SDDStypes.h:37
#define SDDS_LONGDOUBLE
Identifier for the long double data type.
Definition SDDStypes.h:31
#define SDDS_LONG64
Identifier for the signed 64-bit integer data type.
Definition SDDStypes.h:49