1 /* PSPP - RANK. -*-c-*-
3 Copyright (C) 2005 Free Software Foundation, Inc.
4 Author: John Darrington 2005
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include "sort-criteria.h"
24 #include <data/dictionary.h>
25 #include <procedure.h>
26 #include <data/variable.h>
27 #include <language/command.h>
28 #include <language/stats/sort-criteria.h>
29 #include <libpspp/compiler.h>
30 #include <math/sort.h>
33 #define _(msgid) gettext (msgid)
49 +missing=miss:!exclude/include.
72 struct variable **destvars;
73 struct variable *srcvar;
77 static struct rank_spec *rank_specs;
78 static size_t n_rank_specs;
80 static struct sort_criteria *sc;
82 static struct variable **group_vars;
83 static size_t n_group_vars;
85 static struct cmd_rank cmd;
97 if ( !parse_rank(&cmd) )
101 for (i = 0 ; i < sc->crit_cnt ; ++i )
103 struct sort_criterion *crit = &sc->crits[i];
105 printf("Dir: %d; Index: %d\n", crit->dir, crit->fv);
108 for (i = 0 ; i < n_group_vars ; ++i )
109 printf("Group var: %s\n",group_vars[0]->name);
111 for (i = 0 ; i < n_rank_specs ; ++i )
114 printf("Ranks spec %d; Func: %d\n",i, rank_specs[i].rfunc);
116 for (j=0; j < sc->crit_cnt ; ++j )
117 printf("Dest var is \"%s\"\n", rank_specs[i].destvars[j]->name);
124 for (i = 0 ; i < n_rank_specs ; ++i )
126 free(rank_specs[i].destvars);
131 sort_destroy_criteria(sc);
138 /* Parser for the variables sub command
139 Returns 1 on success */
141 rank_custom_variables(struct cmd_rank *cmd UNUSED)
143 static const int terminators[2] = {T_BY, 0};
147 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
151 sc = sort_parse_criteria (default_dict, 0, 0, 0, terminators);
153 if ( lex_match(T_BY) )
155 if ((token != T_ID || dict_lookup_var (default_dict, tokid) == NULL))
160 if (!parse_variables (default_dict, &group_vars, &n_group_vars,
161 PV_NO_DUPLICATE | PV_NUMERIC | PV_NO_SCRATCH) )
172 /* Return a name for a new variable which ranks the variable VAR_NAME,
173 according to the ranking function F.
174 If IDX is non zero, then IDX is used as a disambiguating number.
175 FIXME: This is not very robust.
178 new_variable_name(const char *ranked_var_name, enum RANK_FUNC f, int idx)
180 static char new_name[SHORT_NAME_LEN + 1];
181 char temp[SHORT_NAME_LEN + 1];
189 strcpy(new_name,"R");
195 strcpy(new_name,"N");
200 strcpy(new_name,"P");
204 strcpy(new_name,"S");
212 strncat(new_name, ranked_var_name, 7);
216 strncpy(temp, ranked_var_name, 3);
217 snprintf(new_name, SHORT_NAME_LEN, "%s%03d", temp, idx);
223 /* Parse the [/rank INTO var1 var2 ... varN ] clause */
225 parse_rank_function(struct cmd_rank *cmd UNUSED, enum RANK_FUNC f)
227 static const struct fmt_spec f8_2 = {FMT_F, 8, 2};
231 rank_specs = xnrealloc(rank_specs, n_rank_specs, sizeof *rank_specs);
232 rank_specs[n_rank_specs - 1].rfunc = f;
234 rank_specs[n_rank_specs - 1].destvars =
235 xcalloc (sc->crit_cnt, sizeof (struct variable *));
237 if (lex_match_id("INTO"))
239 struct variable *destvar;
241 while( token == T_ID )
244 if ( dict_lookup_var (default_dict, tokid) != NULL )
246 msg(SE, _("Variable %s already exists."), tokid);
249 if ( var_count > sc->crit_cnt )
251 msg(SE, _("Too many variables in INTO clause."));
255 destvar = dict_create_var (default_dict, tokid, 0);
258 destvar->print = destvar->write = f8_2;
261 rank_specs[n_rank_specs - 1].destvars[var_count - 1] = destvar ;
268 /* Allocate rank variable names to all those which haven't had INTO
269 variables assigned */
270 while (var_count < sc->crit_cnt)
273 struct variable *destvar ;
274 const struct variable *v = dict_get_var(default_dict,
275 sc->crits[var_count].fv);
280 new_name = new_variable_name(v->name, f, idx);
282 destvar = dict_create_var (default_dict, new_name, 0);
286 } while( !destvar ) ;
288 destvar->print = destvar->write = f8_2;
290 rank_specs[n_rank_specs - 1].destvars[var_count] = destvar ;
300 rank_custom_rank(struct cmd_rank *cmd )
302 return parse_rank_function(cmd, RANK);
306 rank_custom_normal(struct cmd_rank *cmd )
308 return parse_rank_function(cmd, NORMAL);
312 rank_custom_percent(struct cmd_rank *cmd )
314 return parse_rank_function(cmd, NORMAL);
318 rank_custom_rfraction(struct cmd_rank *cmd )
320 return parse_rank_function(cmd, RFRACTION);
324 rank_custom_proportion(struct cmd_rank *cmd )
326 return parse_rank_function(cmd, PROPORTION);
330 rank_custom_n(struct cmd_rank *cmd )
332 return parse_rank_function(cmd, N);
336 rank_custom_savage(struct cmd_rank *cmd )
338 return parse_rank_function(cmd, SAVAGE);
343 rank_custom_ntiles(struct cmd_rank *cmd )
345 if ( lex_force_match('(') )
347 if ( lex_force_int() )
350 lex_force_match(')');
358 return parse_rank_function(cmd, NTILES);