-// spreadsort float functor sorting example.
-//
-// Copyright Steven Ross 2009.
-//
-// 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.
-
-// Caution: this file contains Quickbook markup as well as code
-// and comments, don't change any of the special comment markups!
-
-#include <boost/sort/spreadsort/spreadsort.hpp>
-#include <time.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <algorithm>
-#include <vector>
-#include <string>
-#include <fstream>
-#include <sstream>
-#include <iostream>
-
-using namespace boost::sort::spreadsort;
-
-//[float_functor_types
-#define CAST_TYPE int
-#define KEY_TYPE float
-//] [/float_functor_types]
-
-
-//[float_functor_datatypes
-struct DATA_TYPE {
- KEY_TYPE key;
- std::string data;
-};
-//] [/float_functor_datatypes]
-
-
-//[float_functor_rightshift
-// Casting to an integer before bitshifting
-struct rightshift {
- int operator()(const DATA_TYPE &x, const unsigned offset) const {
- return float_mem_cast<KEY_TYPE, CAST_TYPE>(x.key) >> offset;
- }
-};
-//] [/float_functor_rightshift]
-
-//[float_functor_lessthan
-struct lessthan {
- bool operator()(const DATA_TYPE &x, const DATA_TYPE &y) const {
- return x.key < y.key;
- }
-};
-//] [/float_functor_lessthan]
-
-// 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<DATA_TYPE> 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<char *>(&(array[v].key)),
- sizeof(array[v].key));
- //Checking for denormalized numbers; float_sort looks too fast on them.
- if (!(float_mem_cast<KEY_TYPE, CAST_TYPE>(array[v].key) & 0x7f800000)) {
- //Make the top exponent bit high
- CAST_TYPE temp = 0x40000000 |
- float_mem_cast<KEY_TYPE, CAST_TYPE>(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<double>(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<char *>(&(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;
-}
+// spreadsort float functor sorting example.\r
+//\r
+// Copyright Steven Ross 2009.\r
+//\r
+// Distributed under the Boost Software License, Version 1.0.\r
+// (See accompanying file LICENSE_1_0.txt or copy at\r
+// http://www.boost.org/LICENSE_1_0.txt)\r
+\r
+// See http://www.boost.org/libs/sort for library home page.\r
+\r
+// Caution: this file contains Quickbook markup as well as code\r
+// and comments, don't change any of the special comment markups!\r
+\r
+#include <boost/sort/spreadsort/spreadsort.hpp>\r
+#include <time.h>\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <algorithm>\r
+#include <vector>\r
+#include <string>\r
+#include <fstream>\r
+#include <sstream>\r
+#include <iostream>\r
+\r
+using namespace boost::sort::spreadsort;\r
+\r
+//[float_functor_types\r
+#define CAST_TYPE int\r
+#define KEY_TYPE float\r
+//] [/float_functor_types]\r
+\r
+\r
+//[float_functor_datatypes\r
+struct DATA_TYPE {\r
+ KEY_TYPE key;\r
+ std::string data;\r
+};\r
+//] [/float_functor_datatypes]\r
+\r
+\r
+//[float_functor_rightshift\r
+// Casting to an integer before bitshifting\r
+struct rightshift {\r
+ int operator()(const DATA_TYPE &x, const unsigned offset) const {\r
+ return float_mem_cast<KEY_TYPE, CAST_TYPE>(x.key) >> offset;\r
+ }\r
+};\r
+//] [/float_functor_rightshift]\r
+\r
+//[float_functor_lessthan\r
+struct lessthan {\r
+ bool operator()(const DATA_TYPE &x, const DATA_TYPE &y) const {\r
+ return x.key < y.key;\r
+ }\r
+};\r
+//] [/float_functor_lessthan]\r
+\r
+// Pass in an argument to test std::sort\r
+// Note that this converts NaNs and -0.0 to 0.0, so that sorting results are\r
+// identical every time\r
+int main(int argc, const char ** argv) {\r
+ size_t uCount,uSize=sizeof(DATA_TYPE);\r
+ bool stdSort = false;\r
+ unsigned loopCount = 1;\r
+ for (int u = 1; u < argc; ++u) {\r
+ if (std::string(argv[u]) == "-std")\r
+ stdSort = true;\r
+ else\r
+ loopCount = atoi(argv[u]);\r
+ }\r
+ std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);\r
+ if (input.fail()) {\r
+ printf("input.txt could not be opened\n");\r
+ return 1;\r
+ }\r
+ double total = 0.0;\r
+ std::vector<DATA_TYPE> array;\r
+ input.seekg (0, std::ios_base::end);\r
+ size_t length = input.tellg();\r
+ uCount = length/uSize;\r
+ //Run multiple loops, if requested\r
+ for (unsigned u = 0; u < loopCount; ++u) {\r
+ input.seekg (0, std::ios_base::beg);\r
+ //Conversion to a vector\r
+ array.resize(uCount);\r
+ unsigned v = 0;\r
+ while (input.good() && v < uCount) {\r
+ input.read(reinterpret_cast<char *>(&(array[v].key)),\r
+ sizeof(array[v].key));\r
+ //Checking for denormalized numbers; float_sort looks too fast on them.\r
+ if (!(float_mem_cast<KEY_TYPE, CAST_TYPE>(array[v].key) & 0x7f800000)) {\r
+ //Make the top exponent bit high\r
+ CAST_TYPE temp = 0x40000000 |\r
+ float_mem_cast<KEY_TYPE, CAST_TYPE>(array[v].key);\r
+ memcpy(&(array[v].key), &temp, sizeof(KEY_TYPE));\r
+ }\r
+ //Testcase doesn't sort NaNs; they just cause confusion\r
+ if (!(array[v].key < 0.0) && !(0.0 < array[v].key))\r
+ array[v].key = 0.0;\r
+ //Adding the data, in this case a string\r
+ std::stringstream intstr;\r
+ intstr << array[v].key;\r
+ array[v].data = intstr.str();\r
+ ++v;\r
+ }\r
+ clock_t start, end;\r
+ double elapsed;\r
+ start = clock();\r
+ if (stdSort)\r
+ std::sort(array.begin(), array.end(), lessthan());\r
+ else\r
+ float_sort(array.begin(), array.end(), rightshift(), lessthan());\r
+ end = clock();\r
+ elapsed = static_cast<double>(end - start) ;\r
+ std::ofstream ofile;\r
+ if (stdSort)\r
+ ofile.open("standard_sort_out.txt", std::ios_base::out |\r
+ std::ios_base::binary | std::ios_base::trunc);\r
+ else\r
+ ofile.open("boost_sort_out.txt", std::ios_base::out |\r
+ std::ios_base::binary | std::ios_base::trunc);\r
+ if (ofile.good()) {\r
+ for (unsigned v = 0; v < array.size(); ++v) {\r
+ ofile.write(reinterpret_cast<char *>(&(array[v].key)),\r
+ sizeof(array[v].key));\r
+ ofile << array[v].data;\r
+ }\r
+ ofile.close();\r
+ }\r
+ total += elapsed;\r
+ array.clear();\r
+ }\r
+ if (stdSort)\r
+ printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);\r
+ else\r
+ printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);\r
+ return 0;\r
+}\r