Skip to content

Instantly share code, notes, and snippets.

@nathan-russell
Created August 11, 2016 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathan-russell/9a77e000f3e86386a6f31796d3d92169 to your computer and use it in GitHub Desktop.
Save nathan-russell/9a77e000f3e86386a6f31796d3d92169 to your computer and use it in GitHub Desktop.
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
template <typename T>
struct packet_t {
T value;
std::size_t id;
packet_t(const T& x, std::size_t y)
: value(x), id(y)
{}
packet_t() {}
};
template <typename T>
inline bool operator<(const packet_t<T>& lhs, const packet_t<T>& rhs) {
return lhs.value < rhs.value;
}
// [[Rcpp::export]]
void packet_sort(const arma::vec& x) {
std::size_t i = 0, sz = x.size();
std::vector<packet_t<double> > packets(sz);
for ( ; i < sz; i++) {
packets[i] = packet_t<double>(x[i], i);
}
std::sort(packets.begin(), packets.end());
for (i = 0; i < sz; i++) {
std::printf(\
"Position: %d, value: %.2f, id: %d\n",
(int)i, packets[i].value, (int)packets[i].id
);
}
}
// [[Rcpp::export]]
void stable_packet_sort(const arma::vec& x) {
std::size_t i = 0, sz = x.size();
std::vector<packet_t<double> > packets(sz);
for ( ; i < sz; i++) {
packets[i] = packet_t<double>(x[i], i);
}
std::stable_sort(packets.begin(), packets.end());
for (i = 0; i < sz; i++) {
std::printf(\
"Position: %d, value: %.2f, id: %d\n",
(int)i, packets[i].value, (int)packets[i].id
);
}
}
/*** R
set.seed(1991)
v <- sample(1:8, 20, TRUE)
packet_sort(v)
# Position: 0, value: 1.00, id: 2
# Position: 1, value: 1.00, id: 12
# Position: 2, value: 1.00, id: 9
# Position: 3, value: 2.00, id: 1
# Position: 4, value: 2.00, id: 15
# Position: 5, value: 2.00, id: 0
# Position: 6, value: 3.00, id: 11
# Position: 7, value: 4.00, id: 14
# Position: 8, value: 4.00, id: 13
# Position: 9, value: 4.00, id: 8
# Position: 10, value: 5.00, id: 10
# Position: 11, value: 5.00, id: 3
# Position: 12, value: 6.00, id: 7
# Position: 13, value: 6.00, id: 5
# Position: 14, value: 7.00, id: 6
# Position: 15, value: 7.00, id: 4
# Position: 16, value: 7.00, id: 17
# Position: 17, value: 7.00, id: 18
# Position: 18, value: 8.00, id: 16
# Position: 19, value: 8.00, id: 19
stable_packet_sort(v)
# Position: 0, value: 1.00, id: 2
# Position: 1, value: 1.00, id: 9
# Position: 2, value: 1.00, id: 12
# Position: 3, value: 2.00, id: 0
# Position: 4, value: 2.00, id: 1
# Position: 5, value: 2.00, id: 15
# Position: 6, value: 3.00, id: 11
# Position: 7, value: 4.00, id: 8
# Position: 8, value: 4.00, id: 13
# Position: 9, value: 4.00, id: 14
# Position: 10, value: 5.00, id: 3
# Position: 11, value: 5.00, id: 10
# Position: 12, value: 6.00, id: 5
# Position: 13, value: 6.00, id: 7
# Position: 14, value: 7.00, id: 4
# Position: 15, value: 7.00, id: 6
# Position: 16, value: 7.00, id: 17
# Position: 17, value: 7.00, id: 18
# Position: 18, value: 8.00, id: 16
# Position: 19, value: 8.00, id: 19
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment