@@ -52,20 +52,23 @@ SEXP MCMCDyn_wrapper(SEXP stateR, // ergm_state
52
52
memset (REAL (sample ), 0 , (asInteger (nsteps ) + 1 )* m -> n_stats * sizeof (double ));
53
53
memcpy (REAL (sample ), s -> stats , m -> n_stats * sizeof (double ));
54
54
55
- SEXP difftime = PROTECT (allocVector (INTSXP , asInteger (log_changes ) ? asInteger (maxchanges ) : 0 ));
56
- SEXP difftail = PROTECT (allocVector (INTSXP , asInteger (log_changes ) ? asInteger (maxchanges ) : 0 ));
57
- SEXP diffhead = PROTECT (allocVector (INTSXP , asInteger (log_changes ) ? asInteger (maxchanges ) : 0 ));
58
- SEXP diffto = PROTECT (allocVector (INTSXP , asInteger (log_changes ) ? asInteger (maxchanges ) : 0 ));
59
- memset (INTEGER (difftime ), 0 , asInteger (log_changes ) ? asInteger (maxchanges )* sizeof (int ) : 0 );
60
- memset (INTEGER (difftail ), 0 , asInteger (log_changes ) ? asInteger (maxchanges )* sizeof (int ) : 0 );
61
- memset (INTEGER (diffhead ), 0 , asInteger (log_changes ) ? asInteger (maxchanges )* sizeof (int ) : 0 );
62
- memset (INTEGER (diffto ), 0 , asInteger (log_changes ) ? asInteger (maxchanges )* sizeof (int ) : 0 );
55
+ kvint difftime , difftail , diffhead , diffto ;
56
+ kv_init (difftime );
57
+ kv_init (difftail );
58
+ kv_init (diffhead );
59
+ kv_init (diffto );
60
+
61
+ // pre-allocate the 0th element for size
62
+ kv_push (int , difftime , 0 );
63
+ kv_push (int , difftail , 0 );
64
+ kv_push (int , diffhead , 0 );
65
+ kv_push (int , diffto , 0 );
63
66
64
67
SEXP status ;
65
68
if (MHp ) status = PROTECT (ScalarInteger (MCMCSampleDyn (s ,
66
69
dur_inf ,
67
70
REAL (eta ),
68
- asInteger (collect )?REAL (sample ):NULL , asInteger (maxedges ), asInteger (maxchanges ), asInteger (log_changes ), ( Vertex * ) INTEGER ( difftime ), ( Vertex * ) INTEGER ( difftail ), ( Vertex * ) INTEGER ( diffhead ), INTEGER ( diffto ) ,
71
+ asInteger (collect )?REAL (sample ):NULL , asInteger (maxedges ), asInteger (maxchanges ), asInteger (log_changes ), & difftime , & difftail , & diffhead , & diffto ,
69
72
asInteger (nsteps ), asInteger (min_MH_interval ), asInteger (max_MH_interval ), asReal (MH_pval ), asReal (MH_interval_add ), asInteger (burnin ), asInteger (interval ),
70
73
asInteger (verbose ))));
71
74
else status = PROTECT (ScalarInteger (MCMCDyn_MH_FAILED ));
@@ -81,10 +84,15 @@ SEXP MCMCDyn_wrapper(SEXP stateR, // ergm_state
81
84
SET_VECTOR_ELT (outl , 2 , ErgmStateRSave (s ));
82
85
}
83
86
84
- SET_VECTOR_ELT (outl , 3 , difftime );
85
- SET_VECTOR_ELT (outl , 4 , difftail );
86
- SET_VECTOR_ELT (outl , 5 , diffhead );
87
- SET_VECTOR_ELT (outl , 6 , diffto );
87
+ SET_VECTOR_ELT (outl , 3 , PROTECT (kvint_to_SEXP (difftime )));
88
+ SET_VECTOR_ELT (outl , 4 , PROTECT (kvint_to_SEXP (difftail )));
89
+ SET_VECTOR_ELT (outl , 5 , PROTECT (kvint_to_SEXP (diffhead )));
90
+ SET_VECTOR_ELT (outl , 6 , PROTECT (kvint_to_SEXP (diffto )));
91
+
92
+ kv_destroy (difftime );
93
+ kv_destroy (difftail );
94
+ kv_destroy (diffhead );
95
+ kv_destroy (diffto );
88
96
89
97
ErgmStateDestroy (s );
90
98
PutRNGstate (); /* Disable RNG before returning */
@@ -110,7 +118,7 @@ MCMCDynStatus MCMCSampleDyn(ErgmState *s,
110
118
int maxedges ,
111
119
int maxchanges ,
112
120
int log_changes ,
113
- Vertex * difftime , Vertex * difftail , Vertex * diffhead , int * diffto ,
121
+ kvint * difftime , kvint * difftail , kvint * diffhead , kvint * diffto ,
114
122
// MCMC settings.
115
123
unsigned int nsteps , unsigned int min_MH_interval , unsigned int max_MH_interval , double MH_pval , double MH_interval_add ,
116
124
unsigned int burnin , unsigned int interval ,
@@ -190,7 +198,12 @@ MCMCDynStatus MCMCSampleDyn(ErgmState *s,
190
198
}
191
199
}
192
200
193
- if (log_changes ) difftime [0 ]= difftail [0 ]= diffhead [0 ]= diffto [0 ]= nextdiffedge - 1 ;
201
+ if (log_changes ) {
202
+ kv_A (* difftime , 0 ) = nextdiffedge - 1 ;
203
+ kv_A (* difftail , 0 ) = nextdiffedge - 1 ;
204
+ kv_A (* diffhead , 0 ) = nextdiffedge - 1 ;
205
+ kv_A (* diffto , 0 ) = nextdiffedge - 1 ;
206
+ }
194
207
return MCMCDyn_OK ;
195
208
}
196
209
@@ -223,7 +236,7 @@ MCMCDynStatus MCMCDyn1Step(ErgmState *s,
223
236
// Space for output.
224
237
double * stats ,
225
238
unsigned int maxchanges , Edge * nextdiffedge ,
226
- Vertex * difftime , Vertex * difftail , Vertex * diffhead , int * diffto ,
239
+ kvint * difftime , kvint * difftail , kvint * diffhead , kvint * diffto ,
227
240
// MCMC settings.
228
241
unsigned int min_MH_interval , unsigned int max_MH_interval , double MH_pval , double MH_interval_add ,
229
242
// Verbosity.
@@ -343,7 +356,7 @@ MCMCDynStatus MCMCDyn1Step_advance(ErgmState *s,
343
356
// Space for output.
344
357
double * stats ,
345
358
unsigned int maxchanges , Edge * nextdiffedge ,
346
- Vertex * difftime , Vertex * difftail , Vertex * diffhead , int * diffto ,
359
+ kvint * difftime , kvint * difftail , kvint * diffhead , kvint * diffto ,
347
360
// Verbosity.
348
361
int verbose ){
349
362
StoreDyadMapInt * discord = dur_inf -> discord ;
@@ -358,10 +371,10 @@ MCMCDynStatus MCMCDyn1Step_advance(ErgmState *s,
358
371
kh_foreach_key (discord , dyad ,{
359
372
if (* nextdiffedge < maxchanges ){
360
373
// and record the toggle.
361
- if (difftime ) difftime [ * nextdiffedge ] = t ;
362
- if (difftail ) difftail [ * nextdiffedge ] = dyad .tail ;
363
- if (diffhead ) diffhead [ * nextdiffedge ] = dyad .head ;
364
- if (diffto ) diffto [ * nextdiffedge ] = GetEdge (dyad .tail ,dyad .head ,nwp );
374
+ if (difftime ) kv_push ( int , * difftime , t ) ;
375
+ if (difftail ) kv_push ( int , * difftail , dyad .tail ) ;
376
+ if (diffhead ) kv_push ( int , * diffhead , dyad .head ) ;
377
+ if (diffto ) kv_push ( int , * diffto , GetEdge (dyad .tail ,dyad .head ,nwp ) );
365
378
(* nextdiffedge )++ ;
366
379
}else {
367
380
return (MCMCDyn_TOO_MANY_CHANGES );
0 commit comments