Created
August 28, 2017 16:01
-
-
Save ADCDS/7e802dc04f9d29b4f2bdc1cc82677101 to your computer and use it in GitHub Desktop.
Simple example of usage of integer_sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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