|
| 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