Skip to content

Commit 545cc97

Browse files
authored
Merge pull request #42 from SpatLyu/dev
collect convert utility
2 parents 243d8e1 + 2a98308 commit 545cc97

8 files changed

Lines changed: 77 additions & 230 deletions

File tree

inst/include/infoxtr.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// ============================================================
2222
// Dependency Guard: Encourage best practices (non-blocking)
2323
// ============================================================
24+
2425
#if defined(Rcpp_hpp) && !defined(COMPILING_INFOXTR)
2526
#warning "It is recommended to include <infoxtr.h> alone, as it already includes <Rcpp.h> and <RcppThread.h>."
2627
#endif
@@ -48,4 +49,10 @@
4849
#include "infoxtr/transferentropy.hpp"
4950
#include "infoxtr/surd.hpp"
5051

51-
#endif // INFOXTR_INFOXTR_H
52+
// ============================================================
53+
// Convenience Converters (Inline helpers for R/C++ interop)
54+
// ============================================================
55+
56+
#include "infoxtr/convert.hpp"
57+
58+
#endif // INFOXTR_INFOXTR_H
Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
/********************************************************************
2+
* File: convert.hpp
3+
*
4+
* Data Structure Conversion Utilities for R and C++
5+
*
6+
* This header provides a collection of lightweight helper
7+
* functions that convert data structures between the R
8+
* environment (via Rcpp) and standard C++ containers.
9+
*
10+
* The goal is to provide efficient and predictable translation
11+
* between the two ecosystems so that computational routines
12+
* implemented in C++ can operate on native STL structures while
13+
* remaining fully compatible with R data objects.
14+
*
15+
* Author: Wenbo Lyu (Github: @SpatLyu)
16+
* License: GPL-3
17+
**********************************************************************/
18+
19+
#ifndef INFOXTR_CONVERT_HPP
20+
#define INFOXTR_CONVERT_HPP
21+
122
#include <vector>
223
#include <cstdint>
324
#include <string>
@@ -7,6 +28,12 @@
728
#include <unordered_map>
829
#include <Rcpp.h>
930

31+
namespace infoxtr
32+
{
33+
34+
namespace convert
35+
{
36+
1037
/********************************************************************
1138
*
1239
* Spatial Neighbor Structure Conversion Utilities
@@ -55,7 +82,7 @@
5582
********************************************************************/
5683

5784
// Function to convert Rcpp::List to std::vector<std::vector<size_t>> (the `nb` object)
58-
std::vector<std::vector<size_t>> nb2std(const Rcpp::List& nb) {
85+
inline std::vector<std::vector<size_t>> nb2std(const Rcpp::List& nb) {
5986
// Get the number of elements in the nb object
6087
size_t n = static_cast<size_t>(nb.size());
6188
if (n <= 1) {
@@ -89,7 +116,7 @@ std::vector<std::vector<size_t>> nb2std(const Rcpp::List& nb) {
89116
}
90117

91118
// Function to convert std::vector<std::vector<size_t>> (the `nb` object) to Rcpp::List
92-
Rcpp::List std2nb(const std::vector<std::vector<size_t>>& nb) {
119+
inline Rcpp::List std2nb(const std::vector<std::vector<size_t>>& nb) {
93120
size_t n = nb.size();
94121
Rcpp::List result(n);
95122

@@ -162,7 +189,7 @@ Rcpp::List std2nb(const std::vector<std::vector<size_t>>& nb) {
162189
********************************************************************/
163190

164191
// Function to convert Rcpp::NumericMatrix to std::vector<std::vector<double>>
165-
std::vector<std::vector<double>> mat_r2std(
192+
inline std::vector<std::vector<double>> mat_r2std(
166193
const Rcpp::NumericMatrix& mat,
167194
bool byrow = true
168195
) {
@@ -208,7 +235,7 @@ std::vector<std::vector<double>> mat_r2std(
208235
}
209236

210237
// Function to convert std::vector<std::vector<double>> to Rcpp::NumericMatrix
211-
Rcpp::NumericMatrix mat_std2r(
238+
inline Rcpp::NumericMatrix mat_std2r(
212239
const std::vector<std::vector<double>>& mat,
213240
bool byrow = true
214241
) {
@@ -315,7 +342,7 @@ Rcpp::NumericMatrix mat_std2r(
315342
* NA encoded as {0}
316343
*
317344
********************************************************************/
318-
std::vector<std::vector<uint64_t>> pat_r2std(SEXP x, bool byrow = true)
345+
inline std::vector<std::vector<uint64_t>> pat_r2std(SEXP x, bool byrow = true)
319346
{
320347
if (!Rf_isMatrix(x))
321348
Rcpp::stop("Input must be a matrix.");
@@ -585,3 +612,9 @@ std::vector<std::vector<uint64_t>> pat_r2std(SEXP x, bool byrow = true)
585612

586613
return mat;
587614
}
615+
616+
} // namespace convert
617+
618+
}
619+
620+
#endif // INFOXTR_CONVERT_HPP

src/DataTrans.h

Lines changed: 0 additions & 188 deletions
This file was deleted.

src/DiscExps.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <numeric>
66
#include <algorithm>
77
#include "infoxtr.h"
8-
#include "DataTrans.h"
98

109
// Wrapper function to discretize continuous numeric vector
1110
// [[Rcpp::export(rng = false)]]

src/DistExps.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <numeric>
66
#include <algorithm>
77
#include "infoxtr.h"
8-
#include "DataTrans.h"
98

109
// Wrapper function to compute the distance between two vectors
1110
// [[Rcpp::export(rng = false)]]
@@ -34,13 +33,13 @@ Rcpp::NumericVector RcppDist4Mat(
3433
bool byrow = true
3534
) {
3635
// Convert Rcpp::NumericMatrix to std::vector<std::vector<double>>
37-
std::vector<std::vector<double>> cppMat = mat_r2std(mat, true);
36+
std::vector<std::vector<double>> cppMat = infoxtr::convert::mat_r2std(mat, true);
3837

3938
// Call the distance function
4039
std::vector<std::vector<double>> distm = infoxtr::distance::distance(cppMat, method, na_rm, byrow);
4140

4241
// Convert std::vector<std::vector<double>> to Rcpp::NumericMatrix and return
43-
return mat_std2r(distm, true);
42+
return infoxtr::convert::mat_std2r(distm, true);
4443
}
4544

4645
// Wrapper function to compute a row-wise distance matrix for an input matrix subset
@@ -54,7 +53,7 @@ Rcpp::NumericVector RcppDist4MatSub(
5453
bool byrow = true
5554
) {
5655
// Convert Rcpp::NumericMatrix to std::vector<std::vector<double>>
57-
std::vector<std::vector<double>> cppMat = mat_r2std(mat, true);
56+
std::vector<std::vector<double>> cppMat = infoxtr::convert::mat_r2std(mat, true);
5857

5958
const int n_obs = byrow ? mat.nrow() : mat.ncol();
6059

@@ -82,5 +81,5 @@ Rcpp::NumericVector RcppDist4MatSub(
8281
cppMat, lib_std, pred_std, method, na_rm, byrow);
8382

8483
// Convert std::vector<std::vector<double>> to Rcpp::NumericMatrix and return
85-
return mat_std2r(distm, true);
84+
return infoxtr::convert::mat_std2r(distm, true);
8685
}

0 commit comments

Comments
 (0)