Release PSPP version 1.4.0.
[pspp.git] / src / data / case.h
blob1f5e192f3d748ec0050f3d00c5f6006b156434e7
1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004, 2007, 2009, 2010, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
17 #ifndef DATA_CASE_H
18 #define DATA_CASE_H 1
20 #include <limits.h>
21 #include <stddef.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
25 #include "libpspp/compiler.h"
26 #include "data/caseproto.h"
28 struct variable;
30 /* A count of cases or the index of a case within a collection of
31 them. */
32 #define CASENUMBER_MAX LONG_MAX
33 typedef long int casenumber;
35 /* Reference-counted case implementation.
37 A newly created case has a single owner (the code that created
38 it), represented by an initial reference count of 1. Other
39 code that receives the case may keep a virtual copy of it by
40 calling case_ref, which increments the case's reference count.
41 When this is done, the case becomes shared between its
42 original owner and each piece of code that incremented the
43 reference count.
45 A shared case (one whose reference count is greater than 1)
46 must not be modified, because this would make the case change
47 in the view of every reference count holder, not just the one
48 that intended to change the case. Because the purpose of
49 keeping the reference count is to make a virtual copy of the
50 case, this is undesirable behavior. The case_unshare function
51 provides a solution, by making a new, unshared copy of a
52 shared case. */
53 struct ccase
55 struct caseproto *proto; /* Case prototype. */
56 size_t ref_cnt; /* Reference count. */
57 union value values[1]; /* Values. */
60 struct ccase *case_create (const struct caseproto *) MALLOC_LIKE;
61 struct ccase *case_try_create (const struct caseproto *) MALLOC_LIKE;
62 struct ccase *case_clone (const struct ccase *) MALLOC_LIKE;
64 static inline struct ccase *case_unshare (struct ccase *) WARN_UNUSED_RESULT;
65 struct ccase *case_ref (const struct ccase *) WARN_UNUSED_RESULT;
66 static inline void case_unref (struct ccase *);
67 static inline bool case_is_shared (const struct ccase *);
69 static inline size_t case_get_value_cnt (const struct ccase *);
70 static inline const struct caseproto *case_get_proto (const struct ccase *);
72 size_t case_get_cost (const struct caseproto *);
74 struct ccase *case_resize (struct ccase *, const struct caseproto *)
75 WARN_UNUSED_RESULT;
76 struct ccase *case_unshare_and_resize (struct ccase *,
77 const struct caseproto *)
78 WARN_UNUSED_RESULT;
80 void case_set_missing (struct ccase *);
82 void case_copy (struct ccase *dst, size_t dst_idx,
83 const struct ccase *src, size_t src_idx,
84 size_t cnt);
85 void case_copy_out (const struct ccase *,
86 size_t start_idx, union value *, size_t n_values);
87 void case_copy_in (struct ccase *,
88 size_t start_idx, const union value *, size_t n_values);
90 const union value *case_data (const struct ccase *, const struct variable *);
91 const union value *case_data_idx (const struct ccase *, size_t idx);
92 union value *case_data_rw (struct ccase *, const struct variable *);
93 union value *case_data_rw_idx (struct ccase *, size_t idx);
95 double case_num (const struct ccase *, const struct variable *);
96 double case_num_idx (const struct ccase *, size_t idx);
98 const uint8_t *case_str (const struct ccase *, const struct variable *);
99 const uint8_t *case_str_idx (const struct ccase *, size_t idx);
100 uint8_t *case_str_rw (struct ccase *, const struct variable *);
101 uint8_t *case_str_rw_idx (struct ccase *, size_t idx);
103 int case_compare (const struct ccase *, const struct ccase *,
104 const struct variable *const *, size_t n_vars);
105 int case_compare_2dict (const struct ccase *, const struct ccase *,
106 const struct variable *const *,
107 const struct variable *const *,
108 size_t n_vars);
110 const union value *case_data_all (const struct ccase *);
111 union value *case_data_all_rw (struct ccase *);
113 struct ccase *case_unshare__ (struct ccase *);
114 void case_unref__ (struct ccase *);
116 /* If C is a shared case, that is, if it has a reference count
117 greater than 1, makes a new unshared copy and returns it,
118 decrementing C's reference count. If C is not shared (its
119 reference count is 1), returns C.
121 This function should be used before attempting to modify any
122 of the data in a case that might be shared, e.g.:
123 c = case_unshare (c); // Make sure that C is not shared.
124 case_data_rw (c, myvar)->f = 1; // Modify data in C.
126 static inline struct ccase *
127 case_unshare (struct ccase *c)
129 if (case_is_shared (c))
130 c = case_unshare__ (c);
131 return c;
134 /* Decrements case C's reference count. Frees C if its
135 reference count drops to 0.
137 If C is a null pointer, this function has no effect. */
138 static inline void
139 case_unref (struct ccase *c)
141 if (c != NULL && !--c->ref_cnt)
142 case_unref__ (c);
145 /* Returns true if case C is shared. A case that is shared
146 cannot be modified directly. Instead, an unshared copy must
147 first be made with case_unshare(). */
148 static inline bool
149 case_is_shared (const struct ccase *c)
151 return c->ref_cnt > 1;
154 /* Returns the number of union values in C. */
155 static inline size_t
156 case_get_value_cnt (const struct ccase *c)
158 return caseproto_get_n_widths (c->proto);
161 /* Returns the prototype that describes the format of case C.
162 The caller must not unref the returned prototype. */
163 static inline const struct caseproto *
164 case_get_proto (const struct ccase *c)
166 return c->proto;
169 #endif /* data/case.h */