@@ -210,6 +210,158 @@ inline internal::DimNameProxy colnames(SEXP x) {
210
210
return internal::DimNameProxy (x, 1 );
211
211
}
212
212
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
+
213
365
}
214
366
215
367
#endif
0 commit comments