-
-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathrinterface_extra.c
More file actions
7782 lines (6701 loc) · 248 KB
/
rinterface_extra.c
File metadata and controls
7782 lines (6701 loc) · 248 KB
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
/* -*- mode: C -*- */
/*
IGraph library R interface.
Copyright (C) 2013 Gabor Csardi <csardi.gabor@gmail.com>
334 Harvard street, Cambridge, MA 02139 USA
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "rinterface.h"
#include "rrandom.h"
#include <Rversion.h>
#include <R_ext/Visibility.h>
#include <R_ext/Altrep.h>
#include <stdio.h>
#include <inttypes.h>
#if defined(__SANITIZE_ADDRESS__)
# define IGRAPH_SANITIZER_AVAILABLE 1
#elif defined(__has_feature)
# if __has_feature(address_sanitizer)
# define IGRAPH_SANITIZER_AVAILABLE 1
# endif
#endif
#ifdef IGRAPH_SANITIZER_AVAILABLE
#include <sanitizer/asan_interface.h>
#endif
enum igraph_t_idx {
igraph_t_idx_n = 0,
igraph_t_idx_directed = 1,
igraph_t_idx_from = 2,
igraph_t_idx_to = 3,
igraph_t_idx_oi = 4,
igraph_t_idx_ii = 5,
igraph_t_idx_os = 6,
igraph_t_idx_is = 7,
igraph_t_idx_attr = 8,
igraph_t_idx_env = 9,
igraph_t_idx_max = 10,
};
// format versions
enum igraph_versions {
ver_0_1_1, // 0.1.1
ver_0_4, // 0.4
ver_0_7_999, // 0.7.999
ver_0_8, // 0.8
ver_1_5_0, // 1.5.0
ver_current = ver_1_5_0
};
#define R_IGRAPH_VERSION_VAR ".__igraph_version__."
/* The following three R_check_... functions must only be called from top-level C code,
* i.e. in contexts where igraph_error() does NOT return. */
void R_check_int_scalar(SEXP value)
{
if (Rf_xlength(value) != 1) {
igraph_errorf("Expecting a scalar integer but received a vector of length %" PRIuPTR ".",
__FILE__, __LINE__, IGRAPH_EINVAL, (uintptr_t) Rf_xlength(value));
}
if (((igraph_integer_t) REAL(value)[0]) != REAL(value)[0]) {
igraph_errorf("The value %.17g is not representable as an integer.",
__FILE__, __LINE__, IGRAPH_EINVAL, REAL(value)[0]);
}
}
void R_check_real_scalar(SEXP value)
{
if (Rf_xlength(value) != 1) {
igraph_errorf("Expecting a scalar real but received a vector of length %" PRIuPTR ".",
__FILE__, __LINE__, IGRAPH_EINVAL, (uintptr_t) Rf_xlength(value));
}
}
void R_check_bool_scalar(SEXP value)
{
if (Rf_xlength(value) != 1) {
igraph_errorf("Expecting a scalar logical but received a vector of length %" PRIuPTR ".",
__FILE__, __LINE__, IGRAPH_EINVAL, (uintptr_t) Rf_xlength(value));
}
}
// These functions are never meant to be called directly, only through IGRAPH_FINALLY_PV.
void igraph_destroy_pv(void* pv_ptr)
{
igraph_destroy((igraph_t*) pv_ptr);
}
void igraph_matrix_destroy_pv(void *pv_ptr)
{
igraph_matrix_destroy((igraph_matrix_t*) pv_ptr);
}
void igraph_vector_destroy_pv(void *pv_ptr)
{
igraph_vector_destroy((igraph_vector_t*) pv_ptr);
}
void igraph_vector_int_destroy_pv(void* pv_ptr)
{
igraph_vector_int_destroy((igraph_vector_int_t*) pv_ptr);
}
void igraph_vector_bool_destroy_pv(void *pv_ptr)
{
igraph_vector_bool_destroy((igraph_vector_bool_t*) pv_ptr);
}
void igraph_vector_int_list_destroy_pv(void *pv_ptr)
{
igraph_vector_int_list_destroy((igraph_vector_int_list_t*) pv_ptr);
}
igraph_error_t Rw_get_int_scalar(SEXP sexp, R_xlen_t index, igraph_integer_t *res)
{
if (Rf_xlength(sexp) <= index)
{
IGRAPH_ERRORF("Wrong index. Attempt to get element with index %" PRIuPTR " from vector of length %" PRIuPTR ".", IGRAPH_EINVAL, (uintptr_t) index, (uintptr_t) Rf_xlength(sexp));
}
*res = (igraph_integer_t)REAL(sexp)[index];
return IGRAPH_SUCCESS;
}
igraph_error_t Rw_get_real_scalar(SEXP sexp, R_xlen_t index, igraph_real_t *res)
{
if (Rf_xlength(sexp) <= index)
{
IGRAPH_ERRORF("Wrong index. Attempt to get element with index %" PRIuPTR " from vector of length %" PRIuPTR ".", IGRAPH_EINVAL, (uintptr_t) index, (uintptr_t) Rf_xlength(sexp));
}
*res = (igraph_real_t)REAL(sexp)[index];
return IGRAPH_SUCCESS;
}
igraph_error_t Rw_get_bool_scalar(SEXP sexp, R_xlen_t index, igraph_bool_t *res)
{
if (Rf_xlength(sexp) <= index)
{
IGRAPH_ERRORF("Wrong index. Attempt to get element with index %" PRIuPTR " from vector of length %" PRIuPTR ".", IGRAPH_EINVAL, (uintptr_t) index, (uintptr_t) Rf_xlength(sexp));
}
*res = (igraph_bool_t)LOGICAL(sexp)[index];
return IGRAPH_SUCCESS;
}
FILE* Ry_igraph_fopen_read(SEXP instream) {
FILE *file;
file=fopen(CHAR(STRING_ELT(instream, 0)), "r");
if (file==0) { igraph_error("Cannot open file for reading", __FILE__, __LINE__,
IGRAPH_EFILE); }
return file;
}
FILE* Ry_igraph_fopen_write(SEXP outstream) {
FILE *file;
file=fopen(CHAR(STRING_ELT(outstream, 0)), "w");
if (file==0) { igraph_error("Cannot open file for writing", __FILE__, __LINE__,
IGRAPH_EFILE); }
return file;
}
SEXP Rx_igraph_i_lang7(SEXP s, SEXP t, SEXP u, SEXP v, SEXP w, SEXP x, SEXP y)
{
PROTECT(s);
PROTECT(t);
PROTECT(u);
s = Rf_lcons(s, Rf_lcons(t, Rf_lcons(u, Rf_list4(v, w, x, y))));
UNPROTECT(3);
return s;
}
/* get the list element named str, or return NULL */
/* from the R Manual */
SEXP Rx_igraph_getListElement(SEXP list, const char *str)
{
SEXP elmt = R_NilValue, names = Rf_getAttrib(list, R_NamesSymbol);
for (R_xlen_t i = 0; i < Rf_xlength(list); i++)
if(strcmp(CHAR(STRING_ELT(names, i)), str) == 0) {
elmt = VECTOR_ELT(list, i);
break;
}
return elmt;
}
SEXP Rx_igraph_c2(SEXP x1, SEXP x2) {
SEXP cc = PROTECT(Rf_install("c"));
SEXP lc = PROTECT(Rf_lang3(cc, x1, x2));
SEXP ret = Rf_eval(lc, R_GlobalEnv);
UNPROTECT(2);
return ret;
}
/* evaluate an expression in a tryCatch() block to ensure that errors do not
* longjmp() back to the top level. Adapted from include/Rcpp/api/meat/Rcpp_eval.h
* in the Rcpp project */
typedef enum {
SAFEEVAL_OK = 0,
SAFEEVAL_ERROR = 1,
SAFEEVAL_INTERRUPTION = 2
} Rx_igraph_safe_eval_result_t;
Rx_igraph_safe_eval_result_t Rx_igraph_safe_eval_classify_result(SEXP result) {
if (Rf_inherits(result, "condition")) {
if (Rf_inherits(result, "error")) {
return SAFEEVAL_ERROR;
} else if (Rf_inherits(result, "interrupt")) {
return SAFEEVAL_INTERRUPTION;
}
}
return SAFEEVAL_OK;
}
SEXP Rx_igraph_safe_eval_in_env(SEXP expr_call, SEXP rho, Rx_igraph_safe_eval_result_t* result) {
/* find `identity` function used to capture errors */
SEXP identity = PROTECT(Rf_install("identity"));
SEXP identity_func = PROTECT(Rf_findFun(identity, R_BaseNamespace));
if (identity_func == R_UnboundValue) {
Rf_error("Failed to find 'base::identity()'");
}
/* define the call -- enclose with `tryCatch` so we can record errors */
SEXP try_catch = PROTECT(Rf_install("tryCatch"));
SEXP try_catch_call = PROTECT(Rf_lang4(try_catch, expr_call, identity_func, identity_func));
SET_TAG(CDDR(try_catch_call), Rf_install("error"));
SET_TAG(CDDR(CDR(try_catch_call)), Rf_install("interrupt"));
/* execute the call */
SEXP retval = PROTECT(Rf_eval(try_catch_call, rho));
/* did we get an error or an interrupt? */
if (result) {
*result = Rx_igraph_safe_eval_classify_result(retval);
}
UNPROTECT(5);
return retval;
}
SEXP Rx_igraph_handle_safe_eval_result_in_env(SEXP result, SEXP rho) {
switch (Rx_igraph_safe_eval_classify_result(result)) {
case SAFEEVAL_OK:
return result;
case SAFEEVAL_ERROR:
/* extract the error message, call IGRAPH_FINALLY_FREE() and then throw
* the error. We cannot raise the error directly because that would
* longjmp() and could potentially overwrite stack-allocated data structures
* that are also in the "finally" stack */
IGRAPH_FINALLY_FREE();
SEXP condition_message = PROTECT(Rf_install("conditionMessage"));
SEXP condition_message_call = PROTECT(Rf_lang2(condition_message, result));
SEXP evaluated_condition_message = PROTECT(Rf_eval(condition_message_call, rho));
Rf_error("%s", CHAR(STRING_ELT(evaluated_condition_message, 0)));
UNPROTECT(3);
return R_NilValue;
case SAFEEVAL_INTERRUPTION:
IGRAPH_FINALLY_FREE();
Rf_error("Interrupted by user");
return R_NilValue;
default:
Rf_error(
"Invalid object type returned from Rx_igraph_safe_eval(). This is a "
"bug; please report it to the developers."
);
return R_NilValue;
}
}
SEXP Rx_igraph_safe_eval(SEXP expr_call, Rx_igraph_safe_eval_result_t* result) {
return Rx_igraph_safe_eval_in_env(expr_call, R_GlobalEnv, result);
}
SEXP Rx_igraph_handle_safe_eval_result(SEXP result) {
return Rx_igraph_handle_safe_eval_result_in_env(result, R_GlobalEnv);
}
/******************************************************
* Attributes *
*****************************************************/
SEXP Rx_igraph_get_attr_mode(SEXP graph, SEXP pwhich) {
int which=INTEGER(pwhich)[0]-1;
SEXP obj=VECTOR_ELT(VECTOR_ELT(graph, igraph_t_idx_attr), which);
igraph_integer_t len=Rf_xlength(obj);
SEXP result;
PROTECT(result=NEW_CHARACTER(len));
for (igraph_integer_t i=0; i<len; i++) {
SEXP obji=VECTOR_ELT(obj, i);
if (IS_NUMERIC(obji) || IS_INTEGER(obji)) {
SET_STRING_ELT(result, i, Rf_mkChar("n"));
} else if (IS_CHARACTER(obji)) {
SET_STRING_ELT(result, i, Rf_mkChar("c"));
} else if (IS_LOGICAL(obji)) {
SET_STRING_ELT(result, i, Rf_mkChar("l"));
} else {
SET_STRING_ELT(result, i, Rf_mkChar("x"));
}
}
UNPROTECT(1);
return result;
}
igraph_error_t Rz_SEXP_to_attr_comb(SEXP input, igraph_attribute_combination_t *comb) {
igraph_integer_t n = Rf_xlength(input);
IGRAPH_CHECK(igraph_attribute_combination_init(comb));
IGRAPH_FINALLY(igraph_attribute_combination_destroy, comb);
for (igraph_integer_t i=0; i<n; i++) {
const char *name = NULL;
igraph_attribute_combination_type_t type;
igraph_function_pointer_t func;
/* Name */
if (!Rf_isNull(GET_NAMES(input))) {
name = CHAR(STRING_ELT(GET_NAMES(input), i));
}
if (name && strlen(name) == 0) {
name = NULL;
}
/* Type and function, if any */
if (Rf_isFunction(VECTOR_ELT(input, i))) {
type=IGRAPH_ATTRIBUTE_COMBINE_FUNCTION;
func=(igraph_function_pointer_t) (void*) VECTOR_ELT(input, i);
} else {
type=REAL(AS_NUMERIC(VECTOR_ELT(input, i)))[0];
func=NULL;
}
IGRAPH_CHECK(igraph_attribute_combination_add(comb, name, type, func));
}
IGRAPH_FINALLY_CLEAN(1);
return IGRAPH_SUCCESS;
}
static SEXP Rx_igraph_attribute_preserve_list;
SEXP Rx_igraph_attribute_add_to_preserve_list(SEXP attr) {
if (!Rx_igraph_attribute_preserve_list) {
// We don't care about freeing this, typically this is just a single node
Rx_igraph_attribute_preserve_list = Rf_cons(R_NilValue, R_NilValue);
R_PreserveObject(Rx_igraph_attribute_preserve_list);
}
// Create a new node, add it to the head of the list.
SEXP node = Rf_cons(attr, CDR(Rx_igraph_attribute_preserve_list));
SETCDR(Rx_igraph_attribute_preserve_list, node);
return attr;
}
void Rx_igraph_attribute_clean_preserve_list(void) {
if (Rx_igraph_attribute_preserve_list) {
// Mark the entire list available for garbage collection.
// Attributes that have been assigned to an R graph object will remain protected.
// Dangling attributes will be GC-ed eventually.
// This is called *before* entering an igraph function that might allocate
// attributes; after such a function returns, we need to keep preserving
// all attributes because they may be put into R graph objects
// and returned to R.
SETCDR(Rx_igraph_attribute_preserve_list, R_NilValue);
}
}
igraph_error_t Rx_igraph_attribute_init(igraph_t *graph, igraph_vector_ptr_t *attr) {
SEXP result, names, gal;
int px = 0;
result=PROTECT(NEW_LIST(4));
// The "preserve list" will be cleared with the next invocation of an igraph function.
// Adding to that list ensures that the attributes aren't GC-ed prematurely.
result = Rx_igraph_attribute_add_to_preserve_list(result);
UNPROTECT(1);
/* Add dummy vector for compatibility with CRAN versions */
SEXP dummy = NEW_NUMERIC(3);
NUMERIC_POINTER(dummy)[0] = 1.0;
NUMERIC_POINTER(dummy)[1] = 0.0;
NUMERIC_POINTER(dummy)[2] = 1.0;
SET_VECTOR_ELT(result, 0, dummy);
/* Add vertex and edge attributes */
for (R_xlen_t i=2; i<4; i++) {
SEXP attr = PROTECT(NEW_LIST(0));
SET_NAMES(attr, NEW_CHARACTER(0));
SET_VECTOR_ELT(result, i, attr); /* gal, val, eal */
UNPROTECT(1);
}
graph->attr=result;
/* Add graph attributes */
igraph_integer_t attrno= attr==NULL ? 0 : igraph_vector_ptr_size(attr);
SET_VECTOR_ELT(result, 1, NEW_LIST(attrno));
gal=VECTOR_ELT(result, 1);
PROTECT(names=NEW_CHARACTER(attrno)); px++;
for (igraph_integer_t i=0; i<attrno; i++) {
igraph_attribute_record_t *rec=VECTOR(*attr)[i];
igraph_vector_t *vec;
igraph_vector_bool_t *log;
igraph_strvector_t *strvec;
SET_STRING_ELT(names, i, Rf_mkChar(rec->name));
SET_VECTOR_ELT(gal, i, R_NilValue);
switch (rec->type) {
case IGRAPH_ATTRIBUTE_NUMERIC:
vec=(igraph_vector_t*) rec->value;
if (igraph_vector_size(vec) > 0) {
SET_VECTOR_ELT(gal, i, NEW_NUMERIC(1));
REAL(VECTOR_ELT(gal, i))[0]=VECTOR(*vec)[0];
}
break;
case IGRAPH_ATTRIBUTE_BOOLEAN:
log=(igraph_vector_bool_t*) rec->value;
if (igraph_vector_bool_size(log) > 0) {
SET_VECTOR_ELT(gal, i, NEW_LOGICAL(1));
LOGICAL(VECTOR_ELT(gal, i))[0]=VECTOR(*log)[0];
}
break;
case IGRAPH_ATTRIBUTE_STRING:
strvec=(igraph_strvector_t*) rec->value;
if (igraph_strvector_size(strvec) > 0) {
SET_VECTOR_ELT(gal, i, NEW_CHARACTER(1));
SET_STRING_ELT(VECTOR_ELT(gal,i), 0, Rf_mkChar(igraph_strvector_get(strvec, 0)));
}
break;
case IGRAPH_ATTRIBUTE_OBJECT:
UNPROTECT(px);
IGRAPH_ERROR("R_objects not implemented yet", IGRAPH_UNIMPLEMENTED);
break;
case IGRAPH_ATTRIBUTE_UNSPECIFIED:
default:
UNPROTECT(px);
IGRAPH_ERROR("Unknown attribute type, this should not happen",
IGRAPH_EINTERNAL);
break;
}
}
SET_NAMES(gal, names);
UNPROTECT(px);
return 0;
}
void Rx_igraph_attribute_destroy(igraph_t *graph) {
// Owned by the R graph object, will be garbage-collected
graph->attr=0;
}
/* If not copying all three attribute kinds are requested, then
we don't refcount, but really copy the requested ones, because
1) we can only refcount all three at the same time, and
2) the not-copied attributes will be set up by subsequent calls
to permute_vertices and/or permute/edges anyway. */
igraph_error_t Rx_igraph_attribute_copy(igraph_t *to, const igraph_t *from,
igraph_bool_t ga, igraph_bool_t va, igraph_bool_t ea) {
SEXP fromattr=from->attr;
if (ga && va && ea) {
to->attr=from->attr;
} else {
Rx_igraph_attribute_init(to,0); /* Sets up many things */
SEXP toattr=to->attr;
if (ga) {
SET_VECTOR_ELT(toattr, 1, Rf_duplicate(VECTOR_ELT(fromattr, 1)));
}
if (va) {
SET_VECTOR_ELT(toattr, 2, Rf_duplicate(VECTOR_ELT(fromattr, 2)));
}
if (ea) {
SET_VECTOR_ELT(toattr, 3, Rf_duplicate(VECTOR_ELT(fromattr, 3)));
}
}
return 0;
}
SEXP Rx_igraph_attribute_add_vertices_append1(igraph_vector_ptr_t *nattr,
int j, int nv) {
SEXP app = R_NilValue;
igraph_attribute_record_t *tmprec=VECTOR(*nattr)[j-1];
igraph_integer_t len = 0;
switch (tmprec->type) {
case IGRAPH_ATTRIBUTE_NUMERIC:
len = igraph_vector_size(tmprec->value);
break;
case IGRAPH_ATTRIBUTE_BOOLEAN:
len = igraph_vector_bool_size(tmprec->value);
break;
case IGRAPH_ATTRIBUTE_STRING:
len = igraph_strvector_size(tmprec->value);
break;
case IGRAPH_ATTRIBUTE_OBJECT:
igraph_error("R objects not implemented yet", __FILE__, __LINE__,
IGRAPH_UNIMPLEMENTED);
return R_NilValue;
break;
default:
igraph_error("Unknown attribute type, internal error", __FILE__, __LINE__,
IGRAPH_EINVAL);
return R_NilValue;
break;
}
if (len != nv) {
igraph_error("Invalid attribute length", __FILE__, __LINE__,
IGRAPH_EINVAL);
return R_NilValue;
}
switch (tmprec->type) {
case IGRAPH_ATTRIBUTE_NUMERIC:
PROTECT(app=NEW_NUMERIC(nv));
igraph_vector_copy_to(tmprec->value, REAL(app));
break;
case IGRAPH_ATTRIBUTE_BOOLEAN:
PROTECT(app=Ry_igraph_vector_bool_to_SEXP(tmprec->value));
break;
default: /* IGRAPH_ATTRIBUTE_STRING */
PROTECT(app=Rx_igraph_strvector_to_SEXP(tmprec->value));
break;
}
UNPROTECT(1);
return app;
}
void Rx_igraph_attribute_add_vertices_append(SEXP val, igraph_integer_t nv,
igraph_vector_ptr_t *nattr) {
SEXP names;
igraph_integer_t valno, nattrno;
SEXP rep = R_NilValue;
int px = 0;
valno = Rf_xlength(val);
names=PROTECT(GET_NAMES(val)); px++;
if (nattr==NULL) {
nattrno=0;
} else {
nattrno=igraph_vector_ptr_size(nattr);
}
for (igraph_integer_t i=0; i<valno; i++) {
SEXP oldva=VECTOR_ELT(val, i), newva;
const char *sexpname=CHAR(STRING_ELT(names,i));
igraph_bool_t l=0;
igraph_integer_t j;
for (j=0; !l && j<nattrno; j++) {
igraph_attribute_record_t *tmp=VECTOR(*nattr)[j];
l=!strcmp(sexpname, tmp->name);
}
if (l) {
/* This attribute is present in nattr */
SEXP app = PROTECT(Rx_igraph_attribute_add_vertices_append1(nattr, j, nv));
SEXP newva = PROTECT(Rx_igraph_c2(oldva, app));
SET_VECTOR_ELT(val, i, newva);
UNPROTECT(2);
} else {
/* No such attribute, append NA's */
if (Rf_isNull(rep)) {
SEXP l1 = PROTECT(Rf_install("rep")); px++;
SEXP l2 = PROTECT(Rf_ScalarLogical(NA_LOGICAL)); px++;
SEXP l3 = PROTECT(Rf_ScalarReal((double) nv)); px++;
SEXP l4 = PROTECT(Rf_lang3(l1, l2, l3)); px++;
PROTECT(rep=Rf_eval(l4, R_GlobalEnv)); px++;
}
PROTECT(newva=Rx_igraph_c2(oldva, rep));
SET_VECTOR_ELT(val, i, newva);
UNPROTECT(1);
}
}
UNPROTECT(px);
}
SEXP Rx_igraph_attribute_add_vertices_dup(SEXP attr) {
SEXP newattr;
PROTECT(newattr=Rf_duplicate(attr));
Rx_igraph_attribute_add_to_preserve_list(newattr);
UNPROTECT(1);
return newattr;
}
igraph_error_t Rx_igraph_attribute_add_vertices(igraph_t *graph, igraph_integer_t nv,
igraph_vector_ptr_t *nattr) {
SEXP attr=graph->attr;
SEXP val, rep=0, names, newnames;
igraph_vector_int_t news;
igraph_integer_t valno, origlen, nattrno, newattrs;
int px = 0;
SEXP newattr = PROTECT(Rx_igraph_attribute_add_vertices_dup(attr)); px++;
attr=graph->attr=newattr;
val=VECTOR_ELT(attr, 2);
valno=Rf_xlength(val);
names=PROTECT(GET_NAMES(val)); px++;
if (nattr==NULL) {
nattrno=0;
} else {
nattrno=igraph_vector_ptr_size(nattr);
}
origlen=igraph_vcount(graph)-nv;
/* First add the new attributes, if any */
newattrs=0;
if (igraph_vector_int_init(&news, 0)) Rf_error("Out of memory");
IGRAPH_FINALLY_PV(igraph_vector_int_destroy, &news);
for (igraph_integer_t i=0; i<nattrno; i++) {
igraph_attribute_record_t *nattr_entry=VECTOR(*nattr)[i];
const char *nname=nattr_entry->name;
igraph_bool_t l=0;
for (igraph_integer_t j=0; !l && j<valno; j++) {
l=!strcmp(nname, CHAR(STRING_ELT(names, j)));
}
if (!l) {
newattrs++;
if (igraph_vector_int_push_back(&news, i)) Rf_error("Out of memory");
}
}
if (newattrs != 0) {
SEXP app, newval;
PROTECT(app=NEW_LIST(newattrs));
PROTECT(newnames=NEW_CHARACTER(newattrs));
SEXP l1 = PROTECT(Rf_install("rep"));
SEXP l2 = PROTECT(Rf_ScalarLogical(NA_LOGICAL));
SEXP l3 = PROTECT(Rf_ScalarReal((double) origlen));
SEXP l4 = PROTECT(Rf_lang3(l1, l2, l3));
PROTECT(rep = Rf_eval(l4, R_GlobalEnv));
for (igraph_integer_t i=0; i<newattrs; i++) {
igraph_attribute_record_t *tmp = VECTOR(*nattr)[VECTOR(news)[i]];
SET_VECTOR_ELT(app, i, rep);
SET_STRING_ELT(newnames, i, Rf_mkChar(tmp->name));
}
PROTECT(newval=Rx_igraph_c2(val, app));
PROTECT(newnames=Rx_igraph_c2(names, newnames));
SET_NAMES(newval, newnames);
SET_VECTOR_ELT(attr, 2, newval);
val=VECTOR_ELT(attr, 2);
UNPROTECT(9);
}
igraph_vector_int_destroy(&news);
IGRAPH_FINALLY_CLEAN(1); /* news */
/* Now append the new values */
Rx_igraph_attribute_add_vertices_append(val, nv, nattr);
UNPROTECT(px);
return 0;
}
/* void Rx_igraph_attribute_delete_vertices(igraph_t *graph, */
/* const igraph_vector_t *eidx, */
/* const igraph_vector_t *vidx) { */
/* SEXP attr=graph->attr; */
/* SEXP eal, val; */
/* long int valno, ealno, i; */
/* SEXP newattr; */
/* PROTECT(newattr=Rf_duplicate(attr)); */
/* attr=graph->attr=newattr; */
/* /\* Vertices *\/ */
/* val=VECTOR_ELT(attr, 2); */
/* valno=GET_LENGTH(val); */
/* for (i=0; i<valno; i++) { */
/* SEXP oldva=VECTOR_ELT(val, i), newva, ss; */
/* long int origlen=GET_LENGTH(oldva); */
/* long int newlen=0, j; */
/* for (j=0; j<igraph_vector_size(vidx); j++) { */
/* if (VECTOR(*vidx)[j] > 0) { */
/* newlen++; */
/* } */
/* } */
/* PROTECT(ss=NEW_NUMERIC(newlen)); */
/* for (j=0; j<origlen; j++) { */
/* if (VECTOR(*vidx)[j]>0) { */
/* REAL(ss)[(long int)VECTOR(*vidx)[j]-1]=j+1; */
/* } */
/* } */
/* PROTECT(newva=EVAL(lang3(Rf_install("["), oldva, ss))); */
/* SET_VECTOR_ELT(val, i, newva); */
/* UNPROTECT(2); */
/* } */
/* /\* Edges *\/ */
/* eal=VECTOR_ELT(attr, 3); */
/* ealno=GET_LENGTH(eal); */
/* for (i=0; i<ealno; i++) { */
/* SEXP oldea=VECTOR_ELT(eal, i), newea, ss; */
/* long int origlen=GET_LENGTH(oldea); */
/* long int newlen=0, j; */
/* /\* calculate new length *\/ */
/* for (j=0; j<origlen; j++) { */
/* if (VECTOR(*eidx)[j] > 0) { */
/* newlen++; */
/* } */
/* } */
/* PROTECT(ss=NEW_NUMERIC(newlen)); */
/* for (j=0; j<origlen; j++) { */
/* if (VECTOR(*eidx)[j]>0) { */
/* REAL(ss)[(long int)VECTOR(*eidx)[j]-1]=j+1; */
/* } */
/* } */
/* PROTECT(newea=EVAL(lang3(Rf_install("["), oldea, ss))); */
/* SET_VECTOR_ELT(eal, i, newea); */
/* UNPROTECT(2); */
/* } */
/* } */
igraph_error_t Rx_igraph_attribute_permute_vertices_same(const igraph_t *graph,
igraph_t *newgraph,
const igraph_vector_int_t *idx) {
SEXP attr=newgraph->attr;
SEXP val;
igraph_integer_t valno;
igraph_integer_t idxlen = igraph_vector_int_size(idx);
SEXP ss;
int px = 0;
SEXP newattr = PROTECT(Rf_duplicate(attr));
Rx_igraph_attribute_add_to_preserve_list(newattr);
UNPROTECT(1);
attr=newgraph->attr=newattr;
val=VECTOR_ELT(attr,2);
valno=Rf_xlength(val);
/* If we have no vertex attributes, then we don't need to do anything */
if (valno==0) { UNPROTECT(px); return 0; }
/* Convert idx to an R object, we will use this for indexing */
PROTECT(ss=NEW_NUMERIC(idxlen)); px++;
for (igraph_integer_t i=0; i<idxlen; i++) {
REAL(ss)[i] = (double) VECTOR(*idx)[i]+1;
}
for (igraph_integer_t i=0; i<valno; i++) {
SEXP oldva=VECTOR_ELT(val, i);
/* We do NOT do any copying, the attributes were already copied,
we're doing this in place. */
SEXP l1 = PROTECT(Rf_install("["));
SEXP l2 = PROTECT(Rf_lang3(l1, oldva, ss));
SEXP newva = PROTECT(Rf_eval(l2, R_GlobalEnv));
SET_VECTOR_ELT(val, i, newva);
UNPROTECT(3);
}
UNPROTECT(px);
return IGRAPH_SUCCESS;
}
igraph_error_t Rx_igraph_attribute_permute_vertices_diff(const igraph_t *graph,
igraph_t *newgraph,
const igraph_vector_int_t *idx) {
SEXP attr=graph->attr;
SEXP toattr=newgraph->attr;
SEXP val, toval;
SEXP names;
igraph_integer_t valno;
igraph_integer_t idxlen=igraph_vector_int_size(idx);
SEXP ss;
int px = 0;
val=VECTOR_ELT(attr,2);
valno=Rf_xlength(val);
/* If we have no vertex attributes, then we don't need to do anything */
if (valno==0) { return 0; }
/* Convert idx to an R object, we will use this for indexing */
PROTECT(ss=NEW_NUMERIC(idxlen)); px++;
for (igraph_integer_t i=0; i<idxlen; i++) {
REAL(ss)[i] = (double) VECTOR(*idx)[i]+1;
}
/* Resize the vertex attribute list in 'newgraph' */
PROTECT(toval=NEW_LIST(valno)); px++;
PROTECT(names=GET_NAMES(val)); px++;
SET_NAMES(toval, names);
for (igraph_integer_t i=0; i<valno; i++) {
SEXP oldva=VECTOR_ELT(val, i);
SEXP l1 = PROTECT(Rf_install("["));
SEXP l2 = PROTECT(Rf_lang3(l1, oldva, ss));
SEXP newva = PROTECT(Rf_eval(l2, R_GlobalEnv));
SET_VECTOR_ELT(toval, i, newva);
UNPROTECT(3);
}
SET_VECTOR_ELT(toattr, 2, toval);
UNPROTECT(px);
return IGRAPH_SUCCESS;
}
igraph_error_t Rx_igraph_attribute_permute_vertices(const igraph_t *graph,
igraph_t *newgraph,
const igraph_vector_int_t *idx) {
if (graph == newgraph) {
return Rx_igraph_attribute_permute_vertices_same(graph, newgraph, idx);
} else {
return Rx_igraph_attribute_permute_vertices_diff(graph, newgraph, idx);
}
}
SEXP Rx_igraph_attribute_add_edges_dup(SEXP attr) {
SEXP newattr;
PROTECT(newattr=Rf_duplicate(attr));
Rx_igraph_attribute_add_to_preserve_list(newattr);
UNPROTECT(1);
return newattr;
}
SEXP Rx_igraph_attribute_add_edges_append1(igraph_vector_ptr_t *nattr, igraph_integer_t j,
igraph_integer_t ne) {
SEXP app = R_NilValue;
igraph_attribute_record_t *tmprec=VECTOR(*nattr)[j-1];
igraph_integer_t len = 0;
switch(tmprec->type) {
case IGRAPH_ATTRIBUTE_NUMERIC:
len = igraph_vector_size(tmprec->value);
break;
case IGRAPH_ATTRIBUTE_BOOLEAN:
len = igraph_vector_bool_size(tmprec->value);
break;
case IGRAPH_ATTRIBUTE_STRING:
len = igraph_strvector_size(tmprec->value);
break;
case IGRAPH_ATTRIBUTE_OBJECT:
igraph_error("R objects not implemented yet", __FILE__, __LINE__,
IGRAPH_UNIMPLEMENTED);
return R_NilValue;
break;
default:
igraph_error("Unknown attribute type, internal error", __FILE__, __LINE__,
IGRAPH_EINVAL);
return R_NilValue;
break;
}
if (len != ne) {
igraph_error("Invalid attribute length", __FILE__, __LINE__,
IGRAPH_EINVAL);
return R_NilValue;
}
switch (tmprec->type) {
case IGRAPH_ATTRIBUTE_NUMERIC:
PROTECT(app=NEW_NUMERIC(ne));
igraph_vector_copy_to(tmprec->value, REAL(app));
break;
case IGRAPH_ATTRIBUTE_BOOLEAN:
PROTECT(app=Ry_igraph_vector_bool_to_SEXP(tmprec->value));
break;
default: /* IGRAPH_ATTRIBUTE_STRING */
PROTECT(app=Rx_igraph_strvector_to_SEXP(tmprec->value));
break;
}
UNPROTECT(1);
return app;
}
void Rx_igraph_attribute_add_edges_append(SEXP eal,
const igraph_vector_int_t *edges,
igraph_vector_ptr_t *nattr) {
SEXP names;
igraph_integer_t ealno;
igraph_integer_t ne=igraph_vector_int_size(edges)/2, nattrno;
SEXP rep = R_NilValue;
int px = 0;
ealno=Rf_xlength(eal);
names=PROTECT(GET_NAMES(eal)); px++;
if (nattr==NULL) {
nattrno=0;
} else {
nattrno=igraph_vector_ptr_size(nattr);
}
for (igraph_integer_t i=0; i<ealno; i++) {
SEXP oldea=VECTOR_ELT(eal, i);
const char *sexpname=CHAR(STRING_ELT(names, i));
igraph_bool_t l=0;
igraph_integer_t j;
for (j=0; !l && j<nattrno; j++) {
igraph_attribute_record_t *tmp=VECTOR(*nattr)[j];
l=!strcmp(sexpname, tmp->name);
}
if (l) {
/* This attribute is present in nattr */
SEXP app = PROTECT(Rx_igraph_attribute_add_edges_append1(nattr, j, ne));
SEXP newea = PROTECT(Rx_igraph_c2(oldea, app));
SET_VECTOR_ELT(eal, i, newea);
UNPROTECT(2);
} else {
/* No such attribute, append NA's */
if (Rf_isNull(rep)) {
SEXP l1 = PROTECT(Rf_install("rep")); px++;
SEXP l2 = PROTECT(Rf_ScalarLogical(NA_LOGICAL)); px++;
SEXP l3 = PROTECT(Rf_ScalarReal((double) ne)); px++;
SEXP l4 = PROTECT(Rf_lang3(l1, l2, l3)); px++;
PROTECT(rep = Rf_eval(l4, R_GlobalEnv)); px++;
}
SEXP newea = PROTECT(Rx_igraph_c2(oldea, rep));
SET_VECTOR_ELT(eal, i, newea);
UNPROTECT(1);
}
}
UNPROTECT(px);
}
igraph_error_t Rx_igraph_attribute_add_edges(igraph_t *graph, const igraph_vector_int_t *edges,
igraph_vector_ptr_t *nattr) {
SEXP attr=graph->attr;
SEXP eal, names, newnames;
igraph_vector_int_t news;
igraph_integer_t ealno, origlen, nattrno, newattrs;
igraph_integer_t ne=igraph_vector_int_size(edges)/2;
int px = 0;
if (igraph_vector_int_init(&news, 0)) Rf_error("Out of memory");
IGRAPH_FINALLY_PV(igraph_vector_int_destroy, &news);
SEXP newattr = PROTECT(Rx_igraph_attribute_add_edges_dup(attr)); px++;
attr=graph->attr=newattr;
eal=VECTOR_ELT(attr, 3);
ealno=Rf_xlength(eal);
names=PROTECT(GET_NAMES(eal)); px++;
if (nattr==NULL) {
nattrno=0;
} else {
nattrno=igraph_vector_ptr_size(nattr);
}
origlen=igraph_ecount(graph)-ne;
/* First add the new attributes, if any */
newattrs=0;
for (igraph_integer_t i=0; i<nattrno; i++) {
igraph_attribute_record_t *nattr_entry=VECTOR(*nattr)[i];
const char *nname=nattr_entry->name;
igraph_bool_t l=0;
for (igraph_integer_t j=0; !l && j<ealno; j++) {
l=!strcmp(nname, CHAR(STRING_ELT(names, j)));
}
if (!l) {
newattrs++;
if (igraph_vector_int_push_back(&news, i)) Rf_error("Out of memory");
}
}
if (newattrs != 0) {
SEXP app, neweal;
PROTECT(app=NEW_LIST(newattrs));
PROTECT(newnames=NEW_CHARACTER(newattrs));
SEXP l1 = PROTECT(Rf_install("rep"));
SEXP l2 = PROTECT(Rf_ScalarLogical(NA_LOGICAL));
SEXP l3 = PROTECT(Rf_ScalarInteger((int) origlen));
SEXP l4 = PROTECT(Rf_lang3(l1, l2, l3));
SEXP rep = PROTECT(Rf_eval(l4, R_GlobalEnv));
for (igraph_integer_t i=0; i<newattrs; i++) {
igraph_attribute_record_t *tmp=
VECTOR(*nattr)[VECTOR(news)[i]];