Update all #include directives to the currently preferred style.
[pspp-builds.git] / src / data / transformations.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009, 2011 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "data/transformations.h"
20
21 #include <assert.h>
22 #include <stdlib.h>
23
24 #include "libpspp/str.h"
25
26 #include "gl/xalloc.h"
27
28 /* A single transformation. */
29 struct transformation
30   {
31     /* Offset to add to EXECUTE's return value, if it returns a
32        transformation index.  Normally 0 but set to the starting
33        index of a spliced chain after splicing. */
34     int idx_ofs;
35     trns_finalize_func *finalize;       /* Finalize proc. */
36     trns_proc_func *execute;            /* Executes the transformation. */
37     trns_free_func *free;               /* Garbage collector proc. */
38     void *aux;                          /* Auxiliary data. */
39   };
40
41 /* A chain of transformations. */
42 struct trns_chain
43   {
44     struct transformation *trns;        /* Array of transformations. */
45     size_t trns_cnt;                    /* Number of transformations. */
46     size_t trns_cap;                    /* Allocated capacity. */
47     bool finalized;                     /* Finalize functions called? */
48   };
49
50 /* Allocates and returns a new transformation chain. */
51 struct trns_chain *
52 trns_chain_create (void)
53 {
54   struct trns_chain *chain = xmalloc (sizeof *chain);
55   chain->trns = NULL;
56   chain->trns_cnt = 0;
57   chain->trns_cap = 0;
58   chain->finalized = false;
59   return chain;
60 }
61
62 /* Finalizes all the transformations in CHAIN.
63    A chain is only finalized once; afterward, calling this
64    function is a no-op.
65    Finalizers may add transformations to CHAIN, but after
66    finalization the chain's contents are fixed, so that no more
67    transformations may be added afterward. */
68 void
69 trns_chain_finalize (struct trns_chain *chain)
70 {
71   if (!chain->finalized)
72     {
73       size_t i;
74
75       for (i = 0; i < chain->trns_cnt; i++)
76         {
77           struct transformation *trns = &chain->trns[i];
78           if (trns->finalize != NULL)
79             trns->finalize (trns->aux);
80         }
81       chain->finalized = true;
82     }
83 }
84
85 /* Destroys CHAIN, finalizing it in the process if it has not
86    already been finalized. */
87 bool
88 trns_chain_destroy (struct trns_chain *chain)
89 {
90   bool ok = true;
91
92   if (chain != NULL)
93     {
94       size_t i;
95
96       /* Needed to ensure that the control stack gets cleared. */
97       trns_chain_finalize (chain);
98
99       for (i = 0; i < chain->trns_cnt; i++)
100         {
101           struct transformation *trns = &chain->trns[i];
102           if (trns->free != NULL)
103             ok = trns->free (trns->aux) && ok;
104         }
105       free (chain->trns);
106       free (chain);
107     }
108
109   return ok;
110 }
111
112 /* Returns true if CHAIN contains any transformations,
113    false otherwise. */
114 bool
115 trns_chain_is_empty (const struct trns_chain *chain)
116 {
117   return chain->trns_cnt == 0;
118 }
119
120 /* Adds a transformation to CHAIN with finalize function
121    FINALIZE, execute function EXECUTE, free function FREE, and
122    auxiliary data AUX. */
123 void
124 trns_chain_append (struct trns_chain *chain, trns_finalize_func *finalize,
125                    trns_proc_func *execute, trns_free_func *free,
126                    void *aux)
127 {
128   struct transformation *trns;
129
130   assert (!chain->finalized);
131
132   if (chain->trns_cnt == chain->trns_cap)
133     chain->trns = x2nrealloc (chain->trns, &chain->trns_cap,
134                               sizeof *chain->trns);
135
136   trns = &chain->trns[chain->trns_cnt++];
137   trns->idx_ofs = 0;
138   trns->finalize = finalize;
139   trns->execute = execute;
140   trns->free = free;
141   trns->aux = aux;
142 }
143
144 /* Appends the transformations in SRC to those in DST,
145    and destroys SRC.
146    Both DST and SRC must already be finalized. */
147 void
148 trns_chain_splice (struct trns_chain *dst, struct trns_chain *src)
149 {
150   size_t i;
151
152   assert (dst->finalized);
153   assert (src->finalized);
154
155   if (dst->trns_cnt + src->trns_cnt > dst->trns_cap)
156     {
157       dst->trns_cap = dst->trns_cnt + src->trns_cnt;
158       dst->trns = xnrealloc (dst->trns, dst->trns_cap, sizeof *dst->trns);
159     }
160
161   for (i = 0; i < src->trns_cnt; i++)
162     {
163       struct transformation *d = &dst->trns[i + dst->trns_cnt];
164       const struct transformation *s = &src->trns[i];
165       *d = *s;
166       d->idx_ofs += src->trns_cnt;
167     }
168   dst->trns_cnt += src->trns_cnt;
169
170   trns_chain_destroy (src);
171 }
172
173 /* Returns the index that a transformation execution function may
174    return to "jump" to the next transformation to be added. */
175 size_t
176 trns_chain_next (struct trns_chain *chain)
177 {
178   return chain->trns_cnt;
179 }
180
181 /* Executes the given CHAIN of transformations on *C,
182    passing CASE_NR as the case number.
183    *C may be replaced by a new case.
184    Returns the result code that caused the transformations to
185    terminate, or TRNS_CONTINUE if the transformations finished
186    due to "falling off the end" of the set of transformations. */
187 enum trns_result
188 trns_chain_execute (const struct trns_chain *chain, enum trns_result start,
189                     struct ccase **c, casenumber case_nr)
190 {
191   size_t i;
192
193   assert (chain->finalized);
194   for (i = start < 0 ? 0 : start; i < chain->trns_cnt; )
195     {
196       struct transformation *trns = &chain->trns[i];
197       int retval = trns->execute (trns->aux, c, case_nr);
198       if (retval == TRNS_CONTINUE)
199         i++;
200       else if (retval >= 0)
201         i = retval + trns->idx_ofs;
202       else
203         return retval == TRNS_END_CASE ? i + 1 : retval;
204     }
205
206   return TRNS_CONTINUE;
207 }