Respect the constness of caseproto.
[pspp-builds.git] / src / data / caseproto.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 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_CASEPROTO_H
18 #define DATA_CASEPROTO_H 1
19
20 #include <assert.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <data/value.h>
25 #include <libpspp/compiler.h>
26
27 /* Case prototype.
28
29    A case prototype specifies the number and type of the values
30    in a case.  It is essentially an array of integers, where the
31    array index is an index into a case and each element
32    represents the width of a value in a case.  Valid widths are:
33
34        * 0, indicating a numeric value.
35
36        * A positive integer between 1 and 32767, indicating the
37          size in bytes of a string value.
38
39        * -1, indicating that the value at this index in the case
40          is not used at all.  (This is rarely useful.)
41
42    Case prototypes are reference counted.  A newly created case
43    prototype has a single owner (the code that created it),
44    represented by an initial reference count of 1.  Other code
45    that receives the case prototype may keep a virtual copy of it
46    by calling caseproto_ref, which increments the case
47    prototype's reference count.  When this is done, the case
48    prototype becomes shared between its original owner and each
49    piece of code that incremented the reference count.
50
51    Functions that modifying case prototypes automatically unshare
52    them as necessary.  All of these functions potentially move
53    the caseproto around in memory even when the case prototype is
54    not shared.  Thus it is very important that every caller of a
55    function that modifies a case prototype thereafter uses the returned
56    caseproto instead of the one passed in as an argument.
57
58    Only the case prototype code should refer to caseproto members
59    directly.  Other code should use the provided helper
60    functions. */
61 struct caseproto
62   {
63     size_t ref_cnt;             /* Reference count. */
64
65     /* Tracking of long string widths.  Lazily maintained: when
66        'long_strings' is null and 'n_long_strings' is nonzero,
67        the former must be regenerated. */
68     size_t *long_strings;       /* Array of indexes of long string widths. */
69     size_t n_long_strings;      /* Number of long string widths. */
70
71     /* Widths. */
72     size_t n_widths;            /* Number of widths. */
73     size_t allocated_widths;    /* Space allocated for 'widths' array. */
74     short int widths[1];        /* Width of each case value. */
75   };
76
77 struct pool;
78
79 /* Creation and destruction. */
80 struct caseproto *caseproto_create (void) MALLOC_LIKE;
81 struct caseproto *caseproto_clone (const struct caseproto *) ;
82 static inline struct caseproto *caseproto_ref (const struct caseproto *);
83 struct caseproto *caseproto_ref_pool (const struct caseproto *, struct pool *);
84 static inline void caseproto_unref (struct caseproto *);
85
86 /* Inspecting stored widths.  */
87 static inline int caseproto_get_width (const struct caseproto *, size_t idx);
88 static inline size_t caseproto_get_n_widths (const struct caseproto *);
89
90 /* Adding and removing widths. */
91 struct caseproto *caseproto_reserve (struct caseproto *, size_t n_widths)
92   WARN_UNUSED_RESULT;
93 struct caseproto *caseproto_add_width (struct caseproto *, int width)
94   WARN_UNUSED_RESULT;
95 struct caseproto *caseproto_set_width (struct caseproto *,
96                                        size_t idx, int width)
97   WARN_UNUSED_RESULT;
98 struct caseproto *caseproto_insert_width (struct caseproto *,
99                                           size_t before, int width)
100   WARN_UNUSED_RESULT;
101 struct caseproto *caseproto_remove_widths (struct caseproto *,
102                                            size_t idx, size_t cnt)
103   WARN_UNUSED_RESULT;
104 struct caseproto *caseproto_move_widths (struct caseproto *,
105                                          size_t old_start, size_t new_start,
106                                          size_t cnt)
107   WARN_UNUSED_RESULT;
108
109 /* Working with "union value" arrays. */
110 bool caseproto_needs_init_values (const struct caseproto *);
111 void caseproto_init_values (const struct caseproto *, union value[]);
112 bool caseproto_try_init_values (const struct caseproto *, union value[]);
113 void caseproto_reinit_values (const struct caseproto *old,
114                               const struct caseproto *new, union value[]);
115 void caseproto_destroy_values (const struct caseproto *, union value[]);
116
117 void caseproto_copy (const struct caseproto *, size_t idx, size_t count,
118                      union value *dst, const union value *src);
119
120 /* Inspecting the cache of long string widths.
121
122    (These functions are useful for allocating cases, which
123    requires allocating a block memory for each long string value
124    in the case.) */
125 static inline size_t caseproto_get_n_long_strings (const struct caseproto *);
126 static inline size_t caseproto_get_long_string_idx (const struct caseproto *,
127                                                     size_t idx1);
128
129 /* For use in assertions. */
130 bool caseproto_range_is_valid (const struct caseproto *,
131                                size_t ofs, size_t count);
132 bool caseproto_is_conformable (const struct caseproto *a,
133                                const struct caseproto *b);
134 bool caseproto_equal (const struct caseproto *a, size_t a_start,
135                       const struct caseproto *b, size_t b_start,
136                       size_t n);
137 \f
138 /* Creation and destruction. */
139
140 void caseproto_free__ (struct caseproto *);
141
142 /* Increments case prototype PROTO's reference count and returns
143    PROTO.  Afterward, PROTO is shared among its reference count
144    holders. */
145 static inline struct caseproto *
146 caseproto_ref (const struct caseproto *proto_)
147 {
148   struct caseproto *proto = (struct caseproto *) proto_;
149   proto->ref_cnt++;
150   return proto;
151 }
152
153 /* Decrements case prototype PROTO's reference count.  Frees
154    PROTO if its reference count drops to 0.
155
156    If PROTO is a null pointer, this function has no effect. */
157 static inline void
158 caseproto_unref (struct caseproto *proto)
159 {
160   if (proto != NULL && !--proto->ref_cnt)
161     caseproto_free__ (proto);
162 }
163 \f
164 /* Inspecting stored widths.  */
165
166 /* Returns case prototype PROTO's width with the given IDX.  IDX
167    must be less than caseproto_get_n_widths(PROTO). */
168 static inline int
169 caseproto_get_width (const struct caseproto *proto, size_t idx)
170 {
171   assert (idx < proto->n_widths);
172   return proto->widths[idx];
173 }
174
175 /* Returns the number of widths in case prototype PROTO. */
176 static inline size_t
177 caseproto_get_n_widths (const struct caseproto *proto)
178 {
179   return proto->n_widths;
180 }
181 \f
182 /* Inspecting the cache of long string widths. */
183
184 void caseproto_refresh_long_string_cache__ (const struct caseproto *);
185
186 /* Returns the number of long string widths in PROTO; that is,
187    the number of widths in PROTO that are greater than to
188    MAX_SHORT_STRING. */
189 static inline size_t
190 caseproto_get_n_long_strings (const struct caseproto *proto)
191 {
192   return proto->n_long_strings;
193 }
194
195 /* Given long string width IDX1, returns a value IDX2 for which
196    caseproto_get_width(PROTO, IDX2) will return a value greater
197    than MAX_SHORT_STRING.  IDX1 must be less than
198    caseproto_get_n_long_strings(PROTO), and IDX2 will be less
199    than caseproto_get_n_widths(PROTO). */
200 static inline size_t
201 caseproto_get_long_string_idx (const struct caseproto *proto, size_t idx1)
202 {
203   if (proto->long_strings == NULL)
204     caseproto_refresh_long_string_cache__ (proto);
205
206   assert (idx1 < proto->n_long_strings);
207   return proto->long_strings[idx1];
208 }
209
210 #endif /* data/caseproto.h */