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