15namespace layer1_foundations {
16namespace data_structures {
61 int s, i, j, a, h, len, offset = 0;
83 for (j = 0; j < s + 1; j++) {
84 snprintf(str, 1000,
"C%d", j);
92 for (i = 0; i < S->
nb_sets; i++) {
94 snprintf(str, 1000,
"%ld", S->
Set_size[i]);
101 for (j = 0; j < S->
Set_size[i]; j++) {
104 snprintf(str, 1000,
"%d", a);
130 snprintf(str, 1000,
"%d", a);
156 int col_idx,
const char *text)
172 int col_idx, std::string &text)
184 strcpy(
tokens[t], text.c_str());
188 int col_idx,
long int val)
193 sprintf(str,
"%ld", val);
208 const char **text,
const char *heading)
212 for (i = 0; i <
nb_rows; i++) {
222 strcpy(
tokens[t], heading);
225 l = strlen(text[i - 1]);
227 strcpy(
tokens[t], text[i - 1]);
233 int *data,
const char *heading)
238 for (i = 0; i <
nb_rows; i++) {
248 strcpy(
tokens[t], heading);
251 snprintf(str, 1000,
"%d", data[i - 1]);
260 long int *data,
const char *heading)
265 for (i = 0; i <
nb_rows; i++) {
275 strcpy(
tokens[t], heading);
278 sprintf(str,
"%ld", data[i - 1]);
287 int col_idx,
const char *heading)
292 for (i = 0; i <
nb_rows; i++) {
302 strcpy(
tokens[t], heading);
305 snprintf(str, 1000,
"%d", i - 1);
324 for (i = 0, j = 0; i < len; i++) {
325 if ((
int)label[i] < 0) {
326 cout <<
"spreadsheet::add_token negative character "
327 << (int) label[i] << endl;
342 int f_v = (verbose_level >= 1);
352 cout <<
"Written file " << fname <<
" of size "
359 int f_v = (verbose_level >= 1);
365 cout <<
"spreadsheet::read_spreadsheet reading file "
366 << fname <<
" of size " << Fio.
file_size(fname) << endl;
372 cout <<
"spreadsheet::read_spreadsheet before tokenize" << endl;
377 cout <<
"spreadsheet::read_spreadsheet read file with "
382 cout << setw(6) << i <<
" : '" <<
tokens[i] <<
"'" << endl;
390 cout <<
"spreadsheet::read_spreadsheet before find_rows" << endl;
395 cout <<
"spreadsheet::read_spreadsheet Found "
406 cout <<
"Line " << i <<
" : ";
407 for (j = 0; j < l; j++) {
408 cout <<
"'" <<
tokens[f + j] <<
"'";
423 for (i = 0; i <
nb_rows; i++) {
424 for (j = 0; j <
nb_cols; j++) {
428 for (i = 0; i <
nb_rows; i++) {
435 cout <<
"spreadsheet::read_spreadsheet" << endl;
436 for (i = 0; i <
nb_rows; i++) {
437 for (j = 0; j <
nb_cols; j++) {
438 cout <<
"row " << i <<
" column " << j <<
" entry '"
445 cout <<
"spreadsheet::read_spreadsheet reading file "
446 << fname <<
" of size " << Fio.
file_size(fname)
457 for (i = 0; i <
nb_rows; i++) {
463 ostream &ost,
int f_enclose_in_parentheses)
466 int *f_column_select;
469 for (j = 0; j <
nb_cols; j++) {
470 f_column_select[j] =
TRUE;
474 ost <<
"\\begin{tabular}{|c|";
475 for (j = 0; j <
nb_cols; j++) {
479 for (i = 0; i <
nb_rows; i++) {
481 f_column_select, f_enclose_in_parentheses, ost);
483 ost <<
"\\end{tabular}" << endl;
489 int *f_column_select,
int f_enclose_in_parentheses,
490 int nb_lines_per_table)
499 for (I = 0; I < (nb_r + nb_lines_per_table - 1) / nb_lines_per_table; I++) {
500 ost <<
"\\begin{tabular}[t]{|";
501 for (j = 0; j <
nb_cols; j++) {
502 if (f_column_select[j]) {
508 ost <<
"\\hline" << endl;
512 f_enclose_in_parentheses,
514 ost <<
"\\hline" << endl;
515 ost <<
"\\hline" << endl;
518 for (i = 0; i < nb_lines_per_table; i++) {
519 if (1 + I * nb_lines_per_table + i <
nb_rows) {
522 f_enclose_in_parentheses,
524 ost <<
"\\hline" << endl;
527 ost <<
"\\end{tabular}" << endl;
532 int f_enclose_in_parentheses, ostream &ost)
538 for (j = 0; j <
nb_cols; j++) {
542 if (row == 0 && j == 0) {
543 cout <<
"printing token '" <<
tokens[t] <<
"'" << endl;
544 for (h = 0; h < 10; h++) {
545 cout << h <<
" : " << (int)
tokens[t][h] << endl;
549 if (
tokens[t][0] ==
'\"') {
559 cout <<
"spreadsheet::print_table_row token[t] == NULL, "
577 int *f_column_select,
int f_enclose_in_parentheses,
585 for (j = 0; j <
nb_cols; j++) {
586 if (f_column_select[j]) {
596 if (row == 0 && j == 0) {
597 cout <<
"printing token '" <<
tokens[t] <<
"'" << endl;
598 for (h = 0; h < 10; h++) {
599 cout << h <<
" : " << (int)
tokens[t][h] << endl;
603 if (f_enclose_in_parentheses) {
606 if (
tokens[t][0] ==
'"') {
610 if (
tokens[t][l - 1] ==
'"') {
614 if (f_enclose_in_parentheses) {
620 ost <<
"\\\\" << endl;
627 ost <<
"Row " << row <<
" of the table is:" << endl;
628 for (j = 0; j <
nb_cols; j++) {
629 ost <<
"Column " << j <<
" / " <<
nb_cols <<
" : ";
642 int f_enclose_in_parentheses,
643 int *Col_selection,
int nb_cols_selected, std::ostream &ost)
649 for (h = 0; h < nb_cols_selected; h++) {
650 j = Col_selection[h] + 1;
654 if (row == 0 && j == 0) {
655 cout <<
"printing token '" <<
tokens[t] <<
"'" << endl;
656 for (h = 0; h < 10; h++) {
657 cout << h <<
" : " << (int)
tokens[t][h] << endl;
661 if (
tokens[t][0] ==
'\"') {
671 cout <<
"spreadsheet::print_table_row_with_column_selection token[t] == NULL, "
681 if (h < nb_cols_selected - 1) {
690 int *f_selected, ostream &ost)
695 for (i = 0; i <
nb_rows; i++) {
696 if (!f_selected[i]) {
715 for (i = 0; i <
nb_rows - 1; i++) {
719 if (
tokens[t][0] ==
'"') {
721 strcpy(labels[i],
tokens[t] + 1);
725 strcpy(labels[i],
tokens[t]);
739 for (i = 0; i <
nb_rows; i++) {
744 ii = perm[i - 1] + 1;
758 for (i = 1; i <
nb_rows; i++) {
773 for (i = 1; i <
nb_rows; i++) {
774 snprintf(str, 1000,
"%d", Value[i - 1]);
787 for (i = 1; i <
nb_rows; i++) {
801 for (i = 0; i <
nb_rows; i++) {
802 for (j = 0; j <
nb_cols; j++) {
819 for (i = 0; i <
nb_rows; i++) {
820 for (j = 0; j <
nb_cols; j++) {
824 for (j = 0; j <
nb_cols; j++) {
841 for (j = 0; j <
nb_cols; j++) {
844 c = strncmp(
tokens[t], join_by, strlen(join_by));
846 cout <<
"comparing '" <<
tokens[t] <<
"' with '"
847 << join_by <<
"' yields " << c << endl;
848 for (h = 0; h < (int)strlen(join_by); h++) {
849 cout << h <<
" : " <<
tokens[t][h] <<
" : "
850 << join_by[h] << endl;
860 char join_by_in_quotes[1000];
862 snprintf(join_by_in_quotes, 1000,
"\"%s",join_by);
863 for (j = 0; j <
nb_cols; j++) {
866 c = strncmp(
tokens[t], join_by_in_quotes,
867 strlen(join_by_in_quotes));
869 cout <<
"comparing '" <<
tokens[t] <<
"' with '"
870 << join_by <<
"' yields " << c << endl;
871 for (h = 0; h < (int)strlen(join_by); h++) {
872 cout << h <<
" : " << (int)
tokens[t][h] <<
" : "
873 << (
int) join_by[h] << endl;
881 cout <<
"by column not found, join_by='" << join_by <<
"'" << endl;
882 cout <<
"The first row of the table is:" << endl;
890 char **&tokens,
int &nb_tokens,
int verbose_level)
892 int f_v = (verbose_level >= 1);
893 int f_vv = (verbose_level >= 2);
902 cout <<
"spreadsheet::tokenize file=" << fname << endl;
903 cout <<
"spreadsheet::tokenize verbose_level="
904 << verbose_level << endl;
920 fp.getline(buf, sz,
'\n');
922 cout <<
"Line read :'" << buf <<
"'" << endl;
925 if (strncmp(buf,
"END", 3) == 0) {
931 int len = strlen(buf);
932 for (i = 0, j = 0; i < len; i++) {
933 if ((
int) buf[i] >= 0) {
937 cout <<
"spreadsheet::tokenize skipping "
938 "negative character" << endl;
954 cout <<
"Token " << setw(6) << i <<
" is '"
955 << str <<
"'" << endl;
958 if (strcmp(str,
",") == 0) {
982 fp.getline(buf, sz,
'\n');
984 if (strncmp(buf,
"END", 3) == 0) {
988 cout <<
"read line '" << p_buf <<
"'" <<
" i=" << i << endl;
993 int len = strlen(buf);
994 for (i = 0, j = 0; i < len; i++) {
995 if ((
int) buf[i] >= 0) {
999 cout <<
"spreadsheet::tokenize skipping "
1000 "negative character" << endl;
1015 if (strcmp(str,
",") == 0) {
1022 cout <<
"Token " << setw(6) << i <<
" is '"
1023 <<
tokens[i] <<
"'" << endl;
1029 snprintf(str, sz,
"END_OF_LINE");
1033 cout <<
"Token " << setw(6) << i <<
" is '"
1034 <<
tokens[i] <<
"'" << endl;
1049 for (i = 1; i <
nb_rows; i++) {
1050 for (j = 0; j <
nb_cols; j++) {
1055 if (
tokens[t][0] ==
'"') {
1057 for (h = 1; h < l; h++) {
1063 if (l &&
tokens[t][l - 1] ==
'"') {
1071 const char *drop_label,
int verbose_level)
1073 int f_v = (verbose_level >= 1);
1075 int i, j, h, t, idx, nbr, f_delete;
1078 cout <<
"spreadsheet::remove_rows" << endl;
1082 cout <<
"drop column is " << idx << endl;
1083 cout <<
"drop label is " << drop_label << endl;
1085 for (i = 1; i <
nb_rows; i++) {
1087 if (t >= 0 && strcmp(
tokens[t], drop_label) == 0) {
1094 for (j = 0; j <
nb_cols; j++) {
1102 cout <<
"spreadsheet::remove_rows, removed "
1103 << nbr -
nb_rows <<
" rows" << endl;
1108 const char *drop_column,
int verbose_level)
1110 int f_v = (verbose_level >= 1);
1112 int i, j, h, t, idx, nbr, f_delete;
1115 cout <<
"spreadsheet::remove_rows_where_field_is_empty" << endl;
1119 cout <<
"drop column is " << idx << endl;
1121 for (i = 1; i <
nb_rows; i++) {
1126 else if (t >= 0 && strlen(
tokens[t]) == 0) {
1133 for (j = 0; j <
nb_cols; j++) {
1141 cout <<
"spreadsheet::remove_rows_where_field_is_empty, "
1142 "removed " << nbr -
nb_rows <<
" rows" << endl;
1148 int f_v = (verbose_level >= 1);
1149 int f_vv = (verbose_level >= 2);
1153 cout <<
"find_rows" << endl;
1158 if (strcmp(
tokens[i],
"END_OF_LINE") == 0) {
1171 cout <<
"cnt=" << cnt <<
" i=" << i
1172 <<
" tokens[i]=" <<
tokens[i] << endl;
1174 if (strcmp(
tokens[i],
"END_OF_LINE") == 0) {
1177 cout <<
"end of line" << endl;
1186 double &val,
int &f_NA)
1192 cout <<
"spreadsheet::get_value_double_or_NA str=" << str << endl;
1204void spreadsheet::get_string_entry(std::string &entry,
int i,
int j)
1230 str.assign(
tokens[t] + 1);
1271 int by1,
int by2,
int verbose_level)
1273 int f_v = (verbose_level >= 1);
1274 int f_vv = (verbose_level >= 2);
1275 int f_v3 = (verbose_level >= 3);
1286 cout <<
"spreadsheet::join_with" << endl;
1287 cout <<
"verbose_level=" << verbose_level << endl;
1293 cout <<
"by1=" << by1 <<
" by2=" << by2 << endl;
1298 for (i2 = 1; i2 < S2->
nb_rows; i2++) {
1305 if (strlen(T2) == 0) {
1308 for (i1 = 1; i1 <
nb_rows; i1++) {
1320 cout <<
"adding a row corresponding to " << T2 << endl;
1328 for (j2 = 0; j2 < S2->
nb_cols; j2++) {
1330 cout <<
"j2=" << j2 << endl;
1340 cout <<
"joining column " << S2->
tokens[t2] << endl;
1343 for (j1 = 0; j1 <
nb_cols; j1++) {
1357 cout <<
"reallocating table" << endl;
1359 cout <<
"reallocating table done" << endl;
1362 cout <<
"added token " << S2->
tokens[t2]
1363 <<
" as a column heading" << endl;
1368 cout <<
"joining columns " <<
tokens[t1] <<
" and "
1369 << S2->
tokens[t2] << endl;
1372 for (i2 = 1; i2 < S2->
nb_rows; i2++) {
1374 cout <<
"i2=" << i2 << endl;
1378 cout <<
"tt2=" << tt2 << endl;
1388 cout <<
"label2='" << label2 <<
"'" << endl;
1390 for (i1 = 1; i1 <
nb_rows; i1++) {
1402 cout <<
"entry " << label2 <<
" not found in "
1403 "first table" << endl;
1411 cout <<
"label2 " << label2 <<
" found in row "
1412 << i1 <<
" in first table" << endl;
1415 f_need_to_add =
TRUE;
1418 cout <<
"i1=" << i1 <<
" i2=" << i2 <<
" we have "
1420 << S2->
tokens[tt2] << endl;
1424 f_need_to_add =
FALSE;
1428 cout <<
"f_need_to_add=" << f_need_to_add << endl;
1430 if (f_need_to_add) {
1432 cout <<
"adding token " << S2->
tokens[tt2] << endl;
1437 cout <<
"added token " << S2->
tokens[tt2]
1444 cout <<
"no need to add" << endl;
1450 cout <<
"spreadsheet::join_with done" << endl;
1465 cout <<
"spreadsheet::patch_with by1=" << by1 << endl;
1466 cout <<
"spreadsheet::patch_with S2->nb_rows=" << S2->
nb_rows << endl;
1470 for (i2 = 1; i2 < S2->
nb_rows; i2++) {
1482 if (strlen(what) == 0) {
1485 if (strcmp(what,
"-1") == 0) {
1489 if (strlen(who) == 0) {
1492 patch_value = S2->
tokens[t3];
1494 for (i1 = 1; i1 <
nb_rows; i1++) {
1505 cout <<
"spreadsheet::patch_with Did not find " << who
1506 <<
" in first table" << endl;
1512 cout <<
"patch " << nb_patch <<
" applied, " << who
1513 <<
" now has " << patch_value <<
" in " << what << endl;
1517 cout <<
"spreadsheet::patch_with applied " << nb_patch
1518 <<
" patches" << endl;
a collection of functions related to sorted vectors
void quicksort_array_with_perm(int len, void **v, int *perm, int(*compare_func)(void *a, void *b, void *data), void *data)
for reading and writing of csv files
void print_table_row_detailed(int row, std::ostream &ost)
void read_spreadsheet(std::string &fname, int verbose_level)
void fill_column_with_text(int col_idx, const char **text, const char *heading)
void print_table_with_row_selection(int *f_selected, std::ostream &ost)
void init_int_matrix(int nb_rows, int nb_cols, int *A)
void join_with(spreadsheet *S2, int by1, int by2, int verbose_level)
void get_value_double_or_NA(int i, int j, double &val, int &f_NA)
void fill_column_with_row_index(int col_idx, const char *heading)
void print_table_row_latex(int row, int *f_column_select, int f_enclose_in_parentheses, std::ostream &ost)
void add_token(const char *label)
void print_table_sorted(std::ostream &ost, const char *sort_by)
void reallocate_table_add_row()
void patch_with(spreadsheet *S2, char *join_by)
double get_double(int i, int j)
void remove_quotes(int verbose_level)
void tokenize(std::string &fname, char **&tokens, int &nb_tokens, int verbose_level)
void remove_rows(const char *drop_column, const char *drop_label, int verbose_level)
void fill_column_with_lint(int col_idx, long int *data, const char *heading)
void set_entry_lint(int row_idx, int col_idx, long int val)
void find_rows(int verbose_level)
void print_table_row(int row, int f_enclose_in_parentheses, std::ostream &ost)
void add_column_with_text(const char *label, char **Value)
void remove_rows_where_field_is_empty(const char *drop_column, int verbose_level)
void save(std::string &fname, int verbose_level)
void print_table_latex(std::ostream &ost, int *f_column_select, int f_enclose_in_parentheses, int nb_lines_per_table)
void init_empty_table(int nb_rows, int nb_cols)
void print_table_latex_all_columns(std::ostream &ost, int f_enclose_in_parentheses)
void print_table_row_with_column_selection(int row, int f_enclose_in_parentheses, int *Col_selection, int nb_cols_selected, std::ostream &ost)
long int get_int(int i, int j)
void add_column_with_constant_value(const char *label, char *value)
int find_column(std::string &column_label)
void init_set_of_sets(set_of_sets *S, int f_make_heading)
void fill_column_with_int(int col_idx, int *data, const char *heading)
void fill_entry_with_text(int row_idx, int col_idx, const char *text)
void get_string(std::string &str, int i, int j)
void print_table(std::ostream &ost, int f_enclose_in_parentheses)
int find_by_column(const char *join_by)
void add_column_with_int(const char *label, int *Value)
a collection of functions related to file io
long int file_size(std::string &fname)
int string_tools_compare_strings(void *a, void *b, void *data)
the orbiter library for the classification of combinatorial objects