1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004, 2007, 2009 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/>. */
24 #include <libpspp/compiler.h>
25 #include <data/caseproto.h>
29 /* A count of cases or the index of a case within a collection of
31 #define CASENUMBER_MAX LONG_MAX
32 typedef long int casenumber;
34 /* Reference-counted case implementation.
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
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
54 struct caseproto *proto; /* Case prototype. */
55 size_t ref_cnt; /* Reference count. */
56 union value values[1]; /* Values. */
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;
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 *);
68 static inline size_t case_get_value_cnt (const struct ccase *);
69 static inline const struct caseproto *case_get_proto (const struct ccase *);
71 size_t case_get_cost (const struct caseproto *);
73 struct ccase *case_resize (struct ccase *, const struct caseproto *)
75 struct ccase *case_unshare_and_resize (struct ccase *,
76 const struct caseproto *)
79 void case_set_missing (struct ccase *);
81 void case_copy (struct ccase *dst, size_t dst_idx,
82 const struct ccase *src, size_t src_idx,
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);
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);
94 double case_num (const struct ccase *, const struct variable *);
95 double case_num_idx (const struct ccase *, size_t idx);
97 const char *case_str (const struct ccase *, const struct variable *);
98 const char *case_str_idx (const struct ccase *, size_t idx);
99 char *case_str_rw (struct ccase *, const struct variable *);
100 char *case_str_rw_idx (struct ccase *, size_t idx);
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 *,
109 const union value *case_data_all (const struct ccase *);
110 union value *case_data_all_rw (struct ccase *);
112 struct ccase *case_unshare__ (struct ccase *);
113 void case_unref__ (struct ccase *);
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.
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.
125 static inline struct ccase *
126 case_unshare (struct ccase *c)
128 if (case_is_shared (c))
129 c = case_unshare__ (c);
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_)
138 struct ccase *c = (struct ccase *) c_;
143 /* Decrements case C's reference count. Frees C if its
144 reference count drops to 0.
146 If C is a null pointer, this function has no effect. */
148 case_unref (struct ccase *c)
150 if (c != NULL && !--c->ref_cnt)
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(). */
158 case_is_shared (const struct ccase *c)
160 return c->ref_cnt > 1;
163 /* Returns the number of union values in C. */
165 case_get_value_cnt (const struct ccase *c)
167 return caseproto_get_n_widths (c->proto);
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)
178 #endif /* data/case.h */