SDDSlib
Loading...
Searching...
No Matches
sddssort.c File Reference

Sorts an SDDS (Self Describing Data Set) data set by the values in one or more columns. More...

#include "mdb.h"
#include "SDDS.h"
#include "scan.h"
#include "non_dominated_sort.h"
#include <unistd.h>

Go to the source code of this file.

Classes

struct  SORT_REQUEST
 

Macros

#define SWAP_BUFFER_SIZE   16
 

Enumerations

enum  option_type {
  SET_COLUMN , SET_PARAMETER , SET_NOWARNINGS , SET_PIPE ,
  SET_UNIQUE , SET_NUMERICHIGH , SET_NON_DOMINATE_SORT , SET_MAJOR_ORDER ,
  N_OPTIONS
}
 

Functions

long SDDS_SortRows (SDDS_DATASET *SDDS_dataset, SORT_REQUEST *xsort_request, long xsort_requests, long non_dominate_sort)
 
long SDDS_UnsetDuplicateRows (SDDS_DATASET *SDDS_dataset, SORT_REQUEST *xsort_request, long xsort_requests, long provideIdenticalCount)
 
long SDDS_SortAll (SDDS_DATASET *SDDS_input, SDDS_DATASET *SDDS_output, SORT_REQUEST *xsort_request, long xsort_requests, SORT_REQUEST *xsort_parameter, long xsort_parameters, long uniqueRows, long provideIdenticalCount, long pipeFlags, long non_dominate_sort)
 
double * read_constr_violation (SDDS_DATASET *SDDS_dataset)
 
int main (int argc, char **argv)
 
int SDDS_CompareData (SDDS_DATASET *SDDS_dataset, short type, short absolute, void *data1, void *data2)
 
int SDDS_CompareRows (const void *vrow1, const void *vrow2)
 
long SDDS_SwapRows (SDDS_DATASET *SDDS_dataset, int64_t row1, int64_t row2)
 
int SDDS_ComparePages (const void *vpage1, const void *vpage2)
 

Variables

char * option [N_OPTIONS]
 
char * USAGE
 
static char * order_mode [5]
 
long numericHigh = 0
 
long constDefined = 0
 
static SDDS_DATASETSDDS_sort
 
static SORT_REQUESTsort_request
 
static long sort_requests
 
static int64_t * sort_row_index
 
static SORT_REQUESTsort_parameter
 
static long sort_parameters
 
static SDDS_DATASETSDDS_sortpage
 

Detailed Description

Sorts an SDDS (Self Describing Data Set) data set by the values in one or more columns.

The sddssort program allows users to sort SDDS data sets based on specified columns or parameters. It supports various sorting options, including non-dominated sorting, unique row elimination, and numeric character prioritization. The program can handle both regular sorting and non-dominated sorting for multi-criteria optimization problems.

Usage

sddssort [<SDDSinput>] [<SDDSoutput>] [options]

Options

  • -pipe=[input][,output]
  • -column=<name>[,{increasing|decreasing}|{minimize|maximize}][,absolute]...
  • -unique[=count]
  • -nowarnings
  • -parameter=<name>[,{increasing|decreasing}]...
  • -numericHigh
  • -nonDominateSort
  • -majorOrder=row|column
License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday, H. Shang

Definition in file sddssort.c.

Enumeration Type Documentation

◆ option_type

enum option_type

Definition at line 51 of file sddssort.c.

51 {
52 SET_COLUMN,
53 SET_PARAMETER,
54 SET_NOWARNINGS,
55 SET_PIPE,
56 SET_UNIQUE,
57 SET_NUMERICHIGH,
58 SET_NON_DOMINATE_SORT,
59 SET_MAJOR_ORDER,
60 N_OPTIONS
61};

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 129 of file sddssort.c.

