23862b067215d98be695ecb022076b4f64b4c33c
[pspp-builds.git] / src / data / case.h
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2004 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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
18    02110-1301, USA. */
19
20 #ifndef HEADER_CASE
21 #define HEADER_CASE
22
23 #include <stddef.h>
24 #include <stdbool.h>
25 #include "value.h"
26 #include "variable.h"
27
28 /* Opaque structure that represents a case.  Use accessor
29    functions instead of accessing any members directly.  Use
30    case_move() or case_clone() instead of copying.  */
31 struct ccase 
32   {
33     struct case_data *case_data;        /* Actual data. */
34   };
35
36 /* Invisible to user code. */
37 struct case_data
38   {
39     size_t value_cnt;                   /* Number of values. */
40     unsigned ref_cnt;                   /* Reference count. */
41     union value values[1];              /* Values. */
42   };
43
44 #ifdef DEBUGGING
45 #define CASE_INLINE
46 #else
47 #define CASE_INLINE static
48 #endif
49
50 CASE_INLINE void case_nullify (struct ccase *);
51 CASE_INLINE int case_is_null (const struct ccase *);
52
53 void case_create (struct ccase *, size_t value_cnt);
54 CASE_INLINE void case_clone (struct ccase *, const struct ccase *);
55 CASE_INLINE void case_move (struct ccase *, struct ccase *);
56 CASE_INLINE void case_destroy (struct ccase *);
57
58 void case_resize (struct ccase *, size_t old_cnt, size_t new_cnt);
59 void case_swap (struct ccase *, struct ccase *);
60
61 bool case_try_create (struct ccase *, size_t value_cnt);
62 bool case_try_clone (struct ccase *, const struct ccase *);
63
64 CASE_INLINE void case_copy (struct ccase *dst, size_t dst_idx,
65                             const struct ccase *src, size_t src_idx,
66                             size_t cnt);
67
68 CASE_INLINE void case_to_values (const struct ccase *, union value *, size_t);
69 CASE_INLINE void case_from_values (struct ccase *,
70                                    const union value *, size_t);
71
72 static inline const union value *case_data (const struct ccase *,
73                                             const struct variable *);
74 static inline double case_num (const struct ccase *, const struct variable *);
75 static inline const char *case_str (const struct ccase *,
76                                     const struct variable *);
77 static inline union value *case_data_rw (struct ccase *,
78                                          const struct variable *);
79
80 CASE_INLINE const union value *case_data_idx (const struct ccase *,
81                                               size_t idx);
82 CASE_INLINE double case_num_idx (const struct ccase *, size_t idx);
83 CASE_INLINE const char *case_str_idx (const struct ccase *, size_t idx);
84 CASE_INLINE union value *case_data_rw_idx (struct ccase *, size_t idx);
85
86 struct variable;
87 int case_compare (const struct ccase *, const struct ccase *,
88                   struct variable *const *, size_t var_cnt);
89 int case_compare_2dict (const struct ccase *, const struct ccase *,
90                         struct variable *const *, struct variable *const *,
91                         size_t var_cnt);
92
93 const union value *case_data_all (const struct ccase *);
94 union value *case_data_all_rw (struct ccase *);
95
96 void case_unshare (struct ccase *);
97
98 #ifndef DEBUGGING
99 #include <stdlib.h>
100 #include <libpspp/str.h>
101
102 static inline void
103 case_nullify (struct ccase *c) 
104 {
105   c->case_data = NULL;
106 }
107
108 static inline int
109 case_is_null (const struct ccase *c) 
110 {
111   return c->case_data == NULL;
112 }
113
114 static inline void
115 case_clone (struct ccase *clone, const struct ccase *orig)
116 {
117   *clone = *orig;
118   orig->case_data->ref_cnt++;
119 }
120
121 static inline void
122 case_move (struct ccase *dst, struct ccase *src) 
123 {
124   if (dst != src) 
125     {
126       *dst = *src;
127       src->case_data = NULL; 
128     }
129 }
130
131 static inline void
132 case_destroy (struct ccase *c) 
133 {
134   struct case_data *cd = c->case_data;
135   if (cd != NULL && --cd->ref_cnt == 0)
136     free (cd);
137 }
138
139 static inline void
140 case_copy (struct ccase *dst, size_t dst_idx,
141            const struct ccase *src, size_t src_idx,
142            size_t value_cnt) 
143 {
144   if (dst->case_data != src->case_data || dst_idx != src_idx) 
145     {
146       if (dst->case_data->ref_cnt > 1)
147         case_unshare (dst);
148       memmove (dst->case_data->values + dst_idx,
149                src->case_data->values + src_idx,
150                sizeof *dst->case_data->values * value_cnt); 
151     }
152 }
153
154 static inline void
155 case_to_values (const struct ccase *c, union value *output,
156                 size_t output_size ) 
157 {
158   memcpy (output, c->case_data->values,
159           output_size * sizeof *output);
160 }
161
162 static inline void
163 case_from_values (struct ccase *c, const union value *input,
164                   size_t input_size UNUSED) 
165 {
166   if (c->case_data->ref_cnt > 1)
167     case_unshare (c);
168   memcpy (c->case_data->values, input,
169           c->case_data->value_cnt * sizeof *input);
170 }
171
172 static inline const union value *
173 case_data_idx (const struct ccase *c, size_t idx) 
174 {
175   return &c->case_data->values[idx];
176 }
177
178 static inline double
179 case_num_idx (const struct ccase *c, size_t idx) 
180 {
181   return c->case_data->values[idx].f;
182 }
183
184 static inline const char *
185 case_str_idx (const struct ccase *c, size_t idx)
186 {
187   return c->case_data->values[idx].s;
188 }
189
190 static inline union value *
191 case_data_rw_idx (struct ccase *c, size_t idx)
192 {
193   if (c->case_data->ref_cnt > 1)
194     case_unshare (c);
195   return &c->case_data->values[idx];
196 }
197 #endif /* !DEBUGGING */
198
199 /* Returns a pointer to the `union value' used for the
200    element of C for variable V.
201    Case C must be drawn from V's dictionary.
202    The caller must not modify the returned data. */
203 static inline const union value *
204 case_data (const struct ccase *c, const struct variable *v)
205 {
206   return case_data_idx (c, var_get_case_index (v));
207 }
208
209 /* Returns the numeric value of the `union value' in C for
210    variable V.
211    Case C must be drawn from V's dictionary. */
212 static inline double
213 case_num (const struct ccase *c, const struct variable *v) 
214 {
215   return case_num_idx (c, var_get_case_index (v));
216 }
217
218 /* Returns the string value of the `union value' in C for
219    variable V.
220    Case C must be drawn from V's dictionary.
221    (Note that the value is not null-terminated.)
222    The caller must not modify the return value. */
223 static inline const char *
224 case_str (const struct ccase *c, const struct variable *v) 
225 {
226   return case_str_idx (c, var_get_case_index (v));
227 }
228
229 /* Returns a pointer to the `union value' used for the
230    element of C for variable V.
231    Case C must be drawn from V's dictionary.   
232    The caller is allowed to modify the returned data. */
233 static inline union value *
234 case_data_rw (struct ccase *c, const struct variable *v) 
235 {
236   return case_data_rw_idx (c, var_get_case_index (v));
237 }
238
239 #endif /* case.h */