Skip to content

Commit

Permalink
setting up api
Browse files Browse the repository at this point in the history
  • Loading branch information
SymbolixAU committed Jun 7, 2019
1 parent 86b2ca8 commit ec120f5
Show file tree
Hide file tree
Showing 7 changed files with 326 additions and 33 deletions.
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

rcpp_colour_values_hex <- function(x, palette, alpha, na_colour = "#808080", include_alpha = TRUE, format = FALSE, format_type = "numeric", digits = 2L, summary = FALSE, n_summaries = 0L) {
.Call(`_colourvalues_rcpp_colour_values_hex`, x, palette, alpha, na_colour, include_alpha, format, format_type, digits, summary, n_summaries)
}

rcpp_colour_num_value_string_palette_hex <- function(x, palette, na_colour, alpha, include_alpha) {
.Call(`_colourvalues_rcpp_colour_num_value_string_palette_hex`, x, palette, na_colour, alpha, include_alpha)
}
Expand Down
14 changes: 0 additions & 14 deletions R/scratch.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@


# library(microbenchmark)
#
# l <- list(
# x = 1:100
# , y = letters
# , z = list( list( x = letters ) )
# , a = list( list( list( x = list( letters ) ) ) )
# )
#
# colourvalues:::list_size( l, 0, 10 )


# l <- list(
# x = 1:100
Expand All @@ -19,9 +8,6 @@
# , a = 10.1
# , b = as.POSIXct("2018-01-01 00:00:00")
# )
#
# colourvalues:::list_size( l, 0, 10 )

# colourvalues:::colour_list( l )

# l <- list(
Expand Down
219 changes: 219 additions & 0 deletions inst/include/colourvalues/api.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
#include <Rcpp.h>

#include "colourvalues/colours/colours_hex.hpp"
#include "colourvalues/colours/colours_rgb.hpp"

