### Parse Command-Line Arguments and Initialize Main Function in C++ Source: https://www.boost.org/doc/libs/latest/libs/sort/example/keyplusdatasample Processes command-line arguments to determine whether to use std::sort or spreadsort, and sets loop count for performance testing. Initializes variables and prepares the sorting comparison environment. ```cpp int main(int argc, const char ** argv) { size_t uSize = sizeof(int); bool stdSort = false; unsigned loopCount = 1; for (int u = 1; u < argc; ++u) { if (std::string(argv[u]) == "-std") stdSort = true; else loopCount = atoi(argv[u]); } // ... sorting logic ... if (stdSort) printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC); else printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC); return 0; } ``` -------------------------------- ### Complete Spreadsort String Functor Sorting Example in C++ Source: https://www.boost.org/doc/libs/latest/libs/sort/example/stringfunctorsample Full C++ program demonstrating Boost spreadsort string sorting with custom functors. Loads strings from input.txt, sorts them using either spreadsort or std::sort (with -std flag), handles edge cases like embedded nulls and empty strings, and outputs results with performance timing. ```cpp #include #include #include #include #include #include #include #include #include using std::string; using namespace boost::sort::spreadsort; struct DATA_TYPE { string a; }; struct lessthan { inline bool operator()(const DATA_TYPE &x, const DATA_TYPE &y) const { return x.a < y.a; } }; struct bracket { inline unsigned char operator()(const DATA_TYPE &x, size_t offset) const { return x.a[offset]; } }; struct getsize { inline size_t operator()(const DATA_TYPE &x) const{ return x.a.size(); } }; int main(int argc, const char ** argv) { std::ifstream indata; std::ofstream outfile; bool stdSort = false; unsigned loopCount = 1; for (int u = 1; u < argc; ++u) { if (std::string(argv[u]) == "-std") stdSort = true; else loopCount = atoi(argv[u]); } double total = 0.0; std::vector array; for (unsigned u = 0; u < loopCount; ++u) { indata.open("input.txt", std::ios_base::in | std::ios_base::binary); if (indata.bad()) { printf("input.txt could not be opened\n"); return 1; } DATA_TYPE inval; indata >> inval.a; while (!indata.eof() ) { array.push_back(inval); if (!(array.size() % 100)) { if (inval.a.empty() || !(array.size() % 1000)) { inval.a = ""; array.push_back(inval); } else { inval.a[0] = '\0'; array.push_back(inval); } } indata >> inval.a; } indata.close(); clock_t start, end; double elapsed; start = clock(); if (stdSort) { std::sort(array.begin(), array.end(), lessthan()); } else { string_sort(array.begin(), array.end(), bracket(), getsize(), lessthan()); } end = clock(); elapsed = static_cast(end - start); if (stdSort) { outfile.open("standard_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); } else { outfile.open("boost_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); } if (outfile.good()) { for (unsigned u = 0; u < array.size(); ++u) outfile << array[u].a << "\n"; outfile.close(); } total += elapsed; array.clear(); } if (stdSort) { printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC); } else { printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC); } return 0; } ``` -------------------------------- ### Main Function for Spreadsort Float Example Source: https://www.boost.org/doc/libs/latest/libs/sort/example/floatfunctorsample The main function orchestrates the sorting process. It handles command-line arguments to choose between `std::sort` and `boost::sort::spreadsort`, reads data from `input.txt`, performs the sort, writes results to output files, and times the operation. ```cpp int main(int argc, const char ** argv) { size_t uCount,uSize=sizeof(DATA_TYPE); bool stdSort = false; unsigned loopCount = 1; for (int u = 1; u < argc; ++u) { if (std::string(argv[u]) == "-std") stdSort = true; else loopCount = atoi(argv[u]); } std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary); if (input.fail()) { printf("input.txt could not be opened\n"); return 1; } double total = 0.0; std::vector array; input.seekg (0, std::ios_base::end); size_t length = input.tellg(); uCount = length/uSize; //Run multiple loops, if requested for (unsigned u = 0; u < loopCount; ++u) { input.seekg (0, std::ios_base::beg); //Conversion to a vector array.resize(uCount); unsigned v = 0; while (input.good() && v < uCount) { input.read(reinterpret_cast(&(array[v].key)), sizeof(array[v].key)); //Checking for denormalized numbers; float_sort looks too fast on them. if (!(float_mem_cast(array[v].key) & 0x7f800000)) { //Make the top exponent bit high CAST_TYPE temp = 0x40000000 | float_mem_cast(array[v].key); memcpy(&(array[v].key), &temp, sizeof(KEY_TYPE)); } //Testcase doesn't sort NaNs; they just cause confusion if (!(array[v].key < 0.0) && !(0.0 < array[v].key)) array[v].key = 0.0; //Adding the data, in this case a string std::stringstream intstr; intstr << array[v].key; array[v].data = intstr.str(); ++v; } clock_t start, end; double elapsed; start = clock(); if (stdSort) std::sort(array.begin(), array.end(), lessthan()); else float_sort(array.begin(), array.end(), rightshift(), lessthan()); end = clock(); elapsed = static_cast(end - start) ; std::ofstream ofile; if (stdSort) ofile.open("standard_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); else ofile.open("boost_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); if (ofile.good()) { for (unsigned v = 0; v < array.size(); ++v) { ofile.write(reinterpret_cast(&(array[v].key)), sizeof(array[v].key)); ofile << array[v].data; } ofile.close(); } total += elapsed; array.clear(); } if (stdSort) printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC); else printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC); return 0; } ``` -------------------------------- ### Read Binary Data and Populate Vector in C++ Source: https://www.boost.org/doc/libs/latest/libs/sort/example/keyplusdatasample Reads binary integer data from input.txt file, converts each integer to a string, and populates a vector of DATA_TYPE structures. Handles file opening failures and supports multiple loop iterations for performance testing. ```cpp std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary); if (input.fail()) { printf("input.txt could not be opened\n"); return 1; } input.seekg (0, std::ios_base::end); size_t length = input.tellg(); std::vector array; array.reserve(length/uSize); unsigned uCount = length/uSize; for (unsigned u = 0; u < loopCount; ++u) { input.seekg (0, std::ios_base::beg); unsigned v = 0; while (input.good() && v++ < uCount) { DATA_TYPE element; input.read(reinterpret_cast(&(element.key)), sizeof(element.key)); std::stringstream intstr; intstr << element.key; element.data = intstr.str(); array.push_back(element); } } ``` -------------------------------- ### Custom Functor Examples for Boost.Sort (C++) Source: https://www.boost.org/doc/libs/latest/libs/sort/doc/html/sort/single_thread/spreadsort Provides example implementations of custom functors: 'lessthan' for general comparison, 'bracket' for accessing string characters by offset, and 'getsize' for retrieving string length. These functors can be used with Boost.Sort functions to customize sorting behavior. ```cpp struct lessthan { inline bool operator()(const DATA_TYPE &x, const DATA_TYPE &y) const { return x.a < y.a; } }; struct bracket { inline unsigned char operator()(const DATA_TYPE &x, size_t offset) const { return x.a[offset]; } }; struct getsize { inline size_t operator()(const DATA_TYPE &x) const{ return x.a.size(); } }; ``` -------------------------------- ### C++: Simple Struct Sort using Functors Source: https://www.boost.org/doc/libs/latest/libs/sort/doc/html/sort/single_thread/spreadsort/sort_hpp/string_sort This example demonstrates a basic usage of string_sort for sorting structs. It defines a simple 'lessthan' functor for comparison, a 'bracket' functor for accessing individual bytes (likely for byte-wise comparison), and a 'getsize' functor to determine the sortable size of the struct. This is a foundational example for custom sorting. ```cpp #include // Assuming DATA_TYPE is defined elsewhere, e.g., struct DATA_TYPE { std::string a; }; struct lessthan { inline bool operator()(const DATA_TYPE &x, const DATA_TYPE &y) const { return x.a < y.a; } }; struct bracket { inline unsigned char operator()(const DATA_TYPE &x, size_t offset) const { return x.a[offset]; } }; struct getsize { inline size_t operator()(const DATA_TYPE &x) const { return x.a.size(); } }; int main() { // Assuming 'array' is a collection of DATA_TYPE objects // boost::sort::string_sort(array.begin(), array.end(), bracket(), getsize(), lessthan()); return 0; } ``` -------------------------------- ### Write Sorted Results and Performance Metrics Source: https://www.boost.org/doc/libs/latest/libs/sort/example/reversestringfunctorsample Outputs sorted string data to either standard_sort_out.txt or boost_sort_out.txt depending on the sorting algorithm used. Calculates and displays total elapsed time in seconds for performance comparison between std::sort and spreadsort. ```cpp if (stdSort) outfile.open("standard_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); else outfile.open("boost_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); if (outfile.good()) { for (unsigned u = 0; u < array.size(); ++u) outfile << array[u].a << "\n"; outfile.close(); } if (stdSort) printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC); else printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC); ``` -------------------------------- ### Sort wstring data using Boost.Sort Spreadsort and std::sort Source: https://www.boost.org/doc/libs/latest/libs/sort/example/wstringsample This C++ code sorts a vector of wstrings. It can use either boost::sort::spreadsort::string_sort or std::sort based on a command-line argument. The data is read from 'input.txt' and the sorted output is written to a file. It measures and prints the sorting time. ```cpp // spreadsort wstring sorting example // // Copyright Steven Ross 2009-2014. // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // See http://www.boost.org/libs/sort for library home page. #include #include #include #include #include #include #include #include #include using std::wstring; using namespace boost::sort::spreadsort; #define DATA_TYPE wstring //Pass in an argument to test std::sort int main(int argc, const char ** argv) { std::ifstream indata; std::ofstream outfile; bool stdSort = false; unsigned loopCount = 1; for (int u = 1; u < argc; ++u) { if (std::string(argv[u]) == "-std") stdSort = true; else loopCount = atoi(argv[u]); } double total = 0.0; //Run multiple loops, if requested std::vector array; for (unsigned u = 0; u < loopCount; ++u) { indata.open("input.txt", std::ios_base::in | std::ios_base::binary); if (indata.bad()) { printf("input.txt could not be opened\n"); return 1; } unsigned short inval; DATA_TYPE current; while (indata.good()) { indata.read(reinterpret_cast(&inval), sizeof(inval)); current.push_back(inval); //32 characters is a moderately long string if (static_cast(current.size()) > inval || current.size() >= 32) { array.push_back(current); current.clear(); } } //adding the last string if (!current.empty()) array.push_back(current); indata.close(); clock_t start, end; double elapsed; start = clock(); wchar_t cast_type = 0; if (stdSort) //std::sort(&(array[0]), &(array[0]) + uCount); std::sort(array.begin(), array.end()); else //string_sort(&(array[0]), &(array[0]) + uCount, cast_type); string_sort(array.begin(), array.end(), cast_type); end = clock(); elapsed = static_cast(end - start); if (stdSort) outfile.open("standard_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); else outfile.open("boost_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); if (outfile.good()) { for (unsigned u = 0; u < array.size(); ++u){ for (unsigned v = 0; v < array[u].size(); ++v) outfile << array[u][v]; outfile << "\n"; } outfile.close(); } total += elapsed; array.clear(); } if (stdSort) printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC); else printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC); return 0; } ``` -------------------------------- ### Sort Data Using Spreadsort or std::sort in C++ Source: https://www.boost.org/doc/libs/latest/libs/sort/example/keyplusdatasample Performs sorting using either Boost spreadsort or std::sort based on command-line arguments. Measures elapsed time using clock() and writes sorted results to binary output files. Clears array after each iteration for repeated testing. ```cpp clock_t start, end; double elapsed; start = clock(); if (stdSort) std::sort(array.begin(), array.end(), lessthan()); else integer_sort(array.begin(), array.end(), rightshift(), lessthan()); end = clock(); elapsed = static_cast(end - start); std::ofstream ofile; if (stdSort) ofile.open("standard_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); else ofile.open("boost_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); if (ofile.good()) { for (unsigned v = 0; v < array.size(); ++v) { ofile.write(reinterpret_cast(&(array[v].key)), sizeof(array[v].key)); ofile << array[v].data; } ofile.close(); } total += elapsed; array.clear(); ``` -------------------------------- ### Complete Spreadsort Benchmark Program in C++ Source: https://www.boost.org/doc/libs/latest/libs/sort/example/rightshiftsample Full program that benchmarks integer sorting using Boost spreadsort with a rightshift functor against std::sort. Reads binary integer data from input.txt, performs sorting with optional loop counts and algorithm selection via command-line arguments (-std flag), measures elapsed time, and writes sorted results to output files. Supports performance comparison testing. ```cpp #include #include #include #include #include #include #include #include #include using namespace boost::sort::spreadsort; #define DATA_TYPE int struct rightshift { inline int operator()(DATA_TYPE x, unsigned offset) { return x >> offset; } }; int main(int argc, const char ** argv) { size_t uCount,uSize=sizeof(DATA_TYPE); bool stdSort = false; unsigned loopCount = 1; for (int u = 1; u < argc; ++u) { if (std::string(argv[u]) == "-std") stdSort = true; else loopCount = atoi(argv[u]); } std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary); if (input.fail()) { printf("input.txt could not be opened\n"); return 1; } double total = 0.0; std::vector array; input.seekg (0, std::ios_base::end); size_t length = input.tellg(); uCount = length/uSize; for (unsigned u = 0; u < loopCount; ++u) { input.seekg (0, std::ios_base::beg); array.resize(uCount); unsigned v = 0; while (input.good() && v < uCount) input.read(reinterpret_cast(&(array[v++])), uSize ); if (v < uCount) array.resize(v); clock_t start, end; double elapsed; start = clock(); if (stdSort) { std::sort(array.begin(), array.end()); } else { integer_sort(array.begin(), array.end(), rightshift()); } end = clock(); elapsed = static_cast(end - start) ; std::ofstream ofile; if (stdSort) ofile.open("standard_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); else ofile.open("boost_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); if (ofile.good()) { for (unsigned v = 0; v < array.size(); ++v) { ofile.write(reinterpret_cast(&(array[v])), sizeof(array[v]) ); } ofile.close(); } total += elapsed; array.clear(); } if (stdSort) printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC); else printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC); return 0; } ``` -------------------------------- ### Define Custom Data Structure and Comparison Functors in C++ Source: https://www.boost.org/doc/libs/latest/libs/sort/example/keyplusdatasample Defines a DATA_TYPE struct containing an integer key and string data, along with lessthan and rightshift functors required by spreadsort for custom comparison and bit extraction operations. ```cpp struct DATA_TYPE { int key; std::string data; }; struct lessthan { inline bool operator()(const DATA_TYPE &x, const DATA_TYPE &y) const { return x.key < y.key; } }; struct rightshift { inline int operator()(const DATA_TYPE &x, const unsigned offset) { return x.key >> offset; } }; ``` -------------------------------- ### Load Data and Execute Spreadsort Reverse String Sorting Source: https://www.boost.org/doc/libs/latest/libs/sort/example/reversestringfunctorsample Reads string data from input.txt into a vector, inserts embedded nulls and empty strings for edge case testing, then applies either boost::sort::spreadsort::reverse_string_sort or std::sort based on command-line arguments. Measures elapsed time and writes sorted results to output files. ```cpp std::vector array; for (unsigned u = 0; u < loopCount; ++u) { indata.open("input.txt", std::ios_base::in | std::ios_base::binary); if (!indata) { printf("input.txt could not be opened\n"); return 1; } DATA_TYPE inval; indata >> inval.a; while (!indata.eof()) { array.push_back(inval); if (!(array.size() % 100)) { if (inval.a.empty() || !(array.size() % 1000)) { inval.a = ""; array.push_back(inval); } else { inval.a[0] = '\0'; array.push_back(inval); } } indata >> inval.a; } indata.close(); clock_t start, end; double elapsed; start = clock(); if (stdSort) std::sort(array.begin(), array.end(), greaterthan()); else reverse_string_sort(array.begin(), array.end(), bracket(), getsize(), greaterthan()); end = clock(); elapsed = static_cast(end - start); total += elapsed; array.clear(); } ``` -------------------------------- ### Reverse Integer Sorting Comparison Source: https://www.boost.org/doc/libs/latest/libs/sort/example/reverseintsample Compares the performance of `std::sort` with `std::greater` against Boost's `integer_sort` using a custom `negrightshift` functor for reverse sorting. It reads data from 'input.txt', performs the sort, writes to 'standard_sort_out.txt' or 'boost_sort_out.txt', and prints the elapsed time. ```cpp //Pass in an argument to test std::sort int main(int argc, const char ** argv) { size_t uCount,uSize=sizeof(DATA_TYPE); bool stdSort = false; unsigned loopCount = 1; for (int u = 1; u < argc; ++u) { if (std::string(argv[u]) == "-std") stdSort = true; else loopCount = atoi(argv[u]); } std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary); if (input.fail()) { printf("input.txt could not be opened\n"); return 1; } double total = 0.0; std::vector array; input.seekg (0, std::ios_base::end); size_t length = input.tellg(); uCount = length/uSize; //Run multiple loops, if requested for (unsigned u = 0; u < loopCount; ++u) { input.seekg (0, std::ios_base::beg); //Conversion to a vector array.resize(uCount); unsigned v = 0; while (input.good() && v < uCount) input.read(reinterpret_cast(&(array[v++])), uSize ); if (v < uCount) array.resize(v); clock_t start, end; double elapsed; start = clock(); if (stdSort) //[reverse_int_1 std::sort(array.begin(), array.end(), std::greater()); //] [/reverse_int_1] else //[reverse_int_2 integer_sort(array.begin(), array.end(), negrightshift(), std::greater()); //] [/reverse_int_2] end = clock(); elapsed = static_cast(end - start) ; std::ofstream ofile; if (stdSort) ofile.open("standard_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); else ofile.open("boost_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); if (ofile.good()) { for (unsigned v = 0; v < array.size(); ++v) { ofile.write(reinterpret_cast(&(array[v])), sizeof(array[v])); } ofile.close(); } total += elapsed; array.clear(); } if (stdSort) printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC); else printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC); return 0; } ``` -------------------------------- ### Spreadsort and std::sort Comparison for Integer Data Source: https://www.boost.org/doc/libs/latest/libs/sort/example/sample This C++ code snippet demonstrates how to use boost::sort::spreadsort and std::sort to sort integer data read from a binary file. It measures and prints the elapsed time for each sorting method. The function takes command-line arguments to specify the number of iterations and whether to use std::sort. ```cpp #include #include #include #include #include #include #include #include #include #include using namespace boost::sort::spreadsort; #define DATA_TYPE int //Pass in an argument to test std::sort int main(int argc, const char ** argv) { size_t uCount,uSize=sizeof(DATA_TYPE); bool stdSort = false; unsigned loopCount = 1; for (int u = 1; u < argc; ++u) { if (std::string(argv[u]) == "-std") stdSort = true; else loopCount = atoi(argv[u]); } std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary); if (input.fail()) { printf("input.txt could not be opened\n"); return 1; } double total = 0.0; std::vector array; input.seekg (0, std::ios_base::end); size_t length = input.tellg(); uCount = length/uSize; //Run multiple loops, if requested for (unsigned u = 0; u < loopCount; ++u) { input.seekg (0, std::ios_base::beg); //Conversion to a vector array.resize(uCount); unsigned v = 0; while (input.good() && v < uCount) input.read(reinterpret_cast(&(array[v++])), uSize ); if (v < uCount) array.resize(v); clock_t start, end; double elapsed; start = clock(); if (stdSort) std::sort(array.begin(), array.end()); else boost::sort::spreadsort::spreadsort(array.begin(), array.end()); end = clock(); elapsed = static_cast(end - start); std::ofstream ofile; if (stdSort) ofile.open("standard_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); else ofile.open("boost_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); if (ofile.good()) { for (unsigned v = 0; v < array.size(); ++v) { ofile.write(reinterpret_cast(&(array[v])), sizeof(array[v]) ); } ofile.close(); } total += elapsed; array.clear(); } input.close(); if (stdSort) printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC); else printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC); return 0; } ``` -------------------------------- ### Sort strings using Boost spreadsort with std::sort comparison Source: https://www.boost.org/doc/libs/latest/libs/sort/example/stringsample A complete C++ program that reads strings from an input file, sorts them using either Boost spreadsort's string_sort or std::sort based on command-line arguments, and writes results to output files. The program supports multiple sorting iterations for performance benchmarking and measures elapsed time using clock() timing. ```cpp #include #include #include #include #include #include #include #include #include using std::string; using namespace boost::sort::spreadsort; #define DATA_TYPE string int main(int argc, const char ** argv) { std::ifstream indata; std::ofstream outfile; bool stdSort = false; unsigned loopCount = 1; for (int u = 1; u < argc; ++u) { if (std::string(argv[u]) == "-std") stdSort = true; else loopCount = atoi(argv[u]); } double total = 0.0; std::vector array; for (unsigned u = 0; u < loopCount; ++u) { indata.open("input.txt", std::ios_base::in | std::ios_base::binary); if (indata.bad()) { printf("input.txt could not be opened\n"); return 1; } DATA_TYPE inval; while (!indata.eof()) { indata >> inval; array.push_back(inval); } indata.close(); clock_t start, end; double elapsed; start = clock(); if (stdSort) std::sort(array.begin(), array.end()); else string_sort(array.begin(), array.end()); end = clock(); elapsed = static_cast(end - start); if (stdSort) outfile.open("standard_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); else outfile.open("boost_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); if (outfile.good()) { for (unsigned u = 0; u < array.size(); ++u) outfile << array[u] << "\n"; outfile.close(); } total += elapsed; array.clear(); } if (stdSort) printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC); else printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC); return 0; } ``` -------------------------------- ### Integer Sort Example using Boost Spreadsort Source: https://www.boost.org/doc/libs/latest/libs/sort/example/mostlysorted Demonstrates how to use Boost's integer_sort function to sort a vector of integers. This function is part of the spreadsort library and is optimized for integer types. It reads data from 'input.txt', sorts it, and writes the sorted data to 'boost_sort_out.txt'. ```cpp #include #include #include #include #include #include #include #include #include #include using namespace boost::sort::spreadsort; #define DATA_TYPE int unsigned get_index(unsigned count) { unsigned result = unsigned((rand() % (1 << 16))*uint64_t(count)/(1 << 16)); if (result >= count || result < 0) result = count - 1; return result; } //Pass in an argument to test std::sort int main(int argc, const char ** argv) { srand(1); size_t uCount,uSize=sizeof(DATA_TYPE); bool stdSort = false; unsigned loopCount = 1; for (int u = 1; u < argc; ++u) { if (std::string(argv[u]) == "-std") stdSort = true; else loopCount = atoi(argv[u]); } //Sorts the data once, then times sorting of already-sorted data loopCount += 1; std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary); if (input.fail()) { printf("input.txt could not be opened\n"); return 1; } double total = 0.0; std::vector array; input.seekg (0, std::ios_base::end); size_t length = input.tellg(); uCount = length/uSize; input.seekg (0, std::ios_base::beg); //Conversion to a vector array.resize(uCount); unsigned v = 0; while (input.good() && v < uCount) // EOF or failure stops the reading input.read(reinterpret_cast(&(array[v++])), uSize ); //Run multiple loops, if requested for (unsigned u = 0; u < loopCount; ++u) { for (unsigned v = 0; v < uCount/10; ++v) std::swap(array[get_index(uCount)], array[get_index(uCount)]); clock_t start, end; double elapsed; start = clock(); if (stdSort) //std::sort(&(array[0]), &(array[0]) + uCount); std::sort(array.begin(), array.end()); else //integer_sort(&(array[0]), &(array[0]) + uCount); integer_sort(array.begin(), array.end()); end = clock(); elapsed = static_cast(end - start) ; std::ofstream ofile; if (stdSort) ofile.open("standard_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); else ofile.open("boost_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); if (ofile.good()) { for (unsigned v = 0; v < array.size(); ++v) { ofile.write(reinterpret_cast(&(array[v])), sizeof(array[v]) ); } ofile.close(); } if (u) total += elapsed; } if (stdSort) printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC); else printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC); return 0; } ``` -------------------------------- ### Boost Sort Spreadsort 64-bit Integer Sorting Source: https://www.boost.org/doc/libs/latest/libs/sort/example/int64 This snippet demonstrates how to use Boost.Sort's spreadsort algorithm to sort a vector of 64-bit integers. It reads data from 'input.txt', performs the sort, and writes the results to 'boost_sort_out.txt'. The code includes timing mechanisms and an option to use std::sort for comparison. ```cpp #include #include #include #include #include #include #include #include #include #include using namespace boost::sort::spreadsort; //[int64bit_1 #define DATA_TYPE boost::int64_t //] [/int64bit_1] //Pass in an argument to test std::sort int main(int argc, const char ** argv) { size_t uCount,uSize=sizeof(DATA_TYPE); bool stdSort = false; unsigned loopCount = 1; for (int u = 1; u < argc; ++u) { if (std::string(argv[u]) == "-std") stdSort = true; else loopCount = atoi(argv[u]); } std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary); if (input.fail()) { printf("input.txt could not be opened\n"); return 1; } double total = 0.0; std::vector array; input.seekg (0, std::ios_base::end); size_t length = input.tellg(); uCount = length/uSize; //Run multiple loops, if requested for (unsigned u = 0; u < loopCount; ++u) { input.seekg (0, std::ios_base::beg); //Conversion to a vector array.resize(uCount); unsigned v = 0; while (input.good() && v < uCount) input.read(reinterpret_cast(&(array[v++])), uSize ); if (v < uCount) array.resize(v); clock_t start, end; double elapsed; start = clock(); if (stdSort) //std::sort(&(array[0]), &(array[0]) + uCount); std::sort(array.begin(), array.end()); else //boost::sort::spreadsort::spreadsort(&(array[0]), &(array[0]) + uCount); //[int64bit_2 boost::sort::spreadsort::spreadsort(array.begin(), array.end()); //] [/int64bit_2] end = clock(); elapsed = static_cast(end - start) ; std::ofstream ofile; if (stdSort) ofile.open("standard_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); else ofile.open("boost_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); if (ofile.good()) { for (unsigned v = 0; v < array.size(); ++v) { ofile.write(reinterpret_cast(&(array[v])), sizeof(array[v]) ); } ofile.close(); } total += elapsed; array.clear(); } input.close(); if (stdSort) printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC); else printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC); return 0; } ``` -------------------------------- ### float_sort Basic Usage Example with Vector Source: https://www.boost.org/doc/libs/latest/libs/sort/doc/html/doxygen/boost_sort_c___reference/namespaceboost_1_1sort_1_1spreadsort_1a48eb6c87a87c6a13048e60ed7595d32f Demonstrates sorting a vector of floating-point numbers using the spreadsort function. Creates a vector with unsorted float values, then applies the spreadsort algorithm to arrange them in ascending order. The result is a sorted vector containing the values in ascending sequence. ```cpp vector vec; vec.push_back(1.0); vec.push_back(2.3); vec.push_back(1.3); spreadsort(vec.begin(), vec.end()); ``` -------------------------------- ### Boost Spreadsort and std::sort for Doubles Source: https://www.boost.org/doc/libs/latest/libs/sort/example/double This C++ code sorts a vector of doubles read from 'input.txt' using either Boost's spreadsort or std::sort, based on command-line arguments. It handles denormalized numbers and NaNs, then writes the sorted data to 'boost_sort_out.txt' or 'standard_sort_out.txt'. Input data is read in binary format. Performance timing is included. ```cpp // spreadsort double sorting example. // // Copyright Steven Ross 2009-2014. // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // See http://www.boost.org/libs/sort for library home page. #include #include #include #include #include #include #include #include #include using namespace boost::sort::spreadsort; #define DATA_TYPE double #define CAST_TYPE boost::int64_t //Pass in an argument to test std::sort //Note that this converts NaNs and -0.0 to 0.0, so that sorting results are //identical every time int main(int argc, const char ** argv) { size_t uCount,uSize=sizeof(DATA_TYPE); bool stdSort = false; unsigned loopCount = 1; for (int u = 1; u < argc; ++u) { if (std::string(argv[u]) == "-std") stdSort = true; else loopCount = atoi(argv[u]); } std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary); if (input.fail()) { printf("input.txt could not be opened\n"); return 1; } double total = 0.0; std::vector array; input.seekg (0, std::ios_base::end); size_t length = input.tellg(); uCount = length/uSize; //Using this to support compilers that don't support 64-bit constants CAST_TYPE exponent_mask = 0x7ff00000; exponent_mask <<= 32; CAST_TYPE top_exponent_bit = 0x40000000; top_exponent_bit <<= 32; //Run multiple loops, if requested for (unsigned u = 0; u < loopCount; ++u) { input.seekg (0, std::ios_base::beg); //Conversion to a vector array.resize(uCount); unsigned v = 0; while (input.good() && v < uCount) { input.read(reinterpret_cast(&(array[v])), uSize ); //Checking for denormalized numbers if (!(float_mem_cast(array[v]) & exponent_mask)) { //Make the top exponent bit high CAST_TYPE temp = top_exponent_bit | float_mem_cast(array[v]); memcpy(&(array[v]), &temp, sizeof(DATA_TYPE)); } //Testcase doesn't sort NaNs; they just cause confusion if (!(array[v] < 0.0) && !(0.0 < array[v])) array[v] = 0.0; ++v; } clock_t start, end; double elapsed; start = clock(); if (stdSort) //std::sort(&(array[0]), &(array[0]) + uCount); std::sort(array.begin(), array.end()); else //float_sort(&(array[0]), &(array[0]) + uCount); float_sort(array.begin(), array.end()); end = clock(); elapsed = static_cast(end - start) ; std::ofstream ofile; if (stdSort) ofile.open("standard_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); else ofile.open("boost_sort_out.txt", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); if (ofile.good()) { for (unsigned v = 0; v < array.size(); ++v) { ofile.write(reinterpret_cast(&(array[v])), sizeof(array[v]) ); } ofile.close(); } total += elapsed; array.clear(); } if (stdSort) printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC); else printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC); return 0; } ```