Skip to content

Commit 1ae871d

Browse files
committed
Matrix now has appropriate operator<<. Removed member function print from Vector.
1 parent 465a2e7 commit 1ae871d

File tree

3 files changed

+153
-9
lines changed

3 files changed

+153
-9
lines changed

inst/include/Rcpp/vector/Matrix.h

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,158 @@ inline internal::DimNameProxy colnames(SEXP x) {
210210
return internal::DimNameProxy(x, 1);
211211
}
212212

213+
template<template <class> class StoragePolicy >
214+
inline std::ostream &operator<<(std::ostream & s, const Matrix<REALSXP, StoragePolicy> & rhs) {
215+
typedef Matrix<REALSXP, StoragePolicy> MATRIX;
216+
217+
std::ios::fmtflags flags = s.flags();
218+
s.unsetf(std::ios::floatfield);
219+
std::streamsize precision = s.precision();
220+
221+
const int rows = rhs.rows();
222+
223+
for (int i = 0; i < rows; ++i) {
224+
typename MATRIX::Row row = const_cast<MATRIX &>(rhs).row(i);
225+
226+
typename MATRIX::Row::iterator j = row.begin();
227+
typename MATRIX::Row::iterator jend = row.end();
228+
229+
if (j != jend) {
230+
s << std::setw(precision + 1) << (*j);
231+
j++;
232+
233+
for ( ; j != jend; j++) {
234+
s << " " << std::setw(precision + 1) << (*j);
235+
}
236+
}
237+
238+
s << std::endl;
239+
}
240+
241+
s.flags(flags);
242+
return s;
243+
}
244+
245+
template<template <class> class StoragePolicy >
246+
inline std::ostream &operator<<(std::ostream & s, const Matrix<INTSXP, StoragePolicy> & rhs) {
247+
typedef Matrix<INTSXP, StoragePolicy> MATRIX;
248+
typedef Vector<INTSXP, StoragePolicy> VECTOR;
249+
250+
std::ios::fmtflags flags = s.flags();
251+
252+
s << std::dec;
253+
254+
int min = std::numeric_limits<int>::max();
255+
int max = std::numeric_limits<int>::min();
256+
257+
typename VECTOR::iterator j = static_cast<VECTOR &>(const_cast<MATRIX &>(rhs)).begin();
258+
typename VECTOR::iterator jend = static_cast<VECTOR &>(const_cast<MATRIX &>(rhs)).end();
259+
260+
for ( ; j != jend; ++j) {
261+
if (*j < min) {
262+
min = *j;
263+
}
264+
265+
if (*j > max) {
266+
max = *j;
267+
}
268+
}
269+
270+
int digitsMax = (max >= 0) ? 0 : 1;
271+
int digitsMin = (min >= 0) ? 0 : 1;
272+
273+
while (min != 0)
274+
{
275+
++digitsMin;
276+
min /= 10;
277+
}
278+
279+
while (max != 0)
280+
{
281+
++digitsMax;
282+
max /= 10;
283+
}
284+
285+
int digits = std::max(digitsMin, digitsMax);
286+
287+
const int rows = rhs.rows();
288+
289+
for (int i = 0; i < rows; ++i) {
290+
typename MATRIX::Row row = const_cast<MATRIX &>(rhs).row(i);
291+
292+
typename MATRIX::Row::iterator j = row.begin();
293+
typename MATRIX::Row::iterator jend = row.end();
294+
295+
if (j != jend) {
296+
s << std::setw(digits) << (*j);
297+
++j;
298+
299+
for ( ; j != jend; ++j) {
300+
s << " " << std::setw(digits) << (*j);
301+
}
302+
}
303+
304+
s << std::endl;
305+
}
306+
307+
s.flags(flags);
308+
return s;
309+
}
310+
311+
template<template <class> class StoragePolicy >
312+
inline std::ostream &operator<<(std::ostream & s, const Matrix<STRSXP, StoragePolicy> & rhs) {
313+
typedef Matrix<STRSXP, StoragePolicy> MATRIX;
314+
315+
const int rows = rhs.rows();
316+
317+
for (int i = 0; i < rows; ++i) {
318+
typename MATRIX::Row row = const_cast<MATRIX &>(rhs).row(i);
319+
320+
typename MATRIX::Row::iterator j = row.begin();
321+
typename MATRIX::Row::iterator jend = row.end();
322+
323+
if (j != jend) {
324+
s << "\"" << (*j) << "\"";
325+
j++;
326+
327+
for ( ; j != jend; j++) {
328+
s << " \"" << (*j) << "\"";
329+
}
330+
}
331+
332+
s << std::endl;
333+
}
334+
335+
return s;
336+
}
337+
338+
template<int RTYPE, template <class> class StoragePolicy >
339+
inline std::ostream &operator<<(std::ostream & s, const Matrix<RTYPE, StoragePolicy> & rhs) {
340+
typedef Matrix<RTYPE, StoragePolicy> MATRIX;
341+
342+
const int rows = rhs.rows();
343+
344+
for (int i = 0; i < rows; ++i) {
345+
typename MATRIX::Row row = const_cast<MATRIX &>(rhs).row(i);
346+
347+
typename MATRIX::Row::iterator j = row.begin();
348+
typename MATRIX::Row::iterator jend = row.end();
349+
350+
if (j != jend) {
351+
s << (*j);
352+
j++;
353+
354+
for ( ; j != jend; j++) {
355+
s << (*j);
356+
}
357+
}
358+
359+
s << std::endl;
360+
}
361+
362+
return s;
363+
}
364+
213365
}
214366

215367
#endif

inst/include/Rcpp/vector/Vector.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -582,15 +582,6 @@ 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-
593-
594585
protected:
595586
inline int* dims() const {
596587
if( !::Rf_isMatrix(Storage::get__()) ) throw not_a_matrix() ;

inst/include/RcppCommon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ namespace Rcpp {
4949
#include <iterator>
5050
#include <exception>
5151
#include <iostream>
52+
#include <iomanip>
5253
#include <sstream>
5354
#include <string>
5455
#include <list>

0 commit comments

Comments
 (0)