Skip to content

Commit 465a2e7

Browse files
committed
Added operator<<() and print() functions to Vector
1 parent e4868b8 commit 465a2e7

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

inst/include/Rcpp/vector/Vector.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,14 @@ class Vector :
582582
return -1;
583583
}
584584

585+
inline void print(const std::string & comment = "", std::ostream & stream = Rcout) const {
586+
if (comment.length() > 0) {
587+
stream << comment << std::endl;
588+
}
589+
590+
stream << (*this);
591+
}
592+
585593

586594
protected:
587595
inline int* dims() const {
@@ -1080,6 +1088,44 @@ class Vector :
10801088

10811089
} ; /* Vector */
10821090

1091+
template <int RTYPE, template <class> class StoragePolicy >
1092+
inline std::ostream &operator<<(std::ostream & s, const Vector<RTYPE, StoragePolicy> & rhs) {
1093+
typedef Vector<RTYPE, StoragePolicy> VECTOR;
1094+
1095+
typename VECTOR::iterator i = const_cast<VECTOR &>(rhs).begin();
1096+
typename VECTOR::iterator iend = const_cast<VECTOR &>(rhs).end();
1097+
1098+
if (i != iend) {
1099+
s << (*i);
1100+
++i;
1101+
1102+
for ( ; i != iend; ++i) {
1103+
s << " " << (*i);
1104+
}
1105+
}
1106+
1107+
return s;
1108+
}
1109+
1110+
template<template <class> class StoragePolicy >
1111+
inline std::ostream &operator<<(std::ostream & s, const Vector<STRSXP, StoragePolicy> & rhs) {
1112+
typedef Vector<STRSXP, StoragePolicy> VECTOR;
1113+
1114+
typename VECTOR::iterator i = const_cast<VECTOR &>(rhs).begin();
1115+
typename VECTOR::iterator iend = const_cast<VECTOR &>(rhs).end();
1116+
1117+
if (i != iend) {
1118+
s << "\"" << (*i) << "\"";
1119+
++i;
1120+
1121+
for ( ; i != iend; ++i) {
1122+
s << " \"" << (*i) << "\"";
1123+
}
1124+
}
1125+
1126+
return s;
1127+
}
1128+
10831129
}
10841130

10851131
#endif

inst/unitTests/cpp/Vector.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#include <climits>
2323
#include <Rcpp.h>
24+
#include <sstream>
25+
2426
using namespace Rcpp ;
2527

2628
inline double square( double x){ return x*x; }
@@ -782,3 +784,46 @@ int noprotect_matrix( Matrix<REALSXP, NoProtectStorage> x){
782784
int vec_access_with_bounds_checking(const IntegerVector x, int index) {
783785
return x.at(index);
784786
}
787+
788+
// [[Rcpp::export]]
789+
String vec_print_numeric(NumericVector v) {
790+
std::ostringstream buf;
791+
buf << v;
792+
return buf.str();
793+
}
794+
795+
// [[Rcpp::export]]
796+
String vec_print_numeric_member(NumericVector v) {
797+
std::ostringstream buf;
798+
v.print("", buf);
799+
return buf.str();
800+
}
801+
802+
// [[Rcpp::export]]
803+
String vec_print_character(CharacterVector v) {
804+
std::ostringstream buf;
805+
buf << v;
806+
return buf.str();
807+
}
808+
809+
// [[Rcpp::export]]
810+
String vec_print_character_member(CharacterVector v) {
811+
std::ostringstream buf;
812+
v.print("", buf);
813+
return buf.str();
814+
}
815+
816+
// [[Rcpp::export]]
817+
String vec_print_integer(IntegerVector v) {
818+
std::ostringstream buf;
819+
buf << v;
820+
return buf.str();
821+
}
822+
823+
// [[Rcpp::export]]
824+
String vec_print_integer_member(IntegerVector v) {
825+
std::ostringstream buf;
826+
v.print("", buf);
827+
return buf.str();
828+
}
829+

inst/unitTests/runit.Vector.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,5 +690,41 @@ if (.runThisTest) {
690690
checkException(vec_access_with_bounds_checking(x, 5) , msg = "index out of bounds not detected" )
691691
checkException(vec_access_with_bounds_checking(x, -1) , msg = "index out of bounds not detected" )
692692
}
693+
694+
test.NumericVector.print <- function() {
695+
v <- c(1.1, 2.2, 3.3, 4.4)
696+
s <- vec_print_numeric(v)
697+
checkEquals(s, "1.1 2.2 3.3 4.4")
698+
}
699+
700+
test.NumericVector.print.member <- function() {
701+
v <- c(1.1, 2.2, 3.3, 4.4)
702+
s <- vec_print_numeric_member(v)
703+
checkEquals(s, "1.1 2.2 3.3 4.4")
704+
}
705+
706+
test.IntegerVector.print <- function() {
707+
v <- c(1, 2, 3, 4)
708+
s <-vec_print_integer(v)
709+
checkEquals(s, "1 2 3 4")
710+
}
711+
712+
test.IntegerVector.print.member <- function() {
713+
v <- c(1, 2, 3, 4)
714+
s <- vec_print_integer_member(v)
715+
checkEquals(s, "1 2 3 4")
716+
}
717+
718+
test.CharacterVector.print <- function() {
719+
v <- c("a", "b", "c", "d")
720+
s <- vec_print_character(v)
721+
checkEquals(s, '"a" "b" "c" "d"')
722+
}
723+
724+
test.CharacterVector.print.member <- function() {
725+
v <- c("a", "b", "c", "d")
726+
s <- vec_print_character_member(v)
727+
checkEquals(s, '"a" "b" "c" "d"')
728+
}
693729
}
694730

0 commit comments

Comments
 (0)