1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2011, 2013, 2015 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "data/transformations.h"
24 #include "libpspp/str.h"
25 #include "data/control-stack.h" /* XXX layering violation */
27 #include "gl/xalloc.h"
29 /* A single transformation. */
32 /* Offset to add to EXECUTE's return value, if it returns a
33 transformation index. Normally 0 but set to the starting
34 index of a spliced chain after splicing. */
36 trns_proc_func *execute; /* Executes the transformation. */
37 trns_free_func *free; /* Garbage collector proc. */
38 void *aux; /* Auxiliary data. */
41 /* A chain of transformations. */
44 struct transformation *trns; /* Array of transformations. */
45 size_t trns_cnt; /* Number of transformations. */
46 size_t trns_cap; /* Allocated capacity. */
49 /* Allocates and returns a new transformation chain. */
51 trns_chain_create (void)
53 struct trns_chain *chain = xmalloc (sizeof *chain);
62 trns_chain_destroy (struct trns_chain *chain)
70 for (i = 0; i < chain->trns_cnt; i++)
72 struct transformation *trns = &chain->trns[i];
73 if (trns->free != NULL)
74 ok = trns->free (trns->aux) && ok;
83 /* Returns true if CHAIN contains any transformations,
86 trns_chain_is_empty (const struct trns_chain *chain)
88 return chain->trns_cnt == 0;
91 /* Adds a transformation to CHAIN with execute function EXECUTE, free function
92 FREE, and auxiliary data AUX. */
94 trns_chain_append (struct trns_chain *chain, trns_proc_func *execute,
95 trns_free_func *free, void *aux)
97 struct transformation *trns;
99 if (chain->trns_cnt == chain->trns_cap)
100 chain->trns = x2nrealloc (chain->trns, &chain->trns_cap,
101 sizeof *chain->trns);
103 trns = &chain->trns[chain->trns_cnt++];
105 trns->execute = execute;
110 /* Appends the transformations in SRC to those in DST, and destroys SRC. */
112 trns_chain_splice (struct trns_chain *dst, struct trns_chain *src)
116 if (dst->trns_cnt + src->trns_cnt > dst->trns_cap)
118 dst->trns_cap = dst->trns_cnt + src->trns_cnt;
119 dst->trns = xnrealloc (dst->trns, dst->trns_cap, sizeof *dst->trns);
122 for (i = 0; i < src->trns_cnt; i++)
124 struct transformation *d = &dst->trns[i + dst->trns_cnt];
125 const struct transformation *s = &src->trns[i];
127 d->idx_ofs += src->trns_cnt;
129 dst->trns_cnt += src->trns_cnt;
132 trns_chain_destroy (src);
135 /* Returns the index that a transformation execution function may
136 return to "jump" to the next transformation to be added. */
138 trns_chain_next (struct trns_chain *chain)
140 return chain->trns_cnt;
143 /* Executes the given CHAIN of transformations on *C,
144 passing CASE_NR as the case number.
145 *C may be replaced by a new case.
146 Returns the result code that caused the transformations to
147 terminate, or TRNS_CONTINUE if the transformations finished
148 due to "falling off the end" of the set of transformations. */
150 trns_chain_execute (const struct trns_chain *chain, enum trns_result start,
151 struct ccase **c, casenumber case_nr)
155 for (i = start < 0 ? 0 : start; i < chain->trns_cnt; )
157 struct transformation *trns = &chain->trns[i];
160 retval = trns->execute (trns->aux, c, case_nr);
177 assert (i <= chain->trns_cnt);
182 return TRNS_CONTINUE;