X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fdictionary.c;h=73a9da9a60a8079493b95da846f8dcdb28da8235;hb=888d0f91d57e0c3c5a4206c30ac71eb87bf44227;hp=6fe08f17d14c25e6cf2ea1e5be88d6881a2a4c55;hpb=42489b63e0b4bec2e20c2f55c9791234f7b41764;p=pspp-builds.git diff --git a/src/data/dictionary.c b/src/data/dictionary.c index 6fe08f17..73a9da9a 100644 --- a/src/data/dictionary.c +++ b/src/data/dictionary.c @@ -38,6 +38,8 @@ #include #include +#include "minmax.h" + #include "gettext.h" #define _(msgid) gettext (msgid) @@ -97,8 +99,8 @@ dict_clone (const struct dictionary *s) for (i = 0; i < s->var_cnt; i++) { struct variable *sv = s->var[i]; - struct variable *dv = dict_clone_var_assert (d, sv, sv->name); - var_set_short_name (dv, sv->short_name); + struct variable *dv = dict_clone_var_assert (d, sv, var_get_name (sv)); + var_set_short_name (dv, var_get_short_name (sv)); } d->next_value_idx = s->next_value_idx; @@ -108,14 +110,14 @@ dict_clone (const struct dictionary *s) { d->split = xnmalloc (d->split_cnt, sizeof *d->split); for (i = 0; i < d->split_cnt; i++) - d->split[i] = dict_lookup_var_assert (d, s->split[i]->name); + d->split[i] = dict_lookup_var_assert (d, var_get_name (s->split[i])); } if (s->weight != NULL) - d->weight = dict_lookup_var_assert (d, s->weight->name); + d->weight = dict_lookup_var_assert (d, var_get_name (s->weight)); if (s->filter != NULL) - d->filter = dict_lookup_var_assert (d, s->filter->name); + d->filter = dict_lookup_var_assert (d, var_get_name (s->filter)); d->case_limit = s->case_limit; dict_set_label (d, dict_get_label (s)); @@ -156,6 +158,7 @@ dict_clear (struct dictionary *d) struct variable *v = d->var[i]; var_clear_aux (v); val_labs_destroy (v->val_labs); + var_clear_label (v); free (v->label); free (v); } @@ -244,14 +247,20 @@ dict_get_vars (const struct dictionary *d, struct variable ***vars, count = 0; for (i = 0; i < d->var_cnt; i++) - if (!(exclude_classes & (1u << dict_class_from_id (d->var[i]->name)))) - count++; - + { + enum dict_class class = dict_class_from_id (var_get_name (d->var[i])); + if (!(exclude_classes & (1u << class))) + count++; + } + *vars = xnmalloc (count, sizeof **vars); *cnt = 0; - for (i = 0; i < d->var_cnt; i++) - if (!(exclude_classes & (1u << dict_class_from_id (d->var[i]->name)))) - (*vars)[(*cnt)++] = d->var[i]; + for (i = 0; i < d->var_cnt; i++) + { + enum dict_class class = dict_class_from_id (var_get_name (d->var[i])); + if (!(exclude_classes & (1u << class))) + (*vars)[(*cnt)++] = d->var[i]; + } assert (*cnt == count); } @@ -277,30 +286,28 @@ dict_create_var (struct dictionary *d, const char *name, int width) /* Allocate and initialize variable. */ v = xmalloc (sizeof *v); - str_copy_trunc (v->name, sizeof v->name, name); - v->type = width == 0 ? NUMERIC : ALPHA; + var_set_name (v, name); v->width = width; v->fv = d->next_value_idx; - v->nv = width == 0 ? 1 : DIV_RND_UP (width, MAX_SHORT_STRING); - v->leave = dict_class_from_id (v->name) == DC_SCRATCH; + v->leave = dict_class_from_id (var_get_name (v)) == DC_SCRATCH; v->index = d->var_cnt; mv_init (&v->miss, width); - if (v->type == NUMERIC) + if (var_is_numeric (v)) { - v->print = f8_2; + v->print = fmt_for_output (FMT_F, 8, 2); v->alignment = ALIGN_RIGHT; v->display_width = 8; v->measure = MEASURE_SCALE; } else { - v->print = make_output_format (FMT_A, v->width, 0); + v->print = fmt_for_output (FMT_A, var_get_width (v), 0); v->alignment = ALIGN_LEFT; v->display_width = 8; v->measure = MEASURE_NOMINAL; } v->write = v->print; - v->val_labs = val_labs_create (v->width); + v->val_labs = val_labs_create (var_get_width (v)); v->label = NULL; var_clear_short_name (v); v->aux = NULL; @@ -317,7 +324,7 @@ dict_create_var (struct dictionary *d, const char *name, int width) d->var_cnt++; hsh_force_insert (d->name_tab, v); - d->next_value_idx += v->nv; + d->next_value_idx += var_get_value_cnt (v); return v; } @@ -350,7 +357,7 @@ dict_clone_var (struct dictionary *d, const struct variable *ov, assert (strlen (name) >= 1); assert (strlen (name) <= LONG_NAME_LEN); - nv = dict_create_var (d, name, ov->width); + nv = dict_create_var (d, name, var_get_width (ov)); if (nv == NULL) return NULL; @@ -358,17 +365,16 @@ dict_clone_var (struct dictionary *d, const struct variable *ov, short_name[] is intentionally not copied, because there is no reason to give a new variable with potentially a new name the same short name. */ - nv->leave = ov->leave; - mv_copy (&nv->miss, &ov->miss); - nv->print = ov->print; - nv->write = ov->write; + nv->leave = var_get_leave (ov); + var_set_missing_values (nv, var_get_missing_values (ov)); + var_set_print_format (nv, var_get_print_format (ov)); + var_set_write_format (nv, var_get_write_format (ov)); val_labs_destroy (nv->val_labs); nv->val_labs = val_labs_copy (ov->val_labs); - if (ov->label != NULL) - nv->label = xstrdup (ov->label); - nv->measure = ov->measure; - nv->display_width = ov->display_width; - nv->alignment = ov->alignment; + var_set_label (nv, var_get_label (ov)); + var_set_measure (nv, var_get_measure (ov)); + var_set_display_width (nv, var_get_display_width (ov)); + var_set_alignment (nv, var_get_alignment (ov)); return nv; } @@ -424,7 +430,7 @@ dict_contains_var (const struct dictionary *d, const struct variable *v) /* Compares two double pointers to variables, which should point to elements of a struct dictionary's `var' member array. */ static int -compare_var_ptrs (const void *a_, const void *b_, void *aux UNUSED) +compare_var_ptrs (const void *a_, const void *b_, const void *aux UNUSED) { struct variable *const *a = a_; struct variable *const *b = b_; @@ -435,8 +441,8 @@ compare_var_ptrs (const void *a_, const void *b_, void *aux UNUSED) /* Deletes variable V from dictionary D and frees V. This is a very bad idea if there might be any pointers to V - from outside D. In general, no variable in current_dataset->dict - should be deleted when any transformations are active, because + from outside D. In general, no variable in should be deleted when + any transformations are active on the dictionary's dataset, because those transformations might reference the deleted variable. The safest time to delete a variable is just after a procedure has been executed, as done by MODIFY VARS. @@ -509,7 +515,7 @@ dict_delete_scratch_vars (struct dictionary *d) assert (d != NULL); for (i = 0; i < d->var_cnt; ) - if (dict_class_from_id (d->var[i]->name) == DC_SCRATCH) + if (dict_class_from_id (var_get_name (d->var[i])) == DC_SCRATCH) dict_delete_var (d, d->var[i]); else i++; @@ -532,8 +538,8 @@ dict_reorder_var (struct dictionary *d, struct variable *v, move_element (d->var, d->var_cnt, sizeof *d->var, v->index, new_index); - min_idx = min (v->index, new_index); - max_idx = max (v->index, new_index); + min_idx = MIN (v->index, new_index); + max_idx = MAX (v->index, new_index); for (i = min_idx; i <= max_idx; i++) d->var[i]->index = i; } @@ -585,11 +591,11 @@ dict_rename_var (struct dictionary *d, struct variable *v, assert (new_name != NULL); assert (var_is_plausible_name (new_name, false)); assert (dict_contains_var (d, v)); - assert (!compare_var_names (v->name, new_name, NULL) + assert (!compare_var_names (var_get_name (v), new_name, NULL) || dict_lookup_var (d, new_name) == NULL); hsh_force_delete (d->name_tab, v); - str_copy_trunc (v->name, sizeof v->name, new_name); + var_set_name (v, new_name); hsh_force_insert (d->name_tab, v); if (get_algorithm () == ENHANCED) @@ -623,8 +629,8 @@ dict_rename_vars (struct dictionary *d, assert (d->var[vars[i]->index] == vars[i]); assert (var_is_plausible_name (new_names[i], false)); hsh_force_delete (d->name_tab, vars[i]); - old_names[i] = xstrdup (vars[i]->name); - strcpy (vars[i]->name, new_names[i]); + old_names[i] = xstrdup (var_get_name (vars[i])); + var_set_name (vars[i], new_names[i]); } /* Add the renamed variables back into the name hash, @@ -650,7 +656,7 @@ dict_rename_vars (struct dictionary *d, for (i = 0; i < count; i++) { - strcpy (vars[i]->name, old_names[i]); + var_set_name (vars[i], old_names[i]); hsh_force_insert (d->name_tab, vars[i]); } @@ -701,7 +707,7 @@ dict_get_case_weight (const struct dictionary *d, const struct ccase *c, else { double w = case_num (c, d->weight->fv); - if (w < 0.0 || mv_is_num_missing (&d->weight->miss, w)) + if (w < 0.0 || var_is_num_missing (d->weight, w)) w = 0.0; if ( w == 0.0 && *warn_on_invalid ) { *warn_on_invalid = false; @@ -720,7 +726,7 @@ dict_set_weight (struct dictionary *d, struct variable *v) { assert (d != NULL); assert (v == NULL || dict_contains_var (d, v)); - assert (v == NULL || v->type == NUMERIC); + assert (v == NULL || var_is_numeric (v)); d->weight = v; } @@ -800,10 +806,10 @@ dict_compact_values (struct dictionary *d) { struct variable *v = d->var[i]; - if (dict_class_from_id (v->name) != DC_SCRATCH) + if (dict_class_from_id (var_get_name (v)) != DC_SCRATCH) { v->fv = d->next_value_idx; - d->next_value_idx += v->nv; + d->next_value_idx += var_get_value_cnt (v); i++; } else @@ -821,8 +827,8 @@ dict_get_compacted_value_cnt (const struct dictionary *d) cnt = 0; for (i = 0; i < d->var_cnt; i++) - if (dict_class_from_id (d->var[i]->name) != DC_SCRATCH) - cnt += d->var[i]->nv; + if (dict_class_from_id (var_get_name (d->var[i])) != DC_SCRATCH) + cnt += var_get_value_cnt (d->var[i]); return cnt; } @@ -843,10 +849,10 @@ dict_get_compacted_idx_to_fv (const struct dictionary *d) { struct variable *v = d->var[i]; - if (dict_class_from_id (v->name) != DC_SCRATCH) + if (dict_class_from_id (var_get_name (v)) != DC_SCRATCH) { idx_to_fv[i] = next_value_idx; - next_value_idx += v->nv; + next_value_idx += var_get_value_cnt (v); } else idx_to_fv[i] = -1; @@ -889,7 +895,7 @@ dict_compacting_would_change (const struct dictionary *d) struct variable *v = dict_get_var (d, i); if (v->fv != case_idx) return true; - case_idx += v->nv; + case_idx += var_get_value_cnt (v); } return false; } @@ -935,10 +941,10 @@ dict_make_compactor (const struct dictionary *d) { struct variable *v = d->var[i]; - if (dict_class_from_id (v->name) == DC_SCRATCH) + if (dict_class_from_id (var_get_name (v)) == DC_SCRATCH) continue; if (map != NULL && map->src_idx + map->cnt == v->fv) - map->cnt += v->nv; + map->cnt += var_get_value_cnt (v); else { if (compactor->map_cnt == map_allocated) @@ -947,9 +953,9 @@ dict_make_compactor (const struct dictionary *d) map = &compactor->maps[compactor->map_cnt++]; map->src_idx = v->fv; map->dst_idx = value_idx; - map->cnt = v->nv; + map->cnt = var_get_value_cnt (v); } - value_idx += v->nv; + value_idx += var_get_value_cnt (v); } return compactor; @@ -1164,14 +1170,14 @@ dict_clear_vectors (struct dictionary *d) /* Compares two strings. */ static int -compare_strings (const void *a, const void *b, void *aux UNUSED) +compare_strings (const void *a, const void *b, const void *aux UNUSED) { return strcmp (a, b); } /* Hashes a string. */ static unsigned -hash_string (const void *s, void *aux UNUSED) +hash_string (const void *s, const void *aux UNUSED) { return hsh_hash_string (s); } @@ -1196,9 +1202,10 @@ dict_assign_short_names (struct dictionary *d) for (i = 0; i < d->var_cnt; i++) { struct variable *v = d->var[i]; - if (strlen (v->name) <= SHORT_NAME_LEN) - var_set_short_name (v, v->name); - else if (dict_lookup_var (d, v->short_name) != NULL) + const char *short_name = var_get_short_name (v); + if (strlen (var_get_name (v)) <= SHORT_NAME_LEN) + var_set_short_name (v, var_get_name (v)); + else if (short_name != NULL && dict_lookup_var (d, short_name) != NULL) var_clear_short_name (v); } @@ -1209,7 +1216,8 @@ dict_assign_short_names (struct dictionary *d) for (i = 0; i < d->var_cnt; i++) { struct variable *v = d->var[i]; - if (v->short_name[0] && hsh_insert (short_names, v->short_name) != NULL) + const char *name = var_get_short_name (v); + if (name != NULL && hsh_insert (short_names, (char *) name) != NULL) var_clear_short_name (v); } @@ -1217,16 +1225,23 @@ dict_assign_short_names (struct dictionary *d) for (i = 0; i < d->var_cnt; i++) { struct variable *v = d->var[i]; - if (v->short_name[0] == '\0') + const char *name = var_get_short_name (v); + if (name == NULL) { - int sfx; - - /* Form initial short_name. */ - var_set_short_name (v, v->name); + /* Form initial short_name from the variable name, then + try _A, _B, ... _AA, _AB, etc., if needed.*/ + int trial = 0; + do + { + if (trial == 0) + var_set_short_name (v, var_get_name (v)); + else + var_set_short_name_suffix (v, var_get_name (v), trial - 1); - /* Try _A, _B, ... _AA, _AB, etc., if needed. */ - for (sfx = 0; hsh_insert (short_names, v->short_name) != NULL; sfx++) - var_set_short_name_suffix (v, v->name, sfx); + trial++; + } + while (hsh_insert (short_names, (char *) var_get_short_name (v)) + != NULL); } }