-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathaltrep.c
1180 lines (985 loc) · 33.7 KB
/
altrep.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* R : A Computer Language for Statistical Data Analysis
* Copyright (C) 2016--2023 The R Core Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, a copy is available at
* https://www.R-project.org/Licenses/
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <Defn.h>
#include <R_ext/Altrep.h>
/***
*** ALTREP Abstract Class Framework
***/
/**
** ALTREP Class Registry for Serialization
**/
/* Use ATTRIB field to hold class info. OK since not visible outside. */
#define ALTREP_CLASS_SERIALIZED_CLASS(x) ATTRIB(x)
#define SET_ALTREP_CLASS_SERIALIZED_CLASS(x, csym, psym, stype) \
SET_ATTRIB(x, list3(csym, psym, stype))
#define ALTREP_SERIALIZED_CLASS_CLSSYM(x) CAR(x)
#define ALTREP_SERIALIZED_CLASS_PKGSYM(x) CADR(x)
#define ALTREP_SERIALIZED_CLASS_TYPE(x) INTEGER0(CADDR(x))[0]
#define ALTREP_OBJECT_CLSSYM(x) ALTREP_SERIALIZED_CLASS_CLSSYM( \
ALTREP_SERIALIZED_CLASS(x))
#define ALTREP_OBJECT_PKGSYM(x) ALTREP_SERIALIZED_CLASS_PKGSYM( \
ALTREP_SERIALIZED_CLASS(x))
#define ALTREP_CLASS_BASE_TYPE(x) \
ALTREP_SERIALIZED_CLASS_TYPE(ALTREP_CLASS_SERIALIZED_CLASS(x))
static SEXP Registry = NULL;
static SEXP LookupClassEntry(SEXP csym, SEXP psym)
{
for (SEXP chain = CDR(Registry); chain != R_NilValue; chain = CDR(chain))
if (TAG(CAR(chain)) == csym && CADR(CAR(chain)) == psym)
return CAR(chain);
return NULL;
}
static void
RegisterClass(SEXP class, int type, const char *cname, const char *pname,
DllInfo *dll)
{
PROTECT(class);
if (Registry == NULL) {
Registry = CONS(R_NilValue, R_NilValue);
R_PreserveObject(Registry);
}
SEXP csym = install(cname);
SEXP psym = install(pname);
SEXP stype = PROTECT(ScalarInteger(type));
SEXP iptr = R_MakeExternalPtr(dll, R_NilValue, R_NilValue);
SEXP entry = LookupClassEntry(csym, psym);
if (entry == NULL) {
entry = list4(class, psym, stype, iptr);
SET_TAG(entry, csym);
SETCDR(Registry, CONS(entry, CDR(Registry)));
}
else {
SETCAR(entry, class);
SETCAR(CDR(CDR(entry)), stype);
SETCAR(CDR(CDR(CDR(entry))), iptr);
}
SET_ALTREP_CLASS_SERIALIZED_CLASS(class, csym, psym, stype);
UNPROTECT(2); /* class, stype */
}
static SEXP LookupClass(SEXP csym, SEXP psym)
{
SEXP entry = LookupClassEntry(csym, psym);
return entry != NULL ? CAR(entry) : NULL;
}
static void reinit_altrep_class(SEXP sclass);
attribute_hidden void R_reinit_altrep_classes(DllInfo *dll)
{
for (SEXP chain = CDR(Registry); chain != R_NilValue; chain = CDR(chain)) {
SEXP entry = CAR(chain);
SEXP iptr = CAR(CDR(CDR(CDR(entry))));
if (R_ExternalPtrAddr(iptr) == dll)
reinit_altrep_class(CAR(entry));
}
}
/**
** ALTREP Method Tables and Class Objects
**/
#define ALTREP_ERROR_IN_CLASS(msg, x) do { \
error("%s [class: %s, pkg: %s]", \
msg, \
CHAR(PRINTNAME(ALTREP_OBJECT_CLSSYM(x))), \
CHAR(PRINTNAME(ALTREP_OBJECT_PKGSYM(x)))); \
} while(0)
static void SET_ALTREP_CLASS(SEXP x, SEXP class)
{
SETALTREP(x, 1);
SET_TAG(x, class);
}
#define CLASS_METHODS_TABLE(class) STDVEC_DATAPTR(class)
#define GENERIC_METHODS_TABLE(x, class) \
((class##_methods_t *) CLASS_METHODS_TABLE(ALTREP_CLASS(x)))
#define ALTREP_METHODS_TABLE(x) GENERIC_METHODS_TABLE(x, altrep)
#define ALTVEC_METHODS_TABLE(x) GENERIC_METHODS_TABLE(x, altvec)
#define ALTINTEGER_METHODS_TABLE(x) GENERIC_METHODS_TABLE(x, altinteger)
#define ALTREAL_METHODS_TABLE(x) GENERIC_METHODS_TABLE(x, altreal)
#define ALTLOGICAL_METHODS_TABLE(x) GENERIC_METHODS_TABLE(x, altlogical)
#define ALTRAW_METHODS_TABLE(x) GENERIC_METHODS_TABLE(x, altraw)
#define ALTCOMPLEX_METHODS_TABLE(x) GENERIC_METHODS_TABLE(x, altcomplex)
#define ALTSTRING_METHODS_TABLE(x) GENERIC_METHODS_TABLE(x, altstring)
#define ALTLIST_METHODS_TABLE(x) GENERIC_METHODS_TABLE(x, altlist)
#define ALTREP_METHODS \
R_altrep_UnserializeEX_method_t UnserializeEX; \
R_altrep_Unserialize_method_t Unserialize; \
R_altrep_Serialized_state_method_t Serialized_state; \
R_altrep_DuplicateEX_method_t DuplicateEX; \
R_altrep_Duplicate_method_t Duplicate; \
R_altrep_Coerce_method_t Coerce; \
R_altrep_Inspect_method_t Inspect; \
R_altrep_Length_method_t Length
#define ALTVEC_METHODS \
ALTREP_METHODS; \
R_altvec_Dataptr_method_t Dataptr; \
R_altvec_Dataptr_or_null_method_t Dataptr_or_null; \
R_altvec_Extract_subset_method_t Extract_subset
#define ALTINTEGER_METHODS \
ALTVEC_METHODS; \
R_altinteger_Elt_method_t Elt; \
R_altinteger_Get_region_method_t Get_region; \
R_altinteger_Is_sorted_method_t Is_sorted; \
R_altinteger_No_NA_method_t No_NA; \
R_altinteger_Sum_method_t Sum ; \
R_altinteger_Min_method_t Min; \
R_altinteger_Max_method_t Max
#define ALTREAL_METHODS \
ALTVEC_METHODS; \
R_altreal_Elt_method_t Elt; \
R_altreal_Get_region_method_t Get_region; \
R_altreal_Is_sorted_method_t Is_sorted; \
R_altreal_No_NA_method_t No_NA; \
R_altreal_Sum_method_t Sum; \
R_altreal_Min_method_t Min; \
R_altreal_Max_method_t Max
#define ALTLOGICAL_METHODS \
ALTVEC_METHODS; \
R_altlogical_Elt_method_t Elt; \
R_altlogical_Get_region_method_t Get_region;\
R_altlogical_Is_sorted_method_t Is_sorted; \
R_altlogical_No_NA_method_t No_NA; \
R_altlogical_Sum_method_t Sum
#define ALTRAW_METHODS \
ALTVEC_METHODS; \
R_altraw_Elt_method_t Elt; \
R_altraw_Get_region_method_t Get_region
#define ALTCOMPLEX_METHODS \
ALTVEC_METHODS; \
R_altcomplex_Elt_method_t Elt; \
R_altcomplex_Get_region_method_t Get_region
#define ALTSTRING_METHODS \
ALTVEC_METHODS; \
R_altstring_Elt_method_t Elt; \
R_altstring_Set_elt_method_t Set_elt; \
R_altstring_Is_sorted_method_t Is_sorted; \
R_altstring_No_NA_method_t No_NA
#define ALTLIST_METHODS \
ALTVEC_METHODS; \
R_altlist_Elt_method_t Elt; \
R_altlist_Set_elt_method_t Set_elt
typedef struct { ALTREP_METHODS; } altrep_methods_t;
typedef struct { ALTVEC_METHODS; } altvec_methods_t;
typedef struct { ALTINTEGER_METHODS; } altinteger_methods_t;
typedef struct { ALTREAL_METHODS; } altreal_methods_t;
typedef struct { ALTLOGICAL_METHODS; } altlogical_methods_t;
typedef struct { ALTRAW_METHODS; } altraw_methods_t;
typedef struct { ALTCOMPLEX_METHODS; } altcomplex_methods_t;
typedef struct { ALTSTRING_METHODS; } altstring_methods_t;
typedef struct { ALTLIST_METHODS; } altlist_methods_t;
/* Macro to extract first element from ... macro argument.
From Richard Hansen's answer in
http://stackoverflow.com/questions/5588855/standard-alternative-to-gccs-va-args-trick
*/
#define DISPATCH_TARGET(...) DISPATCH_TARGET_HELPER(__VA_ARGS__, dummy)
#define DISPATCH_TARGET_HELPER(x, ...) x
#define DO_DISPATCH(type, fun, ...) \
type##_METHODS_TABLE(DISPATCH_TARGET(__VA_ARGS__))->fun(__VA_ARGS__)
#define ALTREP_DISPATCH(fun, ...) DO_DISPATCH(ALTREP, fun, __VA_ARGS__)
#define ALTVEC_DISPATCH(fun, ...) DO_DISPATCH(ALTVEC, fun, __VA_ARGS__)
#define ALTINTEGER_DISPATCH(fun, ...) DO_DISPATCH(ALTINTEGER, fun, __VA_ARGS__)
#define ALTREAL_DISPATCH(fun, ...) DO_DISPATCH(ALTREAL, fun, __VA_ARGS__)
#define ALTLOGICAL_DISPATCH(fun, ...) DO_DISPATCH(ALTLOGICAL, fun, __VA_ARGS__)
#define ALTRAW_DISPATCH(fun, ...) DO_DISPATCH(ALTRAW, fun, __VA_ARGS__)
#define ALTCOMPLEX_DISPATCH(fun, ...) DO_DISPATCH(ALTCOMPLEX, fun, __VA_ARGS__)
#define ALTSTRING_DISPATCH(fun, ...) DO_DISPATCH(ALTSTRING, fun, __VA_ARGS__)
#define ALTLIST_DISPATCH(fun, ...) DO_DISPATCH(ALTLIST, fun, __VA_ARGS__)
/*
* Generic ALTREP support
*/
attribute_hidden SEXP ALTREP_COERCE(SEXP x, int type)
{
return ALTREP_DISPATCH(Coerce, x, type);
}
static SEXP ALTREP_DUPLICATE(SEXP x, Rboolean deep)
{
return ALTREP_DISPATCH(Duplicate, x, deep);
}
attribute_hidden SEXP ALTREP_DUPLICATE_EX(SEXP x, Rboolean deep)
{
return ALTREP_DISPATCH(DuplicateEX, x, deep);
}
attribute_hidden Rboolean
ALTREP_INSPECT(SEXP x, int pre, int deep, int pvec,
void (*inspect_subtree)(SEXP, int, int, int))
{
return ALTREP_DISPATCH(Inspect, x, pre, deep, pvec, inspect_subtree);
}
attribute_hidden SEXP
ALTREP_SERIALIZED_STATE(SEXP x)
{
return ALTREP_DISPATCH(Serialized_state, x);
}
attribute_hidden SEXP
ALTREP_SERIALIZED_CLASS(SEXP x)
{
SEXP val = ALTREP_CLASS_SERIALIZED_CLASS(ALTREP_CLASS(x));
return val != R_NilValue ? val : NULL;
}
static SEXP find_namespace(void *data) { return R_FindNamespace((SEXP) data); }
static SEXP handle_namespace_error(SEXP cond, void *data) { return R_NilValue; }
static SEXP ALTREP_UNSERIALIZE_CLASS(SEXP info)
{
if (TYPEOF(info) == LISTSXP) {
SEXP csym = ALTREP_SERIALIZED_CLASS_CLSSYM(info);
SEXP psym = ALTREP_SERIALIZED_CLASS_PKGSYM(info);
SEXP class = LookupClass(csym, psym);
if (class == NULL) {
SEXP pname = ScalarString(PRINTNAME(psym));
PROTECT(pname);
R_tryCatchError(find_namespace, pname,
handle_namespace_error, NULL);
class = LookupClass(csym, psym);
UNPROTECT(1);
}
return class;
}
return NULL;
}
attribute_hidden SEXP
ALTREP_UNSERIALIZE_EX(SEXP info, SEXP state, SEXP attr, int objf, int levs)
{
SEXP csym = ALTREP_SERIALIZED_CLASS_CLSSYM(info);
SEXP psym = ALTREP_SERIALIZED_CLASS_PKGSYM(info);
int type = ALTREP_SERIALIZED_CLASS_TYPE(info);
/* look up the class in the registry and handle failure */
SEXP class = ALTREP_UNSERIALIZE_CLASS(info);
if (class == NULL) {
switch(type) {
case LGLSXP:
case INTSXP:
case REALSXP:
case CPLXSXP:
case STRSXP:
case RAWSXP:
case VECSXP:
case EXPRSXP:
warning("cannot unserialize ALTVEC object of class '%s' from "
"package '%s'; returning length zero vector",
CHAR(PRINTNAME(csym)), CHAR(PRINTNAME(psym)));
return allocVector(type, 0);
default:
error("cannot unserialize this ALTREP object");
}
}
/* check the registered and unserialized types match */
int rtype = ALTREP_CLASS_BASE_TYPE(class);
if (type != rtype)
warning("serialized class '%s' from package '%s' has type %s; "
"registered class has type %s",
CHAR(PRINTNAME(csym)), CHAR(PRINTNAME(psym)),
type2char(type), type2char(rtype));
/* dispatch to a class method */
altrep_methods_t *m = CLASS_METHODS_TABLE(class);
SEXP val = m->UnserializeEX(class, state, attr, objf, levs);
return val;
}
/*attribute_hidden*/ R_xlen_t ALTREP_LENGTH(SEXP x)
{
return ALTREP_DISPATCH(Length, x);
}
attribute_hidden R_xlen_t ALTREP_TRUELENGTH(SEXP x) { return 0; }
/*
* Generic ALTVEC support
*/
static R_INLINE void *ALTVEC_DATAPTR_EX(SEXP x, Rboolean writable)
{
/* Disallow taking the writable `DATAPTR()` of an ALTLIST. This
check could be moved to `DATAPTR()` to catch more faulty
usages. */
if (TYPEOF(x) == VECSXP && writable)
ALTREP_ERROR_IN_CLASS("cannot take a writable DATAPTR of an ALTLIST",
x);
/**** move GC disabling into methods? */
if (R_in_gc)
error("cannot get ALTVEC DATAPTR during GC");
R_CHECK_THREAD;
int enabled = R_GCEnabled;
R_GCEnabled = FALSE;
void *val = ALTVEC_DISPATCH(Dataptr, x, writable);
R_GCEnabled = enabled;
return val;
}
/*attribute_hidden*/ void *ALTVEC_DATAPTR(SEXP x)
{
return ALTVEC_DATAPTR_EX(x, TRUE);
}
/*attribute_hidden*/ const void *ALTVEC_DATAPTR_RO(SEXP x)
{
return ALTVEC_DATAPTR_EX(x, FALSE);
}
attribute_hidden const void *ALTVEC_DATAPTR_OR_NULL(SEXP x)
{
return ALTVEC_DISPATCH(Dataptr_or_null, x);
}
attribute_hidden SEXP ALTVEC_EXTRACT_SUBSET(SEXP x, SEXP indx, SEXP call)
{
return ALTVEC_DISPATCH(Extract_subset, x, indx, call);
}
/*
* Typed ALTVEC support
*/
attribute_hidden int ALTINTEGER_ELT(SEXP x, R_xlen_t i)
{
return ALTINTEGER_DISPATCH(Elt, x, i);
}
R_xlen_t INTEGER_GET_REGION(SEXP sx, R_xlen_t i, R_xlen_t n, int *buf)
{
const int *x = INTEGER_OR_NULL(sx);
if (x != NULL) {
R_xlen_t size = XLENGTH(sx);
R_xlen_t ncopy = size - i > n ? n : size - i;
for (R_xlen_t k = 0; k < ncopy; k++)
buf[k] = x[k + i];
//memcpy(buf, x + i, ncopy * sizeof(int));
return ncopy;
}
else
return ALTINTEGER_DISPATCH(Get_region, sx, i, n, buf);
}
int INTEGER_IS_SORTED(SEXP x)
{
return ALTREP(x) ? ALTINTEGER_DISPATCH(Is_sorted, x) : UNKNOWN_SORTEDNESS;
}
int INTEGER_NO_NA(SEXP x)
{
return ALTREP(x) ? ALTINTEGER_DISPATCH(No_NA, x) : 0;
}
attribute_hidden double ALTREAL_ELT(SEXP x, R_xlen_t i)
{
return ALTREAL_DISPATCH(Elt, x, i);
}
R_xlen_t REAL_GET_REGION(SEXP sx, R_xlen_t i, R_xlen_t n, double *buf)
{
const double *x = REAL_OR_NULL(sx);
if (x != NULL) {
R_xlen_t size = XLENGTH(sx);
R_xlen_t ncopy = size - i > n ? n : size - i;
for (R_xlen_t k = 0; k < ncopy; k++)
buf[k] = x[k + i];
//memcpy(buf, x + i, ncopy * sizeof(double));
return ncopy;
}
else
return ALTREAL_DISPATCH(Get_region, sx, i, n, buf);
}
int REAL_IS_SORTED(SEXP x)
{
return ALTREP(x) ? ALTREAL_DISPATCH(Is_sorted, x) : UNKNOWN_SORTEDNESS;
}
int REAL_NO_NA(SEXP x)
{
return ALTREP(x) ? ALTREAL_DISPATCH(No_NA, x) : 0;
}
R_xlen_t LOGICAL_GET_REGION(SEXP sx, R_xlen_t i, R_xlen_t n, int *buf)
{
const int *x = DATAPTR_OR_NULL(sx);
if (x != NULL) {
R_xlen_t size = XLENGTH(sx);
R_xlen_t ncopy = size - i > n ? n : size - i;
for (R_xlen_t k = 0; k < ncopy; k++)
buf[k] = x[k + i];
//memcpy(buf, x + i, ncopy * sizeof(int));
return ncopy;
}
else
return ALTLOGICAL_DISPATCH(Get_region, sx, i, n, buf);
}
attribute_hidden int LOGICAL_IS_SORTED(SEXP x)
{
return ALTREP(x) ? ALTLOGICAL_DISPATCH(Is_sorted, x) : UNKNOWN_SORTEDNESS;
}
int LOGICAL_NO_NA(SEXP x)
{
return ALTREP(x) ? ALTLOGICAL_DISPATCH(No_NA, x) : 0;
}
R_xlen_t RAW_GET_REGION(SEXP sx, R_xlen_t i, R_xlen_t n, Rbyte *buf)
{
const Rbyte *x = DATAPTR_OR_NULL(sx);
if (x != NULL) {
R_xlen_t size = XLENGTH(sx);
R_xlen_t ncopy = size - i > n ? n : size - i;
for (R_xlen_t k = 0; k < ncopy; k++)
buf[k] = x[k + i];
//memcpy(buf, x + i, ncopy * sizeof(int));
return ncopy;
}
else
return ALTRAW_DISPATCH(Get_region, sx, i, n, buf);
}
R_xlen_t COMPLEX_GET_REGION(SEXP sx, R_xlen_t i, R_xlen_t n, Rcomplex *buf)
{
const Rcomplex *x = DATAPTR_OR_NULL(sx);
if (x != NULL) {
R_xlen_t size = XLENGTH(sx);
R_xlen_t ncopy = size - i > n ? n : size - i;
for (R_xlen_t k = 0; k < ncopy; k++)
buf[k] = x[k + i];
//memcpy(buf, x + i, ncopy * sizeof(int));
return ncopy;
}
else
return ALTCOMPLEX_DISPATCH(Get_region, sx, i, n, buf);
}
SEXP /*attribute_hidden*/ ALTSTRING_ELT(SEXP x, R_xlen_t i)
{
SEXP val = NULL;
/**** move GC disabling into method? */
if (R_in_gc)
error("cannot get ALTSTRING_ELT during GC");
R_CHECK_THREAD;
int enabled = R_GCEnabled;
R_GCEnabled = FALSE;
val = ALTSTRING_DISPATCH(Elt, x, i);
R_GCEnabled = enabled;
return val;
}
attribute_hidden void ALTSTRING_SET_ELT(SEXP x, R_xlen_t i, SEXP v)
{
/**** move GC disabling into method? */
if (R_in_gc)
error("cannot set ALTSTRING_ELT during GC");
R_CHECK_THREAD;
int enabled = R_GCEnabled;
R_GCEnabled = FALSE;
ALTSTRING_DISPATCH(Set_elt, x, i, v);
R_GCEnabled = enabled;
}
int STRING_IS_SORTED(SEXP x)
{
return ALTREP(x) ? ALTSTRING_DISPATCH(Is_sorted, x) : UNKNOWN_SORTEDNESS;
}
int STRING_NO_NA(SEXP x)
{
return ALTREP(x) ? ALTSTRING_DISPATCH(No_NA, x) : 0;
}
attribute_hidden SEXP ALTLIST_ELT(SEXP x, R_xlen_t i)
{
SEXP val = NULL;
/**** move GC disabling into method? */
if (R_in_gc)
error("cannot get ALTLIST_ELT during GC");
R_CHECK_THREAD;
int enabled = R_GCEnabled;
R_GCEnabled = FALSE;
val = ALTLIST_DISPATCH(Elt, x, i);
R_GCEnabled = enabled;
return val;
}
attribute_hidden void ALTLIST_SET_ELT(SEXP x, R_xlen_t i, SEXP v)
{
/**** move GC disabling into method? */
if (R_in_gc)
error("cannot set ALTLIST_ELT during GC");
R_CHECK_THREAD;
int enabled = R_GCEnabled;
R_GCEnabled = FALSE;
ALTLIST_DISPATCH(Set_elt, x, i, v);
R_GCEnabled = enabled;
}
attribute_hidden SEXP ALTINTEGER_SUM(SEXP x, Rboolean narm)
{
return ALTINTEGER_DISPATCH(Sum, x, narm);
}
attribute_hidden SEXP ALTINTEGER_MIN(SEXP x, Rboolean narm)
{
return ALTINTEGER_DISPATCH(Min, x, narm);
}
attribute_hidden SEXP ALTINTEGER_MAX(SEXP x, Rboolean narm)
{
return ALTINTEGER_DISPATCH(Max, x, narm);
}
attribute_hidden SEXP ALTREAL_SUM(SEXP x, Rboolean narm)
{
return ALTREAL_DISPATCH(Sum, x, narm);
}
attribute_hidden SEXP ALTREAL_MIN(SEXP x, Rboolean narm)
{
return ALTREAL_DISPATCH(Min, x, narm);
}
attribute_hidden SEXP ALTREAL_MAX(SEXP x, Rboolean narm)
{
return ALTREAL_DISPATCH(Max, x, narm);
}
attribute_hidden SEXP ALTLOGICAL_SUM(SEXP x, Rboolean narm)
{
return ALTLOGICAL_DISPATCH(Sum, x, narm);
}
attribute_hidden int ALTLOGICAL_ELT(SEXP x, R_xlen_t i)
{
return ALTLOGICAL_DISPATCH(Elt, x, i);
}
attribute_hidden Rcomplex ALTCOMPLEX_ELT(SEXP x, R_xlen_t i)
{
return ALTCOMPLEX_DISPATCH(Elt, x, i);
}
attribute_hidden Rbyte ALTRAW_ELT(SEXP x, R_xlen_t i)
{
return ALTRAW_DISPATCH(Elt, x, i);
}
/*
* Not yet implemented
*/
attribute_hidden void ALTINTEGER_SET_ELT(SEXP x, R_xlen_t i, int v)
{
INTEGER(x)[i] = v; /* dispatch here */
}
attribute_hidden void ALTLOGICAL_SET_ELT(SEXP x, R_xlen_t i, int v)
{
LOGICAL(x)[i] = v; /* dispatch here */
}
attribute_hidden void ALTREAL_SET_ELT(SEXP x, R_xlen_t i, double v)
{
REAL(x)[i] = v; /* dispatch here */
}
attribute_hidden void ALTCOMPLEX_SET_ELT(SEXP x, R_xlen_t i, Rcomplex v)
{
COMPLEX(x)[i] = v; /* dispatch here */
}
attribute_hidden void ALTRAW_SET_ELT(SEXP x, R_xlen_t i, Rbyte v)
{
RAW(x)[i] = v; /* dispatch here */
}
/**
** ALTREP Default Methods
**/
static SEXP altrep_UnserializeEX_default(SEXP class, SEXP state, SEXP attr,
int objf, int levs)
{
altrep_methods_t *m = CLASS_METHODS_TABLE(class);
SEXP val = m->Unserialize(class, state);
SET_ATTRIB(val, attr);
SET_OBJECT(val, objf);
SETLEVELS(val, levs);
return val;
}
static SEXP altrep_Serialized_state_default(SEXP x) { return NULL; }
static SEXP altrep_Unserialize_default(SEXP class, SEXP state)
{
error("cannot unserialize this ALTREP object yet");
}
static SEXP altrep_Coerce_default(SEXP x, int type) { return NULL; }
static SEXP altrep_Duplicate_default(SEXP x, Rboolean deep)
{
return NULL;
}
static SEXP altrep_DuplicateEX_default(SEXP x, Rboolean deep)
{
SEXP ans = ALTREP_DUPLICATE(x, deep);
if (ans != NULL &&
ans != x) { /* leave attributes alone if returning original */
/* handle attributes generically */
SEXP attr = ATTRIB(x);
if (attr != R_NilValue) {
PROTECT(ans);
SET_ATTRIB(ans, deep ? duplicate(attr) : shallow_duplicate(attr));
SET_OBJECT(ans, OBJECT(x));
IS_S4_OBJECT(x) ? SET_S4_OBJECT(ans) : UNSET_S4_OBJECT(ans);
UNPROTECT(1);
}
else if (ATTRIB(ans) != R_NilValue) {
SET_ATTRIB(ans, R_NilValue);
SET_OBJECT(ans, FALSE);
UNSET_S4_OBJECT(ans);
}
}
return ans;
}
static
Rboolean altrep_Inspect_default(SEXP x, int pre, int deep, int pvec,
void (*inspect_subtree)(SEXP, int, int, int))
{
return FALSE;
}
static R_xlen_t altrep_Length_default(SEXP x)
{
ALTREP_ERROR_IN_CLASS("no ALTREP Length method defined", x);
}
static void *altvec_Dataptr_default(SEXP x, Rboolean writable)
{
ALTREP_ERROR_IN_CLASS("cannot access data pointer for this ALTVEC object", x);
}
static const void *altvec_Dataptr_or_null_default(SEXP x)
{
return NULL;
}
static SEXP altvec_Extract_subset_default(SEXP x, SEXP indx, SEXP call)
{
return NULL;
}
static int altinteger_Elt_default(SEXP x, R_xlen_t i) { return INTEGER(x)[i]; }
static R_xlen_t
altinteger_Get_region_default(SEXP sx, R_xlen_t i, R_xlen_t n, int *buf)
{
R_xlen_t size = XLENGTH(sx);
R_xlen_t ncopy = size - i > n ? n : size - i;
for (R_xlen_t k = 0; k < ncopy; k++)
buf[k] = INTEGER_ELT(sx, k + i);
return ncopy;
}
static int altinteger_Is_sorted_default(SEXP x) { return UNKNOWN_SORTEDNESS; }
static int altinteger_No_NA_default(SEXP x) { return 0; }
static SEXP altinteger_Sum_default(SEXP x, Rboolean narm) { return NULL; }
static SEXP altinteger_Min_default(SEXP x, Rboolean narm) { return NULL; }
static SEXP altinteger_Max_default(SEXP x, Rboolean narm) { return NULL; }
static double altreal_Elt_default(SEXP x, R_xlen_t i) { return REAL(x)[i]; }
static R_xlen_t
altreal_Get_region_default(SEXP sx, R_xlen_t i, R_xlen_t n, double *buf)
{
R_xlen_t size = XLENGTH(sx);
R_xlen_t ncopy = size - i > n ? n : size - i;
for (R_xlen_t k = 0; k < ncopy; k++)
buf[k] = REAL_ELT(sx, k + i);
return ncopy;
}
static int altreal_Is_sorted_default(SEXP x) { return UNKNOWN_SORTEDNESS; }
static int altreal_No_NA_default(SEXP x) { return 0; }
static SEXP altreal_Sum_default(SEXP x, Rboolean narm) { return NULL; }
static SEXP altreal_Min_default(SEXP x, Rboolean narm) { return NULL; }
static SEXP altreal_Max_default(SEXP x, Rboolean narm) { return NULL; }
static int altlogical_Elt_default(SEXP x, R_xlen_t i) { return LOGICAL(x)[i]; }
static R_xlen_t
altlogical_Get_region_default(SEXP sx, R_xlen_t i, R_xlen_t n, int *buf)
{
R_xlen_t size = XLENGTH(sx);
R_xlen_t ncopy = size - i > n ? n : size - i;
for (R_xlen_t k = 0; k < ncopy; k++)
buf[k] = LOGICAL_ELT(sx, k + i);
return ncopy;
}
static int altlogical_Is_sorted_default(SEXP x) { return UNKNOWN_SORTEDNESS; }
static int altlogical_No_NA_default(SEXP x) { return 0; }
static SEXP altlogical_Sum_default(SEXP x, Rboolean narm) { return NULL; }
static Rbyte altraw_Elt_default(SEXP x, R_xlen_t i) { return RAW(x)[i]; }
static R_xlen_t
altraw_Get_region_default(SEXP sx, R_xlen_t i, R_xlen_t n, Rbyte *buf)
{
R_xlen_t size = XLENGTH(sx);
R_xlen_t ncopy = size - i > n ? n : size - i;
for (R_xlen_t k = 0; k < ncopy; k++)
buf[k] = RAW_ELT(sx, k + i);
return ncopy;
}
static Rcomplex altcomplex_Elt_default(SEXP x, R_xlen_t i)
{
return COMPLEX(x)[i];
}
static R_xlen_t
altcomplex_Get_region_default(SEXP sx, R_xlen_t i, R_xlen_t n, Rcomplex *buf)
{
R_xlen_t size = XLENGTH(sx);
R_xlen_t ncopy = size - i > n ? n : size - i;
for (R_xlen_t k = 0; k < ncopy; k++)
buf[k] = COMPLEX_ELT(sx, k + i);
return ncopy;
}
static SEXP altstring_Elt_default(SEXP x, R_xlen_t i)
{
ALTREP_ERROR_IN_CLASS("No Elt method found for ALTSTRING class", x);
}
static void altstring_Set_elt_default(SEXP x, R_xlen_t i, SEXP v)
{
ALTREP_ERROR_IN_CLASS("No Set_elt found for ALTSTRING class", x);
}
static int altstring_Is_sorted_default(SEXP x) { return UNKNOWN_SORTEDNESS; }
static int altstring_No_NA_default(SEXP x) { return 0; }
static SEXP altlist_Elt_default(SEXP x, R_xlen_t i)
{
ALTREP_ERROR_IN_CLASS("ALTLIST classes must provide an Elt method", x);
}
static void altlist_Set_elt_default(SEXP x, R_xlen_t i, SEXP v)
{
ALTREP_ERROR_IN_CLASS("ALTLIST classes must provide a Set_elt method", x);
}
static void *altlist_Dataptr_default(SEXP x, Rboolean writable)
{
ALTREP_ERROR_IN_CLASS("No Dataptr method found for ALTLIST class", x);
}
static const void *altlist_Dataptr_or_null_default(SEXP x)
{
return NULL;
}
/**
** ALTREP Initial Method Tables
**/
static altinteger_methods_t altinteger_default_methods = {
.UnserializeEX = altrep_UnserializeEX_default,
.Unserialize = altrep_Unserialize_default,
.Serialized_state = altrep_Serialized_state_default,
.DuplicateEX = altrep_DuplicateEX_default,
.Duplicate = altrep_Duplicate_default,
.Coerce = altrep_Coerce_default,
.Inspect = altrep_Inspect_default,
.Length = altrep_Length_default,
.Dataptr = altvec_Dataptr_default,
.Dataptr_or_null = altvec_Dataptr_or_null_default,
.Extract_subset = altvec_Extract_subset_default,
.Elt = altinteger_Elt_default,
.Get_region = altinteger_Get_region_default,
.Is_sorted = altinteger_Is_sorted_default,
.No_NA = altinteger_No_NA_default,
.Sum = altinteger_Sum_default,
.Min = altinteger_Min_default,
.Max = altinteger_Max_default
};
static altreal_methods_t altreal_default_methods = {
.UnserializeEX = altrep_UnserializeEX_default,
.Unserialize = altrep_Unserialize_default,
.Serialized_state = altrep_Serialized_state_default,
.DuplicateEX = altrep_DuplicateEX_default,
.Duplicate = altrep_Duplicate_default,
.Coerce = altrep_Coerce_default,
.Inspect = altrep_Inspect_default,
.Length = altrep_Length_default,
.Dataptr = altvec_Dataptr_default,
.Dataptr_or_null = altvec_Dataptr_or_null_default,
.Extract_subset = altvec_Extract_subset_default,
.Elt = altreal_Elt_default,
.Get_region = altreal_Get_region_default,
.Is_sorted = altreal_Is_sorted_default,
.No_NA = altreal_No_NA_default,
.Sum = altreal_Sum_default,
.Min = altreal_Min_default,
.Max = altreal_Max_default
};
static altlogical_methods_t altlogical_default_methods = {
.UnserializeEX = altrep_UnserializeEX_default,
.Unserialize = altrep_Unserialize_default,
.Serialized_state = altrep_Serialized_state_default,
.DuplicateEX = altrep_DuplicateEX_default,
.Duplicate = altrep_Duplicate_default,
.Coerce = altrep_Coerce_default,
.Inspect = altrep_Inspect_default,
.Length = altrep_Length_default,
.Dataptr = altvec_Dataptr_default,
.Dataptr_or_null = altvec_Dataptr_or_null_default,
.Extract_subset = altvec_Extract_subset_default,
.Elt = altlogical_Elt_default,
.Get_region = altlogical_Get_region_default,
.Is_sorted = altlogical_Is_sorted_default,
.No_NA = altlogical_No_NA_default,
.Sum = altlogical_Sum_default
};
static altraw_methods_t altraw_default_methods = {
.UnserializeEX = altrep_UnserializeEX_default,
.Unserialize = altrep_Unserialize_default,
.Serialized_state = altrep_Serialized_state_default,
.DuplicateEX = altrep_DuplicateEX_default,
.Duplicate = altrep_Duplicate_default,
.Coerce = altrep_Coerce_default,
.Inspect = altrep_Inspect_default,
.Length = altrep_Length_default,
.Dataptr = altvec_Dataptr_default,
.Dataptr_or_null = altvec_Dataptr_or_null_default,
.Extract_subset = altvec_Extract_subset_default,
.Elt = altraw_Elt_default,
.Get_region = altraw_Get_region_default
};
static altcomplex_methods_t altcomplex_default_methods = {
.UnserializeEX = altrep_UnserializeEX_default,
.Unserialize = altrep_Unserialize_default,
.Serialized_state = altrep_Serialized_state_default,
.DuplicateEX = altrep_DuplicateEX_default,
.Duplicate = altrep_Duplicate_default,
.Coerce = altrep_Coerce_default,
.Inspect = altrep_Inspect_default,
.Length = altrep_Length_default,
.Dataptr = altvec_Dataptr_default,
.Dataptr_or_null = altvec_Dataptr_or_null_default,
.Extract_subset = altvec_Extract_subset_default,
.Elt = altcomplex_Elt_default,
.Get_region = altcomplex_Get_region_default
};
static altstring_methods_t altstring_default_methods = {
.UnserializeEX = altrep_UnserializeEX_default,
.Unserialize = altrep_Unserialize_default,
.Serialized_state = altrep_Serialized_state_default,
.DuplicateEX = altrep_DuplicateEX_default,
.Duplicate = altrep_Duplicate_default,
.Coerce = altrep_Coerce_default,
.Inspect = altrep_Inspect_default,
.Length = altrep_Length_default,
.Dataptr = altvec_Dataptr_default,
.Dataptr_or_null = altvec_Dataptr_or_null_default,
.Extract_subset = altvec_Extract_subset_default,
.Elt = altstring_Elt_default,
.Set_elt = altstring_Set_elt_default,
.Is_sorted = altstring_Is_sorted_default,
.No_NA = altstring_No_NA_default
};
static altlist_methods_t altlist_default_methods = {
.UnserializeEX = altrep_UnserializeEX_default,
.Unserialize = altrep_Unserialize_default,
.Serialized_state = altrep_Serialized_state_default,
.DuplicateEX = altrep_DuplicateEX_default,
.Duplicate = altrep_Duplicate_default,
.Coerce = altrep_Coerce_default,