#include "vardict.h"
#include "variable.h"
#include "vector.h"
-#include <libpspp/alloc.h>
#include <libpspp/array.h>
#include <libpspp/compiler.h>
#include <libpspp/hash.h>
#include <libpspp/str.h>
#include "minmax.h"
+#include "xalloc.h"
#include "gettext.h"
#define _(msgid) gettext (msgid)
void *cb_data ; /* Data passed to callbacks */
};
+/* Print a representation of dictionary D to stdout, for
+ debugging purposes. */
+void
+dict_dump (const struct dictionary *d)
+{
+ int i;
+ for (i = 0 ; i < d->var_cnt ; ++i )
+ {
+ const struct variable *v =
+ d->var[i];
+ printf ("Name: %s;\tdict_idx: %d; case_idx: %d\n",
+ var_get_name (v),
+ var_get_dict_index (v),
+ var_get_case_index (v));
+
+ }
+}
/* Associate CALLBACKS with DICT. Callbacks will be invoked whenever
the dictionary or any of the variables it contains are modified.
}
/* Creates and returns a (deep) copy of an existing
- dictionary. */
+ dictionary.
+
+ The new dictionary's case indexes are copied from the old
+ dictionary. If the new dictionary won't be used to access
+ cases produced with the old dictionary, then the new
+ dictionary's case indexes should be compacted with
+ dict_compact_values to save space. */
struct dictionary *
dict_clone (const struct dictionary *s)
{
for (i = 0; i < s->var_cnt; i++)
{
+ const struct vardict_info *svdi;
+ struct vardict_info dvdi;
struct variable *sv = s->var[i];
struct variable *dv = dict_clone_var_assert (d, sv, var_get_name (sv));
size_t i;
for (i = 0; i < var_get_short_name_cnt (sv); i++)
var_set_short_name (dv, i, var_get_short_name (sv, i));
+
+ svdi = var_get_vardict (sv);
+ dvdi = *svdi;
+ dvdi.dict = d;
+ var_set_vardict (dv, &dvdi);
}
d->next_value_idx = s->next_value_idx;
while (d->var_cnt > 0 )
{
- struct variable *v = d->var[d->var_cnt - 1];
- int dict_index = var_get_dict_index (v);
- int case_index = var_get_case_index (v);
- int val_cnt = var_get_value_cnt (v);
-
- var_clear_vardict (v);
- var_destroy (v);
-
- d->var_cnt--;
-
- if (d->callbacks && d->callbacks->var_deleted )
- d->callbacks->var_deleted (d,
- dict_index, case_index, val_cnt,
- d->cb_data);
+ dict_delete_var (d, d->var[d->var_cnt - 1]);
}
free (d->var);
struct variable *target ;
struct variable *result ;
- if ( ! var_is_valid_name (name, false))
+ if ( ! var_is_plausible_name (name, false))
return NULL;
target = var_create (name, 0);
result = hsh_find (d->name_tab, target);
var_destroy (target);
+ if ( result && var_has_vardict (result))
+ {
+ const struct vardict_info *vdi = var_get_vardict (result);
+ assert (vdi->dict == d);
+ }
+
return result;
}
var_clear_vardict (v);
var_destroy (v);
+
if (d->callbacks && d->callbacks->var_deleted )
d->callbacks->var_deleted (d, dict_index, case_index, val_cnt, d->cb_data);
}
return cnt;
}
\f
-/* How to copy a contiguous range of values between cases. */
-struct copy_map
- {
- size_t src_idx; /* Starting value index in source case. */
- size_t dst_idx; /* Starting value index in target case. */
- size_t cnt; /* Number of values. */
- };
-
-/* How to compact a case. */
-struct dict_compactor
- {
- struct copy_map *maps; /* Array of mappings. */
- size_t map_cnt; /* Number of mappings. */
- };
-
-/* Creates and returns a dict_compactor that can be used to
- compact cases for dictionary D.
-
- Compacting a case eliminates "holes" between values and after
- the last value. (Holes are created by deleting variables.)
-
- All variables are compacted if EXCLUDE_CLASSES is 0, or it may
- contain one or more of (1u << DC_ORDINARY), (1u << DC_SYSTEM),
- or (1u << DC_SCRATCH) to cause the corresponding type of
- variable to be deleted during compaction. */
-struct dict_compactor *
-dict_make_compactor (const struct dictionary *d, unsigned int exclude_classes)
-{
- struct dict_compactor *compactor;
- struct copy_map *map;
- size_t map_allocated;
- size_t value_idx;
- size_t i;
-
- assert ((exclude_classes & ~((1u << DC_ORDINARY)
- | (1u << DC_SYSTEM)
- | (1u << DC_SCRATCH))) == 0);
-
- compactor = xmalloc (sizeof *compactor);
- compactor->maps = NULL;
- compactor->map_cnt = 0;
- map_allocated = 0;
-
- value_idx = 0;
- map = NULL;
- for (i = 0; i < d->var_cnt; i++)
- {
- struct variable *v = d->var[i];
- enum dict_class class = dict_class_from_id (var_get_name (v));
- if (exclude_classes & (1u << class))
- continue;
-
- if (map != NULL && map->src_idx + map->cnt == var_get_case_index (v))
- map->cnt += var_get_value_cnt (v);
- else
- {
- if (compactor->map_cnt == map_allocated)
- compactor->maps = x2nrealloc (compactor->maps, &map_allocated,
- sizeof *compactor->maps);
- map = &compactor->maps[compactor->map_cnt++];
- map->src_idx = var_get_case_index (v);
- map->dst_idx = value_idx;
- map->cnt = var_get_value_cnt (v);
- }
- value_idx += var_get_value_cnt (v);
- }
-
- return compactor;
-}
-
-/* Compacts SRC by copying it to DST according to the scheme in
- COMPACTOR.
-
- Compacting a case eliminates "holes" between values and after
- the last value. (Holes are created by deleting variables.) */
-void
-dict_compactor_compact (const struct dict_compactor *compactor,
- struct ccase *dst, const struct ccase *src)
-{
- size_t i;
-
- for (i = 0; i < compactor->map_cnt; i++)
- {
- const struct copy_map *map = &compactor->maps[i];
- case_copy (dst, map->dst_idx, src, map->src_idx, map->cnt);
- }
-}
-
-/* Destroys COMPACTOR. */
-void
-dict_compactor_destroy (struct dict_compactor *compactor)
-{
- if (compactor != NULL)
- {
- free (compactor->maps);
- free (compactor);
- }
-}
-
/* Returns the SPLIT FILE vars (see cmd_split_file()). Call
dict_get_split_cnt() to determine how many SPLIT FILE vars
there are. Returns a null pointer if and only if there are no
assert (cnt == 0 || split != NULL);
d->split_cnt = cnt;
- d->split = cnt > 0 ? xnrealloc (d->split, cnt, sizeof *d->split) : NULL;
- memcpy (d->split, split, cnt * sizeof *d->split);
+ if ( cnt > 0 )
+ {
+ d->split = xnrealloc (d->split, cnt, sizeof *d->split) ;
+ memcpy (d->split, split, cnt * sizeof *d->split);
+ }
+ else
+ {
+ free (d->split);
+ d->split = NULL;
+ }
if ( d->callbacks && d->callbacks->split_changed )
d->callbacks->split_changed (d, d->cb_data);
if ( var_has_vardict (v))
{
const struct vardict_info *vdi = var_get_vardict (v);
- struct dictionary *d;
-
- d = vdi->dict;
+ struct dictionary *d = vdi->dict;
if ( d->callbacks && d->callbacks->var_changed )
d->callbacks->var_changed (d, var_get_dict_index (v), d->cb_data);