Skip to content

Instantly share code, notes, and snippets.

@ADCDS
Created August 28, 2017 16:01
Show Gist options
  • Save ADCDS/7e802dc04f9d29b4f2bdc1cc82677101 to your computer and use it in GitHub Desktop.
Save ADCDS/7e802dc04f9d29b4f2bdc1cc82677101 to your computer and use it in GitHub Desktop.
Simple example of usage of integer_sort
#include <string>
#include <iostream>
#include <boost/sort/spreadsort/spreadsort.hpp>
struct user
{
uint32_t id;
char* name;
};
void gen_random(char *s, const int len) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < len; ++i) {
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
s[len] = 0;
}
/* BOOST INTERGERSORT FUNCTIONS*/
struct lessthan {
inline bool operator()(const struct user &x, const struct user &y) const {
return x.id < y.id;
}
};
struct rightshift {
inline int operator()(const struct user &x, const unsigned offset) {
return x.id >> offset;
}
};
/* BOOST INTERGERSORT FUNCTIONS*/
int main()
{
std::vector<struct user> users(100);
for (auto i = 0; i < 100; i++)
{
char* rand_str = (char * )malloc(sizeof(char)*10);
users[i].id = rand() % 10000;
gen_random(rand_str, 10);
users[i].name = rand_str;
}
std::cout << "Users loaded";
boost::sort::spreadsort::integer_sort(users.begin(), users.end(), rightshift(), lessthan());
std::cout << "Users sorted";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment