The R Project SVN R

Rev

Rev 66087 | Rev 68947 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 66087 Rev 66734
Line 52... Line 52...
52
 
52
 
53
/* moved from printutils.c */
53
/* moved from printutils.c */
54
 
54
 
55
static void MatrixColumnLabel(SEXP cl, int j, int w)
55
static void MatrixColumnLabel(SEXP cl, int j, int w)
56
{
56
{
57
    int l;
-
 
58
    SEXP tmp;
-
 
59
 
-
 
60
    if (!isNull(cl)) {
57
    if (!isNull(cl)) {
61
	tmp = STRING_ELT(cl, j);
58
	SEXP tmp = STRING_ELT(cl, j);
62
	if(tmp == NA_STRING) l = R_print.na_width_noquote;
59
	int l = (tmp == NA_STRING) ? R_print.na_width_noquote : Rstrlen(tmp, 0);
63
	else l = Rstrlen(tmp, 0);
-
 
64
	Rprintf("%*s%s", w-l, "",
60
	Rprintf("%*s%s", w-l, "",
65
		EncodeString(tmp, l, 0, Rprt_adj_left));
61
		EncodeString(tmp, l, 0, Rprt_adj_left));
66
    }
62
    }
67
    else {
63
    else {
68
	Rprintf("%*s[,%ld]", w-IndexWidth(j+1)-3, "", j+1);
64
	Rprintf("%*s[,%ld]", w-IndexWidth(j+1)-3, "", j+1);
69
    }
65
    }
70
}
66
}
71
 
67
 
72
static void RightMatrixColumnLabel(SEXP cl, int j, int w)
68
static void RightMatrixColumnLabel(SEXP cl, int j, int w)
73
{
69
{
74
    int l;
-
 
75
    SEXP tmp;
-
 
76
 
-
 
77
    if (!isNull(cl)) {
70
    if (!isNull(cl)) {
78
	tmp = STRING_ELT(cl, j);
71
	SEXP tmp = STRING_ELT(cl, j);
79
	if(tmp == NA_STRING) l = R_print.na_width_noquote;
72
	int l = (tmp == NA_STRING) ? R_print.na_width_noquote : Rstrlen(tmp, 0);
80
	else l = Rstrlen(tmp, 0);
-
 
81
	/* This does not work correctly at least on FC3
73
	/* This does not work correctly at least on FC3
82
	Rprintf("%*s", R_print.gap+w,
74
	Rprintf("%*s", R_print.gap+w,
83
		EncodeString(tmp, l, 0, Rprt_adj_right)); */
75
		EncodeString(tmp, l, 0, Rprt_adj_right)); */
84
	Rprintf("%*s%s", R_print.gap+w-l, "",
76
	Rprintf("%*s%s", R_print.gap+w-l, "",
85
		EncodeString(tmp, l, 0, Rprt_adj_right));
77
		EncodeString(tmp, l, 0, Rprt_adj_right));
Line 89... Line 81...
89
    }
81
    }
90
}
82
}
91
 
83
 
92
static void LeftMatrixColumnLabel(SEXP cl, int j, int w)
84
static void LeftMatrixColumnLabel(SEXP cl, int j, int w)
93
{
85
{
94
    int l;
-
 
95
    SEXP tmp;
-
 
96
 
-
 
97
    if (!isNull(cl)) {
86
    if (!isNull(cl)) {
98
	tmp= STRING_ELT(cl, j);
87
	SEXP tmp = STRING_ELT(cl, j);
99
	if(tmp == NA_STRING) l = R_print.na_width_noquote;
88
	int l = (tmp == NA_STRING) ? R_print.na_width_noquote : Rstrlen(tmp, 0);
100
	else l = Rstrlen(tmp, 0);
-
 
101
	Rprintf("%*s%s%*s", R_print.gap, "",
89
	Rprintf("%*s%s%*s", R_print.gap, "",
102
		EncodeString(tmp, l, 0, Rprt_adj_left), w-l, "");
90
		EncodeString(tmp, l, 0, Rprt_adj_left), w-l, "");
103
    }
91
    }
104
    else {
92
    else {
105
	Rprintf("%*s[,%ld]%*s", R_print.gap, "", j+1, w-IndexWidth(j+1)-3, "");
93
	Rprintf("%*s[,%ld]%*s", R_print.gap, "", j+1, w-IndexWidth(j+1)-3, "");
106
    }
94
    }
107
}
95
}
108
 
96
 
