1 /* PSPP - computes sample statistics.
2 Copyright (C) 2004 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
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. */
33 struct case_data *case_data; /* Actual data. */
36 /* Invisible to user code. */
39 size_t value_cnt; /* Number of values. */
40 unsigned ref_cnt; /* Reference count. */
41 union value values[1]; /* Values. */
47 #define CASE_INLINE static
50 CASE_INLINE void case_nullify (struct ccase *);
51 CASE_INLINE int case_is_null (const struct ccase *);
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 *);
58 void case_resize (struct ccase *, size_t old_cnt, size_t new_cnt);
59 void case_swap (struct ccase *, struct ccase *);
61 bool case_try_create (struct ccase *, size_t value_cnt);
62 bool case_try_clone (struct ccase *, const struct ccase *);
64 CASE_INLINE void case_copy (struct ccase *dst, size_t dst_idx,
65 const struct ccase *src, size_t src_idx,
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);
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 *);
80 CASE_INLINE const union value *case_data_idx (const struct ccase *,
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);
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 *,
93 const union value *case_data_all (const struct ccase *);
94 union value *case_data_all_rw (struct ccase *);
96 void case_unshare (struct ccase *);
100 #include <libpspp/str.h>
103 case_nullify (struct ccase *c)
109 case_is_null (const struct ccase *c)
111 return c->case_data == NULL;
115 case_clone (struct ccase *clone, const struct ccase *orig)
118 orig->case_data->ref_cnt++;
122 case_move (struct ccase *dst, struct ccase *src)
127 src->case_data = NULL;
132 case_destroy (struct ccase *c)
134 struct case_data *cd = c->case_data;
135 if (cd != NULL && --cd->ref_cnt == 0)
140 case_copy (struct ccase *dst, size_t dst_idx,
141 const struct ccase *src, size_t src_idx,
144 if (dst->case_data != src->case_data || dst_idx != src_idx)
146 if (dst->case_data->ref_cnt > 1)
148 memmove (dst->case_data->values + dst_idx,
149 src->case_data->values + src_idx,
150 sizeof *dst->case_data->values * value_cnt);
155 case_to_values (const struct ccase *c, union value *output,
158 memcpy (output, c->case_data->values,
159 output_size * sizeof *output);
163 case_from_values (struct ccase *c, const union value *input,
164 size_t input_size UNUSED)
166 if (c->case_data->ref_cnt > 1)
168 memcpy (c->case_data->values, input,
169 c->case_data->value_cnt * sizeof *input);
172 static inline const union value *
173 case_data_idx (const struct ccase *c, size_t idx)
175 return &c->case_data->values[idx];
179 case_num_idx (const struct ccase *c, size_t idx)
181 return c->case_data->values[idx].f;
184 static inline const char *
185 case_str_idx (const struct ccase *c, size_t idx)
187 return c->case_data->values[idx].s;
190 static inline union value *
191 case_data_rw_idx (struct ccase *c, size_t idx)
193 if (c->case_data->ref_cnt > 1)
195 return &c->case_data->values[idx];
197 #endif /* !DEBUGGING */
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)
206 return case_data_idx (c, var_get_case_index (v));
209 /* Returns the numeric value of the `union value' in C for
211 Case C must be drawn from V's dictionary. */
213 case_num (const struct ccase *c, const struct variable *v)
215 return case_num_idx (c, var_get_case_index (v));
218 /* Returns the string value of the `union value' in C for
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)
226 return case_str_idx (c, var_get_case_index (v));
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)
236 return case_data_rw_idx (c, var_get_case_index (v));