summaryrefslogtreecommitdiff
path: root/libs/sort/example/reverseintsample.cpp
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@baserock.org>2015-04-08 03:09:47 +0000
committer <>2015-05-05 14:37:32 +0000
commitf2541bb90af059680aa7036f315f052175999355 (patch)
treea5b214744b256f07e1dc2bd7273035a7808c659f /libs/sort/example/reverseintsample.cpp
parented232fdd34968697a68783b3195b1da4226915b5 (diff)
downloadboost-tarball-master.tar.gz
Imported from /home/lorry/working-area/delta_boost-tarball/boost_1_58_0.tar.bz2.HEADboost_1_58_0master
Diffstat (limited to 'libs/sort/example/reverseintsample.cpp')
-rw-r--r--libs/sort/example/reverseintsample.cpp101
1 files changed, 101 insertions, 0 deletions
diff --git a/libs/sort/example/reverseintsample.cpp b/libs/sort/example/reverseintsample.cpp
new file mode 100644
index 000000000..fe31edf8c
--- /dev/null
+++ b/libs/sort/example/reverseintsample.cpp
@@ -0,0 +1,101 @@
+//! \file
+//! \brief integer sort with a rightshift functor reverse 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.
+
+// 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 <iostream>
+#include <functional>
+using namespace boost::sort::spreadsort;
+
+#define DATA_TYPE int
+
+struct negrightshift {
+ inline int operator()(const int &x, const unsigned offset) {
+ return -(x >> offset);
+ }
+};
+
+//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<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++])), 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<DATA_TYPE>());
+//] [/reverse_int_1]
+ else
+//[reverse_int_2
+ integer_sort(array.begin(), array.end(), negrightshift(), std::greater<DATA_TYPE>());
+//] [/reverse_int_2]
+ 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])), 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;
+}