-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathstring_proxy.h
More file actions
163 lines (134 loc) · 4.87 KB
/
string_proxy.h
File metadata and controls
163 lines (134 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef Rcpp__vector__string_proxy_h
#define Rcpp__vector__string_proxy_h
namespace Rcpp{
namespace internal{
template <typename Vec>
class string_proxy {
public:
friend class Proxy_Iterator<string_proxy> ;
/**
* Creates a proxy
*
* @param v reference to the associated character vector
* @param index index
*/
string_proxy( Vec& v, int index_ ) :
parent(v), index(index_){}
string_proxy( const string_proxy& other ) = default ;
/**
* lhs use. Assign the value of the referred element to
* the current content of the element referred by the
* rhs proxy
*
* @param rhs another proxy, possibly from another vector
*/
string_proxy& operator=(const string_proxy& other){
set( other.get() ) ;
return *this ;
}
/**
* lhs use. Assigns the value of the referred element
* of the character vector
*
* @param rhs new content for the element referred by this proxy
*/
template <typename T>
string_proxy& operator=(const T& rhs) {
set(rhs) ;
return *this ;
}
string_proxy& operator=(SEXP rhs){
set( rhs ) ;
return *this ;
}
/**
* lhs use. Adds the content of the rhs proxy to the
* element this proxy refers to.
*/
template <typename T>
string_proxy& operator+=(const T& rhs) ;
/**
* rhs use. Retrieves the current value of the
* element this proxy refers to.
*/
operator SEXP() const {
return get() ;
}
inline operator std::string() const {
return std::string( CHAR(get()) );
}
inline operator std::string() {
return std::string( CHAR(get()) );
}
friend inline std::ostream& operator<<(std::ostream& os, const string_proxy& proxy) {
os << std::string(proxy) ;
return os;
}
friend inline std::string operator+( const std::string& x, const string_proxy& y ){
return x + std::string(y) ;
}
friend inline void swap( string_proxy<Vec>& a, string_proxy<Vec>& b){
SEXP tmp = STRING_ELT(a.parent, a.index) ;
SET_STRING_ELT( a.parent, a.index, STRING_ELT( b.parent, b.index) ) ;
SET_STRING_ELT( b.parent, b.index, tmp ) ;
}
inline SEXP get() const {
return STRING_ELT( parent, index ) ;
}
template <typename T>
inline void set( const T& x );
inline void set(SEXP x){
SET_STRING_ELT( parent, index, x ) ;
}
inline int size() const { return strlen( begin() ) ; }
Rboolean operator==( const char* other){
return strcmp( begin(), other ) == 0 ? TRUE : FALSE ;
}
Rboolean operator!=( const char* other){
return strcmp( begin(), other ) != 0 ? TRUE : FALSE ;
}
Rboolean operator==( const string_proxy& other){
return strcmp( begin(), other.begin() ) == 0 ? TRUE : FALSE ;
}
Rboolean operator!=( const string_proxy& other){
return strcmp( begin(), other.begin() ) != 0 ? TRUE : FALSE;
}
private:
Vec& parent;
int index ;
typedef const char* iterator ;
typedef const char& reference ;
inline iterator begin() const { return CHAR( STRING_ELT( parent, index ) ) ; }
inline iterator end() const { return begin() + size() ; }
} ;
template <typename Vec1, typename Vec2>
bool operator<( const string_proxy<Vec1>& lhs, const string_proxy<Vec2>& rhs) {
return strcmp(
const_cast<char *>(lhs.begin() ),
const_cast<char *>(rhs.begin())
) < 0 ;
}
template <typename Vec1, typename Vec2>
bool operator>( const string_proxy<Vec1>& lhs, const string_proxy<Vec2>& rhs) {
return strcmp(
const_cast<char *>(lhs.begin() ),
const_cast<char *>(rhs.begin())
) > 0 ;
}
template <typename Vec1, typename Vec2>
bool operator>=( const string_proxy<Vec1>& lhs, const string_proxy<Vec2>& rhs) {
return strcmp(
const_cast<char *>(lhs.begin() ),
const_cast<char *>(rhs.begin())
) >= 0 ;
}
template <typename Vec1, typename Vec2>
bool operator<=( const string_proxy<Vec1>& lhs, const string_proxy<Vec2>& rhs) {
return strcmp(
const_cast<char *>(lhs.begin() ),
const_cast<char *>(rhs.begin())
) <= 0 ;
}
}
}
#endif