1// spreadsort wstring sorting example
2//
3// Copyright Steven Ross 2009-2014.
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8
9// See http://www.boost.org/libs/sort for library home page.
10
11#include <boost/sort/spreadsort/string_sort.hpp>
12#include <time.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <algorithm>
16#include <vector>
17#include <iostream>
18#include <fstream>
19#include <string>
20using std::wstring;
21using namespace boost::sort::spreadsort;
22
23#define DATA_TYPE wstring
24
25//Pass in an argument to test std::sort
26int main(int argc, const char ** argv) {
27 std::ifstream indata;
28 std::ofstream outfile;
29 bool stdSort = false;
30 unsigned loopCount = 1;
31 for (int u = 1; u < argc; ++u) {
32 if (std::string(argv[u]) == "-std")
33 stdSort = true;
34 else
35 loopCount = atoi(nptr: argv[u]);
36 }
37 double total = 0.0;
38 //Run multiple loops, if requested
39 std::vector<DATA_TYPE> array;
40 for (unsigned u = 0; u < loopCount; ++u) {
41 indata.open(s: "input.txt", mode: std::ios_base::in | std::ios_base::binary);
42 if (indata.bad()) {
43 printf(format: "input.txt could not be opened\n");
44 return 1;
45 }
46 unsigned short inval;
47 DATA_TYPE current;
48 while (indata.good()) {
49 indata.read(s: reinterpret_cast<char *>(&inval), n: sizeof(inval));
50 current.push_back(c: inval);
51 //32 characters is a moderately long string
52 if (static_cast<int>(current.size()) > inval || current.size() >= 32) {
53 array.push_back(x: current);
54 current.clear();
55 }
56 }
57 //adding the last string
58 if (!current.empty())
59 array.push_back(x: current);
60
61 indata.close();
62 clock_t start, end;
63 double elapsed;
64 start = clock();
65 wchar_t cast_type = 0;
66 if (stdSort)
67 //std::sort(&(array[0]), &(array[0]) + uCount);
68 std::sort(first: array.begin(), last: array.end());
69 else
70 //string_sort(&(array[0]), &(array[0]) + uCount, cast_type);
71 string_sort(first: array.begin(), last: array.end(), unused: cast_type);
72 end = clock();
73 elapsed = static_cast<double>(end - start);
74 if (stdSort)
75 outfile.open(s: "standard_sort_out.txt", mode: std::ios_base::out |
76 std::ios_base::binary | std::ios_base::trunc);
77 else
78 outfile.open(s: "boost_sort_out.txt", mode: std::ios_base::out |
79 std::ios_base::binary | std::ios_base::trunc);
80 if (outfile.good()) {
81 for (unsigned u = 0; u < array.size(); ++u){
82 for (unsigned v = 0; v < array[u].size(); ++v)
83 outfile << array[u][v];
84 outfile << "\n";
85 }
86 outfile.close();
87 }
88 total += elapsed;
89 array.clear();
90 }
91 if (stdSort)
92 printf(format: "std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
93 else
94 printf(format: "spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
95 return 0;
96}
97

source code of boost/libs/sort/example/wstringsample.cpp