Changed a lot of ints to bools.
[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   };
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 CASE_INLINE const union value *case_data (const struct ccase *, size_t idx);
72 CASE_INLINE double case_num (const struct ccase *, size_t idx);
73 CASE_INLINE const char *case_str (const struct ccase *, size_t idx);
74
75 CASE_INLINE union value *case_data_rw (struct ccase *, size_t idx);
76
77 struct variable;
78 int case_compare (const struct ccase *, const struct ccase *,
79                   struct variable *const *, size_t var_cnt);
80 int case_compare_2dict (const struct ccase *, const struct ccase *,
81                         struct variable *const *, struct variable *const *,
82                         size_t var_cnt);
83
84 const union value *case_data_all (const struct ccase *);
85 union value *case_data_all_rw (struct ccase *);
86
87 void case_unshare (struct ccase *);
88
89 #ifndef DEBUGGING
90 #include <stdlib.h>
91 #include <libpspp/str.h>
92
93 static inline void
94 case_nullify (struct ccase *c) 
95 {
96   c->case_data = NULL;
97 }
98
99 static inline int
100 case_is_null (const struct ccase *c) 
101 {
102   return c->case_data == NULL;
103 }
104
105 static inline void
106 case_clone (struct ccase *clone, const struct ccase *orig)
107 {
108   *clone = *orig;
109   orig->case_data->ref_cnt++;
110 }
111
112 static inline void
113 case_move (struct ccase *dst, struct ccase *src) 
114 {
115   if (dst != src) 
116     {
117       *dst = *src;
118       src->case_data = NULL; 
119     }
120 }
121
122 static inline void
123 case_destroy (struct ccase *c) 
124 {
125   struct case_data *cd = c->case_data;
126   if (cd != NULL && --cd->ref_cnt == 0)
127     free (cd);
128 }
129
130 static inline void
131 case_copy (struct ccase *dst, size_t dst_idx,
132            const struct ccase *src, size_t src_idx,
133            size_t value_cnt) 
134 {
135   if (dst->case_data != src->case_data || dst_idx != src_idx) 
136     {
137       if (dst->case_data->ref_cnt > 1)
138         case_unshare (dst);
139       memmove (dst->case_data->values + dst_idx,
140                src->case_data->values + src_idx,
141                sizeof *dst->case_data->values * value_cnt); 
142     }
143 }
144
145 static inline void
146 case_to_values (const struct ccase *c, union value *output,
147                 size_t output_size ) 
148 {
149   memcpy (output, c->case_data->values,
150           output_size * sizeof *output);
151 }
152
153 static inline void
154 case_from_values (struct ccase *c, const union value *input,
155                   size_t input_size UNUSED) 
156 {
157   if (c->case_data->ref_cnt > 1)
158     case_unshare (c);
159   memcpy (c->case_data->values, input,
160           c->case_data->value_cnt * sizeof *input);
161 }
162
163 static inline const union value *
164 case_data (const struct ccase *c, size_t idx) 
165 {
166   return &c->case_data->values[idx];
167 }
168
169 static inline double
170 case_num (const struct ccase *c, size_t idx) 
171 {
172   return c->case_data->values[idx].f;
173 }
174
175 static inline const char *
176 case_str (const struct ccase *c, size_t idx)
177 {
178   return c->case_data->values[idx].s;
179 }
180
181 static inline union value *
182 case_data_rw (struct ccase *c, size_t idx)
183 {
184   if (c->case_data->ref_cnt > 1)
185     case_unshare (c);
186   return &c->case_data->values[idx];
187 }
188 #endif /* !DEBUGGING */
189
190 #endif /* case.h */