Skip to content

Commit

Permalink
Merge pull request #55 from HaoZeke/cppRosciUpd
Browse files Browse the repository at this point in the history
MAINT,ENH: Handle `~` and cleanup C++ code
  • Loading branch information
HaoZeke authored Oct 23, 2023
2 parents 79133db + 079dd61 commit e4578e6
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 53 deletions.
12 changes: 6 additions & 6 deletions R/cpp11.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Generated by cpp11: do not edit by hand

fmm_to_vec <- function(filename) {
.Call(`_fastMatMR_fmm_to_vec`, filename)
cpp_fmm_to_vec <- function(filename) {
.Call(`_fastMatMR_cpp_fmm_to_vec`, filename)
}

fmm_to_mat <- function(filename) {
.Call(`_fastMatMR_fmm_to_mat`, filename)
cpp_fmm_to_mat <- function(filename) {
.Call(`_fastMatMR_cpp_fmm_to_mat`, filename)
}

fmm_to_sparse_Matrix <- function(filename) {
.Call(`_fastMatMR_fmm_to_sparse_Matrix`, filename)
cpp_fmm_to_sparse_Matrix <- function(filename) {
.Call(`_fastMatMR_cpp_fmm_to_sparse_Matrix`, filename)
}

vec_to_fmm <- function(r_vec, filename) {
Expand Down
50 changes: 30 additions & 20 deletions R/fastMatMR-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
#' \dontrun{
#' vec <- fmm_to_vec("matrix.mtx")
#' }
NULL
fmm_to_vec <- function(filename) {
expanded_filename <- path.expand(filename)
result <- cpp_fmm_to_vec(expanded_filename)
return(result)
}

#' @export fmm_to_mat
#' @rdname fmm_to_mat
Expand All @@ -29,7 +33,11 @@ NULL
#' \dontrun{
#' mat <- fmm_to_mat("matrix.mtx")
#' }
NULL
fmm_to_mat <- function(filename) {
expanded_filename <- path.expand(filename)
result <- cpp_fmm_to_mat(expanded_filename)
return(result)
}

#' @export fmm_to_sparse_Matrix
#' @rdname fmm_to_sparse_Matrix
Expand All @@ -44,19 +52,22 @@ NULL
#' \dontrun{
#' sparse_mat <- fmm_to_sparse_Matrix("sparse_matrix.mtx")
#' }
NULL
fmm_to_sparse_Matrix <- function(filename) {
expanded_filename <- path.expand(filename)
result <- cpp_fmm_to_sparse_Matrix(expanded_filename)
return(result)
}

#' @export vec_to_fmm
#' @rdname vec_to_fmm
#' @name vec_to_fmm
#' @title Convert a Numeric Vector to Matrix Market Format
#' @description This function takes a numeric vector and converts it into a
#' Matrix Market file.
#' Matrix Market output file.
#' @param input A numeric vector to be converted.
#' @param fname The name of the output file where the Matrix Market formatted
#' @param filename The name of the output file where the Matrix Market formatted
#' data will be saved.
#' @return This function has no return value. It writes a Matrix Market
#' formatted file to disk.
#' @return A boolean indicating success or failure. Writes a MTX file to disk.
#' @examples
#' \dontrun{
#' vec <- c(1, 2, 3)
Expand All @@ -71,10 +82,9 @@ NULL
#' @description This function takes a numeric matrix and converts it into a
#' Matrix Market file.
#' @param input A numeric matrix to be converted.
#' @param fname The name of the output file where the Matrix Market formatted
#' @param filename The name of the output file where the Matrix Market formatted
#' data will be saved.
#' @return This function has no return value. It writes a Matrix Market
#' formatted file to disk.
#' @return A boolean indicating success or failure. Writes a MTX file to disk.
#' @examples
#' \dontrun{
#' mat <- matrix(c(1, 2, 3, 4), nrow = 2)
Expand All @@ -89,10 +99,9 @@ NULL
#' @description This function takes a sparse numeric matrix and converts it into
#' a Matrix Market file.
#' @param input A sparse numeric matrix to be converted.
#' @param fname The name of the output file where the Matrix Market formatted
#' @param filename The name of the output file where the Matrix Market formatted
#' data will be saved.
#' @return This function has no return value. It writes a Matrix Market
#' formatted file to disk.
#' @return A boolean indicating success or failure. Writes a MTX file to disk.
#' @examples
#' \dontrun{
#' sparse_mat <- Matrix::Matrix(c(1, 0, 0, 2), nrow = 2, sparse = TRUE)
Expand All @@ -108,12 +117,11 @@ NULL
#'
#' @param input A numeric object to be converted. This can be a numeric vector,
#' a matrix, or a sparse matrix.
#' @param fname The name of the output file
#' @param filename The name of the output file
#' where the Matrix Market formatted data will be saved. It is recommended to
#' use a filename ending with ".mtx" for clarity.
#'
#' @return This function has no return value. It writes a Matrix Market
#' formatted file to disk.
#' @return A boolean indicating success or failure. Writes a MTX file to disk.
#'
#' @examples
#' \dontrun{
Expand All @@ -130,13 +138,15 @@ NULL
#' }
#'
#' @export
write_fmm <- function(input, fname = "out.mtx") {
write_fmm <- function(input, filename = "out.mtx") {
# Expand the ~ character to the full path
expanded_fname <- path.expand(filename)
if (is.vector(input)) {
return(vec_to_fmm(input, fname)) # nolint. C++ function.
return(vec_to_fmm(input, expanded_fname)) # nolint. C++ function.
} else if (is.matrix(input)) {
return(mat_to_fmm(input, fname)) # nolint. C++ function.
return(mat_to_fmm(input, expanded_fname)) # nolint. C++ function.
} else if (inherits(input, "sparseMatrix")) {
return(sparse_to_fmm(input, fname)) # nolint. C++ function.
return(sparse_to_fmm(input, expanded_fname)) # nolint. C++ function.
} else {
stop(
paste(
Expand Down
3 changes: 3 additions & 0 deletions man/fmm_to_mat.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/fmm_to_sparse_Matrix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/fmm_to_vec.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/mat_to_fmm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/sparse_to_fmm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/vec_to_fmm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/write_fmm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
#include <R_ext/Visibility.h>

// from_file.cpp
cpp11::doubles fmm_to_vec(const std::string & filename);
extern "C" SEXP _fastMatMR_fmm_to_vec(SEXP filename) {
cpp11::doubles cpp_fmm_to_vec(const std::string & filename);
extern "C" SEXP _fastMatMR_cpp_fmm_to_vec(SEXP filename) {
BEGIN_CPP11
return cpp11::as_sexp(fmm_to_vec(cpp11::as_cpp<cpp11::decay_t<const std::string &>>(filename)));
return cpp11::as_sexp(cpp_fmm_to_vec(cpp11::as_cpp<cpp11::decay_t<const std::string &>>(filename)));
END_CPP11
}
// from_file.cpp
cpp11::doubles_matrix<> fmm_to_mat(const std::string & filename);
extern "C" SEXP _fastMatMR_fmm_to_mat(SEXP filename) {
cpp11::doubles_matrix<> cpp_fmm_to_mat(const std::string & filename);
extern "C" SEXP _fastMatMR_cpp_fmm_to_mat(SEXP filename) {
BEGIN_CPP11
return cpp11::as_sexp(fmm_to_mat(cpp11::as_cpp<cpp11::decay_t<const std::string &>>(filename)));
return cpp11::as_sexp(cpp_fmm_to_mat(cpp11::as_cpp<cpp11::decay_t<const std::string &>>(filename)));
END_CPP11
}
// from_file.cpp
cpp11::sexp fmm_to_sparse_Matrix(const std::string & filename);
extern "C" SEXP _fastMatMR_fmm_to_sparse_Matrix(SEXP filename) {
cpp11::sexp cpp_fmm_to_sparse_Matrix(const std::string & filename);
extern "C" SEXP _fastMatMR_cpp_fmm_to_sparse_Matrix(SEXP filename) {
BEGIN_CPP11
return cpp11::as_sexp(fmm_to_sparse_Matrix(cpp11::as_cpp<cpp11::decay_t<const std::string &>>(filename)));
return cpp11::as_sexp(cpp_fmm_to_sparse_Matrix(cpp11::as_cpp<cpp11::decay_t<const std::string &>>(filename)));
END_CPP11
}
// to_file.cpp
Expand All @@ -50,12 +50,12 @@ extern "C" SEXP _fastMatMR_sparse_to_fmm(SEXP input, SEXP filename) {

extern "C" {
static const R_CallMethodDef CallEntries[] = {
{"_fastMatMR_fmm_to_mat", (DL_FUNC) &_fastMatMR_fmm_to_mat, 1},
{"_fastMatMR_fmm_to_sparse_Matrix", (DL_FUNC) &_fastMatMR_fmm_to_sparse_Matrix, 1},
{"_fastMatMR_fmm_to_vec", (DL_FUNC) &_fastMatMR_fmm_to_vec, 1},
{"_fastMatMR_mat_to_fmm", (DL_FUNC) &_fastMatMR_mat_to_fmm, 2},
{"_fastMatMR_sparse_to_fmm", (DL_FUNC) &_fastMatMR_sparse_to_fmm, 2},
{"_fastMatMR_vec_to_fmm", (DL_FUNC) &_fastMatMR_vec_to_fmm, 2},
{"_fastMatMR_cpp_fmm_to_mat", (DL_FUNC) &_fastMatMR_cpp_fmm_to_mat, 1},
{"_fastMatMR_cpp_fmm_to_sparse_Matrix", (DL_FUNC) &_fastMatMR_cpp_fmm_to_sparse_Matrix, 1},
{"_fastMatMR_cpp_fmm_to_vec", (DL_FUNC) &_fastMatMR_cpp_fmm_to_vec, 1},
{"_fastMatMR_mat_to_fmm", (DL_FUNC) &_fastMatMR_mat_to_fmm, 2},
{"_fastMatMR_sparse_to_fmm", (DL_FUNC) &_fastMatMR_sparse_to_fmm, 2},
{"_fastMatMR_vec_to_fmm", (DL_FUNC) &_fastMatMR_vec_to_fmm, 2},
{NULL, NULL, 0}
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/from_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace fmm = fast_matrix_market;

[[cpp11::register]] //
cpp11::doubles
fmm_to_vec(const std::string &filename) {
cpp_fmm_to_vec(const std::string &filename) {
std::ifstream file_stream;
std::vector<double> vec;

Expand All @@ -34,7 +34,7 @@ fmm_to_vec(const std::string &filename) {

[[cpp11::register]] //
cpp11::doubles_matrix<>
fmm_to_mat(const std::string &filename) {
cpp_fmm_to_mat(const std::string &filename) {
std::ifstream file_stream;
fmm::matrix_market_header header;
std::vector<double> vec;
Expand All @@ -61,7 +61,7 @@ fmm_to_mat(const std::string &filename) {

[[cpp11::register]] //
cpp11::sexp
fmm_to_sparse_Matrix(const std::string &filename) {
cpp_fmm_to_sparse_Matrix(const std::string &filename) {
// TODO: Can speed this up by constructing from SEXP instead of via Matrix
// constructor later
using namespace cpp11::literals;
Expand Down
4 changes: 0 additions & 4 deletions src/to_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ bool vec_to_fmm(cpp11::doubles r_vec, std::string filename) {
std::vector<double> std_vec(r_vec.size());
std::copy(r_vec.begin(), r_vec.end(), std_vec.begin());
fmm::matrix_market_header header(1, std_vec.size());
// header.comment = std::string("comment");
// Use C++17 filesystem to construct ofstream
std::filesystem::path file_path(filename);
std::ofstream os(file_path);

if (!os.is_open()) {
// Handle error
return false;
}
fmm::write_matrix_market_array(os, header, std_vec);
Expand Down

0 comments on commit e4578e6

Please sign in to comment.