forked from r-lib/vctrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrep.c
385 lines (294 loc) · 10 KB
/
rep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include "vctrs.h"
#include "type-data-frame.h"
#include "decl/rep-decl.h"
r_obj* vec_rep(r_obj* x,
int times,
struct r_lazy error_call,
struct vctrs_arg* p_x_arg,
struct vctrs_arg* p_times_arg) {
check_rep_times(times, error_call, p_times_arg);
if (times == 1) {
return x;
}
const r_ssize times_ = (r_ssize) times;
const r_ssize x_size = vec_size(x);
if (x_size == 1) {
return vec_check_recycle(x, times_, p_x_arg, error_call);
}
if (multiply_would_overflow(x_size, times_)) {
stop_rep_size_oob(error_call);
};
const r_ssize size = x_size * times_;
r_obj* subscript = KEEP(r_alloc_integer(size));
int* v_subscript = r_int_begin(subscript);
r_ssize k = 0;
for (r_ssize i = 0; i < times_; ++i) {
for (r_ssize j = 1; j <= x_size; ++j, ++k) {
v_subscript[k] = j;
}
}
r_obj* out = vec_slice_unsafe(x, subscript);
FREE(1);
return out;
}
r_obj* ffi_vec_rep(r_obj* x, r_obj* ffi_times, r_obj* frame) {
struct r_lazy error_call = { .x = r_syms.error_call, .env = frame };
struct r_lazy x_arg_lazy = { .x = syms.x_arg, .env = frame };
struct vctrs_arg x_arg = new_lazy_arg(&x_arg_lazy);
struct r_lazy times_arg_lazy = { .x = syms.times_arg, .env = frame };
struct vctrs_arg times_arg = new_lazy_arg(×_arg_lazy);
ffi_times = KEEP(vec_cast(ffi_times,
r_globals.empty_int,
×_arg,
vec_args.empty,
error_call));
if (vec_size(ffi_times) != 1) {
stop_rep_times_size(error_call, ×_arg);
}
const int times = r_int_get(ffi_times, 0);
r_obj* out = vec_rep(x, times, error_call, &x_arg, ×_arg);
FREE(1);
return out;
}
// -----------------------------------------------------------------------------
r_obj* vec_rep_each(r_obj* x,
r_obj* times,
struct r_lazy error_call,
struct vctrs_arg* p_x_arg,
struct vctrs_arg* p_times_arg) {
times = KEEP(vec_cast(times,
r_globals.empty_int,
p_times_arg,
vec_args.empty,
error_call));
const r_ssize times_size = vec_size(times);
r_obj* out;
if (times_size == 1) {
const int times_ = r_int_get(times, 0);
if (times_ == 1) {
out = x;
} else if (times_ == 0) {
out = vec_slice_unsafe(x, r_globals.empty_int);
} else {
out = vec_rep_each_uniform(x, times_, error_call, p_times_arg);
}
} else {
out = vec_rep_each_impl(x, times, times_size, error_call, p_times_arg);
}
FREE(1);
return out;
}
r_obj* ffi_vec_rep_each(r_obj* x, r_obj* times, r_obj* frame) {
struct r_lazy error_call = { .x = r_syms.error_call, .env = frame };
struct r_lazy x_arg_lazy = { .x = syms.times_arg, .env = frame };
struct vctrs_arg x_arg = new_lazy_arg(&x_arg_lazy);
struct r_lazy times_arg_lazy = { .x = syms.times_arg, .env = frame };
struct vctrs_arg times_arg = new_lazy_arg(×_arg_lazy);
return vec_rep_each(x, times, error_call, &x_arg, ×_arg);
}
// -----------------------------------------------------------------------------
static
r_obj* vec_rep_each_uniform(r_obj* x,
int times,
struct r_lazy error_call,
struct vctrs_arg* p_times_arg) {
check_rep_each_times(times, 1, error_call, p_times_arg);
const r_ssize times_ = (r_ssize) times;
const r_ssize x_size = vec_size(x);
if (multiply_would_overflow(x_size, times_)) {
stop_rep_size_oob(error_call);
};
const r_ssize size = x_size * times_;
r_obj* subscript = KEEP(r_alloc_integer(size));
int* v_subscript = r_int_begin(subscript);
r_ssize k = 0;
for (r_ssize i = 1; i <= x_size; ++i) {
for (r_ssize j = 0; j < times_; ++j, ++k) {
v_subscript[k] = i;
}
}
r_obj* out = vec_slice_unsafe(x, subscript);
FREE(1);
return out;
}
static r_obj* vec_rep_each_impl(r_obj* x,
r_obj* times,
const r_ssize times_size,
struct r_lazy error_call,
struct vctrs_arg* p_times_arg) {
const r_ssize x_size = vec_size(x);
if (x_size != times_size) {
stop_recycle_incompatible_size(times_size,
x_size,
p_times_arg,
error_call);
}
const int* v_times = r_int_cbegin(times);
r_ssize size = 0;
for (r_ssize i = 0; i < times_size; ++i) {
const int elt_times = v_times[i];
check_rep_each_times(elt_times, i + 1, error_call, p_times_arg);
const r_ssize elt_times_ = (r_ssize) elt_times;
if (plus_would_overflow(size, elt_times_)) {
stop_rep_size_oob(error_call);
}
size += elt_times_;
}
r_obj* subscript = KEEP(r_alloc_integer(size));
int* v_subscript = r_int_begin(subscript);
r_ssize k = 0;
for (r_ssize i = 1; i <= x_size; ++i) {
const r_ssize elt_times = (r_ssize) v_times[i - 1];
for (r_ssize j = 0; j < elt_times; ++j, ++k) {
v_subscript[k] = i;
}
}
r_obj* out = vec_slice_unsafe(x, subscript);
FREE(1);
return out;
}
// -----------------------------------------------------------------------------
// TODO: Modify for long vectors with `R_XLEN_T_MAX` and `R_xlen_t`.
static inline
bool times_is_oob(int times) {
return times > R_LEN_T_MAX;
}
// Only useful for positive or zero inputs
static inline
bool multiply_would_overflow(r_ssize x, r_ssize y) {
return (double) x * y > R_LEN_T_MAX;
}
// Only useful for positive or zero inputs
static inline
bool plus_would_overflow(r_ssize x, r_ssize y) {
return x > R_LEN_T_MAX - y;
}
// -----------------------------------------------------------------------------
static inline
void check_rep_times(int times,
struct r_lazy call,
struct vctrs_arg* p_times_arg) {
if (times < 0) {
if (times == r_globals.na_int) {
stop_rep_times_missing(call, p_times_arg);
} else {
stop_rep_times_negative(call, p_times_arg);
}
} else if (times_is_oob(times)) {
stop_rep_times_oob(times, call, p_times_arg);
}
}
static inline
void stop_rep_times_negative(struct r_lazy call, struct vctrs_arg* p_times_arg) {
r_abort_lazy_call(call,
"%s must be a positive number.",
vec_arg_format(p_times_arg));
}
static inline
void stop_rep_times_missing(struct r_lazy call, struct vctrs_arg* p_times_arg) {
r_abort_lazy_call(call,
"%s can't be missing.",
vec_arg_format(p_times_arg));
}
// Not currently thrown since `r_ssize == int`, but might be once
// long vectors are supported
static inline
void stop_rep_times_oob(int times, struct r_lazy call, struct vctrs_arg* p_times_arg) {
r_abort_lazy_call(
call,
"%s must be less than %i, not %i.",
vec_arg_format(p_times_arg),
R_LEN_T_MAX,
times
);
}
// -----------------------------------------------------------------------------
static inline
void check_rep_each_times(int times,
r_ssize i,
struct r_lazy call,
struct vctrs_arg* p_times_arg) {
if (times < 0) {
if (times == r_globals.na_int) {
stop_rep_each_times_missing(i, call, p_times_arg);
} else {
stop_rep_each_times_negative(i, call, p_times_arg);
}
} else if (times_is_oob(times)) {
stop_rep_each_times_oob(times, i, call, p_times_arg);
}
}
static inline
void stop_rep_each_times_negative(r_ssize i, struct r_lazy call, struct vctrs_arg* p_times_arg) {
r_abort_lazy_call(call,
"%s must be a vector of positive numbers. Location %i is negative.",
vec_arg_format(p_times_arg),
i);
}
static inline
void stop_rep_each_times_missing(r_ssize i, struct r_lazy call, struct vctrs_arg* p_times_arg) {
r_abort_lazy_call(call,
"%s can't be missing. Location %i is missing.",
vec_arg_format(p_times_arg),
i);
}
// Not currently thrown since `r_ssize == int`, but might be once
// long vectors are supported
static inline
void stop_rep_each_times_oob(int times, r_ssize i, struct r_lazy call, struct vctrs_arg* p_times_arg) {
r_abort_lazy_call(
call,
"%s must be less than %i, not %i. ",
"Location %i is too large.",
vec_arg_format(p_times_arg),
R_LEN_T_MAX,
times,
i
);
}
static inline
void stop_rep_size_oob(struct r_lazy call) {
r_abort_lazy_call(
call,
"Long vectors are not yet supported. "
"Requested output size must be less than %i.",
R_LEN_T_MAX
);
}
static inline
void stop_rep_times_size(struct r_lazy call,
struct vctrs_arg* p_times_arg) {
r_abort_lazy_call(call,
"%s must be a single number.",
vec_arg_format(p_times_arg));
}
// -----------------------------------------------------------------------------
static
r_obj* vec_unrep(r_obj* x, struct r_lazy error_call) {
r_obj* times = KEEP(vec_run_sizes(x, error_call));
const int* v_times = r_int_cbegin(times);
const r_ssize size = r_length(times);
r_obj* loc = KEEP(r_alloc_integer(size));
int* v_loc = r_int_begin(loc);
r_ssize current = 1;
for (r_ssize i = 0; i < size; ++i) {
v_loc[i] = current;
current += v_times[i];
}
r_obj* out = KEEP(r_new_list(2));
r_list_poke(out, 0, vec_slice_unsafe(x, loc));
r_list_poke(out, 1, times);
r_obj* names = r_new_character(2);
r_attrib_poke_names(out, names);
r_chr_poke(names, 0, strings_key);
r_chr_poke(names, 1, strings_times);
init_data_frame(out, size);
FREE(3);
return out;
}
r_obj* ffi_vec_unrep(r_obj* x, r_obj* frame) {
struct r_lazy error_call = { .x = frame, .env = r_null };
return vec_unrep(x, error_call);
}
// -----------------------------------------------------------------------------
void vctrs_init_rep(r_obj* ns) { }