Change union value type to contain uint8_t types instead of char.
[pspp-builds.git] / src / data / case.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2007, 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU 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, see <http://www.gnu.org/licenses/>. */
16
17 #ifndef DATA_CASE_H
18 #define DATA_CASE_H 1
19
20 #include <limits.h>
21 #include <stddef.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <libpspp/compiler.h>
25 #include <data/caseproto.h>
26
27 struct variable;
28
29 /* A count of cases or the index of a case within a collection of
30    them. */
31 #define CASENUMBER_MAX LONG_MAX
32 typedef long int casenumber;
33
34 /* Reference-counted case implementation.
35
36    A newly created case has a single owner (the code that created
37    it), represented by an initial reference count of 1.  Other
38    code that receives the case may keep a virtual copy of it by
39    calling case_ref, which increments the case's reference count.
40    When this is done, the case becomes shared between its
41    original owner and each piece of code that incremented the
42    reference count.
43
44    A shared case (one whose reference count is greater than 1)
45    must not be modified, because this would make the case change
46    in the view of every reference count holder, not just the one
47    that intended to change the case.  Because the purpose of
48    keeping the reference count is to make a virtual copy of the
49    case, this is undesirable behavior.  The case_unshare function
50    provides a solution, by making a new, unshared copy of a
51    shared case. */
52 struct ccase
53   {
54     struct caseproto *proto;    /* Case prototype. */
55     size_t ref_cnt;             /* Reference count. */
56     union value values[1];      /* Values. */
57   };
58
59 struct ccase *case_create (const struct caseproto *) MALLOC_LIKE;
60 struct ccase *case_try_create (const struct caseproto *) MALLOC_LIKE;
61 struct ccase *case_clone (const struct ccase *) MALLOC_LIKE;
62
63 static inline struct ccase *case_unshare (struct ccase *) WARN_UNUSED_RESULT;
64 static inline struct ccase *case_ref (const struct ccase *);
65 static inline void case_unref (struct ccase *);
66 static inline bool case_is_shared (const struct ccase *);
67
68 static inline size_t case_get_value_cnt (const struct ccase *);
69 static inline const struct caseproto *case_get_proto (const struct ccase *);
70
71 size_t case_get_cost (const struct caseproto *);
72
73 struct ccase *case_resize (struct ccase *, const struct caseproto *)
74   WARN_UNUSED_RESULT;
75 struct ccase *case_unshare_and_resize (struct ccase *,
76                                        const struct caseproto *)
77   WARN_UNUSED_RESULT;
78
79 void case_set_missing (struct ccase *);
80
81 void case_copy (struct ccase *dst, size_t dst_idx,
82                 const struct ccase *src, size_t src_idx,
83                 size_t cnt);
84 void case_copy_out (const struct ccase *,
85                     size_t start_idx, union value *, size_t n_values);
86 void case_copy_in (struct ccase *,
87                    size_t start_idx, const union value *, size_t n_values);
88
89 const union value *case_data (const struct ccase *, const struct variable *);
90 const union value *case_data_idx (const struct ccase *, size_t idx);
91 union value *case_data_rw (struct ccase *, const struct variable *);
92 union value *case_data_rw_idx (struct ccase *, size_t idx);
93
94 double case_num (const struct ccase *, const struct variable *);
95 double case_num_idx (const struct ccase *, size_t idx);
96
97 const uint8_t *case_str (const struct ccase *, const struct variable *);
98 const uint8_t *case_str_idx (const struct ccase *, size_t idx);
99 uint8_t *case_str_rw (struct ccase *, const struct variable *);
100 uint8_t *case_str_rw_idx (struct ccase *, size_t idx);
101
102 int case_compare (const struct ccase *, const struct ccase *,
103                   const struct variable *const *, size_t n_vars);
104 int case_compare_2dict (const struct ccase *, const struct ccase *,
105                         const struct variable *const *,
106                         const struct variable *const *,
107                         size_t n_vars);
108
109 const union value *case_data_all (const struct ccase *);
110 union value *case_data_all_rw (struct ccase *);
111 \f
112 struct ccase *case_unshare__ (struct ccase *);
113 void case_unref__ (struct ccase *);
114
115 /* If C is a shared case, that is, if it has a reference count
116    greater than 1, makes a new unshared copy and returns it,
117    decrementing C's reference count.  If C is not shared (its
118    reference count is 1), returns C.
119
120    This function should be used before attempting to modify any
121    of the data in a case that might be shared, e.g.:
122         c = case_unshare (c);              // Make sure that C is not shared.
123         case_data_rw (c, myvar)->f = 1;    // Modify data in C.
124 */
125 static inline struct ccase *
126 case_unshare (struct ccase *c)
127 {
128   if (case_is_shared (c))
129     c = case_unshare__ (c);
130   return c;
131 }
132
133 /* Increments case C's reference count and returns C.  Afterward,
134    case C is shared among its reference count holders. */
135 static inline struct ccase *
136 case_ref (const struct ccase *c_)
137 {
138   struct ccase *c = (struct ccase *) c_;
139   c->ref_cnt++;
140   return c;
141 }
142
143 /* Decrements case C's reference count.  Frees C if its
144    reference count drops to 0.
145
146    If C is a null pointer, this function has no effect. */
147 static inline void
148 case_unref (struct ccase *c)
149 {
150   if (c != NULL && !--c->ref_cnt)
151     case_unref__ (c);
152 }
153
154 /* Returns true if case C is shared.  A case that is shared
155    cannot be modified directly.  Instead, an unshared copy must
156    first be made with case_unshare(). */
157 static inline bool
158 case_is_shared (const struct ccase *c)
159 {
160   return c->ref_cnt > 1;
161 }
162
163 /* Returns the number of union values in C. */
164 static inline size_t
165 case_get_value_cnt (const struct ccase *c)
166 {
167   return caseproto_get_n_widths (c->proto);
168 }
169
170 /* Returns the prototype that describes the format of case C.
171    The caller must not unref the returned prototype. */
172 static inline const struct caseproto *
173 case_get_proto (const struct ccase *c)
174 {
175   return c->proto;
176 }
177
178 #endif /* data/case.h */