1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2008 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/>. */
18 #include "order-stats.h"
19 #include <libpspp/assertion.h>
20 #include <data/val-type.h>
21 #include <gl/xalloc.h>
22 #include <data/variable.h>
23 #include <data/casereader.h>
28 update_k_lower (struct k *kk,
29 double y_i, double c_i, double cc_i)
41 update_k_upper (struct k *kk,
42 double y_i, double c_i, double cc_i)
44 if ( cc_i > kk->tc && kk->c_p1 == 0)
54 update_k_values (const struct ccase *cx, double y_i, double c_i, double cc_i,
55 struct order_stats **os, size_t n_os)
58 for (j = 0 ; j < n_os ; ++j)
61 struct order_stats *tos = os[j];
62 struct statistic *stat = (struct statistic *) tos;
63 for (k = 0 ; k < tos->n_k; ++k)
65 struct k *myk = &tos->k[k];
66 update_k_lower (myk, y_i, c_i, cc_i);
67 update_k_upper (myk, y_i, c_i, cc_i);
70 if ( stat->accumulate )
71 stat->accumulate (stat, cx, c_i, cc_i, y_i);
77 order_stats_accumulate (struct order_stats **os, size_t nos,
78 struct casereader *reader,
79 const struct variable *wv,
80 const struct variable *var)
84 double prev_value = -DBL_MAX;
89 case_nullify (&prev_cx);
91 for (; casereader_read (reader, &cx); case_destroy (&cx))
93 const double weight = wv ? case_data (&cx, wv)->f : 1.0;
94 const double this_value = case_data (&cx, var)->f;
96 /* The casereader MUST be sorted */
97 assert (this_value >= prev_value);
99 case_destroy (&prev_cx);
101 if ( prev_value == -DBL_MAX || prev_value == this_value)
104 if ( prev_value > -DBL_MAX && this_value > prev_value)
106 update_k_values (&prev_cx, prev_value, c_i, cc_i, os, nos);
111 prev_value = this_value;
112 case_clone (&prev_cx, &cx);
115 update_k_values (&prev_cx, prev_value, c_i, cc_i, os, nos);
116 case_destroy (&prev_cx);
118 casereader_destroy (reader);