@@ -336,33 +336,18 @@ enable_if_convertible_to_sexp<T, SEXP> as_sexp(const T& from) {
336336 return from;
337337}
338338
339- // Pacha: Specialization for std::map
340- // NOTE: I did not use templates to avoid clashes with doubles/function/etc.
341- inline SEXP as_sexp (const std::map<std::string, SEXP>& map) {
342- R_xlen_t size = map.size ();
343- SEXP result = PROTECT (Rf_allocVector (VECSXP, size));
344- SEXP names = PROTECT (Rf_allocVector (STRSXP, size));
345-
346- auto it = map.begin ();
347- for (R_xlen_t i = 0 ; i < size; ++i, ++it) {
348- SET_VECTOR_ELT (result, i, it->second );
349- SET_STRING_ELT (names, i, Rf_mkCharCE (it->first .c_str (), CE_UTF8));
350- }
351-
352- Rf_setAttrib (result, R_NamesSymbol, names);
353- UNPROTECT (2 );
354- return result;
355- }
356-
357- // Specialization for std::map<double, int>
358- inline SEXP as_sexp (const std::map<double , int >& map) {
339+ // Templated specialization for std::map
340+ template <typename Key, typename Value,
341+ typename = enable_if_t <std::is_arithmetic<Key>::value &&
342+ std::is_arithmetic<Value>::value>>
343+ inline SEXP as_sexp (const std::map<Key, Value>& map) {
359344 R_xlen_t size = map.size ();
360345 SEXP result = PROTECT (Rf_allocVector (VECSXP, size));
361346 SEXP names = PROTECT (Rf_allocVector (REALSXP, size));
362347
363348 auto it = map.begin ();
364349 for (R_xlen_t i = 0 ; i < size; ++i, ++it) {
365- SET_VECTOR_ELT (result, i, Rf_ScalarInteger (it->second ));
350+ SET_VECTOR_ELT (result, i, as_sexp (it->second ));
366351 REAL (names)[i] = it->first ;
367352 }
368353
@@ -371,32 +356,18 @@ inline SEXP as_sexp(const std::map<double, int>& map) {
371356 return result;
372357}
373358
374- // Pacha: Specialization for std::unordered_map
375- inline SEXP as_sexp (const std::unordered_map<std::string, SEXP>& map) {
376- R_xlen_t size = map.size ();
377- SEXP result = PROTECT (Rf_allocVector (VECSXP, size));
378- SEXP names = PROTECT (Rf_allocVector (STRSXP, size));
379-
380- auto it = map.begin ();
381- for (R_xlen_t i = 0 ; i < size; ++i, ++it) {
382- SET_VECTOR_ELT (result, i, it->second );
383- SET_STRING_ELT (names, i, Rf_mkCharCE (it->first .c_str (), CE_UTF8));
384- }
385-
386- Rf_setAttrib (result, R_NamesSymbol, names);
387- UNPROTECT (2 );
388- return result;
389- }
390-
391- // Specialization for std::unordered_map<double, int>
392- inline SEXP as_sexp (const std::unordered_map<double , int >& map) {
359+ // Templated specialization for std::unordered_map
360+ template <typename Key, typename Value,
361+ typename = enable_if_t <std::is_arithmetic<Key>::value &&
362+ std::is_arithmetic<Value>::value>>
363+ inline SEXP as_sexp (const std::unordered_map<Key, Value>& map) {
393364 R_xlen_t size = map.size ();
394365 SEXP result = PROTECT (Rf_allocVector (VECSXP, size));
395366 SEXP names = PROTECT (Rf_allocVector (REALSXP, size));
396367
397368 auto it = map.begin ();
398369 for (R_xlen_t i = 0 ; i < size; ++i, ++it) {
399- SET_VECTOR_ELT (result, i, Rf_ScalarInteger (it->second ));
370+ SET_VECTOR_ELT (result, i, as_sexp (it->second ));
400371 REAL (names)[i] = it->first ;
401372 }
402373
0 commit comments