Skip to content

Commit af3b8dd

Browse files
fix: add user interrupt checks and bounds validation for CRAN compliance
Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) <aider@aider.chat>
1 parent 6a7227e commit af3b8dd

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

src/all_topological_sorts.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <list>
88
#include <string>
99
#include <sstream>
10-
#include <filesystem>
10+
#include <R_ext/Utils.h>
1111
using namespace std;
1212

1313
class Graph {
@@ -37,6 +37,11 @@ void Graph::alltopologicalSortUtil(
3737
std::vector<arma::ivec>& sorts, double save_frac) {
3838
bool flag = false;
3939

40+
// Check for user interrupts in long-running recursive function
41+
if (sort_count % 1000 == 0) {
42+
R_CheckUserInterrupt();
43+
}
44+
4045
for (size_t i{}; i < n_items; i++) {
4146
if (indegree[i] == 0 && !visited[i]) {
4247
list<int>::iterator j;
@@ -102,7 +107,7 @@ arma::imat Graph::alltopologicalSort(long long int& sort_count, double save_frac
102107
//' The function generates all possible topological sorts for the provided preference matrix
103108
//' and saves approximately `save_frac` of the sorts in a matrix which is returned.
104109
//'
105-
//' @return This function returns the number of topological sorts.
110+
//' @return A list containing the number of topological sorts and optionally a matrix of sorts.
106111
//'
107112
//' @export
108113
//' @examples

src/distances.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ unsigned int CayleyDistance::d(const uvec& r1, const uvec& r2) {
3636
}
3737

3838
unsigned int FootruleDistance::d(const uvec& r1, const uvec& r2) {
39+
if(r1.size() != r2.size()) {
40+
Rcpp::stop("Rankings must have the same length");
41+
}
3942
unsigned int value{};
4043
for(size_t i{}; i < r1.size(); i++) {
4144
value += r1[i] > r2[i] ? r1[i] - r2[i] : r2[i] - r1[i];
@@ -61,6 +64,9 @@ unsigned int KendallDistance::d(const uvec& r1, const uvec& r2) {
6164
}
6265

6366
unsigned int SpearmanDistance::d(const uvec& r1, const uvec& r2) {
67+
if(r1.size() != r2.size()) {
68+
Rcpp::stop("Rankings must have the same length");
69+
}
6470
unsigned int value{};
6571
for(size_t i{}; i < r1.size(); i++) {
6672
value += r1[i] > r2[i] ? pow(r1[i] - r2[i], 2) : pow(r2[i] - r1[i], 2);

src/run_smc.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// [[Rcpp::depends(RcppArmadillo)]]
22
#include <RcppArmadillo.h>
3+
#include <R_ext/Utils.h>
34
#include "prior.h"
45
#include "data.h"
56
#include "distances.h"
@@ -42,6 +43,7 @@ Rcpp::List run_smc(
4243
std::vector<double> iteration_times;
4344

4445
for(size_t t{}; t < T; t++) {
46+
R_CheckUserInterrupt();
4547
reporter.report_time(t);
4648

4749
for(auto& p : particle_vector) {

0 commit comments

Comments
 (0)