From 46dfa3ec7417bbb7452f152a6b62435006259633 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Fri, 26 Sep 2008 18:44:02 +0800 Subject: [PATCH] Avoid forcing an int into a void * --- src/data/category.c | 2 +- src/data/value.c | 27 +++++++++++---------------- src/data/value.h | 8 ++------ src/language/stats/binomial.c | 4 ++-- src/language/stats/examine.q | 2 +- src/language/stats/freq.c | 7 +++---- src/language/stats/oneway.q | 20 ++++++++------------ src/language/stats/t-test.q | 16 +++++----------- src/math/coefficient.c | 4 ++-- src/math/covariance-matrix.c | 4 ++-- src/math/group.c | 17 ++++++++++------- src/math/group.h | 12 +++++------- src/ui/gui/find-dialog.c | 2 +- 13 files changed, 53 insertions(+), 72 deletions(-) diff --git a/src/data/category.c b/src/data/category.c index 1620bc7f..7190418c 100644 --- a/src/data/category.c +++ b/src/data/category.c @@ -108,7 +108,7 @@ cat_value_find (const struct variable *v, const union value *val) { candidate = obs_vals->vals + i; assert (candidate != NULL); - if (!compare_values (candidate, val, var_get_width (v))) + if (!compare_values (candidate, val, v)) { return i; } diff --git a/src/data/value.c b/src/data/value.c index 49555d9a..9fcf1cbb 100644 --- a/src/data/value.c +++ b/src/data/value.c @@ -20,6 +20,7 @@ #include #include #include +#include "variable.h" #include "xalloc.h" @@ -46,8 +47,12 @@ value_create (int width) Only the short string portion of longer strings are compared. */ int -compare_values (const union value *a, const union value *b, int width) +compare_values (const void *a_, const void *b_, const void *var_) { + const union value *a = a_; + const union value *b = b_; + const struct variable *var = var_; + int width = var_get_width (var); return (width == 0 ? (a->f < b->f ? -1 : a->f > b->f) : memcmp (a->s, b->s, MIN (MAX_SHORT_STRING, width))); @@ -56,24 +61,14 @@ compare_values (const union value *a, const union value *b, int width) /* Create a hash of V, which has the given WIDTH. Only the short string portion of a longer string is hashed. */ unsigned -hash_value (const union value *v, int width) +hash_value (const void *v_, const void *var_) { + const union value *v = v_; + const struct variable *var = var_; + int width = var_get_width (var); return (width == 0 ? hsh_hash_double (v->f) - : hsh_hash_bytes (v->s, MIN (MAX_SHORT_STRING, width))); -} - - -int -compare_ptr_values (const union value **v1, const union value **v2, int width) -{ - return compare_values (*v1, *v2, width); -} - -unsigned -hash_ptr_value (const union value **v, int width) -{ - return hash_value (*v, width); + : hsh_hash_bytes (v->s, width)); } diff --git a/src/data/value.h b/src/data/value.h index 4554a366..9103c6b3 100644 --- a/src/data/value.h +++ b/src/data/value.h @@ -39,12 +39,8 @@ union value union value *value_dup (const union value *, int width); union value *value_create (int width); -int compare_values (const union value *, const union value *, int width); -unsigned hash_value (const union value *, int width); - -int compare_ptr_values (const union value **, const union value **, int width); -unsigned hash_ptr_value (const union value **, int width); - +int compare_values (const void *, const void *, const void *var); +unsigned hash_value (const void *, const void *var); static inline size_t value_cnt_from_width (int width); void value_copy (union value *, const union value *, int width); diff --git a/src/language/stats/binomial.c b/src/language/stats/binomial.c index f5435051..5022cebd 100644 --- a/src/language/stats/binomial.c +++ b/src/language/stats/binomial.c @@ -119,14 +119,14 @@ do_binomial (const struct dictionary *dict, cat1[v].value = value_dup (value, width); cat1[v].count = w; } - else if ( 0 == compare_values (cat1[v].value, value, width)) + else if ( 0 == compare_values (cat1[v].value, value, var)) cat1[v].count += w; else if ( NULL == cat2[v].value ) { cat2[v].value = value_dup (value, width); cat2[v].count = w; } - else if ( 0 == compare_values (cat2[v].value, value, width)) + else if ( 0 == compare_values (cat2[v].value, value, var)) cat2[v].count += w; else if ( bst->category1 == SYSMIS) msg (ME, _("Variable %s is not dichotomous"), var_get_name (var)); diff --git a/src/language/stats/examine.q b/src/language/stats/examine.q index 7f197ec3..e341b18a 100644 --- a/src/language/stats/examine.q +++ b/src/language/stats/examine.q @@ -1338,7 +1338,7 @@ show_summary (const struct variable **dependent_var, int n_dep_var, if ( last_value == NULL || compare_values (last_value, result->value[0], - var_get_width(fctr->indep_var[0]))) + fctr->indep_var[0])) { struct string str; diff --git a/src/language/stats/freq.c b/src/language/stats/freq.c index f2054380..afad84ee 100644 --- a/src/language/stats/freq.c +++ b/src/language/stats/freq.c @@ -30,16 +30,15 @@ compare_freq ( const void *_f1, const void *_f2, const void *_var) const struct freq *f2 = _f2; const struct variable *var = _var; - return compare_values (f1->value, f2->value, var_get_width (var) ); + return compare_values (f1->value, f2->value, var ); } unsigned int -hash_freq (const void *_f, const void *_var) +hash_freq (const void *_f, const void *var) { const struct freq *f = _f; - const struct variable *var = _var; - return hash_value (f->value, var_get_width (var)); + return hash_value (f->value, var); } /* Free function to be used on FR whose value parameter has been copied */ diff --git a/src/language/stats/oneway.q b/src/language/stats/oneway.q index 46cdd566..8c5d0c99 100644 --- a/src/language/stats/oneway.q +++ b/src/language/stats/oneway.q @@ -873,13 +873,9 @@ precalc ( struct cmd_oneway *cmd UNUSED ) The hash contains a group_statistics structure, and is keyed by value of the independent variable */ - gp->group_hash = - hsh_create(4, - (hsh_compare_func *) compare_group, - (hsh_hash_func *) hash_group, - (hsh_free_func *) free_group, - (void *) var_get_width (indep_var) ); - + gp->group_hash = hsh_create(4, compare_group, hash_group, + (hsh_free_func *) free_group, + indep_var); totals->sum=0; totals->n=0; @@ -919,10 +915,10 @@ run_oneway (struct cmd_oneway *cmd, taint = taint_clone (casereader_get_taint (input)); global_group_hash = hsh_create(4, - (hsh_compare_func *) compare_values, - (hsh_hash_func *) hash_value, + compare_values, + hash_value, free_value, - (void *) var_get_width (indep_var) ); + indep_var); precalc(cmd); @@ -957,7 +953,7 @@ run_oneway (struct cmd_oneway *cmd, struct group_statistics *gs; - gs = hsh_find(group_hash, (void *) indep_val ); + gs = hsh_find (group_hash, indep_val ); if ( ! gs ) { @@ -970,7 +966,7 @@ run_oneway (struct cmd_oneway *cmd, gs->minimum = DBL_MAX; gs->maximum = -DBL_MAX; - hsh_insert ( group_hash, (void *) gs ); + hsh_insert ( group_hash, gs ); } if (!var_is_value_missing (v, val, exclude)) diff --git a/src/language/stats/t-test.q b/src/language/stats/t-test.q index 116f04b4..3d278743 100644 --- a/src/language/stats/t-test.q +++ b/src/language/stats/t-test.q @@ -80,8 +80,8 @@ struct group_properties /* The comparison criterion */ enum comparison criterion; - /* The width of the independent variable */ - int indep_width ; + /* The independent variable */ + struct variable *indep_var; union { /* The value of the independent variable at which groups are determined to @@ -1676,7 +1676,7 @@ group_precalc (struct cmd_t_test *cmd ) /* There's always 2 groups for a T - TEST */ ttpr->n_groups = 2; - gp.indep_width = var_get_width (indep_var); + gp.indep_var = indep_var; ttpr->group_hash = hsh_create (2, (hsh_compare_func *) compare_group_binary, @@ -1888,10 +1888,6 @@ compare_group_binary (const struct group_statistics *a, if ( p->criterion == CMP_LE ) { - /* less-than comparision is not meaningfull for - alpha variables, so we shouldn't ever arrive here */ - assert (p->indep_width == 0 ) ; - flag_a = ( a->id.f < p->v.critical_value ) ; flag_b = ( b->id.f < p->v.critical_value ) ; } @@ -1918,8 +1914,6 @@ hash_group_binary (const struct group_statistics *g, if ( p->criterion == CMP_LE ) { - /* Not meaningfull to do a less than compare for alpha values ? */ - assert (p->indep_width == 0 ) ; flag = ( g->id.f < p->v.critical_value ) ; } else if ( p->criterion == CMP_EQ) @@ -1939,10 +1933,10 @@ short which_group (const struct group_statistics *g, const struct group_properties *p) { - if ( 0 == compare_values (&g->id, &p->v.g_value[0], p->indep_width)) + if ( 0 == compare_values (&g->id, &p->v.g_value[0], p->indep_var)) return 0; - if ( 0 == compare_values (&g->id, &p->v.g_value[1], p->indep_width)) + if ( 0 == compare_values (&g->id, &p->v.g_value[1], p->indep_var)) return 1; return 2; diff --git a/src/math/coefficient.c b/src/math/coefficient.c index 2feeedba..f738c783 100644 --- a/src/math/coefficient.c +++ b/src/math/coefficient.c @@ -160,7 +160,7 @@ pspp_coeff_var_to_coeff (const struct variable *v, struct pspp_coeff **coefs, size_t i = 0; size_t j = 0; size_t v_idx; - int found = 0; + struct pspp_coeff *result = NULL; if (v != NULL) @@ -187,7 +187,7 @@ pspp_coeff_var_to_coeff (const struct variable *v, struct pspp_coeff **coefs, { j = i; while (j < n_coef && compare_values (pspp_coeff_get_value (coefs[j], v), - val, var_get_width (v)) != 0) + val, v) != 0) { j++; } diff --git a/src/math/covariance-matrix.c b/src/math/covariance-matrix.c index 54143791..f929a370 100644 --- a/src/math/covariance-matrix.c +++ b/src/math/covariance-matrix.c @@ -74,7 +74,7 @@ column_iterate (struct design_matrix *cov, const struct variable *v, col += i; y = -1.0 * cat_get_category_count (i, v) / ssize; tmp_val = cat_subscript_to_value (i, v); - if (compare_values (tmp_val, val1, var_get_width (v))) + if (compare_values (tmp_val, val1, v)) { y += -1.0; } @@ -106,7 +106,7 @@ void covariance_pass_two (struct design_matrix *cov, double mean1, double mean2, row += i; x = -1.0 * cat_get_category_count (i, v1) / ssize; tmp_val = cat_subscript_to_value (i, v1); - if (compare_values (tmp_val, val1, var_get_width (v1))) + if (compare_values (tmp_val, val1, v1)) { x += 1.0; } diff --git a/src/math/group.c b/src/math/group.c index 29c5ab23..6101c350 100644 --- a/src/math/group.c +++ b/src/math/group.c @@ -29,21 +29,24 @@ /* Return -1 if the id of a is less than b; +1 if greater than and 0 if equal */ int -compare_group (const struct group_statistics *a, - const struct group_statistics *b, - int width) +compare_group (const void *a_, + const void *b_, + const void *var) { - return compare_values(&a->id, &b->id, width); + const struct group_statistics *a = a_; + const struct group_statistics *b = b_; + return compare_values(&a->id, &b->id, var); } -unsigned -hash_group (const struct group_statistics *g, int width) +unsigned int +hash_group (const void *g_, const void *var) { unsigned id_hash; + const struct group_statistics *g = g_;; - id_hash = hash_value(&g->id, width); + id_hash = hash_value(&g->id, var); return id_hash; } diff --git a/src/math/group.h b/src/math/group.h index bc82c8ab..c5470b25 100644 --- a/src/math/group.h +++ b/src/math/group.h @@ -18,10 +18,8 @@ #ifndef GROUP_H #define GROUP_H - #include - /* Statistics for grouped data */ struct group_statistics { @@ -67,17 +65,17 @@ struct group_statistics }; - +struct variable ; /* These funcs are useful for hash tables */ /* Return -1 if the id of a is less than b; +1 if greater than and 0 if equal */ -int compare_group (const struct group_statistics *a, - const struct group_statistics *b, - int width); +int compare_group (const void *a, + const void *b, + const void *var); -unsigned hash_group (const struct group_statistics *g, int width); +unsigned int hash_group (const void *g, const void *var); void free_group (struct group_statistics *v, void *aux); diff --git a/src/ui/gui/find-dialog.c b/src/ui/gui/find-dialog.c index 5d98b4ac..7764d04a 100644 --- a/src/ui/gui/find-dialog.c +++ b/src/ui/gui/find-dialog.c @@ -464,7 +464,7 @@ value_compare (const struct comparator *cmptr, const union value *v) { const struct value_comparator *vc = (const struct value_comparator *) cmptr; - return 0 == compare_values (v, vc->pattern, var_get_width (cmptr->var)); + return 0 == compare_values (v, vc->pattern, cmptr->var); } -- 2.30.2