109
static void MatrixRowLabel(SEXP rl, int i, int rlabw, int lbloff)
97
static void MatrixRowLabel(SEXP rl, int i, int rlabw, int lbloff)
110
{
98
{
111
    int l;
-
 
112
    SEXP tmp;
-
 
113
 
-
 
114
    if (!isNull(rl)) {
99
    if (!isNull(rl)) {
115
	tmp= STRING_ELT(rl, i);
100
	SEXP tmp = STRING_ELT(rl, i);
116
	if(tmp == NA_STRING) l = R_print.na_width_noquote;
101
	int l = (tmp == NA_STRING) ? R_print.na_width_noquote : Rstrlen(tmp, 0);
117
	else l = Rstrlen(tmp, 0);
-
 
118
	Rprintf("\n%*s%s%*s", lbloff, "",
102
	Rprintf("\n%*s%s%*s", lbloff, "",
119
		EncodeString(tmp, l, 0, Rprt_adj_left),
103
		EncodeString(tmp, l, 0, Rprt_adj_left),
120
		rlabw-l-lbloff, "");
104
		rlabw-l-lbloff, "");
121
    }
105
    }
122
    else {
106
    else {
Line 129... Line 113...
129
/* This is the first (of 6)  print<TYPE>Matrix()  functions.
113
/* This is the first (of 6)  print<TYPE>Matrix()  functions.
130
 * We define macros that will be re-used in the other functions,
114
 * We define macros that will be re-used in the other functions,
131
 * and comment the common code here (only):
115
 * and comment the common code here (only):
132
*/
116
*/
133
static void printLogicalMatrix(SEXP sx, int offset, int r_pr, int r, int c,
117
static void printLogicalMatrix(SEXP sx, int offset, int r_pr, int r, int c,
134
			       SEXP rl, SEXP cl, const char *rn, const char *cn)
118
			       SEXP rl, SEXP cl, const char *rn, const char *cn,
-
 
119
			       Rboolean print_ij)
135
{
120
{
136
/* initialization; particularly of row labels, rl= dimnames(.)[[1]] and
121
/* initialization; particularly of row labels, rl= dimnames(.)[[1]] and
137
 * rn = names(dimnames(.))[1] : */
122
 * rn = names(dimnames(.))[1] : */
138
#define _PRINT_INIT_rl_rn				\
123
#define _PRINT_INIT_rl_rn				\
139
    int *w = (int *) R_alloc(c, sizeof(int));		\
124
    int *w = (int *) R_alloc(c, sizeof(int));		\
Line 153... Line 138...
153
	    lbloff = rnw - rlabw;			\
138
	    lbloff = rnw - rlabw;			\
154
							\
139
							\
155
	rlabw += lbloff;				\
140
	rlabw += lbloff;				\
156
    }
141
    }
157
 
142
 
158
    _PRINT_INIT_rl_rn;
-
 
159
    int *x = LOGICAL(sx) + offset;
143
#   define _COMPUTE_W2_(_FORMAT_j_, _LAST_j_)				\
160
 
-
 
161
    /* compute w[j] = column-width of j(+1)-th column : */
144
    /* compute w[j] = column-width of j(+1)-th column : */		\
162
    for (j = 0; j < c; j++) {
145
    for (j = 0; j < c; j++) {						\
163
	formatLogical(&x[j * r], (R_xlen_t) r, &w[j]);
-
 
164
 
-
 
165
#	define _PRINT_SET_clabw					\
146
	if(print_ij) { _FORMAT_j_; } else w[j] = 0;			\
166
								\
147
									\
167
	if (!isNull(cl)) {					\
148
	if (!isNull(cl)) {						\
168
	    const void *vmax = vmaxget();			\
149
	    const void *vmax = vmaxget();				\
169
	    if(STRING_ELT(cl, j) == NA_STRING)			\
150
	    if(STRING_ELT(cl, j) == NA_STRING)				\
170
		clabw = R_print.na_width_noquote;		\
151
		clabw = R_print.na_width_noquote;			\
171
	    else clabw = strwidth(translateChar(STRING_ELT(cl, j)));	\
152
	    else clabw = strwidth(translateChar(STRING_ELT(cl, j)));	\
172
	    vmaxset(vmax);					\
153
	    vmaxset(vmax);						\
173
	} else							\
154
	} else								\
174
	    clabw = IndexWidth(j + 1) + 3;
155
	    clabw = IndexWidth(j + 1) + 3;				\
175
 
-
 
176
	_PRINT_SET_clabw;
156
									\
177
 
-
 
178
	if (w[j] < clabw)
157
	if (w[j] < clabw)						\
179
	    w[j] = clabw;
158
	    w[j] = clabw;						\
180
	w[j] += R_print.gap;
159
	_LAST_j_;							\
181
    }
160
    }
182
 
161
 
183
#   define _PRINT_DEAL_c_eq_0				\
162
#   define _COMPUTE_W_(F_j) _COMPUTE_W2_(F_j, w[j] += R_print.gap)
184
							\
-
 
185
    if (c == 0) {					\
-
 
186
	for (i = 0; i < r; i++)				\
-
 
187
	    MatrixRowLabel(rl, i, rlabw, lbloff);	\
163
    //                               _LAST_j  ------------------- for all but String
188
	Rprintf("\n");					\
-
 
189
	return;						\
-
 
190
    }
-
 
191
    _PRINT_DEAL_c_eq_0;
-
 
192
 
164
 
-
 
165
#   define _PRINT_ROW_LAB			\
-
 
166
						\
-
 
167
    if (cn != NULL)				\
-
 
168
	Rprintf("%*s%s\n", rlabw, "", cn);	\
-
 
169
    if (rn != NULL)				\
-
 
170
	Rprintf("%*s", -rlabw, rn);		\
-
 
171
    else					\
-
 
172
	Rprintf("%*s", rlabw, "")
-
 
173
 
-
 
174
#   define _PRINT_MATRIX_(_W_EXTRA_, DO_COLUMN_LABELS, ENCODE_I_J)	\
-
 
175
									\
-
 
176
    if (c == 0) {							\
-
 
177
	_PRINT_ROW_LAB;							\
-
 
178
	for (i = 0; i < r; i++)						\
-
 
179
	    MatrixRowLabel(rl, i, rlabw, lbloff);			\
-
 
180
	Rprintf("\n");							\
-
 
181
    }									\
193
    while (jmin < c) {
182
    else while (jmin < c) {						\
194
	/* print columns  jmin:(jmax-1)	 where jmax has to be determined first */
183
	/* print columns  jmin:(jmax-1)	 where jmax has to be determined first */ \
-
 
184
									\
-
 
185
	width = rlabw;							\
-
 
186
	/* initially, jmax = jmin */					\
-
 
187
	do {								\
-
 
188
	    width += w[jmax] _W_EXTRA_;					\
-
 
189
	    jmax++;							\
-
 
190
	}								\
-
 
191
	while (jmax < c && width + w[jmax] _W_EXTRA_ < R_print.width);	\
-
 
192
									\
-
 
193
	_PRINT_ROW_LAB;							\
-
 
194
									\
-
 
195
	DO_COLUMN_LABELS;						\
-
 
196
									\
-
 
197
	for (i = 0; i < r_pr; i++) {					\
-
 
198
	    MatrixRowLabel(rl, i, rlabw, lbloff); /* starting with an "\n" */ \
-
 
199
	    if(print_ij) for (j = jmin; j < jmax; j++) {		\
-
 
200
		ENCODE_I_J;						\
-
 
201
	    }								\
-
 
202
	}								\
-
 
203
	Rprintf("\n");							\
-
 
204
	jmin = jmax;							\
-
 
205
    }
-
 
206
 
-
 
207
#   define STD_ColumnLabels			\
-
 
208
	for (j = jmin; j < jmax ; j++)		\
-
 
209
	    MatrixColumnLabel(cl, j, w[j])
195
 
210
 
196
	width = rlabw;
211
    _PRINT_INIT_rl_rn;
197
	/* initially, jmax = jmin */
-
 
198
	do {
-
 
199
	    width += w[jmax]; jmax++;
212
    int *x = LOGICAL(sx) + offset;
200
 
213
 
-
 
214
    _COMPUTE_W_(formatLogical(&x[j * r], (R_xlen_t) r, &w[j]));
-
 
215
 
-
 
216
    _PRINT_MATRIX_( , STD_ColumnLabels,
201
	} while (jmax < c && width + w[jmax] < R_print.width);
217
		   Rprintf("%s", EncodeLogical(x[i + j * r], w[j])));
202
 
218
 
203
#	define _PRINT_ROW_LAB			\
-
 
204
						\
-
 
205
	if (cn != NULL)				\
-
 
206
	    Rprintf("%*s%s\n", rlabw, "", cn);	\
-
 
207
						\
-
 
208
	if (rn != NULL)				\
-
 
209
	    Rprintf("%*s", -rlabw, rn);		\
-
 
210
	else					\
-
 
211
	    Rprintf("%*s", rlabw, "");
-
 
212
 
-
 
213
	_PRINT_ROW_LAB;
-
 
214
 
-
 
215
	for (j = jmin; j < jmax ; j++)
-
 
216
	    MatrixColumnLabel(cl, j, w[j]);
-
 
217
	for (i = 0; i < r_pr; i++) {
-
 
218
	    MatrixRowLabel(rl, i, rlabw, lbloff); /* starting with an "\n" */
-
 
219
	    for (j = jmin; j < jmax; j++) {
-
 
220
		Rprintf("%s", EncodeLogical(x[i + j * r], w[j]));
-
 
221
	    }
-
 
222
	}
-
 
223
	Rprintf("\n");
-
 
224
	jmin = jmax;
-
 
225
    }
-
 
226
}
219
}
227
 
220
 
228
static void printIntegerMatrix(SEXP sx, int offset, int r_pr, int r, int c,
221
static void printIntegerMatrix(SEXP sx, int offset, int r_pr, int r, int c,
229
			       SEXP rl, SEXP cl, const char *rn, const char *cn)
222
			       SEXP rl, SEXP cl, const char *rn, const char *cn,
-
 
223
			       Rboolean print_ij)
230
{
224
{
231
    _PRINT_INIT_rl_rn;
225
    _PRINT_INIT_rl_rn;
232
    int *x = INTEGER(sx) + offset;
226
    int *x = INTEGER(sx) + offset;
233
 
227
 
234
    for (j = 0; j < c; j++) {
-
 
235
	formatInteger(&x[j * r], (R_xlen_t) r, &w[j]);
228
    _COMPUTE_W_( formatInteger(&x[j * r], (R_xlen_t) r, &w[j]) );
236
	_PRINT_SET_clabw;
-
 
237
	if (w[j] < clabw)
-
 
238
	    w[j] = clabw;
-
 
239
	w[j] += R_print.gap;
-
 
240
    }
-
 
241
    _PRINT_DEAL_c_eq_0;
-
 
242
    while (jmin < c) {
-
 
243
	width = rlabw;
-
 
244
	do {
-
 
245
	    width += w[jmax];
-
 
246
	    jmax++;
-
 
247
	}
-
 
248
	while (jmax < c && width + w[jmax] < R_print.width);
-
 
249
 
229
 
250
	_PRINT_ROW_LAB;
-
 
251
 
-
 
252
	for (j = jmin; j < jmax ; j++)
-
 
253
	    MatrixColumnLabel(cl, j, w[j]);
-
 
254
	for (i = 0; i < r_pr; i++) {
-
 
255
	    MatrixRowLabel(rl, i, rlabw, lbloff);
230
    _PRINT_MATRIX_( , STD_ColumnLabels,
256
	    for (j = jmin; j < jmax; j++) {
-
 
257
		Rprintf("%s", EncodeInteger(x[i + j * r], w[j]));
231
		   Rprintf("%s", EncodeInteger(x[i + j * r], w[j])));
258
	    }
-
 
259
	}
-
 
260
	Rprintf("\n");
-
 
261
	jmin = jmax;
-
 
262
    }
-
 
263
}
232
}
264
 
233
 
265
static void printRealMatrix(SEXP sx, int offset, int r_pr, int r, int c,
234
static void printRealMatrix(SEXP sx, int offset, int r_pr, int r, int c,
266
			    SEXP rl, SEXP cl, const char *rn, const char *cn)
235
			    SEXP rl, SEXP cl, const char *rn, const char *cn,
-
 
236
			    Rboolean print_ij)
267
{
237
{
268
    _PRINT_INIT_rl_rn;
238
    _PRINT_INIT_rl_rn;
269
    double *x = REAL(sx) + offset;
239
    double *x = REAL(sx) + offset;
270
 
-
 
271
    int *d = (int *) R_alloc(c, sizeof(int)),
240
    int *d = (int *) R_alloc(c, sizeof(int)),
272
	*e = (int *) R_alloc(c, sizeof(int));
241
	*e = (int *) R_alloc(c, sizeof(int));
273
 
242
 
274
    for (j = 0; j < c; j++) {
-
 
275
	formatReal(&x[j * r], (R_xlen_t) r, &w[j], &d[j], &e[j], 0);
243
    _COMPUTE_W_( formatReal(&x[j * r], (R_xlen_t) r, &w[j], &d[j], &e[j], 0) );
276
	_PRINT_SET_clabw;
-
 
277
	if (w[j] < clabw)
-
 
278
	    w[j] = clabw;
-
 
279
	w[j] += R_print.gap;
-
 
280
    }
-
 
281
    _PRINT_DEAL_c_eq_0;
-
 
282
    while (jmin < c) {
-
 
283
	width = rlabw;
-
 
284
	do {
-
 
285
	    width += w[jmax];
-
 
286
	    jmax++;
-
 
287
	}
-
 
288
	while (jmax < c && width + w[jmax] < R_print.width);
-
 
289
 
244
 
290
	_PRINT_ROW_LAB;
-
 
291
 
-
 
292
	for (j = jmin; j < jmax ; j++)
-
 
293
	    MatrixColumnLabel(cl, j, w[j]);
-
 
294
	for (i = 0; i < r_pr; i++) {
-
 
295
	    MatrixRowLabel(rl, i, rlabw, lbloff);
245
    _PRINT_MATRIX_( , STD_ColumnLabels,
296
	    for (j = jmin; j < jmax; j++) {
-
 
297
		Rprintf("%s", EncodeReal0(x[i + j * r], w[j], d[j], e[j], OutDec));
246
		   Rprintf("%s", EncodeReal0(x[i + j * r], w[j], d[j], e[j], OutDec)) );
298
	    }
-
 
299
	}
-
 
300
	Rprintf("\n");
-
 
301
	jmin = jmax;
-
 
302
    }
-
 
303
}
247
}
304
 
248
 
305
static void printComplexMatrix(SEXP sx, int offset, int r_pr, int r, int c,
249
static void printComplexMatrix(SEXP sx, int offset, int r_pr, int r, int c,
306
			       SEXP rl, SEXP cl, const char *rn, const char *cn)
250
			       SEXP rl, SEXP cl, const char *rn, const char *cn,
-
 
251
			       Rboolean print_ij)
307
{
252
{
308
    _PRINT_INIT_rl_rn;
253
    _PRINT_INIT_rl_rn;
309
    Rcomplex *x = COMPLEX(sx) + offset;
254
    Rcomplex *x = COMPLEX(sx) + offset;
310
 
-
 
311
    int *dr = (int *) R_alloc(c, sizeof(int)),
255
    int *dr = (int *) R_alloc(c, sizeof(int)),
312
	*er = (int *) R_alloc(c, sizeof(int)),
256
	*er = (int *) R_alloc(c, sizeof(int)),
313
	*wr = (int *) R_alloc(c, sizeof(int)),
257
	*wr = (int *) R_alloc(c, sizeof(int)),
314
	*di = (int *) R_alloc(c, sizeof(int)),
258
	*di = (int *) R_alloc(c, sizeof(int)),
315
	*ei = (int *) R_alloc(c, sizeof(int)),
259
	*ei = (int *) R_alloc(c, sizeof(int)),
316
	*wi = (int *) R_alloc(c, sizeof(int));
260
	*wi = (int *) R_alloc(c, sizeof(int));
317
 
261
 
318
    /* Determine the column widths */
262
    /* Determine the column widths */
319
 
-
 
320
    for (j = 0; j < c; j++) {
-
 
321
	formatComplex(&x[j * r], (R_xlen_t) r,
263
    _COMPUTE_W_( formatComplex(&x[j * r], (R_xlen_t) r,
322
		      &wr[j], &dr[j], &er[j],
264
			       &wr[j], &dr[j], &er[j],
323
		      &wi[j], &di[j], &ei[j], 0);
265
			       &wi[j], &di[j], &ei[j], 0);
324
	_PRINT_SET_clabw;
-
 
325
	w[j] = wr[j] + wi[j] + 2;
266
		 w[j] = wr[j] + wi[j] + 2 );
326
	if (w[j] < clabw)
-
 
327
	    w[j] = clabw;
-
 
328
	w[j] += R_print.gap;
-
 
329
    }
-
 
330
 
267
 
331
    _PRINT_DEAL_c_eq_0;
268
    _PRINT_MATRIX_( , STD_ColumnLabels,
332
    while (jmin < c) {
-
 
333
	width = rlabw;
-
 
334
	do {
-
 
335
	    width += w[jmax];
-
 
336
	    jmax++;
-
 
337
	}
-
 
338
	while (jmax < c && width + w[jmax] < R_print.width);
-
 
339
 
-
 
340
	_PRINT_ROW_LAB;
-
 
341
 
-
 
342
	for (j = jmin; j < jmax ; j++)
-
 
343
	    MatrixColumnLabel(cl, j, w[j]);
-
 
344
	for (i = 0; i < r_pr; i++) {
-
 
345
	    MatrixRowLabel(rl, i, rlabw, lbloff);
-
 
346
	    for (j = jmin; j < jmax; j++) {
-
 
347
		if (ISNA(x[i + j * r].r) || ISNA(x[i + j * r].i))
269
		   if (ISNA(x[i + j * r].r) || ISNA(x[i + j * r].i))
348
		    Rprintf("%s", EncodeReal0(NA_REAL, w[j], 0, 0, OutDec));
270
		       Rprintf("%s", EncodeReal0(NA_REAL, w[j], 0, 0, OutDec));
349
		else
271
		   else
350
		    Rprintf("%s",
272
		       Rprintf("%s",
351
			    EncodeComplex(x[i + j * r],
273
			       EncodeComplex(x[i + j * r],
352
					  wr[j] + R_print.gap, dr[j], er[j],
274
					     wr[j] + R_print.gap, dr[j], er[j],
353
					  wi[j], di[j], ei[j], OutDec));
275
					     wi[j], di[j], ei[j], OutDec)) )
354
	    }
-
 
355
	}
-
 
356
	Rprintf("\n");
-
 
357
	jmin = jmax;
-
 
358
    }
-
 
359
}
276
}
360
 
277
 
361
static void printStringMatrix(SEXP sx, int offset, int r_pr, int r, int c,
278
static void printStringMatrix(SEXP sx, int offset, int r_pr, int r, int c,
362
			      int quote, int right, SEXP rl, SEXP cl,
279
			      int quote, int right, SEXP rl, SEXP cl,
363
			      const char *rn, const char *cn)
280
			      const char *rn, const char *cn, Rboolean print_ij)
364
{
281
{
365
    _PRINT_INIT_rl_rn;
282
    _PRINT_INIT_rl_rn;
366
    SEXP *x = STRING_PTR(sx)+offset;
283
    SEXP *x = STRING_PTR(sx)+offset;
367
 
284
 
368
    for (j = 0; j < c; j++) {
-
 
369
	formatString(&x[j * r], (R_xlen_t) r, &w[j], quote);
285
    _COMPUTE_W2_( formatString(&x[j * r], (R_xlen_t) r, &w[j], quote), );
370
	_PRINT_SET_clabw;
-
 
371
	if (w[j] < clabw)
-
 
372
	    w[j] = clabw;
-
 
373
    }
-
 
374
    _PRINT_DEAL_c_eq_0;
-
 
375
    while (jmin < c) {
-
 
376
	width = rlabw;
-
 
377
	do {
-
 
378
	    width += w[jmax] + R_print.gap;
-
 
379
	    jmax++;
-
 
380
	}
-
 
381
	while (jmax < c && width + w[jmax] + R_print.gap < R_print.width);
-
 
382
 
-
 
383
	_PRINT_ROW_LAB;
-
 
384
 
286
 
-
 
287
    _PRINT_MATRIX_( + R_print.gap,
-
 
288
	           /* DO_COLUMN_LABELS = */
385
	if (right) {
289
		   if (right) {
386
	    for (j = jmin; j < jmax ; j++)
290
		       for (j = jmin; j < jmax ; j++)
387
		RightMatrixColumnLabel(cl, j, w[j]);
291
			   RightMatrixColumnLabel(cl, j, w[j]);
388
	}
292
		   }
389
	else {
293
		   else {
390
	    for (j = jmin; j < jmax ; j++)
294
		       for (j = jmin; j < jmax ; j++)
391
		LeftMatrixColumnLabel(cl, j, w[j]);
295
			   LeftMatrixColumnLabel(cl, j, w[j]);
392
	}
296
		   },
393
	for (i = 0; i < r_pr; i++) {
297
		   /* ENCODE_I = */
394
	    MatrixRowLabel(rl, i, rlabw, lbloff);
-
 
395
	    for (j = jmin; j < jmax; j++) {
-
 
396
		Rprintf("%*s%s", R_print.gap, "",
298
		   Rprintf("%*s%s", R_print.gap, "",
397
			EncodeString(x[i + j * r], w[j], quote, right));
299
			   EncodeString(x[i + j * r], w[j], quote, right)) );
398
	    }
-
 
399
	}
-
 
400
	Rprintf("\n");
-
 
401
	jmin = jmax;
-
 
402
    }
-
 
403
}
300
}
404
 
301
 
405
static void printRawMatrix(SEXP sx, int offset, int r_pr, int r, int c,
302
static void printRawMatrix(SEXP sx, int offset, int r_pr, int r, int c,
406
			   SEXP rl, SEXP cl, const char *rn, const char *cn)
303
			   SEXP rl, SEXP cl, const char *rn, const char *cn,
-
 
304
			   Rboolean print_ij)
407
{
305
{
408
    _PRINT_INIT_rl_rn;
306
    _PRINT_INIT_rl_rn;
409
    Rbyte *x = RAW(sx) + offset;
307
    Rbyte *x = RAW(sx) + offset;
410
 
308
 
411
    for (j = 0; j < c; j++) {
-
 
412
	formatRaw(&x[j * r], (R_xlen_t) r, &w[j]);
309
    _COMPUTE_W_( formatRaw(&x[j * r], (R_xlen_t) r, &w[j]) )
413
	_PRINT_SET_clabw;
-
 
414
	if (w[j] < clabw)
-
 
415
	    w[j] = clabw;
-
 
416
	w[j] += R_print.gap;
-
 
417
    }
-
 
418
    _PRINT_DEAL_c_eq_0;
-
 
419
    while (jmin < c) {
-
 
420
	width = rlabw;
-
 
421
	do {
-
 
422
	    width += w[jmax];
-
 
423
	    jmax++;
-
 
424
	}
-
 
425
	while (jmax < c && width + w[jmax] < R_print.width);
-
 
426
 
-
 
427
	_PRINT_ROW_LAB;
-
 
428
 
310
 
429
	for (j = jmin; j < jmax ; j++)
-
 
430
	    MatrixColumnLabel(cl, j, w[j]);
-
 
431
	for (i = 0; i < r_pr; i++) {
-
 
432
	    MatrixRowLabel(rl, i, rlabw, lbloff);
311
    _PRINT_MATRIX_( , STD_ColumnLabels,
433
	    for (j = jmin; j < jmax; j++)
-
 
434
		Rprintf("%*s%s", w[j]-2, "", EncodeRaw(x[i + j * r], ""));
312
		   Rprintf("%*s%s", w[j]-2, "", EncodeRaw(x[i + j * r], "")) );
435
	}
-
 
436
	Rprintf("\n");
-
 
437
	jmin = jmax;
-
 
438
    }
-
 
439
}
313
}
440
 
314
 
441
attribute_hidden
315
attribute_hidden
442
void printMatrix(SEXP x, int offset, SEXP dim, int quote, int right,
316
void printMatrix(SEXP x, int offset, SEXP dim, int quote, int right,
443
		 SEXP rl, SEXP cl, const char *rn, const char *cn)
317
		 SEXP rl, SEXP cl, const char *rn, const char *cn)
Line 451... Line 325...
451
    /* PR#850 */
325
    /* PR#850 */
452
    if ((rl != R_NilValue) && (r > length(rl)))
326
    if ((rl != R_NilValue) && (r > length(rl)))
453
	error(_("too few row labels"));
327
	error(_("too few row labels"));
454
    if ((cl != R_NilValue) && (c > length(cl)))
328
    if ((cl != R_NilValue) && (c > length(cl)))
455
	error(_("too few column labels"));
329
	error(_("too few column labels"));
456
    if (r == 0 && c == 0) {
330
    if (r == 0 && c == 0) { // FIXME?  names(dimnames(.)) :
457
	Rprintf("<0 x 0 matrix>\n");
331
	Rprintf("<0 x 0 matrix>\n");
458
	return;
332
	return;
459
    }
333
    }
460
    r_pr = r;
334
    r_pr = r;
461
    if(c > 0 && R_print.max / c < r) /* avoid integer overflow */
335
    if(c > 0 && R_print.max / c < r) /* avoid integer overflow */
462
	/* using floor(), not ceil(), since 'c' could be huge: */
336
	/* using floor(), not ceil(), since 'c' could be huge: */
463
	r_pr = R_print.max / c;
337
	r_pr = R_print.max / c;
464
    switch (TYPEOF(x)) {
338
    switch (TYPEOF(x)) {
465
    case LGLSXP:
339
    case LGLSXP:
466
	printLogicalMatrix(x, offset, r_pr, r, c, rl, cl, rn, cn);
340
	printLogicalMatrix(x, offset, r_pr, r, c, rl, cl, rn, cn, TRUE);
467
	break;
341
	break;
468
    case INTSXP:
342
    case INTSXP:
469
	printIntegerMatrix(x, offset, r_pr, r, c, rl, cl, rn, cn);
343
	printIntegerMatrix(x, offset, r_pr, r, c, rl, cl, rn, cn, TRUE);
470
	break;
344
	break;
471
    case REALSXP:
345
    case REALSXP:
472
	printRealMatrix	  (x, offset, r_pr, r, c, rl, cl, rn, cn);
346
	printRealMatrix	  (x, offset, r_pr, r, c, rl, cl, rn, cn, TRUE);
473
	break;
347
	break;
474
    case CPLXSXP:
348
    case CPLXSXP:
475
	printComplexMatrix(x, offset, r_pr, r, c, rl, cl, rn, cn);
349
	printComplexMatrix(x, offset, r_pr, r, c, rl, cl, rn, cn, TRUE);
476
	break;
350
	break;
477
    case STRSXP:
351
    case STRSXP:
478
	if (quote) quote = '"';
352
	if (quote) quote = '"';
479
	printStringMatrix (x, offset, r_pr, r, c, quote, right, rl, cl, rn, cn);
353
	printStringMatrix (x, offset, r_pr, r, c, quote, right, rl, cl, rn, cn, TRUE);
480
	break;
354
	break;
481
    case RAWSXP:
355
    case RAWSXP:
482
	printRawMatrix	  (x, offset, r_pr, r, c, rl, cl, rn, cn);
356
	printRawMatrix	  (x, offset, r_pr, r, c, rl, cl, rn, cn, TRUE);
483
	break;
357
	break;
484
    default:
358
    default:
485
	UNIMPLEMENTED_TYPE("printMatrix", x);
359
	UNIMPLEMENTED_TYPE("printMatrix", x);
486
    }
360
    }
487
#ifdef ENABLE_NLS
361
#ifdef ENABLE_NLS
Line 489... Line 363...
489
	Rprintf(ngettext(" [ reached getOption(\"max.print\") -- omitted %d row ]\n",
363
	Rprintf(ngettext(" [ reached getOption(\"max.print\") -- omitted %d row ]\n",
490
			 " [ reached getOption(\"max.print\") -- omitted %d rows ]\n",
364
			 " [ reached getOption(\"max.print\") -- omitted %d rows ]\n",
491
			 r - r_pr),
365
			 r - r_pr),
492
		r - r_pr);
366
		r - r_pr);
493
#else
367
#else
494
    if(r_pr < r) 
368
    if(r_pr < r)
495
	Rprintf(" [ reached getOption(\"max.print\") -- omitted %d rows ]\n",
369
	Rprintf(" [ reached getOption(\"max.print\") -- omitted %d rows ]\n",
496
		r - r_pr);
370
		r - r_pr);
497
#endif
371
#endif
498
    vmaxset(vmax);
372
    vmaxset(vmax);
499
}
373
}
Line 513... Line 387...
513
	GetMatrixDimnames(x, &rl, &cl, &rn, &cn);
