Skip to content

Commit 63f4ff4

Browse files
committed
Add no_init for matrices (#159)
1 parent cd91929 commit 63f4ff4

File tree

6 files changed

+65
-4
lines changed

6 files changed

+65
-4
lines changed

ChangeLog

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
2014-07-29 Kevin Ushey <[email protected]>
2+
3+
* inst/include/Rcpp/vector/no_init.h: Add no_init for matrices
4+
* inst/include/Rcpp/vector/Matrix.h: Idem
5+
* inst/include/Rcpp/vector/Vector.h: Idem
6+
* inst/unitTests/runit.Matrix.R: Idem
7+
* inst/unitTests/cpp/Matrix.cpp: Idem
8+
19
2014-07-29 Dirk Eddelbuettel <[email protected]>
210

311
* inst/include/Rcpp/vector/proxy.h: Removed operator bool() cast as
4-
discusses by Christian Authmann on rcpp-devel
12+
discusses by Christian Authmann on rcpp-devel
513

614
2014-07-28 Dirk Eddelbuettel <[email protected]>
715

inst/include/Rcpp/vector/Matrix.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Matrix : public Vector<RTYPE, StoragePolicy>, public MatrixBase<RTYPE, tru
3535
typedef MatrixColumn<RTYPE> Column ;
3636
typedef SubMatrix<RTYPE> Sub ;
3737

38+
typedef StoragePolicy<Matrix> Storage ;
3839
typedef Vector<RTYPE, StoragePolicy> VECTOR ;
3940
typedef typename VECTOR::iterator iterator ;
4041
typedef typename VECTOR::const_iterator const_iterator ;
@@ -84,6 +85,10 @@ class Matrix : public Vector<RTYPE, StoragePolicy>, public MatrixBase<RTYPE, tru
8485
}
8586
Matrix& operator=( const SubMatrix<RTYPE>& ) ;
8687

88+
explicit Matrix( const no_init_matrix& obj) {
89+
Storage::set__( Rf_allocMatrix( RTYPE, obj.nrow(), obj.ncol() ) );
90+
}
91+
8792
inline int ncol() const {
8893
return VECTOR::dims()[1];
8994
}

inst/include/Rcpp/vector/Vector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Vector :
7979
Storage::set__( r_cast<RTYPE>(proxy.get()) ) ;
8080
}
8181

82-
explicit Vector( const no_init& obj) {
82+
explicit Vector( const no_init_vector& obj) {
8383
Storage::set__( Rf_allocVector( RTYPE, obj.get() ) ) ;
8484
}
8585

inst/include/Rcpp/vector/no_init.h

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424

2525
namespace Rcpp{
2626

27-
class no_init {
27+
template <int RTYPE, template <class> class StoragePolicy>
28+
class Matrix;
29+
30+
class no_init_vector {
2831
public:
29-
no_init(int size_): size(size_){}
32+
no_init_vector(int size_): size(size_){}
3033

3134
inline int get() const {
3235
return size;
@@ -41,5 +44,36 @@ class no_init {
4144
int size ;
4245
} ;
4346

47+
class no_init_matrix {
48+
public:
49+
no_init_matrix(int nr_, int nc_): nr(nr_), nc(nc_) {}
50+
51+
inline int nrow() const {
52+
return nr;
53+
}
54+
55+
inline int ncol() const {
56+
return nc;
57+
}
58+
59+
template <int RTYPE, template <class> class StoragePolicy >
60+
operator Matrix<RTYPE, StoragePolicy>() const {
61+
return Rf_allocMatrix(RTYPE, nr, nc);
62+
}
63+
64+
private:
65+
int nr;
66+
int nc;
67+
} ;
68+
69+
inline no_init_vector no_init(int size) {
70+
return no_init_vector(size);
71+
}
72+
73+
inline no_init_matrix no_init(int nr, int nc) {
74+
return no_init_matrix(nr, nc);
75+
}
76+
77+
4478
}
4579
#endif

inst/unitTests/cpp/Matrix.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,12 @@ void runit_rownames_colnames_proxy(
216216
void runit_rownames_proxy(NumericMatrix x) {
217217
rownames(x) = CharacterVector::create("A", "B", "C");
218218
}
219+
220+
// [[Rcpp::export]]
221+
NumericMatrix runit_no_init_matrix() {
222+
NumericMatrix x = no_init(2, 2);
223+
for (int i = 0; i < 4; i++) {
224+
x[i] = i;
225+
}
226+
return x;
227+
}

inst/unitTests/runit.Matrix.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,10 @@ if (.runThisTest) {
160160
checkEquals(colnames(m), NULL)
161161
}
162162

163+
test.NumericMatrix.no.init <- function() {
164+
m <- runit_no_init_matrix()
165+
checkEquals(m, matrix(c(0, 1, 2, 3), nrow = 2))
166+
}
167+
163168

164169
}

0 commit comments

Comments
 (0)