1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2007 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU 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, see <http://www.gnu.org/licenses/>. */
19 #include <data/case-map.h>
23 #include <data/dictionary.h>
24 #include <data/variable.h>
25 #include <data/case.h>
26 #include <libpspp/alloc.h>
27 #include <libpspp/assertion.h>
32 size_t value_cnt; /* Number of values in map. */
33 int *map; /* For each destination index, the
34 corresponding source index. */
37 /* Creates and returns an empty map. */
38 static struct case_map *
39 create_case_map (size_t n)
44 map = xmalloc (sizeof *map);
46 map->map = xnmalloc (n, sizeof *map->map);
47 for (i = 0; i < map->value_cnt; i++)
53 /* Inserts into MAP a mapping of the CNT values starting at FROM
54 to the CNT values starting at TO. */
56 insert_mapping (struct case_map *map, size_t from, size_t to, size_t cnt)
60 assert (to + cnt <= map->value_cnt);
61 for (i = 0; i < cnt; i++)
63 assert (map->map[to + i] == -1);
64 map->map[to + i] = from + i;
68 /* Destroys case map MAP. */
70 case_map_destroy (struct case_map *map)
79 /* Maps from SRC to DST, applying case map MAP. */
81 case_map_execute (const struct case_map *map,
82 const struct ccase *src, struct ccase *dst)
86 case_create (dst, map->value_cnt);
87 for (dst_idx = 0; dst_idx < map->value_cnt; dst_idx++)
89 int src_idx = map->map[dst_idx];
91 *case_data_rw_idx (dst, dst_idx) = *case_data_idx (src, src_idx);
95 /* Returns the number of `union value's in cases created by
98 case_map_get_value_cnt (const struct case_map *map)
100 return map->value_cnt;
103 /* Creates and returns a case_map that can be used to compact
104 cases for dictionary D.
106 Compacting a case eliminates "holes" between values and after
107 the last value. (Holes are created by deleting variables.)
109 All variables are compacted if EXCLUDE_CLASSES is 0, or it may
110 contain one or more of (1u << DC_ORDINARY), (1u << DC_SYSTEM),
111 or (1u << DC_SCRATCH) to cause the corresponding type of
112 variable to be deleted during compaction. */
114 case_map_to_compact_dict (const struct dictionary *d,
115 unsigned int exclude_classes)
118 struct case_map *map;
122 assert ((exclude_classes & ~((1u << DC_ORDINARY)
124 | (1u << DC_SCRATCH))) == 0);
126 map = create_case_map (dict_count_values (d, exclude_classes));
127 var_cnt = dict_get_var_cnt (d);
129 for (i = 0; i < var_cnt; i++)
131 struct variable *v = dict_get_var (d, i);
132 enum dict_class class = dict_class_from_id (var_get_name (v));
134 if (!(exclude_classes & (1u << class)))
136 size_t value_cnt = var_get_value_cnt (v);
137 insert_mapping (map, var_get_case_index (v), value_idx, value_cnt);
138 value_idx += value_cnt;
141 assert (value_idx == map->value_cnt);
146 /* Prepares dictionary D for producing a case map. Afterward,
147 the caller may delete, reorder, or rename variables within D
148 at will before using case_map_from_dict() to produce the case
151 Uses D's aux members, which must otherwise not be in use. */
153 case_map_prepare_dict (const struct dictionary *d)
155 size_t var_cnt = dict_get_var_cnt (d);
158 for (i = 0; i < var_cnt; i++)
160 struct variable *v = dict_get_var (d, i);
161 int *src_fv = xmalloc (sizeof *src_fv);
162 *src_fv = var_get_case_index (v);
163 var_attach_aux (v, src_fv, var_dtor_free);
167 /* Produces a case map from dictionary D, which must have been
168 previously prepared with case_map_prepare_dict().
170 Does not retain any reference to D, and clears the aux members
171 set up by case_map_prepare_dict().
173 Returns the new case map, or a null pointer if no mapping is
174 required (that is, no data has changed position). */
176 case_map_from_dict (const struct dictionary *d)
178 struct case_map *map;
179 size_t var_cnt = dict_get_var_cnt (d);
181 bool identity_map = true;
183 map = create_case_map (dict_get_next_value_idx (d));
184 for (i = 0; i < var_cnt; i++)
186 struct variable *v = dict_get_var (d, i);
187 size_t value_cnt = var_get_value_cnt (v);
188 int *src_fv = (int *) var_detach_aux (v);
190 if (var_get_case_index (v) != *src_fv)
191 identity_map = false;
193 insert_mapping (map, *src_fv, var_get_case_index (v), value_cnt);
200 case_map_destroy (map);
204 while (map->value_cnt > 0 && map->map[map->value_cnt - 1] == -1)
210 /* Creates and returns a case map for mapping variables in OLD to
211 variables in NEW based on their name. For every variable in
212 NEW, there must be a variable in OLD with the same name, type,
215 case_map_by_name (const struct dictionary *old,
216 const struct dictionary *new)
218 struct case_map *map;
219 size_t var_cnt = dict_get_var_cnt (new);
222 map = create_case_map (dict_get_next_value_idx (new));
223 for (i = 0; i < var_cnt; i++)
225 struct variable *nv = dict_get_var (new, i);
226 struct variable *ov = dict_lookup_var_assert (old, var_get_name (nv));
227 assert (var_get_width (nv) == var_get_width (ov));
228 insert_mapping (map, var_get_case_index (ov), var_get_case_index (nv),
229 var_get_value_cnt (ov));
234 /* Prints the mapping represented by case map CM to stdout, for
235 debugging purposes. */
237 case_map_dump (const struct case_map *cm)
240 for (i = 0 ; i < cm->value_cnt; ++i )
241 printf ("%d -> %d\n", i, cm->map[i]);