5d7f953827ae49be34cda594c85a0de3d52e98ac
[pspp-builds.git] / src / language / data-io / case-map.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006, 2007 Free Software Foundation, Inc.
3
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.
8
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.
13
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
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <language/data-io/case-map.h>
22
23 #include <stdlib.h>
24
25 #include <data/case.h>
26 #include <data/dictionary.h>
27 #include <data/variable.h>
28 #include <libpspp/assertion.h>
29
30 #include "xalloc.h"
31
32 /* A case map. */
33 struct case_map
34   {
35     size_t value_cnt;   /* Number of values in map. */
36     int *map;           /* For each destination index, the
37                            corresponding source index. */
38   };
39
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
43    map.
44
45    Uses D's aux members, which must otherwise not be in use. */
46 void
47 case_map_prepare (struct dictionary *d) 
48 {
49   size_t var_cnt = dict_get_var_cnt (d);
50   size_t i;
51   
52   for (i = 0; i < var_cnt; i++)
53     {
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);
58     }
59 }
60
61 /* Produces a case map from dictionary D, which must have been
62    previously prepared with start_case_map().
63
64    Does not retain any reference to D, and clears the aux members
65    set up by start_case_map().
66
67    Returns the new case map, or a null pointer if no mapping is
68    required (that is, no data has changed position). */
69 struct case_map *
70 case_map_finish (struct dictionary *d) 
71 {
72   struct case_map *map;
73   size_t var_cnt = dict_get_var_cnt (d);
74   size_t i;
75   int identity_map;
76
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++)
81     map->map[i] = -1;
82
83   identity_map = 1;
84   for (i = 0; i < var_cnt; i++) 
85     {
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);
89       size_t idx;
90
91       if (var_get_case_index (v) != *src_fv)
92         identity_map = 0;
93       
94       for (idx = 0; idx < value_cnt; idx++)
95         {
96           int src_idx = *src_fv + idx;
97           int dst_idx = var_get_case_index (v) + idx;
98           
99           assert (map->map[dst_idx] == -1);
100           map->map[dst_idx] = src_idx;
101         }
102       free (src_fv);
103     }
104
105   if (identity_map) 
106     {
107       case_map_destroy (map);
108       return NULL;
109     }
110
111   while (map->value_cnt > 0 && map->map[map->value_cnt - 1] == -1)
112     map->value_cnt--;
113
114   return map;
115 }
116
117 /* Maps from SRC to DST, applying case map MAP. */
118 void
119 case_map_execute (const struct case_map *map,
120                   const struct ccase *src, struct ccase *dst) 
121 {
122   size_t dst_idx;
123
124   for (dst_idx = 0; dst_idx < map->value_cnt; dst_idx++)
125     {
126       int src_idx = map->map[dst_idx];
127       if (src_idx != -1)
128         *case_data_rw_idx (dst, dst_idx) = *case_data_idx (src, src_idx);
129     }
130 }
131
132 /* Destroys case map MAP. */
133 void
134 case_map_destroy (struct case_map *map) 
135 {
136   if (map != NULL) 
137     {
138       free (map->map);
139       free (map);
140     }
141 }