I've always wondered why the C++ standard template library doesn't seem to have standard bucket/library (distribution) sorts. These seem underused in modern programming, apparently due to the requirement a way to convert an object to an integer to sort by. Both seem relatively simple to me, so why don't we have this in the library?
template<class RandomAccessIterator, class Index, class index_type=unsigned int>
void std::distribution_sort(
RandomAccessIterator begin,
RandomAccessIterator end
index_type minval,
index_type maxval,
Index indexer,);
unsigned int indexer(const std::string& word)
{
switch(word.size()) {
case 0: return 0;
case 1: return (word[0]<<24);
case 2: return (word[0]<<24) | (word[1]<<16);
case 3: return (word[0]<<24) | (word[1]<<16) | (word[2]<<24);
default: return (word[0]<<24) | (word[1]<<16) | (word[2]<<8) | (word[3]);
}
}
int main() {
std::vector<std::string> data;
data.push_back("");
data.push_back("APPLES");
data.push_back("banana");
std::distribution_sort(data.begin(), data.end(), 0, ~0, indexer);
}