1 /* PSPP - computes sample statistics.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 /* Opaque structure that represents a case. Use accessor
28 functions instead of accessing any members directly. Use
29 case_move() or case_clone() instead of copying. */
32 struct case_data *case_data; /* Actual data. */
35 /* Invisible to user code. */
38 size_t value_cnt; /* Number of values. */
39 unsigned ref_cnt; /* Reference count. */
40 union value values[1]; /* Values. */
46 #define CASE_INLINE static
49 CASE_INLINE void case_nullify (struct ccase *);
50 CASE_INLINE int case_is_null (const struct ccase *);
52 void case_create (struct ccase *, size_t value_cnt);
53 CASE_INLINE void case_clone (struct ccase *, const struct ccase *);
54 CASE_INLINE void case_move (struct ccase *, struct ccase *);
55 CASE_INLINE void case_destroy (struct ccase *);
57 void case_resize (struct ccase *, size_t old_cnt, size_t new_cnt);
58 void case_swap (struct ccase *, struct ccase *);
60 bool case_try_create (struct ccase *, size_t value_cnt);
61 bool case_try_clone (struct ccase *, const struct ccase *);
63 CASE_INLINE void case_copy (struct ccase *dst, size_t dst_idx,
64 const struct ccase *src, size_t src_idx,
67 CASE_INLINE void case_to_values (const struct ccase *, union value *, size_t);
68 CASE_INLINE void case_from_values (struct ccase *,
69 const union value *, size_t);
71 static inline const union value *case_data (const struct ccase *,
72 const struct variable *);
73 static inline double case_num (const struct ccase *, const struct variable *);
74 static inline const char *case_str (const struct ccase *,
75 const struct variable *);
76 static inline union value *case_data_rw (struct ccase *,
77 const struct variable *);
79 CASE_INLINE const union value *case_data_idx (const struct ccase *,
81 CASE_INLINE double case_num_idx (const struct ccase *, size_t idx);
82 CASE_INLINE const char *case_str_idx (const struct ccase *, size_t idx);
83 CASE_INLINE union value *case_data_rw_idx (struct ccase *, size_t idx);
86 int case_compare (const struct ccase *, const struct ccase *,
87 struct variable *const *, size_t var_cnt);
88 int case_compare_2dict (const struct ccase *, const struct ccase *,
89 struct variable *const *, struct variable *const *,
92 const union value *case_data_all (const struct ccase *);
93 union value *case_data_all_rw (struct ccase *);
95 void case_unshare (struct ccase *);
99 #include <libpspp/str.h>
102 case_nullify (struct ccase *c)
108 case_is_null (const struct ccase *c)
110 return c->case_data == NULL;
114 case_clone (struct ccase *clone, const struct ccase *orig)
117 orig->case_data->ref_cnt++;
121 case_move (struct ccase *dst, struct ccase *src)
126 src->case_data = NULL;
131 case_destroy (struct ccase *c)
133 struct case_data *cd = c->case_data;
134 if (cd != NULL && --cd->ref_cnt == 0)
139 case_copy (struct ccase *dst, size_t dst_idx,
140 const struct ccase *src, size_t src_idx,
143 if (dst->case_data != src->case_data || dst_idx != src_idx)
145 if (dst->case_data->ref_cnt > 1)
147 memmove (dst->case_data->values + dst_idx,
148 src->case_data->values + src_idx,
149 sizeof *dst->case_data->values * value_cnt);
154 case_to_values (const struct ccase *c, union value *output,
157 memcpy (output, c->case_data->values,
158 output_size * sizeof *output);
162 case_from_values (struct ccase *c, const union value *input,
163 size_t input_size UNUSED)
165 if (c->case_data->ref_cnt > 1)
167 memcpy (c->case_data->values, input,
168 c->case_data->value_cnt * sizeof *input);
171 static inline const union value *
172 case_data_idx (const struct ccase *c, size_t idx)
174 return &c->case_data->values[idx];
178 case_num_idx (const struct ccase *c, size_t idx)
180 return c->case_data->values[idx].f;
183 static inline const char *
184 case_str_idx (const struct ccase *c, size_t idx)
186 return c->case_data->values[idx].s;
189 static inline union value *
190 case_data_rw_idx (struct ccase *c, size_t idx)
192 if (c->case_data->ref_cnt > 1)
194 return &c->case_data->values[idx];
196 #endif /* !DEBUGGING */
198 /* Returns a pointer to the `union value' used for the
199 element of C for variable V.
200 Case C must be drawn from V's dictionary.
201 The caller must not modify the returned data. */
202 static inline const union value *
203 case_data (const struct ccase *c, const struct variable *v)
205 return case_data_idx (c, var_get_case_index (v));
208 /* Returns the numeric value of the `union value' in C for
210 Case C must be drawn from V's dictionary. */
212 case_num (const struct ccase *c, const struct variable *v)
214 return case_num_idx (c, var_get_case_index (v));
217 /* Returns the string value of the `union value' in C for
219 Case C must be drawn from V's dictionary.
220 (Note that the value is not null-terminated.)
221 The caller must not modify the return value. */
222 static inline const char *
223 case_str (const struct ccase *c, const struct variable *v)
225 return case_str_idx (c, var_get_case_index (v));
228 /* Returns a pointer to the `union value' used for the
229 element of C for variable V.
230 Case C must be drawn from V's dictionary.
231 The caller is allowed to modify the returned data. */
232 static inline union value *
233 case_data_rw (struct ccase *c, const struct variable *v)
235 return case_data_rw_idx (c, var_get_case_index (v));