Skip to content

Commit aa055c2

Browse files
committed
restore pointer
1 parent 599e6ed commit aa055c2

File tree

1 file changed

+77
-45
lines changed

1 file changed

+77
-45
lines changed

src/rinterface_extra.c

Lines changed: 77 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,16 +2883,51 @@ void R_igraph_set_pointer(SEXP result, const igraph_t* graph) {
28832883
UNPROTECT(px);
28842884
}
28852885

2886+
void R_igraph_restore_pointer(SEXP graph) {
2887+
igraph_t g;
2888+
igraph_vector_t v;
2889+
igraph_integer_t n=REAL(VECTOR_ELT(graph, igraph_t_idx_n))[0];
2890+
igraph_bool_t directed=LOGICAL(VECTOR_ELT(graph, igraph_t_idx_directed))[0];
2891+
2892+
igraph_vector_t from;
2893+
R_SEXP_to_vector(VECTOR_ELT(graph, igraph_t_idx_from), &from);
2894+
2895+
igraph_vector_t to;
2896+
R_SEXP_to_vector(VECTOR_ELT(graph, igraph_t_idx_to), &to);
2897+
2898+
igraph_integer_t i, s=igraph_vector_size(&from);
2899+
igraph_vector_init(&v, s*2);
2900+
2901+
for (i = 0; i < s; ++i)
2902+
{
2903+
igraph_vector_set(&v, i*2, VECTOR(to)[i]);
2904+
igraph_vector_set(&v, i*2+1, VECTOR(from)[i]);
2905+
}
2906+
2907+
igraph_create(&g, &v, n, directed);
2908+
R_igraph_set_pointer(graph, &g);
2909+
}
2910+
28862911
igraph_t *R_igraph_get_pointer(SEXP graph) {
28872912
if (GET_LENGTH(graph) != igraph_t_idx_max || !Rf_isEnvironment(R_igraph_graph_env(graph))) {
28882913
return NULL;
28892914
}
28902915

28912916
SEXP xp=Rf_findVar(Rf_install("igraph"), R_igraph_graph_env(graph));
28922917
if (xp == R_UnboundValue || xp == R_NilValue) {
2893-
return NULL;
2918+
R_igraph_restore_pointer(graph);
2919+
xp=Rf_findVar(Rf_install("igraph"), R_igraph_graph_env(graph));
28942920
}
2895-
return (igraph_t*)(R_ExternalPtrAddr(xp));
2921+
2922+
igraph_t *pgraph=(igraph_t*)(R_ExternalPtrAddr(xp));
2923+
2924+
if (!pgraph) {
2925+
R_igraph_restore_pointer(graph);
2926+
xp=Rf_findVar(Rf_install("igraph"), R_igraph_graph_env(graph));
2927+
pgraph=(igraph_t*)(R_ExternalPtrAddr(xp));
2928+
}
2929+
2930+
return pgraph;
28962931
}
28972932

