+static struct casefile *rank_sorted_casefile (struct casefile *cf,
+ const struct sort_criteria *,
+ const struct rank_spec *rs,
+ int n_rank_specs,
+ int idx,
+ const struct missing_values *miss
+ );
+static const char *
+fraction_name(void)
+{
+ static char name[10];
+ switch ( cmd.fraction )
+ {
+ case RANK_BLOM:
+ strcpy (name, "BLOM");
+ break;
+ case RANK_RANKIT:
+ strcpy (name, "RANKIT");
+ break;
+ case RANK_TUKEY:
+ strcpy (name, "TUKEY");
+ break;
+ case RANK_VW:
+ strcpy (name, "VW");
+ break;
+ default:
+ NOT_REACHED ();
+ }
+ return name;
+}
+
+/* Create a label on DEST_VAR, describing its derivation from SRC_VAR and F */
+static void
+create_var_label (struct variable *dest_var,
+ const struct variable *src_var, enum RANK_FUNC f)
+{
+ struct string label;
+ ds_init_empty (&label);
+
+ if ( n_group_vars > 0 )
+ {
+ struct string group_var_str;
+ int g;
+
+ ds_init_empty (&group_var_str);
+
+ for (g = 0 ; g < n_group_vars ; ++g )
+ {
+ if ( g > 0 ) ds_put_cstr (&group_var_str, " ");
+ ds_put_cstr (&group_var_str, group_vars[g]->name);
+ }
+
+ ds_put_format (&label, _("%s of %s by %s"), function_name[f],
+ src_var->name, ds_cstr (&group_var_str));
+ ds_destroy (&group_var_str);
+ }
+ else
+ ds_put_format (&label,_("%s of %s"), function_name[f], src_var->name);
+
+ dest_var->label = strdup (ds_cstr (&label) );
+
+ ds_destroy (&label);
+}
+
+
+static bool
+rank_cmd (const struct sort_criteria *sc,
+ const struct rank_spec *rank_specs, int n_rank_specs)
+{
+ struct sort_criteria criteria;
+ bool result = true;
+ int i;
+ const int n_splits = dict_get_split_cnt (default_dict);
+
+ criteria.crit_cnt = n_splits + n_group_vars + 1;
+ criteria.crits = xnmalloc (criteria.crit_cnt, sizeof *criteria.crits);
+ for (i = 0; i < n_splits ; i++)
+ {
+ struct variable *v = dict_get_split_vars (default_dict)[i];
+ criteria.crits[i].fv = v->fv;
+ criteria.crits[i].width = v->width;
+ criteria.crits[i].dir = SRT_ASCEND;
+ }
+ for (i = 0; i < n_group_vars; i++)
+ {
+ criteria.crits[i + n_splits].fv = group_vars[i]->fv;
+ criteria.crits[i + n_splits].width = group_vars[i]->width;
+ criteria.crits[i + n_splits].dir = SRT_ASCEND;
+ }
+ for (i = 0 ; i < sc->crit_cnt ; ++i )
+ {
+ struct casefile *out ;
+ struct casefile *cf ;
+ struct casereader *reader ;
+ struct casefile *sorted_cf ;
+
+ /* Obtain active file in CF. */
+ if (!procedure (NULL, NULL))
+ return false;
+ cf = proc_capture_output ();
+
+ /* Sort CF into SORTED_CF. */
+ reader = casefile_get_destructive_reader (cf) ;
+ criteria.crits[criteria.crit_cnt - 1] = sc->crits[i];
+ assert ( sc->crits[i].fv == src_vars[i]->fv );
+ sorted_cf = sort_execute (reader, &criteria);
+ casefile_destroy (cf);
+
+ out = rank_sorted_casefile (sorted_cf, &criteria,
+ rank_specs, n_rank_specs,
+ i, &src_vars[i]->miss) ;
+ if ( NULL == out )
+ {
+ result = false ;
+ continue ;
+ }
+
+ proc_set_source (storage_source_create (out));
+ }
+ free (criteria.crits);
+
+ return result ;
+}
+
+/* Hardly a rank function !! */
+static double
+rank_n (double c UNUSED, double cc UNUSED, double cc_1 UNUSED,
+ int i UNUSED, double w)
+{
+ return w;
+}
+
+
+static double
+rank_rank (double c, double cc, double cc_1,
+ int i, double w UNUSED)
+{
+ double rank;
+ if ( c >= 1.0 )
+ {
+ switch (cmd.ties)
+ {
+ case RANK_LOW:
+ rank = cc_1 + 1;
+ break;
+ case RANK_HIGH:
+ rank = cc;
+ break;
+ case RANK_MEAN:
+ rank = cc_1 + (c + 1.0)/ 2.0;
+ break;
+ case RANK_CONDENSE:
+ rank = i;
+ break;
+ default:
+ NOT_REACHED ();
+ }
+ }
+ else
+ {
+ switch (cmd.ties)
+ {
+ case RANK_LOW:
+ rank = cc_1;
+ break;
+ case RANK_HIGH:
+ rank = cc;
+ break;
+ case RANK_MEAN:
+ rank = cc_1 + c / 2.0 ;
+ break;
+ case RANK_CONDENSE:
+ rank = i;
+ break;
+ default:
+ NOT_REACHED ();
+ }
+ }
+
+ return rank;
+}
+
+
+static double
+rank_rfraction (double c, double cc, double cc_1,
+ int i, double w)
+{
+ return rank_rank (c, cc, cc_1, i, w) / w ;
+}
+
+
+static double
+rank_percent (double c, double cc, double cc_1,
+ int i, double w)
+{
+ return rank_rank (c, cc, cc_1, i, w) * 100.0 / w ;
+}
+
+
+static double
+rank_proportion (double c, double cc, double cc_1,
+ int i, double w)
+{
+ const double r = rank_rank (c, cc, cc_1, i, w) ;
+
+ double f;
+
+ switch ( cmd.fraction )
+ {
+ case RANK_BLOM:
+ f = (r - 3.0/8.0) / (w + 0.25);
+ break;
+ case RANK_RANKIT:
+ f = (r - 0.5) / w ;
+ break;
+ case RANK_TUKEY:
+ f = (r - 1.0/3.0) / (w + 1.0/3.0);
+ break;
+ case RANK_VW:
+ f = r / ( w + 1.0);
+ break;
+ default:
+ NOT_REACHED ();
+ }
+
+
+ return (f > 0) ? f : SYSMIS;
+}
+
+static double
+rank_normal (double c, double cc, double cc_1,
+ int i, double w)
+{
+ double f = rank_proportion (c, cc, cc_1, i, w);
+
+ return gsl_cdf_ugaussian_Pinv (f);
+}
+
+static double
+rank_ntiles (double c, double cc, double cc_1,
+ int i, double w)
+{
+ double r = rank_rank (c, cc, cc_1, i, w);
+
+
+ return ( floor (( r * k_ntiles) / ( w + 1) ) + 1);
+}
+
+/* Expected value of the order statistics from an exponential distribution */
+static double
+ee (int j, double w_star)
+{
+ int k;
+ double sum = 0.0;
+
+ for (k = 1 ; k <= j; k++)
+ sum += 1.0 / ( w_star + 1 - k );
+
+ return sum;
+}
+
+
+static double
+rank_savage (double c, double cc, double cc_1,
+ int i UNUSED, double w)
+{
+ double int_part;
+ const int i_1 = floor (cc_1);
+ const int i_2 = floor (cc);
+
+ const double w_star = (modf (w, &int_part) == 0 ) ? w : floor (w) + 1;
+
+ const double g_1 = cc_1 - i_1;
+ const double g_2 = cc - i_2;
+
+ /* The second factor is infinite, when the first is zero.
+ Therefore, evaluate the second, only when the first is non-zero */
+ const double expr1 = (1 - g_1) ? (1 - g_1) * ee(i_1+1, w_star) : ( 1 - g_1);
+ const double expr2 = g_2 ? g_2 * ee (i_2+1, w_star) : g_2 ;
+
+ if ( i_1 == i_2 )
+ return ee (i_1 + 1, w_star) - 1;
+
+ if ( i_1 + 1 == i_2 )
+ return ( ( expr1 + expr2 )/c ) - 1;
+
+ if ( i_1 + 2 <= i_2 )
+ {
+ int j;
+ double sigma = 0.0;
+ for (j = i_1 + 2 ; j <= i_2; ++j )
+ sigma += ee (j, w_star);
+ return ( (expr1 + expr2 + sigma) / c) -1;
+ }
+
+ NOT_REACHED();
+}
+
+
+/* Rank the casefile belonging to CR, starting from the current
+ postition of CR continuing up to and including the ENDth case.
+
+ RS points to an array containing the rank specifications to
+ use. N_RANK_SPECS is the number of elements of RS.
+
+
+ DEST_VAR_INDEX is the index into the rank_spec destvar element
+ to be used for this ranking.
+
+ Prerequisites: 1. The casefile must be sorted according to CRITERION.
+ 2. W is the sum of the non-missing caseweights for this
+ range of the casefile.
+*/
+static void
+rank_cases (struct casereader *cr,
+ unsigned long end,
+ const struct sort_criterion *criterion,
+ const struct missing_values *mv,
+ double w,
+ const struct rank_spec *rs,
+ int n_rank_specs,
+ int dest_var_index,
+ struct casefile *dest)
+{
+ bool warn = true;
+ double cc = 0.0;
+ double cc_1;
+ int iter = 1;
+
+ const int fv = criterion->fv;
+ const int width = criterion->width;