The R Project SVN R

Rev

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

Rev 43024 Rev 43878
Line 237... Line 237...
237
/* attribute.  Note that this function mutates x. */
237
/* attribute.  Note that this function mutates x. */
238
/* Duplication should occur before this is called. */
238
/* Duplication should occur before this is called. */
239
 
239
 
240
SEXP DropDims(SEXP x)
240
SEXP DropDims(SEXP x)
241
{
241
{
242
    SEXP q, dims, dimnames, newnames = R_NilValue;
242
    SEXP dims, dimnames, newnames = R_NilValue;
243
    int i, n, ndims;
243
    int i, n, ndims;
244
 
244
 
245
    PROTECT(x);
245
    PROTECT(x);
246
    dims = getAttrib(x, R_DimSymbol);
246
    dims = getAttrib(x, R_DimSymbol);
247
    dimnames = getAttrib(x, R_DimNamesSymbol);
247
    dimnames = getAttrib(x, R_DimNamesSymbol);
Line 263... Line 263...
263
	UNPROTECT(1);
263
	UNPROTECT(1);
264
	return x;
264
	return x;
265
    }
265
    }
266
 
266
 
267
    if (n <= 1) {
267
    if (n <= 1) {
268
	/* We have reduced to a vector result. */
268
	/* We have reduced to a vector result.
-
 
269
	   If that has length one, it is ambiguous which dimnames to use,
-
 
270
	   so use it if there is only one (as from R 2.7.0).
-
 
271
	 */
269
	if (dimnames != R_NilValue) {
272
	if (dimnames != R_NilValue) {
270
	    n = length(dims);
-
 
271
	    if (TYPEOF(dimnames) == VECSXP) {
273
	    if(LENGTH(x) != 1) {
272
		for (i = 0; i < n; i++) {
274
		for (i = 0; i < LENGTH(dims); i++) {
273
		    if (INTEGER(dims)[i] != 1) {
275
		    if (INTEGER(dims)[i] != 1) {
274
			newnames = VECTOR_ELT(dimnames, i);
276
			newnames = VECTOR_ELT(dimnames, i);
275
			break;
277
			break;
276
		    }
278
		    }
277
		}
279
		}
-
 
280
	    } else { /* drop all dims: keep names if unambiguous */
278
	    }
281
		int cnt;
279
	    else {
282
		for(i = 0, cnt = 0; i < LENGTH(dims); i++)
-
 
283
		    if(VECTOR_ELT(dimnames, i) != R_NilValue) cnt++;
280
		q = dimnames;
284
		if(cnt == 1)
281
		for (i = 0; i < n; i++) {
285
		    for (i = 0; i < LENGTH(dims); i++) {
282
		    if (INTEGER(dims)[i] != 1) {
286
			newnames = VECTOR_ELT(dimnames, i);
283
			newnames = CAR(q);
287
			if(newnames != R_NilValue) break;
284
			break;
-
 
285
		    }
288
		    }
286
		    q = CDR(q);
-
 
287
		}
-
 
288
	    }
289
	    }
289
	}
290
	}
290
	PROTECT(newnames);
291
	PROTECT(newnames);
291
	setAttrib(x, R_DimNamesSymbol, R_NilValue);
292
	setAttrib(x, R_DimNamesSymbol, R_NilValue);
292
	setAttrib(x, R_DimSymbol, R_NilValue);
293
	setAttrib(x, R_DimSymbol, R_NilValue);
293
	setAttrib(x, R_NamesSymbol, newnames);
294
	setAttrib(x, R_NamesSymbol, newnames);
294
	UNPROTECT(1);
295
	UNPROTECT(1);
295
    }
-
 
296
    else {
296
    } else {
297
	/* We have a lower dimensional array. */
297
	/* We have a lower dimensional array. */
298
	SEXP newdims, dnn, newnamesnames = R_NilValue;
298
	SEXP newdims, dnn, newnamesnames = R_NilValue;
299
	dnn = getAttrib(dimnames, R_NamesSymbol);
299
	dnn = getAttrib(dimnames, R_NamesSymbol);
300
	PROTECT(newdims = allocVector(INTSXP, n));
300
	PROTECT(newdims = allocVector(INTSXP, n));
301
	for (i = 0, n = 0; i < ndims; i++)
301
	for (i = 0, n = 0; i < ndims; i++)