Skip to content

Commit 486d1ec

Browse files
colnames and rownames getters and setters for matrices. closes #210
1 parent aea59f9 commit 486d1ec

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* New `wrap` implementation for `std::tuple<Args...>` (#195)
44

5+
* `colnames` and `rownames` setters for matrices (#210).
6+
57
# Rcpp11 3.1.1
68

79
* sugar `sum` now supports complex sugar vector expressions

inst/include/Rcpp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ namespace Rcpp{
234234

235235
#include <Rcpp/Timer.h>
236236

237+
#include <Rcpp/vector/dimnames.h>
238+
237239
namespace Rcpp11 = Rcpp ;
238240

239241
#endif
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#ifndef Rcpp__Matrix_Dimnames_h
2+
#define Rcpp__Matrix_Dimnames_h
3+
4+
namespace Rcpp {
5+
6+
template <typename Data>
7+
class RowNamesProxy {
8+
public:
9+
10+
RowNamesProxy( Data& data_ ) : data(data_){}
11+
12+
inline RowNamesProxy& operator=( const CharacterVector& names ){
13+
Language call( "rownames<-", data, names ) ;
14+
data = call.eval() ;
15+
return *this ;
16+
}
17+
18+
inline RowNamesProxy& operator=( std::initializer_list<const char*> names ){
19+
return this->operator=( CharacterVector(names) ) ;
20+
}
21+
22+
inline operator CharacterVector() const {
23+
Language call( "rownames", data ) ;
24+
return call.eval() ;
25+
}
26+
27+
private:
28+
Data& data ;
29+
} ;
30+
31+
template <typename Data>
32+
class ColNamesProxy {
33+
public:
34+
35+
ColNamesProxy( Data& data_ ) : data(data_){}
36+
37+
inline ColNamesProxy& operator=( const CharacterVector& names ){
38+
Language call( "colnames<-", data, names ) ;
39+
data = call.eval() ;
40+
return *this ;
41+
}
42+
43+
inline ColNamesProxy& operator=( std::initializer_list<const char*> names ){
44+
return this->operator=( CharacterVector(names) ) ;
45+
}
46+
47+
inline operator CharacterVector() const {
48+
Language call( "colnames", data ) ;
49+
return call.eval() ;
50+
}
51+
52+
private:
53+
Data& data ;
54+
} ;
55+
56+
57+
template <int RTYPE, typename Storage>
58+
inline RowNamesProxy<Matrix<RTYPE, Storage>> rownames( const Matrix<RTYPE, Storage>& m ){
59+
return RowNamesProxy<Matrix<RTYPE, Storage>>(const_cast<Matrix<RTYPE,Storage>&>(m)) ;
60+
}
61+
62+
template <int RTYPE, typename Storage>
63+
inline ColNamesProxy<Matrix<RTYPE, Storage>> colnames( const Matrix<RTYPE, Storage>& m ){
64+
return ColNamesProxy<Matrix<RTYPE, Storage>>(const_cast<Matrix<RTYPE,Storage>&>(m)) ;
65+
}
66+
67+
}
68+
69+
#endif

0 commit comments

Comments
 (0)