From 83cfb298d6ea3142c8dea295a57fe8a338e9a0e1 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Tue, 16 Mar 2021 08:41:20 -0600 Subject: [PATCH] elt_list, moved qsort to elt_list_combine --- basicsubs.cpp | 99 +++++++------------------------------------------- integrate.cpp | 8 +--- parse.cpp | 8 ++-- prep.cpp | 25 ++----------- print.cpp | 7 +--- step.cpp | 7 +--- structures.cpp | 26 ++++++------- tally.cpp | 12 ------ tidy.cpp | 22 +---------- 9 files changed, 37 insertions(+), 177 deletions(-) diff --git a/basicsubs.cpp b/basicsubs.cpp index a54440cc..dc10e530 100644 --- a/basicsubs.cpp +++ b/basicsubs.cpp @@ -1122,12 +1122,7 @@ diff_layer_total(const char *total_name, const char *surface_name) */ add_elt_list(s_x[j]->next_elt, moles_surface); } - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t) count_elts, - sizeof(struct elt_list), Phreeqc:: elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); /* * Return totals */ @@ -2420,12 +2415,7 @@ surf_total_no_redox(const char *total_name, const char *surface_name) } } } - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); /* * Return totals */ @@ -2836,12 +2826,7 @@ kinetics_formula(std::string kin_name, cxxNameDouble &stoichiometry) } formula.append(kin_name); //elt_list[count_elts].elt = NULL; - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); stoichiometry = elt_list_NameDouble(); break; } @@ -3270,12 +3255,7 @@ system_total_elt(const char *total_name) paren_count = 0; add_elt_list(s_x[i]->next_elt, s_x[i]->moles); - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); /* * Look for element */ @@ -3358,12 +3338,7 @@ system_total_elt(const char *total_name) */ add_elt_list(s_x[j]->next_elt, moles_surface); } - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); /* * Print totals */ @@ -3405,12 +3380,7 @@ system_total_elt(const char *total_name) //struct phase * phase_ptr = phase_bsearch(x[i]->pp_assemblage_comp_name, &j, FALSE); struct phase * phase_ptr = x[i]->phase; add_elt_list(phase_ptr->next_elt, x[i]->moles); - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); for (j = 0; j < count_elts; j++) { if (strcmp(elt_list[j].elt->name, total_name) == 0) @@ -3446,12 +3416,7 @@ system_total_elt(const char *total_name) paren_count = 0; add_elt_list(phase_ptr->next_elt, comp_ptr->Get_moles()); - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); for (j = 0; j < count_elts; j++) { if (strcmp(elt_list[j].elt->name, total_name) == 0) @@ -3486,12 +3451,7 @@ system_total_elt(const char *total_name) count_elts = 0; paren_count = 0; add_elt_list(phase_ptr->next_elt, phase_ptr->moles_x); - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); /* * Look for element */ @@ -3541,12 +3501,7 @@ system_total_elt_secondary(const char *total_name) { add_elt_list(s_x[i]->next_sys_total, s_x[i]->moles); } - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); /* * Look for element */ @@ -3671,12 +3626,7 @@ system_total_elt_secondary(const char *total_name) //struct phase * phase_ptr = phase_bsearch(x[i]->pp_assemblage_comp_name, &j, FALSE); struct phase * phase_ptr = x[i]->phase; add_elt_list(phase_ptr->next_sys_total, x[i]->moles); - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); for (j = 0; j < count_elts; j++) { if (strcmp(elt_list[j].elt->name, total_name) == 0) @@ -3713,12 +3663,7 @@ system_total_elt_secondary(const char *total_name) paren_count = 0; add_elt_list(phase_ptr->next_sys_total, comp_ptr->Get_moles()); - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); for (j = 0; j < count_elts; j++) { if (strcmp(elt_list[j].elt->name, total_name) == 0) @@ -3755,12 +3700,7 @@ system_total_elt_secondary(const char *total_name) add_elt_list(phase_ptr->next_sys_total, phase_ptr->moles_x); - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); /* * Look for element */ @@ -3847,12 +3787,7 @@ solution_sum_secondary(const char *total_name) { add_elt_list(s_x[i]->next_sys_total, s_x[i]->moles); } - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); /* * Look for element */ @@ -3963,13 +3898,7 @@ system_total_solids(cxxExchange *exchange_ptr, it->second.Get_moles()); } } - - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); return (OK); } diff --git a/integrate.cpp b/integrate.cpp index c4488a65..ce6fa6b1 100644 --- a/integrate.cpp +++ b/integrate.cpp @@ -723,13 +723,7 @@ sum_diffuse_layer(cxxSurfaceCharge *charge_ptr) add_elt_list(s_x[j]->next_elt, moles_surface); } add_elt_list(s_h2o->next_elt, mass_water_surface / gfw_water); - - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t) count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); return (OK); } /* ---------------------------------------------------------------------- */ diff --git a/parse.cpp b/parse.cpp index 22e00495..8931f432 100644 --- a/parse.cpp +++ b/parse.cpp @@ -140,8 +140,8 @@ parse_eq(char *eqn, struct elt_list **elt_ptr, int association) /* * Sort elements in reaction and combine */ - qsort(elt_list.data(), (size_t) count_elts, sizeof(struct elt_list), - elt_list_compare); + //qsort(elt_list.data(), (size_t) count_elts, sizeof(struct elt_list), + // elt_list_compare); if (elt_list_combine() == ERROR) return (ERROR); /* @@ -228,8 +228,8 @@ check_eqn(int association) /* * Sort elements in reaction and combine */ - qsort(elt_list.data(), (size_t) count_elts, sizeof(struct elt_list), - elt_list_compare); + //qsort(elt_list.data(), (size_t) count_elts, sizeof(struct elt_list), + // elt_list_compare); if (elt_list_combine() == ERROR) return (ERROR); /* diff --git a/prep.cpp b/prep.cpp index 131cbb62..b837e060 100644 --- a/prep.cpp +++ b/prep.cpp @@ -5301,12 +5301,7 @@ write_mb_eqn_x(void) free_check_null(temp_name); } } - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); return (OK); } @@ -5365,12 +5360,7 @@ write_mb_for_species_list(int n) count_elts++; } } - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); s[n]->next_sys_total = (struct elt_list *) free_check_null(s[n]->next_sys_total); s[n]->next_sys_total = elt_list_save(); @@ -5427,12 +5417,7 @@ write_phase_sys_total(int n) count_elts++; } } - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); phases[n]->next_sys_total = (struct elt_list *) free_check_null(phases[n]->next_sys_total); phases[n]->next_sys_total = elt_list_save(); @@ -6543,8 +6528,6 @@ change_hydrogen_in_elt_list(LDBLE charge) found_o = -1; coef_h = 0.0; coef_o = 0.0; - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); elt_list_combine(); for (j = 0; j < count_elts; j++) { @@ -6569,8 +6552,6 @@ change_hydrogen_in_elt_list(LDBLE charge) elt_list[count_elts].elt = s_hplus->primary->elt; elt_list[count_elts].coef = coef; count_elts++; - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); elt_list_combine(); return (OK); } diff --git a/print.cpp b/print.cpp index 4cd48a86..df04e616 100644 --- a/print.cpp +++ b/print.cpp @@ -316,12 +316,7 @@ print_diffuse_layer(cxxSurfaceCharge *charge_ptr) ptr = &(token[0]); get_elts_in_species (&ptr, mass_water_surface / gfw_water); */ - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); /* * Print totals */ diff --git a/step.cpp b/step.cpp index d9cf2715..c0eb9a4f 100644 --- a/step.cpp +++ b/step.cpp @@ -1026,12 +1026,7 @@ add_gas_phase(cxxGasPhase *gas_phase_ptr) /* * Sort elements in reaction and combine */ - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t) count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); /* * Add gas elements to totals */ diff --git a/structures.cpp b/structures.cpp index 57011da8..fb94a438 100644 --- a/structures.cpp +++ b/structures.cpp @@ -461,15 +461,17 @@ elt_list_combine(void) { int i, j; - if (count_elts < 1) - { - output_msg("elt_list_combine: How did this happen?\n"); - return (ERROR); - } - if (count_elts == 1) + //if (count_elts < 1) + //{ + // output_msg("elt_list_combine: How did this happen?\n"); + // return (ERROR); + //} + if (count_elts <= 1) { return (OK); } + qsort(elt_list.data(), (size_t)count_elts, + sizeof(struct elt_list), Phreeqc::elt_list_compare); j = 0; for (i = 1; i < count_elts; i++) { @@ -585,18 +587,12 @@ elt_list_save(void) /* * Sort elements in reaction and combine */ - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); /* * Malloc space and store element data */ - elt_list_ptr = - (struct elt_list *) PHRQ_malloc((size_t) (count_elts + 1) * - sizeof(struct elt_list)); + elt_list_ptr = (struct elt_list*)PHRQ_malloc((size_t)(count_elts + 1) * + sizeof(struct elt_list)); if (elt_list_ptr == NULL) { malloc_error(); diff --git a/tally.cpp b/tally.cpp index ea45226f..a6ccbb08 100644 --- a/tally.cpp +++ b/tally.cpp @@ -550,8 +550,6 @@ fill_tally_table(int *n_user, int index_conservative, int n_buffer) { add_elt_list(exchange_ptr->Get_exchange_comps()[j].Get_totals(), 1.0); } - qsort(elt_list.data(), (size_t) count_elts, - sizeof(struct elt_list), elt_list_compare); elt_list_combine(); elt_list_to_tally_table(tally_table[i].total[n_buffer]); } @@ -572,8 +570,6 @@ fill_tally_table(int *n_user, int index_conservative, int n_buffer) { add_elt_list(surface_ptr->Get_surface_comps()[j].Get_totals(), 1.0); } - qsort(elt_list.data(), (size_t) count_elts, - sizeof(struct elt_list), elt_list_compare); elt_list_combine(); elt_list_to_tally_table(tally_table[i].total[n_buffer]); } @@ -643,8 +639,6 @@ fill_tally_table(int *n_user, int index_conservative, int n_buffer) add_elt_list(phase_ptr->next_elt, (*gc)[l].Get_moles()); } - qsort(elt_list.data(), (size_t) count_elts, - sizeof(struct elt_list), elt_list_compare); elt_list_combine(); elt_list_to_tally_table(tally_table[i].total[n_buffer]); break; @@ -921,8 +915,6 @@ build_tally_table(void) strcpy(token, phase_ptr->formula); add_elt_list(phase_ptr->next_elt, 1.0); } - qsort(elt_list.data(), (size_t) count_elts, - sizeof(struct elt_list), elt_list_compare); elt_list_combine(); tally_table[n].formula = elt_list_save(); } @@ -973,8 +965,6 @@ build_tally_table(void) paren_count = 0; strcpy(token, phase_ptr->formula); add_elt_list(phase_ptr->next_elt, 1.0); - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); elt_list_combine(); tally_table[n].formula = elt_list_save(); } @@ -1041,8 +1031,6 @@ build_tally_table(void) free_check_null(temp_name); } } - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); elt_list_combine(); tally_table[n].formula = elt_list_save(); } diff --git a/tidy.cpp b/tidy.cpp index 38e90ffc..0ba1ec98 100644 --- a/tidy.cpp +++ b/tidy.cpp @@ -1285,12 +1285,7 @@ tidy_inverse(void) /* * Sort elements in reaction and combine */ - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t) count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); /* * Mark master species list */ @@ -3495,8 +3490,6 @@ tidy_min_exchange(void) error_msg(error_string, CONTINUE); continue; } - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); elt_list_combine(); for (jj = 0; jj < count_elts; jj++) { @@ -3675,8 +3668,6 @@ update_min_exchange(void) error_msg(error_string, CONTINUE); continue; } - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); elt_list_combine(); for (jj = 0; jj < count_elts; jj++) { @@ -3900,8 +3891,6 @@ tidy_min_surface(void) free_check_null(temp_formula); } } - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); elt_list_combine(); /* Makes no sense: sorbed species need not be in mineral structure... */ /* But elements that can desorb into solution must be in mineral */ @@ -4273,8 +4262,6 @@ tidy_kin_surface(void) /* save kinetics formula */ if (count_elts > 0) { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); elt_list_combine(); } elt_list_kinetics = elt_list_save(); @@ -4300,12 +4287,7 @@ tidy_kin_surface(void) free_check_null(temp_formula); } } - if (count_elts > 0) - { - qsort(elt_list.data(), (size_t)count_elts, - sizeof(struct elt_list), elt_list_compare); - elt_list_combine(); - } + elt_list_combine(); for (int j = 0; j < count_elts; j++) { if (elt_list[j].elt == NULL)