From 32967cc9e9842b988da4f58a121a4b957689385e Mon Sep 17 00:00:00 2001 From: Oliver Keyes Date: Wed, 26 Aug 2015 15:38:16 -0400 Subject: [PATCH] add get/set functionality's guts --- R/RcppExports.R | 8 ++++++++ src/RcppExports.cpp | 25 +++++++++++++++++++++++++ src/human_format.h | 1 - src/human_getset.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/human_getset.h | 23 +++++++++++++++++++++++ src/human_parse.cpp | 1 + src/human_parse.h | 2 -- src/humaniformat.cpp | 14 ++++++++++++++ 8 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 src/human_getset.cpp create mode 100644 src/human_getset.h diff --git a/R/RcppExports.R b/R/RcppExports.R index 27af88a..c92d72c 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -77,3 +77,11 @@ format_period <- function(names) { .Call('humaniformat_format_period', PACKAGE = 'humaniformat', names) } +get_ <- function(names, element) { + .Call('humaniformat_get_', PACKAGE = 'humaniformat', names, element) +} + +set_ <- function(names, element, replacement) { + .Call('humaniformat_set_', PACKAGE = 'humaniformat', names, element, replacement) +} + diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index f0d2038..79d2dca 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -38,3 +38,28 @@ BEGIN_RCPP return __result; END_RCPP } +// get_ +std::vector < std::string > get_(std::vector < std::string > names, int element); +RcppExport SEXP humaniformat_get_(SEXP namesSEXP, SEXP elementSEXP) { +BEGIN_RCPP + Rcpp::RObject __result; + Rcpp::RNGScope __rngScope; + Rcpp::traits::input_parameter< std::vector < std::string > >::type names(namesSEXP); + Rcpp::traits::input_parameter< int >::type element(elementSEXP); + __result = Rcpp::wrap(get_(names, element)); + return __result; +END_RCPP +} +// set_ +std::vector < std::string > set_(std::vector < std::string > names, int element, std::string replacement); +RcppExport SEXP humaniformat_set_(SEXP namesSEXP, SEXP elementSEXP, SEXP replacementSEXP) { +BEGIN_RCPP + Rcpp::RObject __result; + Rcpp::RNGScope __rngScope; + Rcpp::traits::input_parameter< std::vector < std::string > >::type names(namesSEXP); + Rcpp::traits::input_parameter< int >::type element(elementSEXP); + Rcpp::traits::input_parameter< std::string >::type replacement(replacementSEXP); + __result = Rcpp::wrap(set_(names, element, replacement)); + return __result; +END_RCPP +} diff --git a/src/human_format.h b/src/human_format.h index 26371b7..280eb56 100644 --- a/src/human_format.h +++ b/src/human_format.h @@ -1,6 +1,5 @@ #include "human_parse.h" - #ifndef __HUMAN_FORMAT__ #define __HUMAN_FORMAT__ diff --git a/src/human_getset.cpp b/src/human_getset.cpp new file mode 100644 index 0000000..2da140e --- /dev/null +++ b/src/human_getset.cpp @@ -0,0 +1,40 @@ +#include "human_getset.h" + +std::string human_getset::get_single(std::string name, int element){ + std::vector < std::string > split_name = parse_single(name); + return split_name[element]; +} + +std::string human_getset::set_single(std::string name, int element, std::string replacement){ + std::vector < std::string > split_name = parse_single(name); + split_name[element] = replacement; + std::string output; + + for(unsigned int i = 0; i < (split_name.size() - 1); i++){ + output.append(split_name[i]); + if(i < (split_name.size() - 1) && split_name[i] != ""){ + output.append(" "); + } + } + + return output; +} + +std::vector < std::string > human_getset::get_vector(std::vector < std::string > names, int element){ + + for(unsigned int i = 0; i < names.size(); i++){ + names[i] = get_single(names[i], element); + } + + return names; +} + +std::vector < std::string > human_getset::set_vector(std::vector < std::string > names, int element, + std::string replacement){ + + for(unsigned int i = 0; i < names.size(); i++){ + names[i] = set_single(names[i], element, replacement); + } + + return names; +} diff --git a/src/human_getset.h b/src/human_getset.h new file mode 100644 index 0000000..b371a9d --- /dev/null +++ b/src/human_getset.h @@ -0,0 +1,23 @@ +#include "human_parse.h" + +#ifndef __HUMAN_GETSET__ +#define __HUMAN_GETSET__ + +class human_getset: public human_parse { + +private: + + std::string get_single(std::string name, int element); + + std::string set_single(std::string name, int element, std::string replacement); + +public: + + std::vector < std::string > get_vector(std::vector < std::string > names, int element); + + std::vector < std::string > set_vector(std::vector < std::string > names, int element, + std::string replacement); + +}; + +#endif diff --git a/src/human_parse.cpp b/src/human_parse.cpp index 8899a7d..23fc7e6 100644 --- a/src/human_parse.cpp +++ b/src/human_parse.cpp @@ -156,6 +156,7 @@ DataFrame human_parse::parse_vector(std::vector < std::string > names){ _["stringsAsFactors"] = false); } + // Constructor human_parse::human_parse(){ diff --git a/src/human_parse.h b/src/human_parse.h index e320cd3..d555994 100644 --- a/src/human_parse.h +++ b/src/human_parse.h @@ -20,8 +20,6 @@ class human_parse { bool match_component(std::string part, std::set < std::string > set_ref); -private: - std::vector < std::string > parse_single(std::string name); public: diff --git a/src/humaniformat.cpp b/src/humaniformat.cpp index b22d2ca..31b1150 100644 --- a/src/humaniformat.cpp +++ b/src/humaniformat.cpp @@ -1,4 +1,5 @@ #include "human_format.h" +#include "human_getset.h" //' @title Parse Human Names //' @description human names are complex things; sometimes people have honorifics, or not. Or a single middle name, or many. Or @@ -81,3 +82,16 @@ std::vector < std::string > format_period(std::vector < std::string > names){ human_format format_inst; return format_inst.period_format_vector(names); } + +// [[Rcpp::export]] +std::vector < std::string > get_(std::vector < std::string > names, int element){ + human_getset getset_inst; + return getset_inst.get_vector(names, element); +} + +// [[Rcpp::export]] +std::vector < std::string > set_(std::vector < std::string > names, int element, + std::string replacement){ + human_getset getset_inst; + return getset_inst.set_vector(names, element, replacement); +}