387
	GetMatrixDimnames(x, &rl, &cl, &rn, &cn);
514
	printMatrix(x, 0, dim, quote, 0, rl, cl, rn, cn);
388
	printMatrix(x, 0, dim, quote, 0, rl, cl, rn, cn);
515
    }
389
    }
516
    else { /* ndim >= 3 */
390
    else { /* ndim >= 3 */
517
	SEXP dn, dnn, dn0, dn1;
391
	SEXP dn, dnn, dn0, dn1;
518
	int i, j, has_dimnames, has_dnn, nb, nb_pr;
392
	int i, j, nb, nb_pr, nr_last,
519
	int nr = INTEGER(dim)[0], nr_last;
393
	    *dims = INTEGER(dim), nr = dims[0], nc = dims[1],
520
	int nc = INTEGER(dim)[1];
-
 
521
	int b = nr * nc;
394
	    b = nr * nc;
-
 
395
	Rboolean max_reached, has_dimnames = (dimnames != R_NilValue),
522
	Rboolean max_reached;
396
	    has_dnn = has_dimnames;
523
 
397
 
524
	if (dimnames == R_NilValue) {
-
 
525
	    has_dimnames = 0;
398
	if (!has_dimnames) {
526
	    has_dnn = 0;
-
 
527
	    dn0 = R_NilValue;
399
	    dn0 = R_NilValue;
528
	    dn1 = R_NilValue;
400
	    dn1 = R_NilValue;
529
	    dnn = R_NilValue; /* -Wall */
401
	    dnn = R_NilValue; /* -Wall */
530
	}
402
	}
531
	else {
403
	else {
532
	    dn0 = VECTOR_ELT(dimnames, 0);
404
	    dn0 = VECTOR_ELT(dimnames, 0);
533
	    dn1 = VECTOR_ELT(dimnames, 1);
405
	    dn1 = VECTOR_ELT(dimnames, 1);
534
	    has_dimnames = 1;
-
 
535
	    dnn = getAttrib(dimnames, R_NamesSymbol);
406
	    dnn = getAttrib(dimnames, R_NamesSymbol);
536
	    has_dnn = !isNull(dnn);
407
	    has_dnn = !isNull(dnn);
537
	    if ( has_dnn ) {
408
	    if ( has_dnn ) {
538
		rn = (char *) translateChar(STRING_ELT(dnn, 0));
409
		rn = (char *) translateChar(STRING_ELT(dnn, 0));
539
		cn = (char *) translateChar(STRING_ELT(dnn, 1));
410
		cn = (char *) translateChar(STRING_ELT(dnn, 1));
Line 541... Line 412...
541
	}
412
	}
542
	/* nb := #{entries} in a slice such as x[1,1,..] or equivalently,
413
	/* nb := #{entries} in a slice such as x[1,1,..] or equivalently,
543
	 *       the number of matrix slices   x[ , , *, ..]  which
414
	 *       the number of matrix slices   x[ , , *, ..]  which
544
	 *       are printed as matrices -- if options("max.print") allows */
415
	 *       are printed as matrices -- if options("max.print") allows */
545
	for (i = 2, nb = 1; i < ndim; i++)
416
	for (i = 2, nb = 1; i < ndim; i++)
546
	    nb *= INTEGER(dim)[i];
417
	    nb *= dims[i];
547
	max_reached = (b > 0 && R_print.max / b < nb);
418
	max_reached = (b > 0 && R_print.max / b < nb);
548
	if (max_reached) { /* i.e., also  b > 0, nr > 0, nc > 0, nb > 0 */
419
	if (max_reached) { /* i.e., also  b > 0, nr > 0, nc > 0, nb > 0 */
549
	    /* nb_pr := the number of matrix slices to be printed */
420
	    /* nb_pr := the number of matrix slices to be printed */
550
	    nb_pr = ceil_DIV(R_print.max, b);
421
	    nb_pr = ceil_DIV(R_print.max, b);
551
	    /* for the last, (nb_pr)th matrix slice, use only nr_last rows;
422
	    /* for the last, (nb_pr)th matrix slice, use only nr_last rows;
552
	     *  using floor(), not ceil(), since 'nc' could be huge: */
423
	     *  using floor(), not ceil(), since 'nc' could be huge: */
553
	    nr_last = (R_print.max - b * (nb_pr - 1)) / nc;
424
	    nr_last = (R_print.max - b * (nb_pr - 1)) / nc;
554
	    if(nr_last == 0) { nb_pr--; nr_last = nr; }
425
	    if(nr_last == 0) { nb_pr--; nr_last = nr; }
555
	} else {
426
	} else {
556
	    nb_pr = nb;
427
	    nb_pr = (nb > 0) ? nb : 1; // do print *something* when dim = c(a,b,0)
557
	    nr_last = nr;
428
	    nr_last = nr;
558
	}
429
	}
559
	for (i = 0; i < nb_pr; i++) {
430
	for (i = 0; i < nb_pr; i++) {
560
	    int k = 1, use_nr = nr;
431
	    Rboolean do_ij = nb > 0,
561
	    if (i == nb_pr - 1) { /* for the last slice :*/
432
		i_last = (i == nb_pr - 1); /* for the last slice */
562
		use_nr = nr_last;
433
	    int use_nr = i_last ? nr_last : nr;
-
 
434
	    if(do_ij) {
563
	    }
435
		int k = 1;
564
	    Rprintf(", ");
436
		Rprintf(", ");
565
	    for (j = 2 ; j < ndim; j++) {
437
		for (j = 2 ; j < ndim; j++) {
566
		int l = (i / k) % INTEGER(dim)[j] + 1;
438
		    int l = (i / k) % dims[j] + 1;
567
		if (has_dimnames &&
439
		    if (has_dimnames &&
568
		    ((dn = VECTOR_ELT(dimnames, j)) != R_NilValue)) {
440
			((dn = VECTOR_ELT(dimnames, j)) != R_NilValue)) {
569
		    if ( has_dnn )
441
			if ( has_dnn )
570
			Rprintf(", %s = %s",
442
			    Rprintf(", %s = %s",
571
				translateChar(STRING_ELT(dnn, j)),
443
				    translateChar(STRING_ELT(dnn, j)),
572
				translateChar(STRING_ELT(dn, l - 1)));
444
				    translateChar(STRING_ELT(dn, l - 1)));
573
		    else
445
			else
574
			Rprintf(", %s", translateChar(STRING_ELT(dn, l - 1)));
446
			    Rprintf(", %s", translateChar(STRING_ELT(dn, l - 1)));
575
		} else
447
		    } else
576
		    Rprintf(", %d", l);
448
			Rprintf(", %d", l);
577
		k = k * INTEGER(dim)[j];
449
		    k *= dims[j];
-
 
450
		}
-
 
451
		Rprintf("\n\n");
-
 
452
	    } else { // nb == 0 -- e.g. <2 x 3 x 0 array of logical>
-
 
453
		for (i = 0; i < ndim; i++)
-
 
454
		    Rprintf("%s%d", (i == 0) ? "<" : " x ", dims[i]);
-
 
455
		Rprintf(" array of %s>\n", CHAR(type2str_nowarn(TYPEOF(x))));
578
	    }
456
	    }
579
	    Rprintf("\n\n");
-
 
580
	    switch (TYPEOF(x)) {
457
	    switch (TYPEOF(x)) {
581
	    case LGLSXP:
458
	    case LGLSXP:
582
		printLogicalMatrix(x, i * b, use_nr, nr, nc, dn0, dn1, rn, cn);
459
		printLogicalMatrix(x, i * b, use_nr, nr, nc, dn0, dn1, rn, cn, do_ij);
583
		break;
460
		break;
584
	    case INTSXP:
461
	    case INTSXP:
585
		printIntegerMatrix(x, i * b, use_nr, nr, nc, dn0, dn1, rn, cn);
462
		printIntegerMatrix(x, i * b, use_nr, nr, nc, dn0, dn1, rn, cn, do_ij);
586
		break;
463
		break;
587
	    case REALSXP:
464
	    case REALSXP:
588
		printRealMatrix   (x, i * b, use_nr, nr, nc, dn0, dn1, rn, cn);
465
		printRealMatrix   (x, i * b, use_nr, nr, nc, dn0, dn1, rn, cn, do_ij);
589
		break;
466
		break;
590
	    case CPLXSXP:
467
	    case CPLXSXP:
591
		printComplexMatrix(x, i * b, use_nr, nr, nc, dn0, dn1, rn, cn);
468
		printComplexMatrix(x, i * b, use_nr, nr, nc, dn0, dn1, rn, cn, do_ij);
592
		break;
469
		break;
593
	    case STRSXP:
470
	    case STRSXP:
594
		if (quote) quote = '"';
471
		if (quote) quote = '"';
595
		printStringMatrix (x, i * b, use_nr, nr, nc,
472
		printStringMatrix (x, i * b, use_nr, nr, nc,
596
				   quote, right, dn0, dn1, rn, cn);
473
				   quote, right, dn0, dn1, rn, cn, do_ij);
597
		break;
474
		break;
598
	    case RAWSXP:
475
	    case RAWSXP:
599
		printRawMatrix    (x, i * b, use_nr, nr, nc, dn0, dn1, rn, cn);
476
		printRawMatrix    (x, i * b, use_nr, nr, nc, dn0, dn1, rn, cn, do_ij);
600
		break;
477
		break;
601
	    }
478
	    }
602
	    Rprintf("\n");
479
	    Rprintf("\n");
603
	}
480
	}
604
 
481