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