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>
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
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
0 commit comments