The R Project SVN R

Rev

Rev 74117 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 74117 Rev 75365
Line 1... Line 1...
1
/*
1
/*
2
 *  R : A Computer Language for Statistical Data Analysis
2
 *  R : A Computer Language for Statistical Data Analysis
3
 *  Copyright (C) 1998-2017   The R Core Team
3
 *  Copyright (C) 1998-2018   The R Core Team
4
 *  Copyright (C) 2002-2015   The R Foundation
4
 *  Copyright (C) 2002-2015   The R Foundation
5
 *  Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
5
 *  Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
6
 *
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
8
 *  it under the terms of the GNU General Public License as published by
Line 240... Line 240...
240
 
240
 
241
    if (nrow < 0 || ncol < 0 || nface < 0)
241
    if (nrow < 0 || ncol < 0 || nface < 0)
242
	error(_("negative extents to 3D array"));
242
	error(_("negative extents to 3D array"));
243
#ifndef LONG_VECTOR_SUPPORT
243
#ifndef LONG_VECTOR_SUPPORT
244
    if ((double)nrow * (double)ncol * (double)nface > INT_MAX)
244
    if ((double)nrow * (double)ncol * (double)nface > INT_MAX)
245
	error(_("'alloc3Darray': too many elements specified"));
245
	error(_("'alloc3DArray': too many elements specified"));
246
#endif
246
#endif
247
    n = ((R_xlen_t) nrow) * ncol * nface;
247
    n = ((R_xlen_t) nrow) * ncol * nface;
248
    PROTECT(s = allocVector(mode, n));
248
    PROTECT(s = allocVector(mode, n));
249
    PROTECT(t = allocVector(INTSXP, 3));
249
    PROTECT(t = allocVector(INTSXP, 3));
250
    INTEGER(t)[0] = nrow;
250
    INTEGER(t)[0] = nrow;
Line 259... Line 259...
259
SEXP allocArray(SEXPTYPE mode, SEXP dims)
259
SEXP allocArray(SEXPTYPE mode, SEXP dims)
260
{
260
{
261
    SEXP array;
261
    SEXP array;
262
    int i;
262
    int i;
263
    R_xlen_t n = 1;
263
    R_xlen_t n = 1;
-
 
264
#ifndef LONG_VECTOR_SUPPORT
264
    double dn = 1;
265
    double dn = 1;
-
 
266
#endif
265
 
267
 
266
    for (i = 0; i < LENGTH(dims); i++) {
268
    for (i = 0; i < LENGTH(dims); i++) {
267
	dn *= INTEGER(dims)[i];
-
 
268
#ifndef LONG_VECTOR_SUPPORT
269
#ifndef LONG_VECTOR_SUPPORT
-
 
270
	dn *= INTEGER(dims)[i];
269
	if(dn > INT_MAX)
271
	if(dn > INT_MAX)
270
	    error(_("'allocArray': too many elements specified by 'dims'"));
272
	    error(_("'allocArray': too many elements specified by 'dims'"));
271
#endif
273
#endif
272
	n *= INTEGER(dims)[i];
274
	n *= INTEGER(dims)[i];
273
    }
275
    }
Line 567... Line 569...
567
    return ans;
569
    return ans;
568
}
570
}
569
 
571
 
570
SEXP attribute_hidden do_rowscols(SEXP call, SEXP op, SEXP args, SEXP rho)
572
SEXP attribute_hidden do_rowscols(SEXP call, SEXP op, SEXP args, SEXP rho)
571
{
573
{
572
    SEXP x, ans;
-
 
573
    int i, j, nr, nc;
-
 
574
 
-
 
575
    checkArity(op, args);
574
    checkArity(op, args);
576
    /* This is the dimensions vector */
575
    SEXP dim = CAR(args);
577
    x = CAR(args);
576
    int nprot = 0;
578
    if (!isInteger(x) || LENGTH(x) != 2)
577
    if (!isInteger(dim)) {
-
 
578
	PROTECT(dim = coerceVector(dim, INTSXP)); nprot++;
-
 
579
    }
-
 
580
    if (LENGTH(dim) != 2)
579
	error(_("a matrix-like object is required as argument to '%s'"),
581
	error(_("a matrix-like object is required as argument to '%s'"),
580
	      (PRIMVAL(op) == 2) ? "col" : "row");
582
	      (PRIMVAL(op) == 2) ? "col" : "row");
581
 
583
 
582
    nr = INTEGER(x)[0];
584
    int nr = INTEGER(dim)[0],
583
    nc = INTEGER(x)[1];
585
	nc = INTEGER(dim)[1];
-
 
586
    if(nprot) UNPROTECT(nprot);
584
 
587
 
585
    ans = allocMatrix(INTSXP, nr, nc);
588
    SEXP ans = allocMatrix(INTSXP, nr, nc);
586
 
589
 
587
    R_xlen_t NR = nr;
590
    R_xlen_t NR = nr;
588
    switch (PRIMVAL(op)) {
591
    switch (PRIMVAL(op)) {
589
    case 1:
592
    case 1: // row() & .row()
590
	for (i = 0; i < nr; i++)
593
	for (int i = 0; i < nr; i++)
591
	    for (j = 0; j < nc; j++)
594
	    for (int j = 0; j < nc; j++)
592
		INTEGER(ans)[i + j * NR] = i + 1;
595
		INTEGER(ans)[i + j * NR] = i + 1;
593
	break;
596
	break;
594
    case 2:
597
    case 2: // col() & .col()
595
	for (i = 0; i < nr; i++)
598
	for (int i = 0; i < nr; i++)
596
	    for (j = 0; j < nc; j++)
599
	    for (int j = 0; j < nc; j++)
597
		INTEGER(ans)[i + j * NR] = j + 1;
600
		INTEGER(ans)[i + j * NR] = j + 1;
598
	break;
601
	break;
599
    }
602
    }
600
    return ans;
603
    return ans;
601
}
604
}