+static
+int reg_inserted (struct variable *v, struct variable **varlist, int n_vars)
+{
+ int i;
+
+ for (i = 0; i < n_vars; i++)
+ {
+ if (v->index == varlist[i]->index)
+ {
+ return 1;
+ }
+ }
+ return 0;
+}
+static void
+reg_print_categorical_encoding (FILE *fp, pspp_linreg_cache *c)
+{
+ int i;
+ size_t j;
+ int n_vars = 0;
+ struct variable **varlist;
+ struct pspp_linreg_coeff coeff;
+ union value *val;
+
+ fprintf (fp, "%s", reg_export_categorical_encode_1);
+
+ varlist = xnmalloc (c->n_indeps, sizeof (*varlist));
+ for (i = 1; i < c->n_indeps; i++) /* c->coeff[0] is the intercept. */
+ {
+ coeff = c->coeff[i];
+ if (coeff.v->type == ALPHA)
+ {
+ if (!reg_inserted (coeff.v, varlist, n_vars))
+ {
+ fprintf (fp, "struct pspp_reg_categorical_variable %s;\n\t", coeff.v->name);
+ varlist[n_vars] = coeff.v;
+ n_vars++;
+ }
+ }
+ }
+ fprintf (fp, "int n_vars = %d;\n\t", n_vars);
+ fprintf (fp, "struct pspp_reg_categorical_variable *varlist[%d] = {", n_vars);
+ for (i = 0; i < n_vars - 1; i++)
+ {
+ fprintf (fp, "&%s,\n\t\t", varlist[i]->name);
+ }
+ fprintf (fp, "&%s};\n\t", varlist[i]->name);
+
+ for (i = 0; i < n_vars; i++)
+ {
+ coeff = c->coeff[i];
+ fprintf (fp, "%s.name = \"%s\";\n\t", varlist[i]->name, varlist[i]->name);
+ fprintf (fp, "%s.n_vals = %d;\n\t", varlist[i]->name, varlist[i]->obs_vals->n_categories);
+
+ for (j = 0; j < varlist[i]->obs_vals->n_categories; j++)
+ {
+ val = cat_subscript_to_value ( (const size_t) j, varlist[i]);
+ fprintf (fp, "%s.values[%d] = \"%s\";\n\t", varlist[i]->name, j, value_to_string (val, varlist[i]));
+ }
+ }
+ fprintf (fp, "%s", reg_export_categorical_encode_2);
+}
+
+static void
+reg_print_depvars (FILE *fp, pspp_linreg_cache *c)
+{
+ int i;
+ struct pspp_linreg_coeff coeff;
+
+ fprintf (fp, "char *model_depvars[%d] = {", c->n_indeps);
+ for (i = 1; i < c->n_indeps; i++)
+ {
+ coeff = c->coeff[i];
+ fprintf (fp, "\"%s\",\n\t\t", coeff.v->name);
+ }
+ coeff = c->coeff[i];
+ fprintf (fp, "\"%s\"};\n\t", coeff.v->name);
+}
+static void
+reg_print_getvar (FILE *fp, pspp_linreg_cache *c)
+{
+ fprintf (fp, "static int\npspp_reg_getvar (char *v_name)\n{\n\t");
+ fprintf (fp, "int i;\n\tint n_vars = %d;\n\t",c->n_indeps);
+ reg_print_depvars (fp, c);
+ fprintf (fp, "for (i = 0; i < n_vars; i++)\n\t{\n\t\t");
+ fprintf (fp, "if (strcmp (v_name, model_depvars[i]) == 0)\n\t\t{\n\t\t\t");
+ fprintf (fp, "return i;\n\t\t}\n\t}\n}\n");
+}