Skip to content

Commit 98fa087

Browse files
committed
Merge pull request #215 from yixuan/sort_assert
Disable sorting for certain vector types
2 parents 2bbc244 + 8c3a03c commit 98fa087

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

ChangeLog

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
2014-12-11 Yixuan Qiu <[email protected]>
2+
3+
* inst/include/Rcpp/internal/r_vector.h: Internal functions to
4+
help detect unqualified vector types for sorting, with the help
5+
of Romain Francois
6+
* inst/include/Rcpp/vector/Vector.h: Disallow sorting on List,
7+
RawVector and ExpressionVector, from the discussion with
8+
Dirk Eddelbuettel and Kevin Ushey
9+
110
2014-11-25 Dirk Eddelbuettel <[email protected]>
211

312
* inst/include/Rcpp/grow.h: Apply additional Shield<> use around tail

inst/include/Rcpp/internal/r_vector.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,61 @@ inline void r_init_vector<EXPRSXP>(SEXP x){}
8484
template<>
8585
inline void r_init_vector<STRSXP>(SEXP x){}
8686

87+
88+
89+
/**
90+
* We do not allow List(RTYPE=VECSXP), RawVector(RTYPE=RAWSXP)
91+
* or ExpressionVector(RTYPE=EXPRSXP) to be sorted, so it is
92+
* desirable to issue a compiler error if user attempts to sort
93+
* these types of Vectors.
94+
*
95+
* We declare a template class without defining the generic
96+
* class body, but complete the definition in specialization
97+
* of qualified Vector types. Hence when using this class
98+
* on unqualified Vectors, the compiler will emit errors.
99+
*/
100+
template<int RTYPE>
101+
class Sort_is_not_allowed_for_this_type;
102+
103+
/**
104+
* Specialization for CPLXSXP, INTSXP, LGLSXP, REALSXP, and STRSXP
105+
*/
106+
template<>
107+
class Sort_is_not_allowed_for_this_type<CPLXSXP>
108+
{
109+
public:
110+
static void do_nothing() {}
111+
};
112+
113+
template<>
114+
class Sort_is_not_allowed_for_this_type<INTSXP>
115+
{
116+
public:
117+
static void do_nothing() {}
118+
};
119+
120+
template<>
121+
class Sort_is_not_allowed_for_this_type<LGLSXP>
122+
{
123+
public:
124+
static void do_nothing() {}
125+
};
126+
127+
template<>
128+
class Sort_is_not_allowed_for_this_type<REALSXP>
129+
{
130+
public:
131+
static void do_nothing() {}
132+
};
133+
134+
template<>
135+
class Sort_is_not_allowed_for_this_type<STRSXP>
136+
{
137+
public:
138+
static void do_nothing() {}
139+
};
140+
141+
87142
} // internal
88143
} // Rcpp
89144

inst/include/Rcpp/vector/Vector.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,13 @@ class Vector :
349349
}
350350

351351
Vector& sort(){
352+
// sort() does not apply to List, RawVector or ExpressionVector.
353+
//
354+
// The function below does nothing for qualified Vector types,
355+
// and is undefined for other types. Hence there will be a
356+
// compiler error when sorting List, RawVector or ExpressionVector.
357+
internal::Sort_is_not_allowed_for_this_type<RTYPE>::do_nothing();
358+
352359
typename traits::storage_type<RTYPE>::type* start = internal::r_vector_start<RTYPE>( Storage::get__() ) ;
353360
std::sort(
354361
start,

0 commit comments

Comments
 (0)