1 #ifndef VCTRS_H
2 #define VCTRS_H
3
4
5 #define R_NO_REMAP
6 #include <R.h>
7 #include <Rinternals.h>
8 #include <Rversion.h>
9
10 #include <stdbool.h>
11 #include <stdint.h>
12
13
14 extern bool vctrs_debug_verbose;
15
16 #define VCTRS_ASSERT(condition) ((void)sizeof(char[1 - 2*!(condition)]))
17
18
19 typedef R_xlen_t r_ssize;
20 #define R_SSIZE_MAX R_XLEN_T_MAX
21
22 #define r_length Rf_xlength
23 #define r_new_vector Rf_allocVector
24
25 // An ERR indicates either a C NULL in case of no error, or a
26 // condition object otherwise
27 #define ERR SEXP
28
29 // Vector types -------------------------------------------------
30
31 enum vctrs_type {
32 vctrs_type_null = 0,
33 vctrs_type_unspecified,
34 vctrs_type_logical,
35 vctrs_type_integer,
36 vctrs_type_double,
37 vctrs_type_complex,
38 vctrs_type_character,
39 vctrs_type_raw,
40 vctrs_type_list,
41 vctrs_type_dataframe,
42 vctrs_type_scalar,
43 vctrs_type_s3 = 255
44 };
45
46 /**
47 * @member type The vector type of the original data.
48 * @member proxy_method The function of the `vec_proxy()` method, if
49 * any. This method is looked up with [vec_proxy_method()].
50 */
51 struct vctrs_type_info {
52 enum vctrs_type type;
53 SEXP proxy_method;
54 };
55 /**
56 * @inheritMembers vctrs_type_info
57 * @member type If `proxy_method` was found, the vector type of the
58 * proxy data. Otherwise, the vector type of the original data.
59 * This is never `vctrs_type_s3`.
60 * @member proxy If `proxy_method` was found, the result of invoking
61 * the method. Otherwise, the original data.
62 */
63 struct vctrs_proxy_info {
64 enum vctrs_type type;
65 SEXP proxy_method;
66 SEXP proxy;
67 };
68
69 /**
70 * Return the type information of a vector or its proxy
71 *
72 * `vec_type_info()` returns the vctrs type of `x`. `vec_proxy_info()`
73 * returns the vctrs type of `x` or its proxy if it has one. The
74 * former returns `vctrs_type_s3` with S3 objects (expect for native
75 * types like bare data frames). The latter returns the bare type of
76 * the proxy, if any. It never returns `vctrs_type_s3`.
77 *
78 * `vec_proxy_info()` returns both the proxy method and the proxy
79 * data. `vec_type_info()` only returns the proxy method, which it
80 * needs to determine whether S3 lists and non-vector base types are
81 * scalars or proxied vectors.
82 *
83 * Use `PROTECT_PROXY_INFO()` and `PROTECT_TYPE_INFO()` to protect the
84 * members of the return value. These helpers take a pointer to a
85 * protection counter that can be passed to `UNPROTECT()`.
86 */
87 struct vctrs_type_info vec_type_info(SEXP x);
88 struct vctrs_proxy_info vec_proxy_info(SEXP x);
89
90 #define PROTECT_PROXY_INFO(info, n) do { \
91 PROTECT((info)->proxy); \
92 PROTECT((info)->proxy_method); \
93 *n += 2; \
94 } while (0)
95
96 #define PROTECT_TYPE_INFO(info, n) do { \
97 PROTECT((info)->proxy_method); \
98 *n += 1; \
99 } while (0)
100
101 enum vctrs_type vec_typeof(SEXP x);
102 enum vctrs_type vec_proxy_typeof(SEXP x);
103 const char* vec_type_as_str(enum vctrs_type type);
104 bool vec_is_list(SEXP x);
105 bool vec_is_vector(SEXP x);
106 bool vec_is_partial(SEXP x);
107
108 // After adding a new `vctrs_dispatch` type, add the missing entries
109 // in `vec_typeof2()`
110 enum vctrs_type2 {
111 vctrs_type2_null_null,
112 vctrs_type2_null_unspecified,
113 vctrs_type2_null_logical,
114 vctrs_type2_null_integer,
115 vctrs_type2_null_double,
116 vctrs_type2_null_complex,
117 vctrs_type2_null_character,
118 vctrs_type2_null_raw,
119 vctrs_type2_null_list,
120 vctrs_type2_null_dataframe,
121 vctrs_type2_null_s3,
122 vctrs_type2_null_scalar,
123
124 vctrs_type2_unspecified_unspecified,
125 vctrs_type2_unspecified_logical,
126 vctrs_type2_unspecified_integer,
127 vctrs_type2_unspecified_double,
128 vctrs_type2_unspecified_complex,
129 vctrs_type2_unspecified_character,
130 vctrs_type2_unspecified_raw,
131 vctrs_type2_unspecified_list,
132 vctrs_type2_unspecified_dataframe,
133 vctrs_type2_unspecified_s3,
134 vctrs_type2_unspecified_scalar,
135
136 vctrs_type2_logical_logical,
137 vctrs_type2_logical_integer,
138 vctrs_type2_logical_double,
139 vctrs_type2_logical_complex,
140 vctrs_type2_logical_character,
141 vctrs_type2_logical_raw,
142 vctrs_type2_logical_list,
143 vctrs_type2_logical_dataframe,
144 vctrs_type2_logical_s3,
145 vctrs_type2_logical_scalar,
146
147 vctrs_type2_integer_integer,
148 vctrs_type2_integer_double,
149 vctrs_type2_integer_complex,
150 vctrs_type2_integer_character,
151 vctrs_type2_integer_raw,
152 vctrs_type2_integer_list,
153 vctrs_type2_integer_dataframe,
154 vctrs_type2_integer_s3,
155 vctrs_type2_integer_scalar,
156
157 vctrs_type2_double_double,
158 vctrs_type2_double_complex,
159 vctrs_type2_double_character,
160 vctrs_type2_double_raw,
161 vctrs_type2_double_list,
162 vctrs_type2_double_dataframe,
163 vctrs_type2_double_s3,
164 vctrs_type2_double_scalar,
165
166 vctrs_type2_complex_complex,
167 vctrs_type2_complex_character,
168 vctrs_type2_complex_raw,
169 vctrs_type2_complex_list,
170 vctrs_type2_complex_dataframe,
171 vctrs_type2_complex_s3,
172 vctrs_type2_complex_scalar,
173
174 vctrs_type2_character_character,
175 vctrs_type2_character_raw,
176 vctrs_type2_character_list,
177 vctrs_type2_character_dataframe,
178 vctrs_type2_character_s3,
179 vctrs_type2_character_scalar,
180
181 vctrs_type2_raw_raw,
182 vctrs_type2_raw_list,
183 vctrs_type2_raw_dataframe,
184 vctrs_type2_raw_s3,
185 vctrs_type2_raw_scalar,
186
187 vctrs_type2_list_list,
188 vctrs_type2_list_dataframe,
189 vctrs_type2_list_s3,
190 vctrs_type2_list_scalar,
191
192 vctrs_type2_dataframe_dataframe,
193 vctrs_type2_dataframe_s3,
194 vctrs_type2_dataframe_scalar,
195
196 vctrs_type2_s3_s3,
197 vctrs_type2_s3_scalar,
198
199 vctrs_type2_scalar_scalar
200 };
201
202 enum vctrs_type2_s3 {
203 vctrs_type2_s3_null_bare_factor,
204 vctrs_type2_s3_null_bare_ordered,
205 vctrs_type2_s3_null_bare_date,
206 vctrs_type2_s3_null_bare_posixct,
207 vctrs_type2_s3_null_bare_posixlt,
208 vctrs_type2_s3_null_bare_tibble,
209 vctrs_type2_s3_null_unknown,
210
211 vctrs_type2_s3_unspecified_bare_factor,
212 vctrs_type2_s3_unspecified_bare_ordered,
213 vctrs_type2_s3_unspecified_bare_date,
214 vctrs_type2_s3_unspecified_bare_posixct,
215 vctrs_type2_s3_unspecified_bare_posixlt,
216 vctrs_type2_s3_unspecified_bare_tibble,
217 vctrs_type2_s3_unspecified_unknown,
218
219 vctrs_type2_s3_logical_bare_factor,
220 vctrs_type2_s3_logical_bare_ordered,
221 vctrs_type2_s3_logical_bare_date,
222 vctrs_type2_s3_logical_bare_posixct,
223 vctrs_type2_s3_logical_bare_posixlt,
224 vctrs_type2_s3_logical_bare_tibble,
225 vctrs_type2_s3_logical_unknown,
226
227 vctrs_type2_s3_integer_bare_factor,
228 vctrs_type2_s3_integer_bare_ordered,
229 vctrs_type2_s3_integer_bare_date,
230 vctrs_type2_s3_integer_bare_posixct,
231 vctrs_type2_s3_integer_bare_posixlt,
232 vctrs_type2_s3_integer_bare_tibble,
233 vctrs_type2_s3_integer_unknown,
234
235 vctrs_type2_s3_double_bare_factor,
236 vctrs_type2_s3_double_bare_ordered,
237 vctrs_type2_s3_double_bare_date,
238 vctrs_type2_s3_double_bare_posixct,
239 vctrs_type2_s3_double_bare_posixlt,
240 vctrs_type2_s3_double_bare_tibble,
241 vctrs_type2_s3_double_unknown,
242
243 vctrs_type2_s3_complex_bare_factor,
244 vctrs_type2_s3_complex_bare_ordered,
245 vctrs_type2_s3_complex_bare_date,
246 vctrs_type2_s3_complex_bare_posixct,
247 vctrs_type2_s3_complex_bare_posixlt,
248 vctrs_type2_s3_complex_bare_tibble,
249 vctrs_type2_s3_complex_unknown,
250
251 vctrs_type2_s3_character_bare_factor,
252 vctrs_type2_s3_character_bare_ordered,
253 vctrs_type2_s3_character_bare_date,
254 vctrs_type2_s3_character_bare_posixct,
255 vctrs_type2_s3_character_bare_posixlt,
256 vctrs_type2_s3_character_bare_tibble,
257 vctrs_type2_s3_character_unknown,
258
259 vctrs_type2_s3_raw_bare_factor,
260 vctrs_type2_s3_raw_bare_ordered,
261 vctrs_type2_s3_raw_bare_date,
262 vctrs_type2_s3_raw_bare_posixct,
263 vctrs_type2_s3_raw_bare_posixlt,
264 vctrs_type2_s3_raw_bare_tibble,
265 vctrs_type2_s3_raw_unknown,
266
267 vctrs_type2_s3_list_bare_factor,
268 vctrs_type2_s3_list_bare_ordered,
269 vctrs_type2_s3_list_bare_date,
270 vctrs_type2_s3_list_bare_posixct,
271 vctrs_type2_s3_list_bare_posixlt,
272 vctrs_type2_s3_list_bare_tibble,
273 vctrs_type2_s3_list_unknown,
274
275 vctrs_type2_s3_dataframe_bare_factor,
276 vctrs_type2_s3_dataframe_bare_ordered,
277 vctrs_type2_s3_dataframe_bare_date,
278 vctrs_type2_s3_dataframe_bare_posixct,
279 vctrs_type2_s3_dataframe_bare_posixlt,
280 vctrs_type2_s3_dataframe_bare_tibble,
281 vctrs_type2_s3_dataframe_unknown,
282
283 vctrs_type2_s3_scalar_bare_factor,
284 vctrs_type2_s3_scalar_bare_ordered,
285 vctrs_type2_s3_scalar_bare_date,
286 vctrs_type2_s3_scalar_bare_posixct,
287 vctrs_type2_s3_scalar_bare_posixlt,
288 vctrs_type2_s3_scalar_bare_tibble,
289 vctrs_type2_s3_scalar_unknown,
290
291 vctrs_type2_s3_bare_factor_bare_factor,
292 vctrs_type2_s3_bare_factor_bare_ordered,
293 vctrs_type2_s3_bare_factor_bare_date,
294 vctrs_type2_s3_bare_factor_bare_posixct,
295 vctrs_type2_s3_bare_factor_bare_posixlt,
296 vctrs_type2_s3_bare_factor_bare_tibble,
297 vctrs_type2_s3_bare_factor_unknown,
298
299 vctrs_type2_s3_bare_ordered_bare_ordered,
300 vctrs_type2_s3_bare_ordered_bare_date,
301 vctrs_type2_s3_bare_ordered_bare_posixct,
302 vctrs_type2_s3_bare_ordered_bare_posixlt,
303 vctrs_type2_s3_bare_ordered_bare_tibble,
304 vctrs_type2_s3_bare_ordered_unknown,
305
306 vctrs_type2_s3_bare_date_bare_date,
307 vctrs_type2_s3_bare_date_bare_posixct,
308 vctrs_type2_s3_bare_date_bare_posixlt,
309 vctrs_type2_s3_bare_date_bare_tibble,
310 vctrs_type2_s3_bare_date_unknown,
311
312 vctrs_type2_s3_bare_posixct_bare_posixct,
313 vctrs_type2_s3_bare_posixct_bare_posixlt,
314 vctrs_type2_s3_bare_posixct_bare_tibble,
315 vctrs_type2_s3_bare_posixct_unknown,
316
317 vctrs_type2_s3_bare_posixlt_bare_posixlt,
318 vctrs_type2_s3_bare_posixlt_bare_tibble,
319 vctrs_type2_s3_bare_posixlt_unknown,
320
321 vctrs_type2_s3_bare_tibble_bare_tibble,
322 vctrs_type2_s3_bare_tibble_unknown,
323
324 vctrs_type2_s3_unknown_unknown
325 };
326
327 enum vctrs_type2 vec_typeof2(SEXP x, SEXP y);
328 const char* vctrs_type2_as_str(enum vctrs_type2 type);
329
330 extern SEXP vctrs_shared_empty_lgl;
331 extern SEXP vctrs_shared_empty_int;
332 extern SEXP vctrs_shared_empty_dbl;
333 extern SEXP vctrs_shared_empty_cpl;
334 extern SEXP vctrs_shared_empty_chr;
335 extern SEXP vctrs_shared_empty_raw;
336 extern SEXP vctrs_shared_empty_list;
337 extern SEXP vctrs_shared_empty_date;
338 extern SEXP vctrs_shared_empty_uns;
339
340 extern SEXP vctrs_shared_true;
341 extern SEXP vctrs_shared_false;
342
343 extern Rcomplex vctrs_shared_na_cpl;
344 extern SEXP vctrs_shared_na_lgl;
345 extern SEXP vctrs_shared_na_list;
346
347 SEXP vec_unspecified(R_len_t n);
348 bool vec_is_unspecified(SEXP x);
349
350
351 // Vector methods ------------------------------------------------
352
353 #include "arg.h"
354 #include "names.h"
355 #include "owned.h"
356
357 enum vctrs_proxy_kind {
358 VCTRS_PROXY_KIND_default,
359 VCTRS_PROXY_KIND_equal,
360 VCTRS_PROXY_KIND_compare,
361 VCTRS_PROXY_KIND_order,
362 VCTRS_PROXY_KIND_complete
363 };
364
365 SEXP vec_proxy(SEXP x);
366 SEXP vec_proxy_equal(SEXP x);
367 SEXP vec_proxy_compare(SEXP x);
368 SEXP vec_proxy_order(SEXP x);
369 SEXP vec_proxy_complete(SEXP x);
370 SEXP vec_restore(SEXP x, SEXP to, SEXP n, const enum vctrs_owned owned);
371 SEXP vec_restore_default(SEXP x, SEXP to, const enum vctrs_owned owned);
372 R_len_t vec_size(SEXP x);
373 R_len_t vec_size_common(SEXP xs, R_len_t absent);
374 SEXP vec_cast_common(SEXP xs, SEXP to);
375 SEXP vec_slice(SEXP x, SEXP subscript);
376 SEXP vec_slice_impl(SEXP x, SEXP subscript);
377 SEXP vec_chop(SEXP x, SEXP indices);
378 SEXP vec_slice_shaped(enum vctrs_type type, SEXP x, SEXP index);
379 SEXP vec_proxy_assign(SEXP proxy, SEXP index, SEXP value);
380 bool vec_requires_fallback(SEXP x, struct vctrs_proxy_info info);
381 SEXP vec_init(SEXP x, R_len_t n);
382 SEXP vec_ptype(SEXP x, struct vctrs_arg* x_arg);
383 SEXP vec_ptype_finalise(SEXP x);
384 bool vec_is_unspecified(SEXP x);
385 SEXP vec_recycle(SEXP x, R_len_t size, struct vctrs_arg* x_arg);
386 SEXP vec_recycle_fallback(SEXP x, R_len_t size, struct vctrs_arg* x_arg);
387 SEXP vec_recycle_common(SEXP xs, R_len_t size);
388 SEXP vec_names(SEXP x);
389 SEXP vec_proxy_names(SEXP x);
390 SEXP vec_group_loc(SEXP x);
391 SEXP vec_identify_runs(SEXP x);
392 SEXP vec_match_params(SEXP needles, SEXP haystack, bool na_equal,
393 struct vctrs_arg* needles_arg, struct vctrs_arg* haystack_arg);
394
395 #include "cast.h"
vec_cast(SEXP x,SEXP to,struct vctrs_arg * x_arg,struct vctrs_arg * to_arg)396 static inline SEXP vec_cast(SEXP x, SEXP to, struct vctrs_arg* x_arg, struct vctrs_arg* to_arg) {
397 struct cast_opts opts = {
398 .x = x,
399 .to = to,
400 .x_arg = x_arg,
401 .to_arg = to_arg
402 };
403 return vec_cast_opts(&opts);
404 }
405
vec_match(SEXP needles,SEXP haystack)406 static inline SEXP vec_match(SEXP needles, SEXP haystack) {
407 return vec_match_params(needles, haystack, true, NULL, NULL);
408 }
409
410
411 SEXP vec_c(SEXP xs,
412 SEXP ptype,
413 SEXP name_spec,
414 const struct name_repair_opts* name_repair);
415
416 bool is_data_frame(SEXP x);
417
418 R_len_t df_size(SEXP x);
419 R_len_t df_rownames_size(SEXP x);
420 R_len_t df_raw_size(SEXP x);
421 R_len_t df_raw_size_from_list(SEXP x);
422 SEXP vec_bare_df_restore(SEXP x, SEXP to, SEXP n, const enum vctrs_owned owned);
423 SEXP vec_df_restore(SEXP x, SEXP to, SEXP n, const enum vctrs_owned owned);
424
425 // equal_object() never propagates missingness, so
426 // it can return a `bool`
427 bool equal_object(SEXP x, SEXP y);
428 bool equal_object_normalized(SEXP x, SEXP y);
429 bool equal_names(SEXP x, SEXP y);
430
431 int compare_scalar(SEXP x, R_len_t i, SEXP y, R_len_t j, bool na_equal);
432
433 uint32_t hash_object(SEXP x);
434 void hash_fill(uint32_t* p, R_len_t n, SEXP x, bool na_equal);
435
436 SEXP vec_unique(SEXP x);
437 bool duplicated_any(SEXP names);
438
439 // Data frame column iteration ----------------------------------
440
441 // Used in functions that treat data frames as vectors of rows, but
442 // iterate over columns. Examples are `vec_equal()` and
443 // `vec_compare()`.
444
445 /**
446 * @member row_known A boolean array of size `n_row`. Allocated on the R heap.
447 * Initially, all values are initialized to `false`. As we iterate along the
448 * columns, we flip the corresponding row's `row_known` value to `true` if we
449 * can determine the `out` value for that row from the current columns.
450 * Once a row's `row_known` value is `true`, we never check that row again
451 * as we continue through the columns.
452 * @member p_row_known A pointer to the boolean array stored in `row_known`.
453 * Initialized with `(bool*) RAW(info.row_known)`.
454 * @member remaining The number of `row_known` values that are still `false`.
455 * If this hits `0` before we traverse the entire data frame, we can exit
456 * immediately because all `out` values are already known.
457 * @member size The number of rows in the data frame.
458 */
459 struct df_short_circuit_info {
460 SEXP row_known;
461 bool* p_row_known;
462 PROTECT_INDEX row_known_pi;
463 R_len_t remaining;
464 R_len_t size;
465 };
466
467 #define PROTECT_DF_SHORT_CIRCUIT_INFO(p_info, p_n) do { \
468 PROTECT_WITH_INDEX((p_info)->row_known, &(p_info)->row_known_pi); \
469 *(p_n) += 1; \
470 } while (0)
471
new_df_short_circuit_info(R_len_t size,bool lazy)472 static inline struct df_short_circuit_info new_df_short_circuit_info(R_len_t size, bool lazy) {
473 SEXP row_known;
474 bool* p_row_known;
475
476 if (lazy) {
477 row_known = PROTECT(R_NilValue);
478 p_row_known = NULL;
479 } else {
480 row_known = PROTECT(Rf_allocVector(RAWSXP, size * sizeof(bool)));
481 p_row_known = (bool*) RAW(row_known);
482
483 // To begin with, no rows have a known comparison value
484 memset(p_row_known, false, size * sizeof(bool));
485 }
486
487 struct df_short_circuit_info info = {
488 .row_known = row_known,
489 .p_row_known = p_row_known,
490 .remaining = size,
491 .size = size
492 };
493
494 UNPROTECT(1);
495 return info;
496 }
497
init_lazy_df_short_circuit_info(struct df_short_circuit_info * p_info)498 static inline void init_lazy_df_short_circuit_info(struct df_short_circuit_info* p_info) {
499 if (p_info->row_known != R_NilValue) {
500 return;
501 }
502
503 p_info->row_known = Rf_allocVector(RAWSXP, p_info->size * sizeof(bool));
504 REPROTECT(p_info->row_known, p_info->row_known_pi);
505
506 p_info->p_row_known = (bool*) RAW(p_info->row_known);
507 }
508
509 // Missing values -----------------------------------------------
510
511 // Annex F of C99 specifies that `double` should conform to the IEEE 754
512 // type `binary64`, which is defined as:
513 // * 1 bit : sign
514 // * 11 bits: exponent
515 // * 52 bits: significand
516 //
517 // R stores the value "1954" in the last 32 bits: this payload marks
518 // the value as a NA, not a regular NaN.
519 //
520 // On big endian systems, this corresponds to the second element of an
521 // integer array of size 2. On little endian systems, this is flipped
522 // and the NA marker is in the first element.
523 //
524 // The type assumptions made here are asserted in `vctrs_init_utils()`
525
526 #ifdef WORDS_BIGENDIAN
527 static const int vctrs_indicator_pos = 1;
528 #else
529 static const int vctrs_indicator_pos = 0;
530 #endif
531
532 union vctrs_dbl_indicator {
533 double value; // 8 bytes
534 unsigned int key[2]; // 4 * 2 bytes
535 };
536
537 enum vctrs_dbl_class {
538 vctrs_dbl_number,
539 vctrs_dbl_missing,
540 vctrs_dbl_nan
541 };
542
543 enum vctrs_dbl_class dbl_classify(double x);
544
545 // Factor methods -----------------------------------------------
546
547 SEXP chr_as_factor(SEXP x, SEXP to, bool* lossy, struct vctrs_arg* to_arg);
548 SEXP chr_as_ordered(SEXP x, SEXP to, bool* lossy, struct vctrs_arg* to_arg);
549
550 SEXP fct_as_character(SEXP x, struct vctrs_arg* x_arg);
551 SEXP fct_as_factor(SEXP x, SEXP to, bool* lossy, struct vctrs_arg* x_arg, struct vctrs_arg* to_arg);
552
553 SEXP ord_as_character(SEXP x, struct vctrs_arg* x_arg);
554
555 // Datetime methods ---------------------------------------------
556
557 SEXP date_as_date(SEXP x);
558 SEXP date_as_posixct(SEXP x, SEXP to);
559 SEXP date_as_posixlt(SEXP x, SEXP to);
560 SEXP posixct_as_date(SEXP x, bool* lossy);
561 SEXP posixlt_as_date(SEXP x, bool* lossy);
562 SEXP posixct_as_posixct(SEXP x, SEXP to);
563 SEXP posixlt_as_posixct(SEXP x, SEXP to);
564 SEXP posixct_as_posixlt(SEXP x, SEXP to);
565 SEXP posixlt_as_posixlt(SEXP x, SEXP to);
566
567 SEXP vec_date_restore(SEXP x, SEXP to, const enum vctrs_owned owned);
568 SEXP vec_posixct_restore(SEXP x, SEXP to, const enum vctrs_owned owned);
569 SEXP vec_posixlt_restore(SEXP x, SEXP to, const enum vctrs_owned owned);
570
571 SEXP date_datetime_ptype2(SEXP x, SEXP y);
572 SEXP datetime_datetime_ptype2(SEXP x, SEXP y);
573
574 // Growable vector ----------------------------------------------
575
576 struct growable {
577 SEXP x;
578 SEXPTYPE type;
579 void* array;
580 PROTECT_INDEX idx;
581 int n;
582 int capacity;
583 };
584
585 struct growable new_growable(SEXPTYPE type, int capacity);
586 SEXP growable_values(struct growable* g);
587
growable_push_int(struct growable * g,int i)588 static inline void growable_push_int(struct growable* g, int i) {
589 if (g->n == g->capacity) {
590 g->capacity *= 2;
591 g->x = Rf_lengthgets(g->x, g->capacity);
592 REPROTECT(g->x, g->idx);
593 g->array = INTEGER(g->x);
594 }
595
596 int* p = (int*) g->array;
597 p[g->n] = i;
598 ++(g->n);
599 }
600
601 #define PROTECT_GROWABLE(g, n) do { \
602 PROTECT_WITH_INDEX((g)->x, &((g)->idx)); \
603 *n += 1; \
604 } while(0)
605
606 #define UNPROTECT_GROWABLE(g) do { UNPROTECT(1);} while(0)
607
608 // Conditions ---------------------------------------------------
609
610 void stop_scalar_type(SEXP x, struct vctrs_arg* arg) __attribute__((noreturn));
611 void vec_assert(SEXP x, struct vctrs_arg* arg);
612 __attribute__((noreturn))
613 void stop_incompatible_size(SEXP x, SEXP y,
614 R_len_t x_size, R_len_t y_size,
615 struct vctrs_arg* x_arg,
616 struct vctrs_arg* y_arg);
617 __attribute__((noreturn))
618 void stop_incompatible_type(SEXP x,
619 SEXP y,
620 struct vctrs_arg* x_arg,
621 struct vctrs_arg* y_arg,
622 bool cast);
623 __attribute__((noreturn))
624 void stop_recycle_incompatible_size(R_len_t x_size, R_len_t size,
625 struct vctrs_arg* x_arg);
626 __attribute__((noreturn))
627 void stop_incompatible_shape(SEXP x, SEXP y,
628 R_len_t x_size, R_len_t y_size, int axis,
629 struct vctrs_arg* p_x_arg, struct vctrs_arg* p_y_arg);
630 void stop_corrupt_factor_levels(SEXP x, struct vctrs_arg* arg) __attribute__((noreturn));
631 void stop_corrupt_ordered_levels(SEXP x, struct vctrs_arg* arg) __attribute__((noreturn));
632
633 // Compatibility ------------------------------------------------
634
635 #if (R_VERSION < R_Version(3, 5, 0))
636 # define LOGICAL_RO(x) ((const int*) LOGICAL(x))
637 # define INTEGER_RO(x) ((const int*) INTEGER(x))
638 # define REAL_RO(x) ((const double*) REAL(x))
639 # define COMPLEX_RO(x) ((const Rcomplex*) COMPLEX(x))
640 # define STRING_PTR_RO(x) ((const SEXP*) STRING_PTR(x))
641 # define RAW_RO(x) ((const Rbyte*) RAW(x))
642 # define DATAPTR_RO(x) ((const void*) STRING_PTR(x))
643 #endif
644
645 #define VECTOR_PTR_RO(x) ((const SEXP*) DATAPTR_RO(x))
646
647
648 #endif
649