[gnumeric] solver: get rid of SolverParameters::input_cells.
- From: Morten Welinder <mortenw src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gnumeric] solver: get rid of SolverParameters::input_cells.
- Date: Mon, 2 Nov 2009 02:27:59 +0000 (UTC)
commit cd5036c094432470c06b3db779caad9c70c26278
Author: Morten Welinder <terra gnome org>
Date: Sun Nov 1 21:27:34 2009 -0500
solver: get rid of SolverParameters::input_cells.
plugins/lpsolve/lpsolve-write.c | 51 ++++--------------------
src/dialogs/dialog-solver.c | 23 +----------
src/solver.h | 2 +-
src/tools/solver/solver.c | 85 +++++++++++++++++++++++++--------------
4 files changed, 66 insertions(+), 95 deletions(-)
---
diff --git a/plugins/lpsolve/lpsolve-write.c b/plugins/lpsolve/lpsolve-write.c
index 8217ae7..986e482 100644
--- a/plugins/lpsolve/lpsolve-write.c
+++ b/plugins/lpsolve/lpsolve-write.c
@@ -132,19 +132,6 @@ lpsolve_affine_func (GString *dst, GnmCell *target,
return TRUE;
}
-static GnmValue *
-cb_grab_cells (GnmCellIter const *iter, gpointer user)
-{
- GList **the_list = user;
- GnmCell *cell;
-
- if (NULL == (cell = iter->cell))
- cell = sheet_cell_create (iter->pp.sheet,
- iter->pp.eval.col, iter->pp.eval.row);
- *the_list = g_list_append (*the_list, cell);
- return NULL;
-}
-
static GString *
lpsolve_create_program (Sheet *sheet, GError **err)
{
@@ -155,36 +142,14 @@ lpsolve_create_program (Sheet *sheet, GError **err)
GString *objfunc = g_string_new (NULL);
GSList *l;
GnmCell *target_cell = gnm_solver_param_get_target_cell (sp);
-
- /* This is insane */
- {
- GnmValue const *vr =
- gnm_expr_top_get_constant (sp->input.texpr);
- GnmEvalPos ep;
-
- g_slist_free (sp->input_cells);
- sp->input_cells = NULL;
-
- if (!vr) {
- g_set_error (err,
- go_error_invalid (),
- 0,
- _("Invalid solver input range."));
- goto fail;
- }
-
- eval_pos_init_sheet (&ep, sheet);
- workbook_foreach_cell_in_range (&ep, vr, CELL_ITER_ALL,
- cb_grab_cells,
- &sp->input_cells);
- }
+ GSList *input_cells = gnm_solver_param_get_input_cells (sp);
/* ---------------------------------------- */
switch (sp->problem_type) {
case SolverEqualTo:
if (!lpsolve_affine_func (constraints, target_cell,
- sp->input_cells, err))
+ input_cells, err))
goto fail;
/* FIXME -- what value goes here? */
g_string_append (constraints, " = 42;\n");
@@ -199,8 +164,7 @@ lpsolve_create_program (Sheet *sheet, GError **err)
g_assert_not_reached ();
}
- if (!lpsolve_affine_func (objfunc, target_cell,
- sp->input_cells, err))
+ if (!lpsolve_affine_func (objfunc, target_cell, input_cells, err))
goto fail;
g_string_append (objfunc, ";\n");
@@ -208,7 +172,7 @@ lpsolve_create_program (Sheet *sheet, GError **err)
if (sp->options.assume_non_negative) {
GSList *l;
- for (l = sp->input_cells; l; l = l->next) {
+ for (l = input_cells; l; l = l->next) {
GnmCell *cell = l->data;
g_string_append (constraints,
lpsolve_var_name (cell));
@@ -218,7 +182,7 @@ lpsolve_create_program (Sheet *sheet, GError **err)
if (sp->options.assume_discrete) {
GSList *l;
- for (l = sp->input_cells; l; l = l->next) {
+ for (l = input_cells; l; l = l->next) {
GnmCell *cell = l->data;
g_string_append (declarations, "int ");
g_string_append (declarations,
@@ -271,7 +235,7 @@ lpsolve_create_program (Sheet *sheet, GError **err)
gboolean ok;
ok = lpsolve_affine_func
- (constraints, lhs, sp->input_cells, err);
+ (constraints, lhs, input_cells, err);
if (!ok)
goto fail;
@@ -280,7 +244,7 @@ lpsolve_create_program (Sheet *sheet, GError **err)
g_string_append_c (constraints, ' ');
ok = lpsolve_affine_func
- (constraints, rhs, sp->input_cells, err);
+ (constraints, rhs, input_cells, err);
if (!ok)
goto fail;
@@ -307,6 +271,7 @@ fail:
g_string_free (objfunc, TRUE);
g_string_free (constraints, TRUE);
g_string_free (declarations, TRUE);
+ g_slist_free (input_cells);
return prg;
}
diff --git a/src/dialogs/dialog-solver.c b/src/dialogs/dialog-solver.c
index f93b3ff..fd39941 100644
--- a/src/dialogs/dialog-solver.c
+++ b/src/dialogs/dialog-solver.c
@@ -420,19 +420,6 @@ cb_dialog_close_clicked (G_GNUC_UNUSED GtkWidget *button,
gtk_widget_destroy (state->dialog);
}
-static GnmValue *
-cb_grab_cells (GnmCellIter const *iter, gpointer user)
-{
- GList **the_list = user;
- GnmCell *cell;
-
- if (NULL == (cell = iter->cell))
- cell = sheet_cell_create (iter->pp.sheet,
- iter->pp.eval.col, iter->pp.eval.row);
- *the_list = g_list_append (*the_list, cell);
- return NULL;
-}
-
static gchar const *
check_int_constraints (GnmValue *input_range, SolverState *state)
{
@@ -638,9 +625,7 @@ cb_dialog_solve_clicked (G_GNUC_UNUSED GtkWidget *button,
SolverResults *res;
GnmValue *target_range;
GnmValue *input_range;
- GSList *input_cells = NULL;
- GnmValue *result;
- GnmEvalPos pos;
+ GSList *input_cells;
gint i;
gboolean answer, sensitivity, limits, performance;
gboolean program, dual_program;
@@ -685,11 +670,7 @@ cb_dialog_solve_clicked (G_GNUC_UNUSED GtkWidget *button,
return;
}
- result = workbook_foreach_cell_in_range (
- eval_pos_init_sheet (&pos, state->sheet),
- input_range, CELL_ITER_ALL, cb_grab_cells, &input_cells);
-
- param->input_cells = input_cells;
+ input_cells = gnm_solver_param_get_input_cells (param);
param->problem_type =
gnumeric_glade_group_value (state->gui, problem_type_group);
diff --git a/src/solver.h b/src/solver.h
index 7c4a2b2..b28aebc 100644
--- a/src/solver.h
+++ b/src/solver.h
@@ -49,7 +49,6 @@ struct _SolverParameters {
Sheet *sheet;
GnmDependent target;
GnmDependent input;
- GSList *input_cells;
GSList *constraints;
int n_constraints;
int n_variables;
@@ -247,6 +246,7 @@ SolverParameters *solver_param_new (Sheet *sheet);
GnmValue const *gnm_solver_param_get_input (SolverParameters const *sp);
void gnm_solver_param_set_input (SolverParameters *sp, GnmValue *v);
+GSList *gnm_solver_param_get_input_cells (SolverParameters const *sp);
const GnmCellRef *gnm_solver_param_get_target (SolverParameters const *sp);
void gnm_solver_param_set_target (SolverParameters *sp, GnmCellRef const *cr);
diff --git a/src/tools/solver/solver.c b/src/tools/solver/solver.c
index ce21d9a..7de43d8 100644
--- a/src/tools/solver/solver.c
+++ b/src/tools/solver/solver.c
@@ -31,6 +31,7 @@
#include "func.h"
#include "cell.h"
#include "sheet.h"
+#include "workbook.h"
#include "sheet-style.h"
#include "dependent.h"
#include "dialogs.h"
@@ -80,7 +81,6 @@ solver_param_new (Sheet *sheet)
res->options.scenario_name = g_strdup ("Optimal");
res->problem_type = SolverMaximize;
res->constraints = NULL;
- res->input_cells = NULL;
res->constraints = NULL;
return res;
@@ -93,7 +93,6 @@ solver_param_destroy (SolverParameters *sp)
dependent_managed_set_expr (&sp->input, NULL);
go_slist_free_custom (sp->constraints,
(GFreeFunc)gnm_solver_constraint_free);
- g_slist_free (sp->input_cells);
g_free (sp->options.scenario_name);
g_free (sp);
}
@@ -115,6 +114,36 @@ gnm_solver_param_set_input (SolverParameters *sp, GnmValue *v)
if (texpr) gnm_expr_top_unref (texpr);
}
+static GnmValue *
+cb_grab_cells (GnmCellIter const *iter, gpointer user)
+{
+ GList **the_list = user;
+ GnmCell *cell;
+
+ if (NULL == (cell = iter->cell))
+ cell = sheet_cell_create (iter->pp.sheet,
+ iter->pp.eval.col, iter->pp.eval.row);
+ *the_list = g_list_append (*the_list, cell);
+ return NULL;
+}
+
+GSList *
+gnm_solver_param_get_input_cells (SolverParameters const *sp)
+{
+ GnmValue const *vr = gnm_solver_param_get_input (sp);
+ GSList *input_cells = NULL;
+ GnmEvalPos ep;
+
+ if (!vr)
+ return NULL;
+
+ eval_pos_init_sheet (&ep, sp->sheet);
+ workbook_foreach_cell_in_range (&ep, vr, CELL_ITER_ALL,
+ cb_grab_cells,
+ input_cells);
+ return input_cells;
+}
+
void
gnm_solver_param_set_target (SolverParameters *sp, GnmCellRef const *cr)
{
@@ -681,23 +710,23 @@ save_original_values (SolverResults *res,
const SolverParameters *param,
Sheet *sheet)
{
-
- GSList *inputs = param->input_cells;
+ GSList *input_cells = gnm_solver_param_get_input_cells (param);
+ GSList *l;
GnmCell *cell;
- int i = 0;
+ int i;
- while (inputs != NULL) {
- cell = (GnmCell *) inputs->data;
+ for (i = 0, l = input_cells; l; i++, l = l->next) {
+ GnmCell *cell = l->data;
if (cell == NULL || cell->value == NULL)
res->original_values[i] = 0;
else
res->original_values[i] =
value_get_as_float (cell->value);
- inputs = inputs->next;
- ++i;
}
+ g_slist_free (input_cells);
+
cell = gnm_solver_param_get_target_cell (param);
res->original_value_of_obj_fn = value_get_as_float (cell->value);
}
@@ -705,12 +734,18 @@ save_original_values (SolverResults *res,
static void
restore_original_values (SolverResults *res)
{
- GSList *ptr;
- int i = 0;
+ GSList *input_cells = gnm_solver_param_get_input_cells (res->param);
+ GSList *l;
+ int i;
+
+ for (i = 0, l = input_cells; l; i++, l = l->next) {
+ GnmCell *cell = l->data;
+ sheet_cell_set_value
+ (cell,
+ value_new_float (res->original_values[i]));
+ }
- for (ptr = res->param->input_cells; ptr != NULL ; ptr = ptr->next)
- sheet_cell_set_value (ptr->data,
- value_new_float (res->original_values[i++]));
+ g_slist_free (input_cells);
}
/************************************************************************
@@ -939,10 +974,10 @@ check_program_definition_failures (Sheet *sheet,
{
GSList *inputs;
GSList *c;
- GnmCell *cell;
int i;
GnmCell **input_cells_array;
SolverConstraint **constraints_array;
+ GSList *input_cells = gnm_solver_param_get_input_cells (param);
param->n_variables = 0;
@@ -952,8 +987,8 @@ check_program_definition_failures (Sheet *sheet,
/* Count the nbr of the input cells and check that each cell
* is in the list only once. */
- for (inputs = param->input_cells; inputs ; inputs = inputs->next) {
- cell = (GnmCell *) inputs->data;
+ for (inputs = input_cells; inputs ; inputs = inputs->next) {
+ GnmCell *cell = inputs->data;
/* Check that the cell contains a number or is empty. */
if (! (cell->value == NULL || VALUE_IS_EMPTY (cell->value)
@@ -961,6 +996,7 @@ check_program_definition_failures (Sheet *sheet,
*errmsg = _("Some of the input cells contain "
"non-numeric values. Specify a valid "
"input range.");
+ g_slist_free (input_cells);
return TRUE;
}
@@ -968,8 +1004,9 @@ check_program_definition_failures (Sheet *sheet,
}
input_cells_array = g_new (GnmCell *, param->n_variables);
i = 0;
- for (inputs = param->input_cells; inputs ; inputs = inputs->next)
+ for (inputs = input_cells; inputs ; inputs = inputs->next)
input_cells_array[i++] = (GnmCell *) inputs->data;
+ g_slist_free (input_cells);
param->n_constraints = 0;
param->n_int_constraints = 0;
@@ -1120,7 +1157,6 @@ solver_lp_copy (const SolverParameters *src_param, Sheet *new_sheet)
{
SolverParameters *dst_param = solver_param_new (new_sheet);
GSList *constraints;
- GSList *inputs;
dst_param->problem_type = src_param->problem_type;
dependent_managed_set_expr (&dst_param->target,
@@ -1144,17 +1180,6 @@ solver_lp_copy (const SolverParameters *src_param, Sheet *new_sheet)
}
dst_param->constraints = g_slist_reverse (dst_param->constraints);
- /* Copy the input cell list */
- for (inputs = src_param->input_cells; inputs ; inputs = inputs->next) {
- GnmCell const *old_cell = inputs->data;
- GnmCell *new_cell = sheet_cell_fetch (new_sheet,
- old_cell->pos.col,
- old_cell->pos.row);
- dst_param->input_cells =
- g_slist_prepend (dst_param->input_cells, new_cell);
- }
- dst_param->input_cells = g_slist_reverse (dst_param->input_cells);
-
dst_param->n_constraints = src_param->n_constraints;
dst_param->n_variables = src_param->n_variables;
dst_param->n_int_constraints = src_param->n_int_constraints;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]