Improvements to case implementation.
[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
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 #if DEBUGGING
34     struct ccase *this;                 /* Detects unauthorized move/copy. */
35 #endif
36   };
37
38 /* Invisible to user code. */
39 struct case_data
40   {
41     size_t value_cnt;                   /* Number of values. */
42     unsigned ref_cnt;                   /* Reference count. */
43     union value values[1];              /* Values. */
44   };
45
46 #ifdef DEBUGGING
47 #define CASE_INLINE
48 #else
49 #define CASE_INLINE static
50 #endif
51
52 CASE_INLINE void case_nullify (struct ccase *);
53 CASE_INLINE int case_is_null (const struct ccase *);
54
55 void case_create (struct ccase *, size_t value_cnt);
56 CASE_INLINE void case_clone (struct ccase *, const struct ccase *);
57 CASE_INLINE void case_move (struct ccase *, struct ccase *);
58 CASE_INLINE void case_destroy (struct ccase *);
59
60 void case_resize (struct ccase *, size_t old_cnt, size_t new_cnt);
61 void case_swap (struct ccase *, struct ccase *);
62
63 int case_try_create (struct ccase *, size_t value_cnt);
64 int case_try_clone (struct ccase *, const struct ccase *);
65
66 CASE_INLINE void case_copy (struct ccase *dst, size_t dst_idx,
67                             const struct ccase *src, size_t src_idx,
68                             size_t cnt);
69
70 CASE_INLINE void case_to_values (const struct ccase *, union value *, size_t);
71 CASE_INLINE void case_from_values (struct ccase *,
72                                    const union value *, size_t);
73
74 CASE_INLINE const union value *case_data (const struct ccase *, size_t idx);
75 CASE_INLINE double case_num (const struct ccase *, size_t idx);
76 CASE_INLINE const char *case_str (const struct ccase *, size_t idx);
77
78 CASE_INLINE union value *case_data_rw (struct ccase *, size_t idx);
79
80 struct variable;
81 int case_compare (const struct ccase *, const struct ccase *,
82                   struct variable *const *, size_t var_cnt);
83 int case_compare_2dict (const struct ccase *, const struct ccase *,
84                         struct variable *const *, struct variable *const *,
85                         size_t var_cnt);
86
87 const union value *case_data_all (const struct ccase *);
88 union value *case_data_all_rw (struct ccase *);
89
90 void case_unshare (struct ccase *);
91
92 #ifndef DEBUGGING
93 #include <stdlib.h>
94 #include <libpspp/str.h>
95
96 static inline void
97 case_nullify (struct ccase *c) 
98 {
99   c->case_data = NULL;
100 }
101
102 static inline int
103 case_is_null (const struct ccase *c) 
104 {
105   return c->case_data == NULL;
106 }
107
108 static inline void
109 case_clone (struct ccase *clone, const struct ccase *orig)
110 {
111   *clone = *orig;
112   orig->case_data->ref_cnt++;
113 }
114
115 static inline void
116 case_move (struct ccase *dst, struct ccase *src) 
117 {
118   if (dst != src) 
119     {
120       *dst = *src;
121       src->case_data = NULL; 
122     }
123 }
124
125 static inline void
126 case_destroy (struct ccase *c) 
127 {
128   struct case_data *cd = c->case_data;
129   if (cd != NULL && --cd->ref_cnt == 0)
130     free (cd);
131 }
132
133 static inline void
134 case_copy (struct ccase *dst, size_t dst_idx,
135            const struct ccase *src, size_t src_idx,
136            size_t value_cnt) 
137 {
138   if (dst->case_data != src->case_data || dst_idx != src_idx) 
139     {
140       if (dst->case_data->ref_cnt > 1)
141         case_unshare (dst);
142       memmove (dst->case_data->values + dst_idx,
143                src->case_data->values + src_idx,
144                sizeof *dst->case_data->values * value_cnt); 
145     }
146 }
147
148 static inline void
149 case_to_values (const struct ccase *c, union value *output,
150                 size_t output_size ) 
151 {
152   memcpy (output, c->case_data->values,
153           output_size * sizeof *output);
154 }
155
156 static inline void
157 case_from_values (struct ccase *c, const union value *input,
158                   size_t input_size UNUSED) 
159 {
160   if (c->case_data->ref_cnt > 1)
161     case_unshare (c);
162   memcpy (c->case_data->values, input,
163           c->case_data->value_cnt * sizeof *input);
164 }
165
166 static inline const union value *
167 case_data (const struct ccase *c, size_t idx) 
168 {
169   return &c->case_data->values[idx];
170 }
171
172 static inline double
173 case_num (const struct ccase *c, size_t idx) 
174 {
175   return c->case_data->values[idx].f;
176 }
177
178 static inline const char *
179 case_str (const struct ccase *c, size_t idx)
180 {
181   return c->case_data->values[idx].s;
182 }
183
184 static inline union value *
185 case_data_rw (struct ccase *c, size_t idx)
186 {
187   if (c->case_data->ref_cnt > 1)
188     case_unshare (c);
189   return &c->case_data->values[idx];
190 }
191 #endif /* !DEBUGGING */
192
193 #endif /* case.h */