Skip to content

Commit

Permalink
progress on Rcpp refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Qile0317 committed Dec 6, 2023
1 parent e911c05 commit 8a7e38c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 102 deletions.
14 changes: 14 additions & 0 deletions src/CirclePacker.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ class CirclePacker {
bool try_place;
bool verbose;

CirclePacker(const std::vector<double>& input_rad_vec) {
verbose = false;
start_progress_bar();

try_place = true;

num_nodes = (int)input_rad_vec.size();
data.resize(num_nodes);

for (int i = 0; i < num_nodes; i++) {
data[i] = CircleNode(input_rad_vec[i]);
}
}

CirclePacker(const std::vector<double>& input_rad_vec, bool _try_place, bool _verbose) {
verbose = _verbose;
start_progress_bar();
Expand Down
44 changes: 44 additions & 0 deletions src/exportedUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <Rcpp.h>
#include <string>

// [[Rcpp::export]]
bool has_repeats(const Rcpp::NumericVector vec1, const Rcpp::NumericVector vec2) {
std::unordered_set<double> s;

for (int i = 0; i < vec1.size(); i++) {
if (s.count(vec1[i]) > 0) {
return true;
}
s.insert(vec1[i]);
}

for (int i = 0; i < vec2.size(); i++) {
if (s.count(vec2[i]) > 0) {
return true;
}
s.insert(vec2[i]);
}
return false;
}

// check if two string vectors has common elements or if the second vector has
// repeated elements
// [[Rcpp::export]]
bool has_common_strs(
const std::vector<std::string>& vec1, const std::vector<std::string>& vec2
) {
// Create a hash set to store strings from vec1
std::unordered_set<std::string> hashSet;
for (const std::string& str : vec1) {
hashSet.insert(str);
}

// Check if any string from vec2 is present in the hash set
for (const std::string& str : vec2) {
if (hashSet.count(str) > 0) {
return true;
}
hashSet.insert(str);
}
return false;
}
7 changes: 6 additions & 1 deletion src/test_runner.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// ensure export of `run_testthat_tests()`
#define TESTTHAT_TEST_RUNNER
#include <testthat.h>
#include <testthat.h>

// tests (bad practisce)
#include "tests/helper-testfunctions.hpp"
#include "tests/test-cpp_circle_layout.hpp"
#include "tests/test-repulsion.hpp"
17 changes: 17 additions & 0 deletions src/tests/helper-testfunctions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <Rcpp.h>
#include <string>

bool approx_equal(double a, double b, double epsilon = 5e-5) {
return std::abs(a - b) <= epsilon;
}

bool elements_are_equal(
Rcpp::NumericVector vec1, Rcpp::NumericVector vec2, double epsilon = 5e-5
) {
for (int i = 0; i < vec1.size(); i++) {
if (!approx_equal(vec1[i], vec2[i], epsilon)) {
return false;
}
}
return true;
}
38 changes: 0 additions & 38 deletions src/tests/test-utils.hpp

This file was deleted.

63 changes: 0 additions & 63 deletions src/utils.h

This file was deleted.

0 comments on commit 8a7e38c

Please sign in to comment.