magic-elimination.patch from patch #6230.
[pspp-builds.git] / src / language / stats / flip.c
index 9d116799553c96583c654e0345dd5322ef63a48c..760b2ffc30932d99872516a8d4154dcf919ed84e 100644 (file)
@@ -1,20 +1,18 @@
-/* PSPP - computes sample statistics.
+/* PSPP - a program for statistical analysis.
    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
 
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License as
-   published by the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-   This program is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02110-1301, USA. */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include "config.h"
 
@@ -23,9 +21,6 @@
 #include <float.h>
 #include <limits.h>
 #include <stdlib.h>
-#ifdef HAVE_SYS_TYPES_H
-#include <sys/types.h>
-#endif
 
 #include <data/case.h>
 #include <data/casereader.h>
@@ -64,10 +59,8 @@ struct flip_pgm
   {
     struct pool *pool;          /* Pool containing FLIP data. */
     const struct variable **var;      /* Variables to transpose. */
-    int *idx_to_fv;             /* var[]->index to compacted sink case fv. */
     size_t var_cnt;             /* Number of elements in `var'. */
     int case_cnt;               /* Pre-flip case count. */
-    size_t case_size;           /* Post-flip bytes per case. */
 
     struct variable *new_names; /* Variable containing new variable names. */
     struct varname *new_names_head; /* First new variable. */
@@ -104,8 +97,6 @@ cmd_flip (struct lexer *lexer, struct dataset *ds)
 
   flip = pool_create_container (struct flip_pgm, pool);
   flip->var = NULL;
-  flip->idx_to_fv = dict_get_compacted_dict_index_to_case_index (dict);
-  pool_register (flip->pool, free, flip->idx_to_fv);
   flip->var_cnt = 0;
   flip->case_cnt = 0;
   flip->new_names = NULL;
@@ -121,7 +112,7 @@ cmd_flip (struct lexer *lexer, struct dataset *ds)
     {
       lex_match (lexer, '=');
       if (!parse_variables_const (lexer, dict, &flip->var, &flip->var_cnt,
-                            PV_NO_DUPLICATE))
+                                  PV_NO_DUPLICATE))
        goto error;
       lex_match (lexer, '/');
     }
@@ -151,8 +142,7 @@ cmd_flip (struct lexer *lexer, struct dataset *ds)
          }
     }
 
-  output_buf = pool_nalloc (flip->pool,
-                                  flip->var_cnt, sizeof *output_buf);
+  output_buf = pool_nalloc (flip->pool, flip->var_cnt, sizeof *output_buf);
 
   flip->file = pool_tmpfile (flip->pool);
   if (flip->file == NULL)
@@ -175,7 +165,6 @@ cmd_flip (struct lexer *lexer, struct dataset *ds)
   flip->case_cnt = 1;
 
   /* Read the active file into a flip_sink. */
-  proc_make_temporary_transformations_permanent (ds);
   proc_discard_output (ds);
 
   input = proc_open (ds);
@@ -201,11 +190,10 @@ cmd_flip (struct lexer *lexer, struct dataset *ds)
       proc_discard_active_file (ds);
       goto error;
     }
-  flip->case_size = dict_get_case_size (dict);
 
   /* Set up flipped data for reading. */
   reader = casereader_create_sequential (NULL, dict_get_next_value_idx (dict),
-                                         flip->case_cnt,
+                                         flip->var_cnt,
                                          &flip_casereader_class, flip);
   proc_set_active_file_data (ds, reader);
   return lex_end_of_command (lexer);
@@ -290,7 +278,7 @@ build_dictionary (struct dictionary *dict, struct flip_pgm *flip)
          return false;
        }
 
-      for (i = 0; i < flip->case_cnt; i++)
+      for (i = 0; i < flip->case_cnt - 1; i++)
        {
           struct variable *v;
          char s[SHORT_NAME_LEN + 1];
@@ -323,11 +311,10 @@ write_flip_case (struct flip_pgm *flip, const struct ccase *c)
   if (flip->new_names != NULL)
     {
       struct varname *v = pool_alloc (flip->pool, sizeof *v);
-      int fv = flip->idx_to_fv[var_get_dict_index (flip->new_names)];
       v->next = NULL;
       if (var_is_numeric (flip->new_names))
         {
-          double f = case_num_idx (c, fv);
+          double f = case_num (c, flip->new_names);
 
           if (f == SYSMIS)
             strcpy (v->name, "VSYSMIS");
@@ -341,7 +328,7 @@ write_flip_case (struct flip_pgm *flip, const struct ccase *c)
       else
        {
          int width = MIN (var_get_width (flip->new_names), MAX_SHORT_STRING);
-         memcpy (v->name, case_str_idx (c, fv), width);
+         memcpy (v->name, case_str (c, flip->new_names), width);
          v->name[width] = 0;
        }
 
@@ -355,15 +342,8 @@ write_flip_case (struct flip_pgm *flip, const struct ccase *c)
   /* Write to external file. */
   for (i = 0; i < flip->var_cnt; i++)
     {
-      double out;
-
-      if (var_is_numeric (flip->var[i]))
-        {
-          int fv = flip->idx_to_fv[var_get_dict_index (flip->var[i])];
-          out = case_num_idx (c, fv);
-        }
-      else
-        out = SYSMIS;
+      const struct variable *v = flip->var[i];
+      double out = var_is_numeric (v) ? case_num (c, v) : SYSMIS;
       fwrite (&out, sizeof out, 1, flip->file);
     }
   return true;