namespace colourvalues {
namespace api {

/*
* when palette is unknown, but vector is numeric
*/
inline SEXP colour_values_hex(
Rcpp::NumericVector& x,
SEXP palette,
Rcpp::NumericVector& alpha,
std::string& na_colour,
bool include_alpha = true,
bool format = false,
std::string format_type = "numeric",
int digits = 2,
int n_summaries = 0
) {

switch( TYPEOF( palette ) ) {
// STringVector - needs to get std::string
case STRSXP: {
Rcpp::StringVector sv = Rcpp::as< Rcpp::StringVector >( palette );
Rcpp::String s = sv[0];
std::string pal = s;
return colourvalues::colours_hex::colour_value_hex(
x, pal, na_colour, alpha, include_alpha, n_summaries, format, format_type, digits
);
}
case INTSXP: {}
case REALSXP: {
if( !Rf_isMatrix( palette ) ) {
Rcpp::stop("Unknown palette type - expecting a matrix");
}
Rcpp::NumericMatrix pal = Rcpp::as< Rcpp::NumericMatrix >( palette );
return colourvalues::colours_hex::colour_value_hex(
x, pal, na_colour, include_alpha, n_summaries, format, format_type, digits
);
}
default: {
Rcpp::stop("Unknown palette type");
}
}

}

/*
* When palette is unknown, but vector is string
*/
inline SEXP colour_values_hex(
Rcpp::StringVector& x,
SEXP palette,
Rcpp::NumericVector& alpha,
std::string na_colour = "#808080",
bool include_alpha = true,
bool format = false,
std::string format_type = "numeric",
int digits = 2,
bool summary = false
) {
switch( TYPEOF( palette ) ) {
case STRSXP: {
Rcpp::StringVector sv = Rcpp::as< Rcpp::StringVector >( palette );
Rcpp::String s = sv[0];
std::string pal = s;
return colourvalues::colours_hex::colour_value_hex(
x, pal, na_colour, alpha, include_alpha, summary
);
}
case INTSXP: {}
case REALSXP: {
if( !Rf_isMatrix( palette ) ) {
Rcpp::stop("Unknown palette type - expecting a matrix");
}
Rcpp::NumericMatrix pal = Rcpp::as< Rcpp::NumericMatrix >( palette );
return colourvalues::colours_hex::colour_value_hex(
x, pal, na_colour, alpha, summary
);
}
default: {
Rcpp::stop("Unknown palette type");
}
}

}

/*
* When the palette is a matrix, but vector is unknown
*/
inline SEXP colour_values_hex(
SEXP x,
Rcpp::NumericMatrix& palette,
Rcpp::NumericVector& alpha,
std::string na_colour = "#808080",
bool include_alpha = true,
bool format = false,
std::string format_type = "numeric",
int digits = 2,
bool summary = false,
int n_summaries = 0
) {
switch( TYPEOF( x ) ) {
case INTSXP: {}
case REALSXP: {
Rcpp::NumericVector nv = Rcpp::as< Rcpp::NumericVector >( x );
return colourvalues::colours_hex::colour_value_hex(
nv, palette, na_colour, include_alpha, n_summaries, format, format_type, digits
);
}
case VECSXP: { // list
Rcpp::List lst = Rcpp::as< Rcpp::List >( x );
// TODO list

}
case LGLSXP: {} // as.character
default: {
Rcpp::StringVector sv = Rcpp::as< Rcpp::StringVector >( x );
return colourvalues::colours_hex::colour_value_hex(
sv, palette, na_colour, alpha, summary
);
}
}

}

/*
* When the palette is a string, but vector is unknown
*/
inline SEXP colour_values_hex(
SEXP x,
Rcpp::StringVector& palette,
Rcpp::NumericVector& alpha,
std::string na_colour = "#808080",
bool include_alpha = true,
bool format = false,
std::string format_type = "numeric",
int digits = 2,
bool summary = false,
int n_summaries = 0
) {

Rcpp::String p = palette[0];
std::string pal = p;
switch( TYPEOF( x ) ) {
case INTSXP: {}
case REALSXP: {
Rcpp::NumericVector nv = Rcpp::as< Rcpp::NumericVector >( x );
return colourvalues::colours_hex::colour_value_hex(
nv, pal, na_colour, alpha, include_alpha, n_summaries, format, format_type, digits
);
}
case VECSXP: { // list
Rcpp::List lst = Rcpp::as< Rcpp::List >( x );

}
case LGLSXP: {} // as.character
default: {
Rcpp::StringVector sv = Rcpp::as< Rcpp::StringVector >( x );
return colourvalues::colours_hex::colour_value_hex(
sv, pal, na_colour, alpha, include_alpha, summary
);
}
}

Rcpp::StringVector sv;
return sv; // never reaches
}

/*
* When neither type of vector/list or palette is known
*/
inline SEXP colour_values_hex(
SEXP x,
SEXP palette,
Rcpp::NumericVector& alpha,
std::string na_colour = "#808080",
bool include_alpha = true,
bool format = false,
std::string format_type = "numeric",
int digits = 2,
bool summary = false,
int n_summaries = 0
) {

switch( TYPEOF( palette ) ) {
case INTSXP: {}
case REALSXP: {
Rcpp::NumericMatrix pal = Rcpp::as< Rcpp::NumericMatrix >( palette );
return colour_values_hex(
x, pal, alpha, na_colour, include_alpha, format, format_type, digits, summary, n_summaries
);
break;
}
case STRSXP: {
Rcpp::StringVector sv = Rcpp::as< Rcpp::StringVector >( palette );
// Rcpp::String s = sv[0];
// std::string pal = s;
return colour_values_hex(
x, sv, alpha, na_colour, include_alpha, format, format_type, digits, summary, n_summaries
);
break;
}
default: {
Rcpp::stop("Unknown palette type");
}
}

Rcpp::StringVector sv;
return sv; // never reaches

}


} // api
} // colourvalues
35 changes: 32 additions & 3 deletions inst/include/colourvalues/list/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ namespace list {
* @param lst_sizes - the dimensions of the list
* @param colorus - vector of values which will go into colour_values_hex()
*/
inline void unlist_list( const Rcpp::List& lst, const Rcpp::List& lst_sizes,
Rcpp::StringVector& colours, int& list_position ) {

inline void unlist_list(
const Rcpp::List& lst, const Rcpp::List& lst_sizes,
Rcpp::StringVector& colours, int& list_position
) {
// - iterate through original list
// - extract each element and insert into 'colours'
size_t n = lst.size();
Expand All @@ -146,6 +147,34 @@ namespace list {
}
}

inline void unlist_list(
const Rcpp::List& lst, const Rcpp::List& lst_sizes,
Rcpp::NumericVector& colours, int& list_position
) {
// - iterate through original list
// - extract each element and insert into 'colours'
size_t n = lst.size();
Rcpp::List res( n );
std::size_t i;
for( i = 0; i < n; i++ ) {
switch( TYPEOF( lst[i] ) ) {
case VECSXP: {
unlist_list( lst[ i ], lst_sizes[ i ], colours, list_position );
break;
}
default: {
Rcpp::IntegerVector n_elements = Rcpp::as< Rcpp::IntegerVector >( lst_sizes[ i ] );
int end_position = list_position + n_elements[0] - 1;
Rcpp::IntegerVector elements = Rcpp::seq( list_position, end_position );
colours[ elements ] = Rcpp::as< Rcpp::NumericVector >( lst[ i ] );

list_position = end_position + 1;
break;
}
}
}
}

} // list
} // colourvalues

