File tree Expand file tree Collapse file tree 5 files changed +36
-10
lines changed Expand file tree Collapse file tree 5 files changed +36
-10
lines changed Original file line number Diff line number Diff line change 1
1
# cpp11 (development version)
2
2
3
+ * Added the missing implementation for ` x.at("name") ` for read only vectors
4
+ (#370 ).
5
+
3
6
* Constructors for writable vectors from ` initializer_list<named_arg> ` now
4
7
check that ` named_arg ` contains a length 1 object of the correct type, and
5
8
throws either a ` cpp11::type_error ` or ` std::length_error ` if that is not the
Original file line number Diff line number Diff line change @@ -495,13 +495,21 @@ context("doubles-C++") {
495
495
cpp11::writable::doubles x ({" a" _nm = 1 ., " b" _nm = 2 .});
496
496
cpp11::doubles y (x);
497
497
498
- expect_true (x[" a" ] == 1 );
499
- expect_true (x[" b" ] == 2 );
500
- expect_error (x[" c" ] == 2 );
498
+ expect_true (x[" a" ] == 1 .);
499
+ expect_true (x[" b" ] == 2 .);
500
+ expect_error (x[" c" ]);
501
+
502
+ expect_true (x.at (" a" ) == 1 .);
503
+ expect_true (x.at (" b" ) == 2 .);
504
+ expect_error (x.at (" c" ));
505
+
506
+ expect_true (y[" a" ] == 1 .);
507
+ expect_true (y[" b" ] == 2 .);
508
+ expect_error (y[" c" ]);
501
509
502
- expect_true (y[ " a" ] == 1 );
503
- expect_true (y[ " b" ] == 2 );
504
- expect_error (y[ " c" ] == 2 );
510
+ expect_true (y. at ( " a" ) == 1 . );
511
+ expect_true (y. at ( " b" ) == 2 . );
512
+ expect_error (y. at ( " c" ) );
505
513
}
506
514
507
515
test_that (" doubles::find" ) {
Original file line number Diff line number Diff line change @@ -250,18 +250,26 @@ context("integers-C++") {
250
250
}
251
251
#endif
252
252
253
- test_that (" operator[] with names" ) {
253
+ test_that (" operator[] and at with names" ) {
254
254
using namespace cpp11 ::literals;
255
255
cpp11::writable::integers x ({" a" _nm = 1 , " b" _nm = 2 });
256
256
cpp11::integers y (x);
257
257
258
258
expect_true (x[" a" ] == 1 );
259
259
expect_true (x[" b" ] == 2 );
260
- expect_error (x[" c" ] == 2 );
260
+ expect_error (x[" c" ]);
261
+
262
+ expect_true (x.at (" a" ) == 1 );
263
+ expect_true (x.at (" b" ) == 2 );
264
+ expect_error (x.at (" c" ));
261
265
262
266
expect_true (y[" a" ] == 1 );
263
267
expect_true (y[" b" ] == 2 );
264
- expect_error (y[" c" ] == 2 );
268
+ expect_error (y[" c" ]);
269
+
270
+ expect_true (y.at (" a" ) == 1 );
271
+ expect_true (y.at (" b" ) == 2 );
272
+ expect_error (y.at (" c" ));
265
273
}
266
274
267
275
test_that (" is_na(integer)" ) {
Original file line number Diff line number Diff line change @@ -168,7 +168,7 @@ context("list-C++") {
168
168
expect_true (x.size () == nms.size ());
169
169
}
170
170
171
- test_that (" list::operator[] by name" ) {
171
+ test_that (" list::operator[] and at by name" ) {
172
172
SEXP x = PROTECT (Rf_allocVector (VECSXP, 1 ));
173
173
174
174
SEXP elt = Rf_allocVector (INTSXP, 1 );
@@ -183,9 +183,11 @@ context("list-C++") {
183
183
184
184
expect_true (lst.named ());
185
185
expect_true (lst[" name" ] == elt);
186
+ expect_true (lst.at (" name" ) == elt);
186
187
187
188
// Lists are the only class where OOB accesses by name return `NULL`
188
189
expect_true (lst[" oob" ] == R_NilValue);
190
+ expect_true (lst.at (" oob" ) == R_NilValue);
189
191
190
192
UNPROTECT (1 );
191
193
}
Original file line number Diff line number Diff line change @@ -502,6 +502,11 @@ inline T r_vector<T>::at(const size_type pos) const {
502
502
return at (static_cast <R_xlen_t>(pos));
503
503
}
504
504
505
+ template <typename T>
506
+ inline T r_vector<T>::at(const r_string& name) const {
507
+ return operator [](name);
508
+ }
509
+
505
510
template <typename T>
506
511
inline bool r_vector<T>::contains(const r_string& name) const {
507
512
SEXP names = this ->names ();
You can’t perform that action at this time.
0 commit comments