1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006, 2007 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <language/data-io/case-map.h>
25 #include <data/case.h>
26 #include <data/dictionary.h>
27 #include <data/variable.h>
28 #include <libpspp/assertion.h>
35 size_t value_cnt; /* Number of values in map. */
36 int *map; /* For each destination index, the
37 corresponding source index. */
40 /* Prepares dictionary D for producing a case map. Afterward,
41 the caller may delete, reorder, or rename variables within D
42 at will before using finish_case_map() to produce the case
45 Uses D's aux members, which must otherwise not be in use. */
47 case_map_prepare (struct dictionary *d)
49 size_t var_cnt = dict_get_var_cnt (d);
52 for (i = 0; i < var_cnt; i++)
54 struct variable *v = dict_get_var (d, i);
55 int *src_fv = xmalloc (sizeof *src_fv);
56 *src_fv = var_get_case_index (v);
57 var_attach_aux (v, src_fv, var_dtor_free);
61 /* Produces a case map from dictionary D, which must have been
62 previously prepared with start_case_map().
64 Does not retain any reference to D, and clears the aux members
65 set up by start_case_map().
67 Returns the new case map, or a null pointer if no mapping is
68 required (that is, no data has changed position). */
70 case_map_finish (struct dictionary *d)
73 size_t var_cnt = dict_get_var_cnt (d);
77 map = xmalloc (sizeof *map);
78 map->value_cnt = dict_get_next_value_idx (d);
79 map->map = xnmalloc (map->value_cnt, sizeof *map->map);
80 for (i = 0; i < map->value_cnt; i++)
84 for (i = 0; i < var_cnt; i++)
86 struct variable *v = dict_get_var (d, i);
87 size_t value_cnt = var_get_value_cnt (v);
88 int *src_fv = var_detach_aux (v);
91 if (var_get_case_index (v) != *src_fv)
94 for (idx = 0; idx < value_cnt; idx++)
96 int src_idx = *src_fv + idx;
97 int dst_idx = var_get_case_index (v) + idx;
99 assert (map->map[dst_idx] == -1);
100 map->map[dst_idx] = src_idx;
107 case_map_destroy (map);
111 while (map->value_cnt > 0 && map->map[map->value_cnt - 1] == -1)
117 /* Maps from SRC to DST, applying case map MAP. */
119 case_map_execute (const struct case_map *map,
120 const struct ccase *src, struct ccase *dst)
124 for (dst_idx = 0; dst_idx < map->value_cnt; dst_idx++)
126 int src_idx = map->map[dst_idx];
128 *case_data_rw_idx (dst, dst_idx) = *case_data_idx (src, src_idx);
132 /* Destroys case map MAP. */
134 case_map_destroy (struct case_map *map)