129 {
130 SDDS_DATASET SDDS_input, SDDS_output, SDDS_tmp;
131 char *input, *output;
132 long i_arg, non_dominate_sort = 0;
133 SCANNED_ARG *s_arg;
134
135 long tmpfile_used, sort_requests, noWarnings, uniqueRows, provideIdenticalCount, tmpfileForInternalPipe;
136 long sort_parameters;
137 SORT_REQUEST *sort_request, *sort_parameter;
138 unsigned long pipeFlags, majorOrderFlag;
139 short columnMajorOrder = -1;
140
142 argc = scanargs(&s_arg, argc, argv);
143 if (argc < 2) {
144 bomb(NULL, USAGE);
145 }
146
147 input = output = NULL;
148 tmpfile_used = sort_requests = noWarnings = sort_parameters = tmpfileForInternalPipe = 0;
149 sort_request = sort_parameter = NULL;
150 pipeFlags = 0;
151 uniqueRows = provideIdenticalCount = 0;
152 for (i_arg = 1; i_arg < argc; i_arg++) {
153 if (s_arg[i_arg].arg_type == OPTION) {
154 switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) {
155 case SET_MAJOR_ORDER:
156 majorOrderFlag = 0;
157 s_arg[i_arg].n_items--;
158 if (s_arg[i_arg].n_items > 0 &&
159 (!scanItemList(&majorOrderFlag, s_arg[i_arg].list + 1, &s_arg[i_arg].n_items, 0,
160 "row", -1, NULL, 0, SDDS_ROW_MAJOR_ORDER,
161 "column", -1, NULL, 0, SDDS_COLUMN_MAJOR_ORDER, NULL)))
162 SDDS_Bomb("invalid -majorOrder syntax/values");
163 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
164 columnMajorOrder = 1;
165 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
166 columnMajorOrder = 0;
167 break;
168 case SET_NON_DOMINATE_SORT:
169 non_dominate_sort = 1;
170 break;
171 case SET_COLUMN:
172 if (s_arg[i_arg].n_items < 2 || s_arg[i_arg].n_items > 4)
173 SDDS_Bomb("invalid -column syntax");
174 sort_request = trealloc(sort_request, sizeof(*sort_request) * (sort_requests + 1));
175 sort_request[sort_requests].name = s_arg[i_arg].list[1];
176 sort_request[sort_requests].maximize_order = 0;
177 sort_request[sort_requests].decreasing_order = 0;
178 sort_request[sort_requests].absolute = 0;
179 if (s_arg[i_arg].n_items >= 3) {
180 int j;
181 for (j = 2; j < s_arg[i_arg].n_items; j++) {
182 switch (match_string(s_arg[i_arg].list[j], order_mode, 5, 0)) {
183 case 0:
184 break;
185 case 1:
186 sort_request[sort_requests].decreasing_order = 1;
187 break;
188 case 2:
189 break;
190 case 3:
191 sort_request[sort_requests].maximize_order = 1;
192 break;
193 case 4:
194 sort_request[sort_requests].absolute = 1;
195 break;
196 default:
197 fprintf(stderr, "unknown sort order specified--give 'increasing' or 'decreasing' for dominated sorting\n or'maximize' or 'minimize' for non-dominated-sorting.\n");
198 exit(EXIT_FAILURE);
199 break;
200 }
201 }
202 }
203 sort_requests++;
204 break;
205 case SET_PARAMETER:
206 if (s_arg[i_arg].n_items < 2 || s_arg[i_arg].n_items > 3)
207 SDDS_Bomb("invalid -parameter syntax");
208 sort_parameter = trealloc(sort_parameter, sizeof(*sort_parameter) * (sort_parameters + 1));
209 sort_parameter[sort_parameters].name = s_arg[i_arg].list[1];
210 if (s_arg[i_arg].n_items == 3) {
211 if ((sort_parameter[sort_parameters].decreasing_order = match_string(s_arg[i_arg].list[2], order_mode, 2, 0)) < 0)
212 SDDS_Bomb("unknown sort order specified--give 'increasing' or 'decreasing'");
213 } else
214 sort_parameter[sort_parameters].decreasing_order = 0;
215 sort_parameters++;
216 break;
217 case SET_NOWARNINGS:
218 noWarnings = 1;
219 break;
220 case SET_NUMERICHIGH:
221 numericHigh = 1;
222 break;
223 case SET_PIPE:
224 if (!processPipeOption(s_arg[i_arg].list + 1, s_arg[i_arg].n_items - 1, &pipeFlags))
225 SDDS_Bomb("invalid -pipe syntax");
226 break;
227 case SET_UNIQUE:
228 uniqueRows = 1;
229 if (s_arg[i_arg].n_items > 1) {
230 str_tolower(s_arg[i_arg].list[1]);
231 if (s_arg[i_arg].n_items > 2 ||
232 strncmp("count", s_arg[i_arg].list[1], strlen(s_arg[i_arg].list[1])) != 0)
233 SDDS_Bomb("invalid -unique syntax");
234 provideIdenticalCount = 1;
235 }
236 break;
237 default:
238 fprintf(stderr, "error: unknown switch: %s\n", s_arg[i_arg].list[0]);
239 exit(EXIT_FAILURE);
240 break;
241 }
242 } else {
243 if (input == NULL)
244 input = s_arg[i_arg].list[0];
245 else if (output == NULL)
246 output = s_arg[i_arg].list[0];
247 else
248 SDDS_Bomb("too many filenames");
249 }
250 }
251
252 if (!sort_requests && !sort_parameters)
253 SDDS_Bomb("No sorting requests!");
254 processFilenames("sddssort", &input, &output, pipeFlags, noWarnings, &tmpfile_used);
255 if (!SDDS_InitializeInput(&SDDS_input, input)) {
256 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
257 exit(EXIT_FAILURE);
258 }
259 if (sort_requests <= 1)
260 non_dominate_sort = 0;
261
262 if (SDDS_input.layout.popenUsed) {
263 /* SDDS library has opened the file using a command on a pipe, usually for
264 * decompression in the absence of the zlib library.
265 */
266 pid_t pid;
267 char tmpfileName[1024];
268 pid = getpid();
269 sprintf(tmpfileName, "/tmp/sddssort.%ld", (long)pid);
270 tmpfileForInternalPipe = 1;
271 if (!SDDS_InitializeCopy(&SDDS_tmp, &SDDS_input, tmpfileName, "w")) {
272 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
273 exit(EXIT_FAILURE);
274 }
275 if (columnMajorOrder != -1)
276 SDDS_tmp.layout.data_mode.column_major = columnMajorOrder;
277 else
278 SDDS_tmp.layout.data_mode.column_major = SDDS_input.layout.data_mode.column_major;
279 if (non_dominate_sort) {
280 if (!SDDS_DefineSimpleColumn(&SDDS_output, "Rank", NULL, SDDS_LONG) ||
281 !SDDS_DefineSimpleColumn(&SDDS_output, "CrowdingDistance", NULL, SDDS_DOUBLE)) {
282 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
283 exit(EXIT_FAILURE);
284 }
285 if (SDDS_CheckColumn(&SDDS_input, "ConstraintsViolation", NULL, SDDS_ANY_NUMERIC_TYPE, NULL) != SDDS_CHECK_OK) {
286 if (!SDDS_DefineSimpleColumn(&SDDS_output, "ConstraintsViolation", NULL, SDDS_DOUBLE))
287 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
288 constDefined = 1;
289 }
290 }
291
292 if (!SDDS_WriteLayout(&SDDS_tmp)) {
293 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
294 exit(EXIT_FAILURE);
295 }
296 while (SDDS_ReadPage(&SDDS_input) > 0) {
297 if (!SDDS_CopyPage(&SDDS_tmp, &SDDS_input) || !SDDS_WritePage(&SDDS_tmp)) {
298 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
299 exit(EXIT_FAILURE);
300 }
301 }
302 if (!SDDS_Terminate(&SDDS_tmp)) {
303 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
304 exit(EXIT_FAILURE);
305 }
306 if (!SDDS_InitializeInput(&SDDS_input, tmpfileName)) {
307 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
308 exit(EXIT_FAILURE);
309 }
310 }
311 if (!SDDS_InitializeCopy(&SDDS_output, &SDDS_input, output, "w")) {
312 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
313 exit(EXIT_FAILURE);
314 }
315 if (columnMajorOrder != -1)
316 SDDS_output.layout.data_mode.column_major = columnMajorOrder;
317 else
318 SDDS_output.layout.data_mode.column_major = SDDS_input.layout.data_mode.column_major;
319 if (provideIdenticalCount &&
320 !SDDS_DefineSimpleColumn(&SDDS_output, "IdenticalCount", NULL, SDDS_LONG64)) {
321 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
322 exit(EXIT_FAILURE);
323 }
324 if (non_dominate_sort) {
325 if (!SDDS_DefineSimpleColumn(&SDDS_output, "Rank", NULL, SDDS_LONG) ||
326 !SDDS_DefineSimpleColumn(&SDDS_output, "CrowdingDistance", NULL, SDDS_DOUBLE)) {
327 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
328 exit(EXIT_FAILURE);
329 }
330 if (SDDS_CheckColumn(&SDDS_input, "ConstraintsViolation", NULL, SDDS_ANY_NUMERIC_TYPE, NULL) != SDDS_CHECK_OK) {
331 if (!SDDS_DefineSimpleColumn(&SDDS_output, "ConstraintsViolation", NULL, SDDS_DOUBLE))
332 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
333 constDefined = 1;
334 }
335 }
336 if (!SDDS_WriteLayout(&SDDS_output))
337 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
338 if (!SDDS_SortAll(&SDDS_input, &SDDS_output, sort_request, sort_requests, sort_parameter, sort_parameters,
339 uniqueRows, provideIdenticalCount, pipeFlags, non_dominate_sort)) {
340 SDDS_SetError("Problem sorting data");
341 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
342 }
343 if (!SDDS_Terminate(&SDDS_input) || !SDDS_Terminate(&SDDS_output)) {
344 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
345 exit(EXIT_FAILURE);
346 }
347 if (tmpfile_used && !replaceFileAndBackUp(input, output))
348 exit(EXIT_FAILURE);
349 free_scanargs(&s_arg, argc);
350 /* if (tmpfileForInternalPipe)
351 system("rm /tmp/tmpfile"); */
352 return EXIT_SUCCESS;
353}
int32_t SDDS_InitializeCopy(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, char *filename, char *filemode)
Definition SDDS_copy.c:40
int32_t SDDS_CopyPage(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
Definition SDDS_copy.c:578
int32_t SDDS_InitializeInput(SDDS_DATASET *SDDS_dataset, char *filename)
Definition SDDS_input.c:49
int32_t SDDS_Terminate(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_ReadPage(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_DefineSimpleColumn(SDDS_DATASET *SDDS_dataset, const char *name, const char *unit, int32_t type)
Defines a simple data column within the SDDS dataset.
int32_t SDDS_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
Definition SDDS_utils.c:379
int32_t SDDS_CheckColumn(SDDS_DATASET *SDDS_dataset, char *name, char *units, int32_t type, FILE *fp_message)
Checks if a column exists in the SDDS dataset with the specified name, units, and type.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
Definition SDDS_utils.c:432
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
Definition SDDS_utils.c:288
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
Definition SDDS_utils.c:342
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
Definition SDDStypes.h:61
#define SDDS_ANY_NUMERIC_TYPE
Special identifier used by SDDS_Check*() routines to accept any numeric type.
Definition SDDStypes.h:157
#define SDDS_DOUBLE
Identifier for the double data type.
Definition SDDStypes.h:37
#define SDDS_LONG64
Identifier for the signed 64-bit integer data type.
Definition SDDStypes.h:49
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
Definition array.c:181
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
long match_string(char *string, char **option, long n_options, long mode)
Matches a given string against an array of option strings based on specified modes.
long replaceFileAndBackUp(char *file, char *replacement)
Replaces a file with a replacement file and creates a backup of the original.
Definition replacefile.c:75
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
Definition scanargs.c:36
long processPipeOption(char **item, long items, unsigned long *flags)
Definition scanargs.c:356
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
Definition scanargs.c:390
void free_scanargs(SCANNED_ARG **scanned, int argc)
Definition scanargs.c:584
long scanItemList(unsigned long *flags, char **item, long *items, unsigned long mode,...)
Scans a list of items and assigns values based on provided keywords and types.
char * str_tolower(char *s)
Convert a string to lower case.
Definition str_tolower.c:27

◆ read_constr_violation()

double * read_constr_violation ( SDDS_DATASET * SDDS_dataset)

Definition at line 927 of file sddssort.c.

927 {
928 double *const_violation = NULL, **data = NULL;
929 int32_t columns, constraints, j;
930 int64_t i, rows;
931 char **columnName = NULL;
932
933 columns = constraints = 0;
934 rows = SDDS_CountRowsOfInterest(SDDS_dataset);
935 if (!constDefined && !(const_violation = (double *)SDDS_GetColumnInDoubles(SDDS_dataset, "ConstraintsViolation")))
936 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
937 else {
938 if (!(columnName = (char **)SDDS_GetColumnNames(SDDS_dataset, &columns)))
939 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
940 for (i = 0; i < columns; i++) {
941 if (wild_match(columnName[i], "*Constraints*") && strcmp(columnName[i], "ConstraintsViolation") != 0) {
942 data = SDDS_Realloc(data, sizeof(*data) * (constraints + 1));
943 data[constraints] = NULL;
944 if (!(data[constraints] = (double *)SDDS_GetColumnInDoubles(SDDS_dataset, columnName[i])))
945 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
946 constraints++;
947 }
948 }
949 if (constraints) {
950 const_violation = calloc(rows, sizeof(*const_violation));
951 for (i = 0; i < rows; i++) {
952 for (j = 0; j < constraints; j++) {
953 if (data[j][i] < 0)
954 const_violation[i] += data[j][i];
955 }
956 }
957 for (j = 0; j < constraints; j++)
958 free(data[j]);
959 free(data);
960 }
961 }
962 return const_violation;
963}
int64_t SDDS_CountRowsOfInterest(SDDS_DATASET *SDDS_dataset)
Counts the number of rows marked as "of interest" in the current data table.
double * SDDS_GetColumnInDoubles(SDDS_DATASET *SDDS_dataset, char *column_name)
Retrieves the data of a specified numerical column as an array of doubles, considering only rows mark...
char ** SDDS_GetColumnNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all columns in the SDDS dataset.
void * SDDS_Realloc(void *old_ptr, size_t new_size)
Reallocates memory to a new size.
Definition SDDS_utils.c:677
int wild_match(char *string, char *template)
Determine whether one string is a wildcard match for another.
Definition wild_match.c:49

◆ SDDS_CompareData()

int SDDS_CompareData ( SDDS_DATASET * SDDS_dataset,
short type,
short absolute,
void * data1,
void * data2 )

Definition at line 360 of file sddssort.c.

360 {
361 double ldouble_diff;
362 double double_diff;
363 float float_diff;
364 int32_t long_diff;
365 int64_t long64_diff;
366 short short_diff;
367 int char_diff;
368
369 if (!absolute) {
370 switch (type) {
371 case SDDS_LONGDOUBLE:
372 if ((ldouble_diff = *(long double *)data1 - *(long double *)data2) > 0)
373 return (1);
374 else if (ldouble_diff < 0)
375 return (-1);
376 return (0);
377 case SDDS_DOUBLE:
378 if ((double_diff = *(double *)data1 - *(double *)data2) > 0)
379 return (1);
380 else if (double_diff < 0)
381 return (-1);
382 return (0);
383 case SDDS_FLOAT:
384 if ((float_diff = *(float *)data1 - *(float *)data2) > 0)
385 return (1);
386 else if (float_diff < 0)
387 return (-1);
388 return (0);
389 case SDDS_LONG64:
390 if ((long64_diff = *(int64_t *)data1 - *(int64_t *)data2) > 0)
391 return (1);
392 else if (long64_diff < 0)
393 return (-1);
394 return (0);
395 case SDDS_LONG:
396 if ((long_diff = *(int32_t *)data1 - *(int32_t *)data2) > 0)
397 return (1);
398 else if (long_diff < 0)
399 return (-1);
400 return (0);
401 case SDDS_SHORT:
402 if ((short_diff = *(short *)data1 - *(short *)data2) > 0)
403 return (1);
404 else if (short_diff < 0)
405 return (-1);
406 return (0);
407 case SDDS_ULONG64:
408 if (*(uint64_t *)data1 > *(uint64_t *)data2)
409 return 1;
410 if (*(uint64_t *)data1 < *(uint64_t *)data2)
411 return -1;
412 return 0;
413 case SDDS_ULONG:
414 if (*(uint32_t *)data1 > *(uint32_t *)data2)
415 return 1;
416 if (*(uint32_t *)data1 < *(uint32_t *)data2)
417 return -1;
418 return 0;
419 case SDDS_USHORT:
420 if (*(unsigned short *)data1 > *(unsigned short *)data2)
421 return 1;
422 if (*(unsigned short *)data1 < *(unsigned short *)data2)
423 return -1;
424 return 0;
425 case SDDS_CHARACTER:
426 if ((char_diff = *(char *)data1 - *(char *)data2) > 0)
427 return (1);
428 else if (char_diff < 0)
429 return (-1);
430 return (0);
431 case SDDS_STRING:
432 if (numericHigh)
433 return (strcmp_nh(*(char **)data1, *(char **)data2));
434 else
435 return (strcmp(*(char **)data1, *(char **)data2));
436 default:
437 SDDS_SetError("Problem doing data comparison--invalid data type (SDDS_CompareData)");
438 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
439 exit(EXIT_FAILURE);
440 }
441 } else {
442 switch (type) {
443 case SDDS_LONGDOUBLE:
444 if ((ldouble_diff = fabsl(*(long double *)data1) - fabsl(*(long double *)data2)) > 0)
445 return (1);
446 else if (ldouble_diff < 0)
447 return (-1);
448 return (0);
449 case SDDS_DOUBLE:
450 if ((double_diff = fabs(*(double *)data1) - fabs(*(double *)data2)) > 0)
451 return (1);
452 else if (double_diff < 0)
453 return (-1);
454 return (0);
455 case SDDS_FLOAT:
456 if ((float_diff = fabsf(*(float *)data1) - fabsf(*(float *)data2)) > 0)
457 return (1);
458 else if (float_diff < 0)
459 return (-1);
460 return (0);
461 case SDDS_LONG64:
462 if ((long64_diff = llabs(*(int64_t *)data1) - llabs(*(int64_t *)data2)) > 0)
463 return (1);
464 else if (long64_diff < 0)
465 return (-1);
466 return (0);
467 case SDDS_LONG:
468 if ((long_diff = labs(*(int32_t *)data1) - labs(*(int32_t *)data2)) > 0)
469 return (1);
470 else if (long_diff < 0)
471 return (-1);
472 return (0);
473 case SDDS_SHORT:
474 if ((short_diff = abs(*(short *)data1) - abs(*(short *)data2)) > 0)
475 return (1);
476 else if (short_diff < 0)
477 return (-1);
478 return (0);
479 case SDDS_ULONG64:
480 if (*(uint64_t *)data1 > *(uint64_t *)data2)
481 return (1);
482 if (*(uint64_t *)data1 < *(uint64_t *)data2)
483 return (-1);
484 return (0);
485 case SDDS_ULONG:
486 if (*(uint32_t *)data1 > *(uint32_t *)data2)
487 return (1);
488 if (*(uint32_t *)data1 < *(uint32_t *)data2)
489 return (-1);
490 return (0);
491 case SDDS_USHORT:
492 if (*(unsigned short *)data1 > *(unsigned short *)data2)
493 return (1);
494 if (*(unsigned short *)data1 < *(unsigned short *)data2)
495 return (-1);
496 return (0);
497 case SDDS_CHARACTER:
498 if ((char_diff = *(char *)data1 - *(char *)data2) > 0)
499 return (1);
500 else if (char_diff < 0)
501 return (-1);
502 return (0);
503 case SDDS_STRING:
504 if (numericHigh)
505 return (strcmp_nh(*(char **)data1, *(char **)data2));
506 else
507 return (strcmp(*(char **)data1, *(char **)data2));
508 default:
509 SDDS_SetError("Problem doing data comparison--invalid data type (SDDS_CompareData)");
510 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
511 exit(EXIT_FAILURE);
512 }
513 }
514}
#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_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_LONGDOUBLE
Identifier for the long double data type.
Definition SDDStypes.h:31
int strcmp_nh(const char *s1, const char *s2)
Compare two strings with a custom non-hierarchical ranking.
Definition wild_match.c:583

◆ SDDS_ComparePages()

int SDDS_ComparePages ( const void * vpage1,
const void * vpage2 )

Definition at line 753 of file sddssort.c.

753 {
754 /* I'm assuming that a double is large enough to store any of the data types,
755 * including a pointer
756 */
757 static int32_t page1, page2;
758 long i;
759 int comparison;
760
761 page1 = *(int32_t *)vpage1;
762 page2 = *(int32_t *)vpage2;
763
764 for (i = 0; i < sort_parameters; i++) {
765 if ((comparison = SDDS_CompareData(SDDS_sortpage, sort_parameter[i].type, sort_parameter[i].absolute,
766 (void *)&(sort_parameter[i].data[page1]), (void *)&(sort_parameter[i].data[page2])))) {
767 /* if (sort_parameter[i].type==SDDS_STRING) {
768 * free(*((char**)&(sort_parameter[i].data[page1])));
769 * free(*((char**)&(sort_parameter[i].data[page2])));
770 * } */
771 return (sort_parameter[i].decreasing_order ? -comparison : comparison);
772 }
773 /* if (sort_parameter[i].type==SDDS_STRING) {
774 * free(*((char**)&(sort_parameter[i].data[page1])));
775 * free(*((char**)&(sort_parameter[i].data[page2])));
776 * } */
777 }
778 return (0);
779}

◆ SDDS_CompareRows()

int SDDS_CompareRows ( const void * vrow1,
const void * vrow2 )

Definition at line 516 of file sddssort.c.

516 {
517 /* I'm assuming that a double is large enough to store any of the data types,
518 * including a pointer
519 */
520 static double data1, data2;
521 static int64_t row1, row2;
522 long i;
523 int comparison;
524 void *p1, *p2;
525 row1 = *(int64_t *)vrow1;
526 row2 = *(int64_t *)vrow2;
527
528 for (i = 0; i < sort_requests; i++) {
529 if (!SDDS_GetValueByAbsIndex(SDDS_sort, sort_request[i].index, row1, &data1) ||
530 !SDDS_GetValueByAbsIndex(SDDS_sort, sort_request[i].index, row2, &data2)) {
531 SDDS_SetError("Problem getting value for sort (SDDS_CompareRows)");
532 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
533 exit(EXIT_FAILURE);
534 }
535 if ((comparison = SDDS_CompareData(SDDS_sort, sort_request[i].type, sort_request[i].absolute,
536 (void *)&data1, (void *)&data2))) {
537 if (sort_request[i].type == SDDS_STRING) {
538 p1 = &data1;
539 p2 = &data2;
540 free(*((char **)p1));
541 free(*((char **)p2));
542 // free(*((char**)&data1));
543 // free(*((char**)&data2));
544 }
545 return (sort_request[i].decreasing_order ? -comparison : comparison);
546 }
547 if (sort_request[i].type == SDDS_STRING) {
548 p1 = &data1;
549 p2 = &data2;
550 free(*((char **)p1));
551 free(*((char **)p2));
552 // free(*((char**)&data1));
553 // free(*((char**)&data2));
554 }
555 }
556 return (0);
557}
void * SDDS_GetValueByAbsIndex(SDDS_DATASET *SDDS_dataset, int32_t column_index, int64_t row_index, void *memory)
Retrieves the value from a specified column and absolute row index, optionally storing it in provided...

◆ SDDS_SortAll()

long SDDS_SortAll ( SDDS_DATASET * SDDS_input,
SDDS_DATASET * SDDS_output,
SORT_REQUEST * xsort_request,
long xsort_requests,
SORT_REQUEST * xsort_parameter,
long xsort_parameters,
long uniqueRows,
long provideIdenticalCount,
long pipeFlags,
long non_dominate_sort )

Definition at line 781 of file sddssort.c.

783 {
784 long i, j, k, pages;
785 int32_t *xsort_page_index;
786 char s[1024];
787 SDDS_DATASET *tmp_datasets;
788
789 tmp_datasets = NULL;
790 SDDS_sortpage = SDDS_inputx;
791 sort_parameter = xsort_parameter;
792 sort_parameters = xsort_parameters;
793 sort_request = xsort_request;
794 sort_requests = xsort_requests;
795 xsort_page_index = NULL;
796 pages = i = j = k = 0;
797 for (i = 0; i < sort_parameters; i++) {
798 if ((sort_parameter[i].index = SDDS_GetParameterIndex(SDDS_sortpage, sort_parameter[i].name)) < 0) {
799 sprintf(s, "Unable to get parameter value--parameter name \"%s\" is not recognized(SDDS_GetParameterIndex)", sort_parameter[i].name);
800 SDDS_SetError(s);
801 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
802 return (0);
803 }
804 sort_parameter[i].type = SDDS_GetParameterType(SDDS_sortpage, sort_parameter[i].index);
805 }
806 if (sort_parameters) {
807 if (xpipeFlags == 0 && !SDDS_sortpage->layout.gzipFile)
809 for (i = 0; i < sort_parameters; i++)
810 sort_parameter[i].data = NULL;
811 while (SDDS_ReadPage(SDDS_sortpage) > 0) {
812 if (xpipeFlags || SDDS_sortpage->layout.gzipFile) {
813 /* copy each page of sortpage into tmp_datasets indexed by the pages, i.e.
814 * tmp_datasets[pages] contains only one page */
815 tmp_datasets = realloc(tmp_datasets, sizeof(*tmp_datasets) * (pages + 1));
816 if (!SDDS_InitializeCopy(&tmp_datasets[pages], SDDS_sortpage, NULL, "m")) {
817 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
818 return (0);
819 }
820 if (!SDDS_CopyPage(&tmp_datasets[pages], SDDS_sortpage)) {
821 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
822 return (0);
823 }
824 }
825 for (i = 0; i < sort_parameters; i++) {
826 sort_parameter[i].data = realloc(sort_parameter[i].data, sizeof(*sort_parameter[i].data) * (pages + 1));
827
828 if (!SDDS_GetParameterByIndex(SDDS_sortpage, sort_parameter[i].index, &(sort_parameter[i].data[pages]))) {
829 SDDS_SetError("Problem getting parameter value for sort (SDDS_SortAll)");
830 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
831 exit(EXIT_FAILURE);
832 }
833 }
834 pages++;
835 }
836 /* sort pages by parameter */
837 xsort_page_index = tmalloc(sizeof(*xsort_page_index) * pages);
838 for (k = 0; k < pages; k++)
839 xsort_page_index[k] = k;
840 if (pages > 1)
841 qsort((void *)xsort_page_index, pages, sizeof(*xsort_page_index), SDDS_ComparePages);
842
843 /* sort_page_index=xsort_page_index; */
844 for (i = 0; i < pages; i++) {
845 j = xsort_page_index[i];
846 if (xpipeFlags || SDDS_sortpage->layout.gzipFile) {
847 if (!SDDS_CopyPage(SDDS_outputx, &tmp_datasets[j])) {
848 SDDS_SetError("Problem copying data from memory");
849 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
850 exit(EXIT_FAILURE);
851 }
852 if (!SDDS_Terminate(&tmp_datasets[j])) {
853 SDDS_SetError("Problem terminate datasets");
854 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
855 exit(EXIT_FAILURE);
856 }
857 } else {
858 if (!SDDS_GotoPage(SDDS_sortpage, j + 1)) {
859 SDDS_SetError("Problem goto page");
860 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
861 exit(EXIT_FAILURE);
862 }
863 if (SDDS_ReadPage(SDDS_sortpage) < 1) {
864 SDDS_SetError("Problem read page");
865 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
866 exit(EXIT_FAILURE);
867 }
868 if (!SDDS_CopyPage(SDDS_outputx, SDDS_sortpage)) {
869 SDDS_SetError("Problem copying data");
870 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
871 exit(EXIT_FAILURE);
872 }
873 }
874 if (sort_requests) {
875 if (SDDS_CountRowsOfInterest(SDDS_outputx) > 0) {
876 if (!SDDS_SortRows(SDDS_outputx, sort_request, sort_requests, non_dominate_sort)) {
877 SDDS_SetError("Problem performing sort");
878 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
879 exit(EXIT_FAILURE);
880 }
881 if (uniqueRows && !SDDS_UnsetDuplicateRows(SDDS_outputx, sort_request, sort_requests, provideIdenticalCount)) {
882 SDDS_SetError("Problem marking duplicate rows.");
883 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
884 exit(EXIT_FAILURE);
885 }
886 }
887 }
888 if (!SDDS_WritePage(SDDS_outputx)) {
889 SDDS_SetError("Problem writing data to output file");
890 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
891 exit(EXIT_FAILURE);
892 }
893 }
894 } else {
895 while (SDDS_ReadPage(SDDS_inputx) > 0) {
896 if (!SDDS_CopyPage(SDDS_outputx, SDDS_inputx)) {
897 SDDS_SetError("Problem copying data for output file");
898 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
899 exit(EXIT_FAILURE);
900 }
901 if (SDDS_CountRowsOfInterest(SDDS_outputx) > 0) {
902 if (!SDDS_SortRows(SDDS_outputx, sort_request, sort_requests, non_dominate_sort)) {
903 SDDS_SetError("Problem performing sort");
904 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
905 exit(EXIT_FAILURE);
906 }
907 if (uniqueRows && !SDDS_UnsetDuplicateRows(SDDS_outputx, sort_request, sort_requests, provideIdenticalCount)) {
908 SDDS_SetError("Problem marking duplicate rows.");
909 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
910 exit(EXIT_FAILURE);
911 }
912 }
913 if (!SDDS_WritePage(SDDS_outputx)) {
914 SDDS_SetError("Problem writing data to output file");
915 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
916 exit(EXIT_FAILURE);
917 }
918 }
919 }
920 if (tmp_datasets)
921 free(tmp_datasets);
922 if (xsort_page_index)
923 free(xsort_page_index);
924 return 1;
925}
int32_t SDDS_SetDefaultIOBufferSize(int32_t newValue)
Definition SDDS_binary.c:66
void * SDDS_GetParameterByIndex(SDDS_DATASET *SDDS_dataset, int32_t index, void *memory)
Retrieves the value of a specified parameter by its index from the current data table of a data set.
int32_t SDDS_GotoPage(SDDS_DATASET *SDDS_dataset, int32_t page_number)
Sets the current page of the SDDS dataset to the specified page number.
int32_t SDDS_GetParameterType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of a parameter in the SDDS dataset by its index.
int32_t SDDS_GetParameterIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named parameter in the SDDS dataset.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:59

◆ SDDS_SortRows()

long SDDS_SortRows ( SDDS_DATASET * SDDS_dataset,
SORT_REQUEST * xsort_request,
long xsort_requests,
long non_dominate_sort )

Definition at line 584 of file sddssort.c.

584 {
585 int64_t i, j, k, rows;
586 int64_t *row_location;
587 char s[1024];
588 double **data = NULL, *dist = NULL, *const_violation = NULL;
589 int32_t *rank = NULL;
590 population pop;
591 long *maximize = NULL;
592
593 SDDS_sort = SDDS_dataset;
594 sort_request = xsort_request;
595 sort_requests = xsort_requests;
596 if ((rows = SDDS_CountRowsOfInterest(SDDS_sort)) < 0)
597 return (0);
598
599 for (i = 0; i < sort_requests; i++) {
600 if ((sort_request[i].index = SDDS_GetColumnIndex(SDDS_sort, sort_request[i].name)) < 0) {
601 sprintf(s, "column name \"%s\" is not recognized(SDDS_GetColumnIndex)", sort_request[i].name);
602 SDDS_SetError(s);
603 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
604 return (0);
605 }
606 sort_request[i].type = SDDS_GetColumnType(SDDS_sort, sort_request[i].index);
607 }
608 if (non_dominate_sort) {
609 data = (double **)malloc(sizeof(*data) * sort_requests);
610 maximize = (long *)malloc(sizeof(*maximize) * sort_requests);
611 for (i = 0; i < sort_requests; i++) {
612 if (sort_request[i].type == SDDS_STRING) {
613 fprintf(stderr, "Non-dominated sort is not available for string column.\n");
614 exit(EXIT_FAILURE);
615 }
616 if (!(data[i] = (double *)SDDS_GetColumnInDoubles(SDDS_sort, sort_request[i].name))) {
617 SDDS_SetError("Problem performing sort");
618 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
619 exit(EXIT_FAILURE);
620 }
621 maximize[i] = sort_request[i].maximize_order;
622 }
623 const_violation = read_constr_violation(SDDS_sort);
624 fill_population(&pop, rows, sort_requests, data, maximize, const_violation);
625 sort_row_index = non_dominated_sort(&pop);
626 rank = (int32_t *)malloc(sizeof(*rank) * rows);
627 dist = (double *)malloc(sizeof(*dist) * rows);
628 if (!const_violation)
629 const_violation = calloc(rows, sizeof(*const_violation));
630 for (i = 0; i < rows; i++) {
631 rank[i] = pop.ind[sort_row_index[i]].rank;
632 dist[i] = pop.ind[sort_row_index[i]].crowd_dist;
633 const_violation[i] = pop.ind[sort_row_index[i]].constr_violation;
634 }
635 free_pop_mem(&pop);
636 for (i = 0; i < sort_requests; i++)
637 free(data[i]);
638 free(data);
639 free(maximize);
640 } else {
641 sort_row_index = tmalloc(sizeof(*sort_row_index) * rows);
642 for (i = 0; i < rows; i++)
643 sort_row_index[i] = i;
644 /* After this sort, sort_row_index will contain the indices of the rows
645 * in sorted order
646 */
647 qsort((void *)sort_row_index, rows, sizeof(*sort_row_index), SDDS_CompareRows);
648
649 /* create an array to give the location in the sort_row_index array of
650 * a particular row
651 */
652#if defined(DEBUG)
653 fprintf(stderr, "new row order:\n");
654 for (i = 0; i < rows; i++)
655 fprintf(stderr, "%" PRId64 " %" PRId64 "\n", i, sort_row_index[i]);
656 fprintf(stderr, "\n");
657#endif
658 }
659 row_location = tmalloc(sizeof(*sort_row_index) * rows);
660 for (i = 0; i < rows; i++)
661 row_location[sort_row_index[i]] = i;
662 for (i = 0; i < rows; i++) {
663 if ((j = sort_row_index[i]) != i) {
664 /* move this row from position i to position j, where it belongs */
665 if (!SDDS_SwapRows(SDDS_sort, i, j)) {
666 SDDS_SetError("Problem swapping rows after index sort (SDDS_SortRows");
667 SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors | SDDS_VERBOSE_PrintErrors);
668 exit(EXIT_FAILURE);
669 }
670 /* adjust the indices to reflect the swap */
671 sort_row_index[i] = i;
672 k = row_location[i];
673 row_location[i] = i;
674 sort_row_index[k] = j;
675 row_location[j] = k;
676 }
677#if defined(DEBUG)
678 fprintf(stderr, "new row order:\n");
679 for (j = 0; j < rows; j++)
680 fprintf(stderr, "%" PRId64 " %" PRId64 "\n", j, sort_row_index[j]);
681 fprintf(stderr, "\n");
682#endif
683 }
684 if (non_dominate_sort) {
685 if (SDDS_SetColumn(SDDS_sort, SDDS_SET_BY_NAME, rank, rows, "Rank", NULL) != 1 ||
686 SDDS_SetColumn(SDDS_sort, SDDS_SET_BY_NAME, dist, rows, "CrowdingDistance", NULL) != 1 ||
687 SDDS_SetColumn(SDDS_sort, SDDS_SET_BY_NAME, const_violation, rows, "ConstraintsViolation", NULL) != 1) {
688 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
689 exit(EXIT_FAILURE);
690 }
691 free(rank);
692 free(dist);
693 free(const_violation);
694 }
695 free(sort_row_index);
696 free(row_location);
697 return 1;
698}
int32_t SDDS_SetColumn(SDDS_DATASET *SDDS_dataset, int32_t mode, void *data, int64_t rows,...)
Sets the values for one data column in the current data table of an SDDS dataset.
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column in the SDDS dataset.
int32_t SDDS_GetColumnType(SDDS_DATASET *SDDS_dataset, int32_t index)
Retrieves the data type of a column in the SDDS dataset by its index.
int64_t * non_dominated_sort(population *new_pop)
Performs non-dominated sorting on a population and assigns ranks and crowding distances.
void fill_population(population *pop, long rows, long columns, double **columnValue, long *maximize, double *const_violation)
Initializes and fills the population structure with individuals and their objective values.
void free_pop_mem(population *pop)
Frees all dynamically allocated memory associated with a population.

◆ SDDS_SwapRows()

long SDDS_SwapRows ( SDDS_DATASET * SDDS_dataset,
int64_t row1,
int64_t row2 )

Definition at line 559 of file sddssort.c.

559 {
560#define SWAP_BUFFER_SIZE 16
561 static char buffer[SWAP_BUFFER_SIZE];
562 void **data;
563 long i, size;
564 data = SDDS_dataset->data;
565#if defined(DEBUG)
566 fprintf(stderr, "swapping row %" PRId64 " with row %" PRId64 "\n", row1, row2);
567#endif
568 for (i = 0; i < SDDS_dataset->layout.n_columns; i++) {
569 if ((size = SDDS_GetTypeSize(SDDS_dataset->layout.column_definition[i].type)) > SWAP_BUFFER_SIZE) {
570 SDDS_SetError("Unable to swap rows--swap buffer is too small (SDDS_SwapRows)");
571 return (0);
572 }
573#if defined(DEBUG)
574 if (SDDS_dataset->layout.column_definition[i].type == SDDS_STRING)
575 fprintf(stderr, " %s <--> %s\n", *(char **)((char *)(data[i]) + row1 * size), *(char **)((char *)(data[i]) + row2 * size));
576#endif
577 memcpy((char *)buffer, (char *)(data[i]) + row1 * size, size);
578 memcpy((char *)(data[i]) + row1 * size, (char *)(data[i]) + row2 * size, size);
579 memcpy((char *)(data[i]) + row2 * size, (char *)buffer, size);
580 }
581 return (1);
582}
int32_t SDDS_GetTypeSize(int32_t type)
Retrieves the size in bytes of a specified SDDS data type.

◆ SDDS_UnsetDuplicateRows()

long SDDS_UnsetDuplicateRows ( SDDS_DATASET * SDDS_dataset,
SORT_REQUEST * xsort_request,
long xsort_requests,
long provideIdenticalCount )

Definition at line 700 of file sddssort.c.

700 {
701 int64_t rows, i, j;
702 int64_t *identicalCount;
703 int32_t *rowFlag;
704 char s[1024];
705
706 SDDS_sort = SDDS_dataset;
707 sort_request = xsort_request;
708 sort_requests = xsort_requests;
709
710 for (i = 0; i < sort_requests; i++) {
711 if ((sort_request[i].index = SDDS_GetColumnIndex(SDDS_sort, sort_request[i].name)) < 0) {
712 sprintf(s, "column name \"%s\" is not recognized(SDDS_GetColumnIndex)", sort_request[i].name);
713 SDDS_SetError(s);
714 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
715 return 0;
716 }
717 sort_request[i].type = SDDS_GetColumnType(SDDS_sort, sort_request[i].index);
718 }
719
720 if ((rows = SDDS_CountRowsOfInterest(SDDS_sort)) < 0)
721 return (0);
722
723 rowFlag = tmalloc(sizeof(*rowFlag) * rows);
724 identicalCount = tmalloc(sizeof(*identicalCount) * rows);
725 for (i = 0; i < rows; i++)
726 rowFlag[i] = identicalCount[i] = 1;
727
728 for (i = 0; i < rows - 1; i++) {
729 if (!rowFlag[i])
730 continue;
731 for (j = i + 1; j < rows; j++) {
732 if (rowFlag[j]) {
733 if (SDDS_CompareRows(&i, &j) == 0) {
734 identicalCount[i] += 1;
735 rowFlag[j] = 0;
736 } else
737 break;
738 }
739 }
740 }
741 if (!SDDS_AssertRowFlags(SDDS_sort, SDDS_FLAG_ARRAY, rowFlag, rows))
742 return 0;
743 if (provideIdenticalCount && !SDDS_SetColumn(SDDS_sort, SDDS_SET_BY_NAME, identicalCount, rows, "IdenticalCount"))
744 return 0;
745 free(rowFlag);
746 return 1;
747}
int32_t SDDS_AssertRowFlags(SDDS_DATASET *SDDS_dataset, uint32_t mode,...)
Sets acceptance flags for rows based on specified criteria.

Variable Documentation

◆ constDefined

long constDefined = 0

Definition at line 127 of file sddssort.c.

◆ numericHigh

long numericHigh = 0

Definition at line 127 of file sddssort.c.

◆ option

char* option[N_OPTIONS]
Initial value:
= {
"column",
"parameter",
"nowarnings",
"pipe",
"unique",
"numerichigh",
"nonDominateSort",
"majorOrder",
}

Definition at line 63 of file sddssort.c.

63 {
64 "column",
65 "parameter",
66 "nowarnings",
67 "pipe",
68 "unique",
69 "numerichigh",
70 "nonDominateSort",
71 "majorOrder",
72};

◆ order_mode

char* order_mode[5]
static
Initial value:
= {
"increasing",
"decreasing",
"minimize",
"maximize",
"absolute",
}

Definition at line 110 of file sddssort.c.

110 {
111 "increasing",
112 "decreasing",
113 "minimize",
114 "maximize",
115 "absolute",
116};

◆ SDDS_sort

SDDS_DATASET* SDDS_sort
static

Definition at line 355 of file sddssort.c.

◆ SDDS_sortpage

SDDS_DATASET* SDDS_sortpage
static

Definition at line 751 of file sddssort.c.

◆ sort_parameter

SORT_REQUEST* sort_parameter
static

Definition at line 749 of file sddssort.c.

◆ sort_parameters

long sort_parameters
static

Definition at line 750 of file sddssort.c.

◆ sort_request

SORT_REQUEST* sort_request
static

Definition at line 356 of file sddssort.c.

◆ sort_requests

long sort_requests
static

Definition at line 357 of file sddssort.c.

◆ sort_row_index

int64_t* sort_row_index
static

Definition at line 358 of file sddssort.c.

◆ USAGE

char* USAGE
Initial value:
=
"Usage: sddssort [<SDDSinput>] [<SDDSoutput>] [options]\n\n"
"Options:\n"
" -pipe=[input][,output]\n"
" Enable piping for input and/or output.\n\n"
" -column=<name>[,{increasing|decreasing}|{minimize|maximize}][,absolute]...\n"
" Specify one or more columns to sort by.\n"
" - 'increasing' or 'decreasing' sets the sorting direction for regular sorting.\n"
" - 'minimize' or 'maximize' sets the sorting direction for non-dominated sorting.\n"
" - 'absolute' sorts based on absolute values.\n\n"
" -unique[=count]\n"
" Eliminate duplicate rows based on sort columns.\n"
" If 'count' is specified, an 'IdenticalCount' column is added to indicate the number of identical rows.\n\n"
" -nowarnings\n"
" Suppress warning messages.\n\n"
" -parameter=<name>[,{increasing|decreasing}]...\n"
" Specify parameters to sort by.\n\n"
" -numericHigh\n"
" Prioritize numeric characters over other characters in string comparisons.\n"
" Also ranks numeric character sets with fewer characters below those with more characters.\n\n"
" -nonDominateSort\n"
" Perform non-dominated sorting when multiple sort columns are provided.\n"
" Note: Non-dominated sorting only works for numeric columns.\n\n"
" -majorOrder=row|column\n"
" Set the major order for data storage, either row-major or column-major.\n\n"
"Program by Michael Borland. (" __DATE__ " " __TIME__ ", SVN revision: " SVN_VERSION ")\n"

Definition at line 75 of file sddssort.c.