- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- 02110-1301, USA. */
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include "data/case.h"
+#include "interaction.h"
+
+#include "data/value.h"
+#include "data/variable.h"
+#include "libpspp/str.h"
+
+#include "gl/xalloc.h"
+
+#include <stdio.h>
+
+
+/*
+ An interaction is a structure containing a "product" of other
+ variables. The variables can be either string or numeric.
+
+ Interaction is commutative. That means, that from a mathematical point of
+ view, the order of the variables is irrelevant. However, for display
+ purposes, and for matching with an interaction's value the order is
+ pertinent.
+
+ Therefore, when using these functions, make sure the orders of variables
+ and values match when appropriate.
+*/
+
+
+struct interaction *
+interaction_create (const struct variable *v)
+{
+ struct interaction *i = xmalloc (sizeof *i);
+ i->vars = xmalloc (sizeof *i->vars);
+ i->n_vars = 0;
+ if ( v )
+ {
+ i->vars[0] = v;
+ i->n_vars = 1;
+ }
+ return i;
+}
+
+/* Deep copy an interaction */
+struct interaction *
+interaction_clone (const struct interaction *iact)
+{
+ int v;
+ struct interaction *i = xmalloc (sizeof *i);
+ i->vars = xcalloc (iact->n_vars, sizeof *i->vars);
+ i->n_vars = iact->n_vars;
+
+ for (v = 0; v < iact->n_vars; ++v)
+ {
+ i->vars[v] = iact->vars[v];
+ }
+
+ return i;
+}
+
+
+void
+interaction_destroy (struct interaction *i)
+{
+ if (NULL == i)
+ return;
+
+ free (i->vars);
+ free (i);
+}
+
+void
+interaction_add_variable (struct interaction *i, const struct variable *v)
+{
+ i->vars = xrealloc (i->vars, sizeof (*i->vars) * ++i->n_vars);
+ i->vars[i->n_vars - 1] = v;
+}
+