28982933
void R_igraph_set_n(SEXP rgraph, const igraph_t *graph) {
@@ -2902,10 +2937,7 @@ void R_igraph_set_n(SEXP rgraph, const igraph_t *graph) {
29022937

29032938
igraph_integer_t R_igraph_get_n(SEXP graph) {
29042939
igraph_t *pgraph=R_igraph_get_pointer(graph);
2905-
if (pgraph) {
2906-
return pgraph->n;
2907-
}
2908-
return REAL(VECTOR_ELT(graph, igraph_t_idx_n))[0];
2940+
return pgraph->n;
29092941
}
29102942

29112943
void R_igraph_set_directed(SEXP rgraph, const igraph_t *graph) {
@@ -2915,10 +2947,7 @@ void R_igraph_set_directed(SEXP rgraph, const igraph_t *graph) {
29152947

29162948
igraph_bool_t R_igraph_get_directed(SEXP graph) {
29172949
igraph_t *pgraph=R_igraph_get_pointer(graph);
2918-
if (pgraph) {
2919-
return pgraph->directed;
2920-
}
2921-
return LOGICAL(VECTOR_ELT(graph, igraph_t_idx_directed))[0];
2950+
return pgraph->directed;
29222951
}
29232952

29242953
void R_igraph_set_from(SEXP rgraph, const igraph_t *graph) {
@@ -2930,11 +2959,7 @@ void R_igraph_set_from(SEXP rgraph, const igraph_t *graph) {
29302959

29312960
void R_igraph_get_from(SEXP graph, igraph_vector_t* from) {
29322961
igraph_t *pgraph=R_igraph_get_pointer(graph);
2933-
if (pgraph) {
2934-
*from = pgraph->from;
2935-
} else {
2936-
R_SEXP_to_vector(VECTOR_ELT(graph, igraph_t_idx_from), from);
2937-
}
2962+
*from = pgraph->from;
29382963
}
29392964

29402965
void R_igraph_set_to(SEXP rgraph, const igraph_t *graph) {
@@ -2946,11 +2971,27 @@ void R_igraph_set_to(SEXP rgraph, const igraph_t *graph) {
29462971

29472972
void R_igraph_get_to(SEXP graph, igraph_vector_t* to) {
29482973
igraph_t *pgraph=R_igraph_get_pointer(graph);
2949-
if (pgraph) {
2950-
*to = pgraph->to;
2951-
} else {
2952-
R_SEXP_to_vector(VECTOR_ELT(graph, igraph_t_idx_to), to);
2953-
}
2974+
*to = pgraph->to;
2975+
}
2976+
2977+
void R_igraph_get_oi(SEXP graph, igraph_vector_t* oi) {
2978+
igraph_t *pgraph=R_igraph_get_pointer(graph);
2979+
*oi = pgraph->oi;
2980+
}
2981+
2982+
void R_igraph_get_ii(SEXP graph, igraph_vector_t* ii) {
2983+
igraph_t *pgraph=R_igraph_get_pointer(graph);
2984+
*ii = pgraph->ii;
2985+
}
2986+
2987+
void R_igraph_get_os(SEXP graph, igraph_vector_t* os) {
2988+
igraph_t *pgraph=R_igraph_get_pointer(graph);
2989+
*os = pgraph->os;
2990+
}
2991+
2992+
void R_igraph_get_is(SEXP graph, igraph_vector_t* is) {
2993+
igraph_t *pgraph=R_igraph_get_pointer(graph);
2994+
*is = pgraph->is;
29542995
}
29552996

29562997
SEXP R_igraph_to_SEXP(const igraph_t *graph) {
@@ -2964,19 +3005,6 @@ SEXP R_igraph_to_SEXP(const igraph_t *graph) {
29643005
R_igraph_set_directed(result, graph);
29653006
R_igraph_set_from(result, graph);
29663007
R_igraph_set_to(result, graph);
2967-
SET_VECTOR_ELT(result, igraph_t_idx_oi, NEW_NUMERIC(no_of_edges));
2968-
SET_VECTOR_ELT(result, igraph_t_idx_ii, NEW_NUMERIC(no_of_edges));
2969-
SET_VECTOR_ELT(result, igraph_t_idx_os, NEW_NUMERIC(no_of_nodes+1));
2970-
SET_VECTOR_ELT(result, igraph_t_idx_is, NEW_NUMERIC(no_of_nodes+1));
2971-
2972-
memcpy(REAL(VECTOR_ELT(result, igraph_t_idx_oi)), graph->oi.stor_begin,
2973-
sizeof(igraph_real_t)*(size_t) no_of_edges);
2974-
memcpy(REAL(VECTOR_ELT(result, igraph_t_idx_ii)), graph->ii.stor_begin,
2975-
sizeof(igraph_real_t)*(size_t) no_of_edges);
2976-
memcpy(REAL(VECTOR_ELT(result, igraph_t_idx_os)), graph->os.stor_begin,
2977-
sizeof(igraph_real_t)*(size_t) (no_of_nodes+1));
2978-
memcpy(REAL(VECTOR_ELT(result, igraph_t_idx_is)), graph->is.stor_begin,
2979-
sizeof(igraph_real_t)*(size_t) (no_of_nodes+1));
29803008

29813009
SET_CLASS(result, Rf_ScalarString(Rf_mkChar("igraph")));
29823010

@@ -3593,10 +3621,10 @@ int R_SEXP_to_igraph(SEXP graph, igraph_t *res) {
35933621
res->directed=R_igraph_get_directed(graph);
35943622
R_igraph_get_from(graph, &res->from);
35953623
R_igraph_get_to(graph, &res->to);
3596-
R_SEXP_to_vector(VECTOR_ELT(graph, igraph_t_idx_oi), &res->oi);
3597-
R_SEXP_to_vector(VECTOR_ELT(graph, igraph_t_idx_ii), &res->ii);
3598-
R_SEXP_to_vector(VECTOR_ELT(graph, igraph_t_idx_os), &res->os);
3599-
R_SEXP_to_vector(VECTOR_ELT(graph, igraph_t_idx_is), &res->is);
3624+
R_igraph_get_oi(graph, &res->oi);
3625+
R_igraph_get_ii(graph, &res->ii);
3626+
R_igraph_get_os(graph, &res->os);
3627+
R_igraph_get_is(graph, &res->is);
36003628

36013629
/* attributes */
36023630
REAL(VECTOR_ELT(VECTOR_ELT(graph, igraph_t_idx_attr), 0))[0] = 1; /* R objects refcount */
@@ -3617,14 +3645,18 @@ int R_SEXP_to_igraph_copy(SEXP graph, igraph_t *res) {
36173645
igraph_vector_t to;
36183646
R_igraph_get_to(graph, &to);
36193647
igraph_vector_copy(&res->to, &to);
3620-
igraph_vector_init_copy(&res->oi, REAL(VECTOR_ELT(graph, igraph_t_idx_oi)),
3621-
GET_LENGTH(VECTOR_ELT(graph, igraph_t_idx_oi)));
3622-
igraph_vector_init_copy(&res->ii, REAL(VECTOR_ELT(graph, igraph_t_idx_ii)),
3623-
GET_LENGTH(VECTOR_ELT(graph, igraph_t_idx_ii)));
3624-
igraph_vector_init_copy(&res->os, REAL(VECTOR_ELT(graph, igraph_t_idx_os)),
3625-
GET_LENGTH(VECTOR_ELT(graph, igraph_t_idx_os)));
3626-
igraph_vector_init_copy(&res->is, REAL(VECTOR_ELT(graph, igraph_t_idx_is)),
3627-
GET_LENGTH(VECTOR_ELT(graph, igraph_t_idx_is)));
3648+
igraph_vector_t oi;
3649+
R_igraph_get_oi(graph, &oi);
3650+
igraph_vector_copy(&res->oi, &oi);
3651+
igraph_vector_t ii;
3652+
R_igraph_get_ii(graph, &ii);
3653+
igraph_vector_copy(&res->ii, &ii);
3654+
igraph_vector_t os;
3655+
R_igraph_get_os(graph, &os);
3656+
igraph_vector_copy(&res->os, &os);
3657+
igraph_vector_t is;
3658+
R_igraph_get_is(graph, &is);
3659+
igraph_vector_copy(&res->is, &is);
36283660

36293661
/* attributes */
36303662
REAL(VECTOR_ELT(VECTOR_ELT(graph, igraph_t_idx_attr), 0))[0] = 1; /* R objects */

0 commit comments

Comments
 (0)