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