Expand Down
22 changes: 21 additions & 1 deletion src/ColourValues_HEX.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
#include <Rcpp.h>

#include "colourvalues/colours/colours_hex.hpp"
//#include "colourvalues/colours.hpp"
//#include "colourvalues/colours/colours_hex.hpp"
#include "colourvalues/api.hpp"


// -----------------------------------------------------------------------------
// return HEX

// [[Rcpp::export]]
Rcpp::StringVector rcpp_colour_values_hex(
SEXP x,
SEXP palette,
Rcpp::NumericVector& alpha,
std::string na_colour = "#808080",
bool include_alpha = true,
bool format = false,
std::string format_type = "numeric",
int digits = 2,
bool summary = false,
int n_summaries = 0
) {
return colourvalues::api::colour_values_hex(
x, palette, alpha, na_colour, include_alpha, format, format_type, digits, summary, n_summaries
);
}

// [[Rcpp::export]]
Rcpp::StringVector rcpp_colour_num_value_string_palette_hex(
Rcpp::NumericVector x,
Expand Down
21 changes: 21 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@

using namespace Rcpp;

// rcpp_colour_values_hex
Rcpp::StringVector rcpp_colour_values_hex(SEXP x, SEXP palette, Rcpp::NumericVector& alpha, std::string na_colour, bool include_alpha, bool format, std::string format_type, int digits, bool summary, int n_summaries);
RcppExport SEXP _colourvalues_rcpp_colour_values_hex(SEXP xSEXP, SEXP paletteSEXP, SEXP alphaSEXP, SEXP na_colourSEXP, SEXP include_alphaSEXP, SEXP formatSEXP, SEXP format_typeSEXP, SEXP digitsSEXP, SEXP summarySEXP, SEXP n_summariesSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< SEXP >::type x(xSEXP);
Rcpp::traits::input_parameter< SEXP >::type palette(paletteSEXP);
Rcpp::traits::input_parameter< Rcpp::NumericVector& >::type alpha(alphaSEXP);
Rcpp::traits::input_parameter< std::string >::type na_colour(na_colourSEXP);
Rcpp::traits::input_parameter< bool >::type include_alpha(include_alphaSEXP);
Rcpp::traits::input_parameter< bool >::type format(formatSEXP);
Rcpp::traits::input_parameter< std::string >::type format_type(format_typeSEXP);
Rcpp::traits::input_parameter< int >::type digits(digitsSEXP);
Rcpp::traits::input_parameter< bool >::type summary(summarySEXP);
Rcpp::traits::input_parameter< int >::type n_summaries(n_summariesSEXP);
rcpp_result_gen = Rcpp::wrap(rcpp_colour_values_hex(x, palette, alpha, na_colour, include_alpha, format, format_type, digits, summary, n_summaries));
return rcpp_result_gen;
END_RCPP
}
// rcpp_colour_num_value_string_palette_hex
Rcpp::StringVector rcpp_colour_num_value_string_palette_hex(Rcpp::NumericVector x, std::string palette, std::string na_colour, Rcpp::NumericVector alpha, bool include_alpha);
RcppExport SEXP _colourvalues_rcpp_colour_num_value_string_palette_hex(SEXP xSEXP, SEXP paletteSEXP, SEXP na_colourSEXP, SEXP alphaSEXP, SEXP include_alphaSEXP) {
Expand Down Expand Up @@ -861,6 +881,7 @@ END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_colourvalues_rcpp_colour_values_hex", (DL_FUNC) &_colourvalues_rcpp_colour_values_hex, 10},
{"_colourvalues_rcpp_colour_num_value_string_palette_hex", (DL_FUNC) &_colourvalues_rcpp_colour_num_value_string_palette_hex, 5},
{"_colourvalues_rcpp_colour_num_value_string_palette_summary_hex", (DL_FUNC) &_colourvalues_rcpp_colour_num_value_string_palette_summary_hex, 9},
{"_colourvalues_rcpp_colour_num_value_rgb_palette_hex", (DL_FUNC) &_colourvalues_rcpp_colour_num_value_rgb_palette_hex, 4},
Expand Down
Loading

0 comments on commit ec120f5

Please sign in to comment.