- if (var_has_vardict (v))
- {
- const struct vardict_info *vdi = var_get_vardict (v);
- return (vdi->dict_index >= 0
- && vdi->dict_index < d->var_cnt
- && d->var[vdi->dict_index] == v);
- }
- else
- return false;
-}
-
-/* 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_, const void *aux UNUSED)
-{
- struct variable *const *a = a_;
- struct variable *const *b = b_;
-
- return *a < *b ? -1 : *a > *b;
-}
-
-/* Sets the dict_index in V's vardict to DICT_INDEX. */
-static void
-set_var_dict_index (struct variable *v, int dict_index)
-{
- struct vardict_info vdi = *var_get_vardict (v);
- struct dictionary *d = vdi.dict;
- vdi.dict_index = dict_index;
- var_set_vardict (v, &vdi);
-
- if ( d->callbacks && d->callbacks->var_changed )
- d->callbacks->var_changed (d, dict_index, d->cb_data);
-}
-
-/* Sets the case_index in V's vardict to DICT_INDEX. */
-static void
-set_var_case_index (struct variable *v, int case_index)
-{
- struct vardict_info vdi = *var_get_vardict (v);
- vdi.case_index = case_index;
- var_set_vardict (v, &vdi);
-}
-
-/* Re-sets the dict_index in the dictionary variables with
- indexes from FROM to TO (exclusive). */
-static void
-reindex_vars (struct dictionary *d, size_t from, size_t to)
-{
- size_t i;
-
- for (i = from; i < to; i++)
- set_var_dict_index (d->var[i], i);
-}
-
-/* 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 the active file's
- dictionary 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.
-
- Pointers to V within D are not a problem, because
- dict_delete_var() knows to remove V from split variables,
- weights, filters, etc. */
-void
-dict_delete_var (struct dictionary *d, struct variable *v)
-{
- int dict_index = var_get_dict_index (v);
-
- assert (dict_contains_var (d, v));
-
- /* Delete aux data. */
- var_clear_aux (v);
-
- dict_unset_split_var (d, v);
-
- if (d->weight == v)
- dict_set_weight (d, NULL);
-
- if (d->filter == v)
- dict_set_filter (d, NULL);
-
- dict_clear_vectors (d);
-
- /* Remove V from var array. */
- remove_element (d->var, d->var_cnt, sizeof *d->var, dict_index);
- d->var_cnt--;
-
- /* Update dict_index for each affected variable. */
- reindex_vars (d, dict_index, d->var_cnt);
-
- /* Update name hash. */
- hsh_force_delete (d->name_tab, v);
-
-
- /* Free memory. */
- var_clear_vardict (v);
- var_destroy (v);
-
- if (d->callbacks && d->callbacks->var_deleted )
- d->callbacks->var_deleted (d, dict_index, d->cb_data);
-}
-
-/* Deletes the COUNT variables listed in VARS from D. This is
- unsafe; see the comment on dict_delete_var() for details. */
-void
-dict_delete_vars (struct dictionary *d,
- struct variable *const *vars, size_t count)
-{
- /* FIXME: this can be done in O(count) time, but this algorithm
- is O(count**2). */
- assert (d != NULL);
- assert (count == 0 || vars != NULL);
-
- while (count-- > 0)
- dict_delete_var (d, *vars++);
-}
-
-/* Deletes the COUNT variables in D starting at index IDX. This
- is unsafe; see the comment on dict_delete_var() for
- details. */
-void
-dict_delete_consecutive_vars (struct dictionary *d, size_t idx, size_t count)
-{
- /* FIXME: this can be done in O(count) time, but this algorithm
- is O(count**2). */
- assert (idx + count <= d->var_cnt);
-
- while (count-- > 0)
- dict_delete_var (d, d->var[idx]);
-}
-
-/* Deletes scratch variables from dictionary D. */
-void
-dict_delete_scratch_vars (struct dictionary *d)
-{
- int i;
-
- /* FIXME: this can be done in O(count) time, but this algorithm
- is O(count**2). */
- assert (d != NULL);
-
- for (i = 0; i < d->var_cnt; )
- if (dict_class_from_id (var_get_name (d->var[i])) == DC_SCRATCH)
- dict_delete_var (d, d->var[i]);
- else
- i++;