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., 59 Temple Place - Suite 330, Boston, MA
26 /* Opaque structure that represents a case. Use accessor
27 functions instead of accessing any members directly. Use
28 case_move() or case_clone() instead of copying. */
31 struct case_data *case_data; /* Actual data. */
33 struct ccase *this; /* Detects unauthorized move/copy. */
37 /* Invisible to user code. */
40 size_t value_cnt; /* Number of values. */
41 unsigned ref_cnt; /* Reference count. */
42 union value values[1]; /* Values. */
45 #ifdef GLOBAL_DEBUGGING
48 #define CASE_INLINE static
51 CASE_INLINE void case_nullify (struct ccase *);
52 CASE_INLINE int case_is_null (const struct ccase *);
54 void case_create (struct ccase *, size_t value_cnt);
55 CASE_INLINE void case_clone (struct ccase *, const struct ccase *);
56 CASE_INLINE void case_move (struct ccase *, struct ccase *);
57 CASE_INLINE void case_destroy (struct ccase *);
59 int case_try_create (struct ccase *, size_t value_cnt);
60 int case_try_clone (struct ccase *, const struct ccase *);
62 CASE_INLINE void case_copy (struct ccase *dst, size_t dst_idx,
63 const struct ccase *src, size_t src_idx,
66 CASE_INLINE void case_to_values (const struct ccase *, union value *, size_t);
67 CASE_INLINE void case_from_values (struct ccase *,
68 const union value *, size_t);
70 CASE_INLINE const union value *case_data (const struct ccase *, size_t idx);
71 CASE_INLINE double case_num (const struct ccase *, size_t idx);
72 CASE_INLINE const char *case_str (const struct ccase *, size_t idx);
74 CASE_INLINE union value *case_data_rw (struct ccase *, size_t idx);
76 const union value *case_data_all (const struct ccase *);
77 union value *case_data_all_rw (struct ccase *);
79 void case_unshare (struct ccase *);
81 #ifndef GLOBAL_DEBUGGING
86 case_nullify (struct ccase *c)
92 case_is_null (const struct ccase *c)
94 return c->case_data == NULL;
98 case_clone (struct ccase *clone, const struct ccase *orig)
101 orig->case_data->ref_cnt++;
105 case_move (struct ccase *dst, struct ccase *src)
108 src->case_data = NULL;
112 case_destroy (struct ccase *c)
114 struct case_data *cd = c->case_data;
115 if (cd != NULL && --cd->ref_cnt == 0)
120 case_copy (struct ccase *dst, size_t dst_idx,
121 const struct ccase *src, size_t src_idx,
124 if (dst->case_data->ref_cnt > 1)
126 if (dst->case_data != src->case_data || dst_idx != src_idx)
127 memmove (dst->case_data->values + dst_idx,
128 src->case_data->values + src_idx,
129 sizeof *dst->case_data->values * value_cnt);
133 case_to_values (const struct ccase *c, union value *output,
134 size_t output_size UNUSED)
136 memcpy (output, c->case_data->values,
137 c->case_data->value_cnt * sizeof *output);
141 case_from_values (struct ccase *c, const union value *input,
142 size_t input_size UNUSED)
144 if (c->case_data->ref_cnt > 1)
146 memcpy (c->case_data->values, input,
147 c->case_data->value_cnt * sizeof *input);
150 static inline const union value *
151 case_data (const struct ccase *c, size_t idx)
153 return &c->case_data->values[idx];
157 case_num (const struct ccase *c, size_t idx)
159 return c->case_data->values[idx].f;
162 static inline const char *
163 case_str (const struct ccase *c, size_t idx)
165 return c->case_data->values[idx].s;
168 static inline union value *
169 case_data_rw (struct ccase *c, size_t idx)
171 if (c->case_data->ref_cnt > 1)
173 return &c->case_data->values[idx];
175 #endif /* !GLOBAL_DEBUGGING */