+
+/* Returns the variable set in DICT with index IDX, which must be between 0 and
+ the count returned by dict_get_n_varsets(), exclusive. */
+const struct varset *
+dict_get_varset (const struct dictionary *dict, size_t idx)
+{
+ assert (idx < dict->n_varsets);
+ return dict->varsets[idx];
+}
+
+/* Returns the number of variable sets in DICT. */
+size_t
+dict_get_n_varsets (const struct dictionary *dict)
+{
+ return dict->n_varsets;
+}
+
+/* Looks for a variable set named NAME in DICT. If it finds one, returns its
+ index; otherwise, returns SIZE_MAX. */
+static size_t
+dict_lookup_varset_idx (const struct dictionary *dict, const char *name)
+{
+ for (size_t i = 0; i < dict->n_varsets; i++)
+ if (!utf8_strcasecmp (name, dict->varsets[i]->name))
+ return i;
+
+ return SIZE_MAX;
+}
+
+/* Looks for a multiple response set named NAME in DICT. If it finds one,
+ returns it; otherwise, returns NULL. */
+const struct varset *
+dict_lookup_varset (const struct dictionary *dict, const char *name)
+{
+ size_t idx = dict_lookup_varset_idx (dict, name);
+ return idx != SIZE_MAX ? dict->varsets[idx] : NULL;
+}
+
+/* Adds VARSET to DICT, replacing any existing set with the same name. Returns
+ true if a set was replaced, false if none existed with the specified name.
+
+ Ownership of VARSET is transferred to DICT. */
+bool
+dict_add_varset (struct dictionary *dict, struct varset *varset)
+{
+ size_t idx = dict_lookup_varset_idx (dict, varset->name);
+ if (idx == SIZE_MAX)
+ {
+ dict->varsets = xrealloc (dict->varsets,
+ (dict->n_varsets + 1) * sizeof *dict->varsets);
+ dict->varsets[dict->n_varsets++] = varset;
+ return true;
+ }
+ else
+ {
+ varset_destroy (dict->varsets[idx]);
+ dict->varsets[idx] = varset;
+ return false;
+ }
+}
+
+/* Deletes all variable sets from DICT. */
+void
+dict_clear_varsets (struct dictionary *dict)
+{
+ for (size_t i = 0; i < dict->n_varsets; i++)
+ varset_destroy (dict->varsets[i]);
+ free (dict->varsets);
+ dict->varsets = NULL;
+ dict->n_varsets = 0;
+}
+
+/* Removes VAR, which must be in DICT, from DICT's multiple response sets. */
+static void
+dict_unset_varset_var (struct dictionary *dict, struct variable *var)
+{
+ assert (dict_contains_var (dict, var));
+
+ for (size_t i = 0; i < dict->n_varsets; i++)
+ {
+ struct varset *varset = dict->varsets[i];
+
+ for (size_t j = 0; j < varset->n_vars;)
+ if (varset->vars[j] == var)
+ remove_element (varset->vars, varset->n_vars--,
+ sizeof *varset->vars, j);
+ else
+ j++;
+ }
+}
+\f