1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include <data/transformations.h>
27 #include <libpspp/str.h>
31 /* A single transformation. */
34 /* Offset to add to EXECUTE's return value, if it returns a
35 transformation index. Normally 0 but set to the starting
36 index of a spliced chain after splicing. */
38 trns_finalize_func *finalize; /* Finalize proc. */
39 trns_proc_func *execute; /* Executes the transformation. */
40 trns_free_func *free; /* Garbage collector proc. */
41 void *aux; /* Auxiliary data. */
44 /* A chain of transformations. */
47 struct transformation *trns; /* Array of transformations. */
48 size_t trns_cnt; /* Number of transformations. */
49 size_t trns_cap; /* Allocated capacity. */
50 bool finalized; /* Finalize functions called? */
53 /* Allocates and returns a new transformation chain. */
55 trns_chain_create (void)
57 struct trns_chain *chain = xmalloc (sizeof *chain);
61 chain->finalized = false;
65 /* Finalizes all the transformations in CHAIN.
66 A chain is only finalized once; afterward, calling this
68 Finalizers may add transformations to CHAIN, but after
69 finalization the chain's contents are fixed, so that no more
70 transformations may be added afterward. */
72 trns_chain_finalize (struct trns_chain *chain)
74 if (!chain->finalized)
78 for (i = 0; i < chain->trns_cnt; i++)
80 struct transformation *trns = &chain->trns[i];
81 if (trns->finalize != NULL)
82 trns->finalize (trns->aux);
84 chain->finalized = true;
88 /* Destroys CHAIN, finalizing it in the process if it has not
89 already been finalized. */
91 trns_chain_destroy (struct trns_chain *chain)
99 /* Needed to ensure that the control stack gets cleared. */
100 trns_chain_finalize (chain);
102 for (i = 0; i < chain->trns_cnt; i++)
104 struct transformation *trns = &chain->trns[i];
105 if (trns->free != NULL)
106 ok = trns->free (trns->aux) && ok;
115 /* Returns true if CHAIN contains any transformations,
118 trns_chain_is_empty (const struct trns_chain *chain)
120 return chain->trns_cnt == 0;
123 /* Adds a transformation to CHAIN with finalize function
124 FINALIZE, execute function EXECUTE, free function FREE, and
125 auxiliary data AUX. */
127 trns_chain_append (struct trns_chain *chain, trns_finalize_func *finalize,
128 trns_proc_func *execute, trns_free_func *free,
131 struct transformation *trns;
133 assert (!chain->finalized);
135 if (chain->trns_cnt == chain->trns_cap)
136 chain->trns = x2nrealloc (chain->trns, &chain->trns_cap,
137 sizeof *chain->trns);
139 trns = &chain->trns[chain->trns_cnt++];
141 trns->finalize = finalize;
142 trns->execute = execute;
147 /* Appends the transformations in SRC to those in DST,
149 Both DST and SRC must already be finalized. */
151 trns_chain_splice (struct trns_chain *dst, struct trns_chain *src)
155 assert (dst->finalized);
156 assert (src->finalized);
158 if (dst->trns_cnt + src->trns_cnt > dst->trns_cap)
160 dst->trns_cap = dst->trns_cnt + src->trns_cnt;
161 dst->trns = xnrealloc (dst->trns, dst->trns_cap, sizeof *dst->trns);
164 for (i = 0; i < src->trns_cnt; i++)
166 struct transformation *d = &dst->trns[i + dst->trns_cnt];
167 const struct transformation *s = &src->trns[i];
169 d->idx_ofs += src->trns_cnt;
171 dst->trns_cnt += src->trns_cnt;
173 trns_chain_destroy (src);
176 /* Returns the index that a transformation execution function may
177 return to "jump" to the next transformation to be added. */
179 trns_chain_next (struct trns_chain *chain)
181 return chain->trns_cnt;
184 /* Executes the given CHAIN of transformations on C,
185 passing *CASE_NR as the case number.
186 If a transformation modifies *CASE_NR, it will affect the case
187 number passed to following transformations.
188 Returns the result code that caused the transformations to
189 terminate, or TRNS_CONTINUE if the transformations finished
190 due to "falling off the end" of the set of transformations. */
192 trns_chain_execute (struct trns_chain *chain, struct ccase *c,
193 const size_t *case_nr)
197 assert (chain->finalized);
198 for (i = 0; i < chain->trns_cnt; )
200 struct transformation *trns = &chain->trns[i];
201 int retval = trns->execute (trns->aux, c, *case_nr);
202 if (retval == TRNS_CONTINUE)
204 else if (retval >= 0)
205 i = retval + trns->idx_ofs;
210 return TRNS_CONTINUE;