/
table.c
599 lines (579 loc) · 15.8 KB
/
table.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
* table.c
*
* Copyright 2019 Ryan Koehler, VerdAscend Sciences, ryan@verdascend.com
*
* The programs and source code of the vertools collection are free software.
* They are distributed in the hope that they will be useful,
* WITHOUT ANY WARRANTY OF FITNESS FOR ANY PARTICULAR PURPOSE.
*
* Permission is granted for research, educational, and possibly commercial use
* and modification as long as 1) Code and any derived works are not
* redistributed for any fee, and 2) Proper credit is given to the authors.
* If you wish to include this software in a product, or use it commercially,
* please contact the authors.
*
* See https://www.verdascend.com/ for more
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "prim.h"
#include "table.h"
#define DB_TAB if(DB[70])
#define DB_TAB_LO if(DB[71])
/**************************************************************************
* Set value precision limit for table
*/
int SetTablePrecisionI(TABLE *tabPO, int prenum)
{
int i,r,c;
DOUB mD,vD;
DB_TAB DB_PrI(">> SetTablePrecisionI %p prenum=%d\n",tabPO,prenum);
VALIDATE(tabPO,TABLE_ID);
if( (prenum>MAX_TAB_PRENUM) || (prenum<MIN_TAB_PRENUM) ) {
printf("Table precision limited %d to %d digits (after decimal)\n",
MIN_TAB_PRENUM, MAX_TAB_PRENUM);
printf("Value %d won't work; ignored\n",prenum);
return(FALSE);
}
tabPO->prenum = prenum;
tabPO->prefac = 1.0;
/***
* Now set working values
*/
if(prenum<0) {
mD = 0.1;
}
else {
mD = 10.0;
}
/***
* Now set working values by incrementing n steps
*/
i = 0;
while(i<ABS_VAL(tabPO->prenum)) {
tabPO->prefac *= mD;
i++;
}
DB_TAB DB_PrI("+ prefac=%1.12f\n",tabPO->prefac);
/***
* Update all values with new settings
*/
for(r=0;r<tabPO->nrow;r++)
{
for(c=0;c<tabPO->ncol;c++)
{
GetTableValI(tabPO,r,c,&vD);
SetTableValPrecisionI(tabPO, vD, &vD);
SetTableValI(tabPO,r,c,vD);
}
}
DB_TAB DB_PrI("<< SetTablePrecisionI %p TRUE\n",tabPO);
return(TRUE);
}
/***************************************************************************
* Limits precision of val based on table settings and sets via pointer
* new value
*/
int SetTableValPrecisionI(TABLE *tabPO, DOUB valD, DOUB *newPD)
{
DOUB intD,fracD;
DB_TAB_LO DB_PrI(">> SetTableValPrecisionI val=%1.12f\n",valD);
VALIDATE(tabPO,TABLE_ID);
DB_TAB_LO DB_PrI("+ prenum=%d prefac=%1.12f\n",tabPO->prenum,tabPO->prefac);
/***
* Only mess with the fractional part
*/
fracD = modf(valD,&intD);
fracD = DNUM( ROUND(fracD * tabPO->prefac) ) / tabPO->prefac;
*newPD = intD + fracD;
DB_TAB_LO DB_PrI("<< SetTableValPrecisionI new=%1.12f TRUE\n",*newPD);
return(TRUE);
}
/**************************************************************************
* Same dimension tables?
*/
int SameDimTablesI(TABLE *tabPO, TABLE *stabPO, int mask,int error)
{
int ok;
ok = ( SameDimTableRowsI(tabPO, stabPO, mask, error) &&
SameDimTableColsI(tabPO, stabPO, mask, error) );
return(ok);
}
/**************************************************************************/
int SameDimTableRowsI(TABLE *tabPO, TABLE *stabPO, int mask,int error)
{
int fr,sr;
VALIDATE(tabPO,TABLE_ID);
VALIDATE(stabPO,TABLE_ID);
fr = GetTableRowsI(tabPO,mask);
sr = GetTableRowsI(stabPO,mask);
if( fr != sr ) {
if(error) {
printf("Table ROW COUNTS DIFFER!!!\n");
printf(" First = %s = %d\n",tabPO->name,fr);
printf(" Second = %s = %d\n",stabPO->name,sr);
}
return(FALSE);
}
return(TRUE);
}
/**************************************************************************/
int SameDimTableColsI(TABLE *tabPO, TABLE *stabPO, int mask,int error)
{
int fc,sc;
VALIDATE(tabPO,TABLE_ID);
VALIDATE(stabPO,TABLE_ID);
fc = GetTableColsI(tabPO,mask);
sc = GetTableColsI(stabPO,mask);
if( fc != sc ) {
if(error) {
printf("Table COLUMN COUNTS DIFFER!!!\n");
printf(" First = %s = %d\n",tabPO->name,fc);
printf(" Second = %s = %d\n",stabPO->name,sc);
}
return(FALSE);
}
return(TRUE);
}
/**************************************************************************
* Is table a square matrix?
*/
int IsTableSquareI(TABLE *tabPO, int mask)
{
int r,c,sq;
r = GetTableColsI(tabPO,mask);
c = GetTableColsI(tabPO,mask);
sq = (r == c) ? TRUE : FALSE;
return(sq);
}
/**************************************************************************
* Set masking for row in table
*/
int SetTableRowMaskI(TABLE *tabPO,int row,int what)
{
VALIDATE(tabPO,TABLE_ID);
if( (row>=0) && (row<tabPO->nrow) ) {
if(what)
{ tabPO->rmask[row] = TRUE; }
else
{ tabPO->rmask[row] = FALSE; }
return(TRUE);
}
return(FALSE);
}
/**************************************************************************
* Set masking for col in table
*/
int SetTableColMaskI(TABLE *tabPO,int col,int what)
{
VALIDATE(tabPO,TABLE_ID);
if( (col>=0) && (col<tabPO->ncol) )
{
if(what)
{ tabPO->cmask[col] = TRUE; }
else
{ tabPO->cmask[col] = FALSE; }
return(TRUE);
}
return(FALSE);
}
/**************************************************************************
* Set masking for range of rows, start to end (exclusive)
*/
int SetTableRowRangeMaskI(TABLE *tabPO,int srow, int erow, int what)
{
int i,any;
any = 0;
for(i=srow; i<erow; i++)
{
if(SetTableRowMaskI(tabPO,i,what)) {
any++;
}
}
return(any);
}
/**************************************************************************
* Set masking for range of cols, start to end (exclusive)
*/
int SetTableColRangeMaskI(TABLE *tabPO,int scol, int ecol, int what)
{
int i,any;
any = 0;
for(i=scol; i<ecol; i++)
{
if(SetTableColMaskI(tabPO,i,what)) {
any++;
}
}
return(any);
}
/**************************************************************************
* Get masking status for row in table
*/
int GetTableRowMaskI(TABLE *tabPO,int row)
{
VALIDATE(tabPO,TABLE_ID);
if( (row>=0) && (row<tabPO->nrow) ) {
return(tabPO->rmask[row]);
}
return(BOGUS);
}
/**************************************************************************
* Get masking status for col in table
*/
int GetTableColMaskI(TABLE *tabPO,int col)
{
VALIDATE(tabPO,TABLE_ID);
if( (col>=0) && (col<tabPO->ncol) ) {
return(tabPO->cmask[col]);
}
return(BOGUS);
}
/*************************************************************************/
int GetTableDimsI(TABLE *tabPO, int *rowPI, int *colPI, int mask)
{
VALIDATE(tabPO,TABLE_ID);
if(rowPI) {
*rowPI = GetTableRowsI(tabPO,mask);
}
if(colPI) {
*colPI = GetTableColsI(tabPO,mask);
}
return(TRUE);
}
/**************************************************************************
* How many rows in table? If mask is true, only masked rows are counted
*/
int GetTableRowsI(TABLE *tabPO,int mask)
{
int r;
VALIDATE(tabPO,TABLE_ID);
if(mask) {
r = MaskCountI(tabPO->rmask,tabPO->nrow);
}
else {
r = tabPO->nrow;
}
return(r);
}
/**************************************************************************
* How many cols in table? If mask is true, only masked cols are counted
*/
int GetTableColsI(TABLE *tabPO,int mask)
{
int c;
VALIDATE(tabPO,TABLE_ID);
if(mask) {
c = MaskCountI(tabPO->cmask,tabPO->ncol);
}
else {
c = tabPO->ncol;
}
return(c);
}
/**************************************************************************
* Set the name of the table
*/
int SetTableNamesI(TABLE *tabPO, char *nameS, char *fnameS, int max)
{
int n;
VALIDATE(tabPO,TABLE_ID);
if(nameS){
n = (max < 0) ? strlen(nameS) : max;
LIMIT_NUM(n,0,NSIZE);
strncpy(tabPO->name,nameS,n);
tabPO->name[n] = '\0';
}
if(fnameS) {
n = (max < 0) ? strlen(fnameS) : max;
LIMIT_NUM(n,0,NSIZE);
strncpy(tabPO->fname,fnameS,n);
tabPO->fname[n] = '\0';
}
return(TRUE);
}
/**************************************************************************
* Copy name of table into nameS up to max
*/
int GetTableNamesI(TABLE *tabPO, char *nameS, char *fnameS, int max)
{
int n;
VALIDATE(tabPO,TABLE_ID);
if(nameS) {
n = (max < 0) ? strlen(nameS) : max;
LIMIT_NUM(n,0,NSIZE);
strncpy(nameS,tabPO->name,n);
nameS[n] = '\0';
}
if(fnameS) {
n = (max < 0) ? strlen(fnameS) : max;
LIMIT_NUM(n,0,NSIZE);
strncpy(fnameS,tabPO->fname,n);
fnameS[n] = '\0';
}
return(TRUE);
}
/**************************************************************************
* Find named row
*/
int FindTableNamedRowI(TABLE *tabPO, int off, char *nameS, int kc, int nc)
{
int i,r;
char rowS[NSIZE];
r = BOGUS;
/***
* No row labels then no chance
*/
if(!tabPO->rlab) {
return(r);
}
/***
* Each row, after offset
*/
LIMIT_NUM(off,0,tabPO->nrow);
for(i=off; i<tabPO->nrow; i++) {
GetTableRowLabI(tabPO,i,rowS,-1);
if(kc) {
if( ((nc>0) && (!strncmp(rowS,nameS,nc))) || (!strcmp(rowS,nameS)) ) {
r = i;
break;
}
}
else {
if( ((nc>0) && (!strcasecmp(rowS,nameS))) || (!strcasecmp(rowS,nameS)) ) {
r = i;
break;
}
}
}
return(r);
}
/**************************************************************************
* Find named col
*/
int FindTableNamedColI(TABLE *tabPO, int off, char *nameS, int kc, int nc)
{
int i,c;
char colS[NSIZE];
c = BOGUS;
/***
* No col labels then no chance
*/
if(!tabPO->clab) {
return(c);
}
/***
* Each row, after offset
*/
LIMIT_NUM(off,0,tabPO->ncol);
for(i=off; i<tabPO->ncol; i++) {
GetTableColLabI(tabPO,i,colS,NSIZE-1);
if(kc) {
if( ((nc>0) && (!strncmp(colS,nameS,nc))) || (!strcmp(colS,nameS)) ) {
c = i;
break;
}
}
else {
if( ((nc>0) && (!strcasecmp(colS,nameS))) || (!strcasecmp(colS,nameS)) ) {
c = i;
break;
}
}
}
return(c);
}
/*************************************************************************/
int AutoTableOutFormattingI(TABLE *tabPO, int val, int row)
{
char valS[DEF_BS], val2S[DEF_BS], rowS[DEF_BS];
VALIDATE(tabPO,TABLE_ID);
INIT_S(valS);
INIT_S(val2S);
INIT_S(rowS);
if(val) {
NumlistAutoFormatStringI(tabPO->vals, NULL, NULL, valS);
}
if(row) {
WordlistAutoFormatStringI(tabPO->rlabs, NULL, rowS);
}
SetTablePrintformI(tabPO, valS, val2S, NULL, rowS, NULL);
return(TRUE);
}
/**************************************************************************
* Set the print format strings for when table is dumped
* valS = Format string to dump (number) values (e.g. "%5.2f")
* sepS = Separator between items (e.g. "\t")
* rowS = Format string to dump (string) row lables (e.g. "%-12s")
* preS = Prefix string for lines (e.g. "# ")
*/
int SetTablePrintformI(TABLE *tabPO, char *valS, char *val2S, char *sepS,
char *rowS, char *preS)
{
VALIDATE(tabPO,TABLE_ID);
if(valS && (!NO_S(valS))) {
strcpy(tabPO->pvform,valS);
}
if(val2S && (!NO_S(val2S))) {
strcpy(tabPO->pvform2,val2S);
}
if(sepS && (!NO_S(sepS))) {
strcpy(tabPO->pvsep,sepS);
}
if(rowS && (!NO_S(rowS))) {
strcpy(tabPO->prlform,rowS);
}
if(preS && (!NO_S(preS))) {
strcpy(tabPO->prefix,preS);
}
return(TRUE);
}
/*************************************************************************/
int GetTablePrintformI(TABLE *tabPO, char *valS, char *val2S, char *sepS,
char *rowS, char *preS)
{
VALIDATE(tabPO,TABLE_ID);
if(valS) {
strcpy(valS,tabPO->pvform);
}
if(val2S) {
strcpy(val2S,tabPO->pvform2);
}
if(sepS) {
strcpy(sepS,tabPO->pvsep);
}
if(rowS) {
strcpy(rowS,tabPO->prlform);
}
if(preS) {
strcpy(preS,tabPO->prefix);
}
return(TRUE);
}
/*************************************************************************/
DOUB GetTableDefValueD(TABLE *tabPO)
{
VALIDATE(tabPO,TABLE_ID);
return(tabPO->defval);
}
/*************************************************************************/
int IsTableCSVFormatI(TABLE *tabPO)
{
VALIDATE(tabPO,TABLE_ID);
return(tabPO->pvsep[0] == ',');
}
/**************************************************************************
* Set row label to nameS, Also sets that table has row lables
*/
int SetTableRowLabI(TABLE *tabPO,int row,char *nameS)
{
int ok;
VALIDATE(tabPO,TABLE_ID);
ok = FALSE;
if( (row>=0) && (row<tabPO->nrow) )
{
if(AddWordlistWordI(tabPO->rlabs, row, nameS, TRUE)) {
ok++;
tabPO->rlab = TRUE;
}
}
return(ok);
}
/**************************************************************************
* Set col label to nameS, Also sets that table has col labels
*/
int SetTableColLabI(TABLE *tabPO,int col,char *nameS)
{
int ok;
VALIDATE(tabPO,TABLE_ID);
ok = FALSE;
if( (col>=0) && (col<tabPO->ncol) )
{
if(AddWordlistWordI(tabPO->clabs, col, nameS, TRUE)) {
ok++;
tabPO->clab = TRUE;
}
}
return(ok);
}
/**************************************************************************
* Set 'corner' label to nameS
*/
int SetTableCornerLabI(TABLE *tabPO, char *nameS)
{
VALIDATE(tabPO,TABLE_ID);
sprintf(tabPO->corner, "%s", nameS);
return(TRUE);
}
/**************************************************************************
* Get row label from table and copy it into nameS up to max
*/
int GetTableRowLabI(TABLE *tabPO,int row,char *nameS,int max)
{
VALIDATE(tabPO,TABLE_ID);
INIT_S(nameS);
return(GetWordlistWordI(tabPO->rlabs, row, nameS, max));
}
/**************************************************************************
* Get col label from table and copy it into nameS up to max
*/
int GetTableColLabI(TABLE *tabPO,int col,char *nameS,int max)
{
VALIDATE(tabPO,TABLE_ID);
INIT_S(nameS);
return(GetWordlistWordI(tabPO->clabs, col, nameS, max));
}
/**************************************************************************
* Turn off flag for row lables
*/
int HideTableRowLabsI(TABLE *tabPO)
{
VALIDATE(tabPO,TABLE_ID);
tabPO->rlab = FALSE;
return(TRUE);
}
/**************************************************************************
* Turn off flag for col lables
*/
int HideTableColLabsI(TABLE *tabPO)
{
VALIDATE(tabPO,TABLE_ID);
tabPO->clab = FALSE;
return(TRUE);
}
/****************************************************************************
* Compare row number and names in two tables
*/
int MatchingTableRowsI(TABLE *t1PO, TABLE *t2PO, int kc)
{
int i;
char n1S[NSIZE],n2S[NSIZE];
VALIDATE(t1PO,TABLE_ID);
VALIDATE(t2PO,TABLE_ID);
if(t1PO->nrow != t2PO->nrow) {
return(FALSE);
}
/***
* Either one has no row labels to compare
*/
if( (!t1PO->rlab) || (!t2PO->rlab) ) {
return(TRUE);
}
/***
* Compare row lables
*/
for(i=0;i<t1PO->nrow;i++)
{
GetTableRowLabI(t1PO,i,n1S,-1);
GetTableRowLabI(t2PO,i,n2S,-1);
if(!SameStringsI(n1S,n2S,kc)) {
return(FALSE);
}
}
return(TRUE);
}