]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/sort/example/caseinsensitive.cpp
1 // Example of sorting a struct using a case-insensitive string key.
3 // Copyright Steven Ross 2009-2014.
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)
9 // See http://www.boost.org/libs/sort for library home page.
11 #include <boost/algorithm/string.hpp>
12 #include <boost/sort/spreadsort/string_sort.hpp>
22 using namespace boost::sort::spreadsort
;
29 inline bool operator()(const DATA_TYPE
&x
, const DATA_TYPE
&y
) const {
30 return boost::algorithm::ilexicographical_compare(x
.a
, y
.a
);
35 inline unsigned char operator()(const DATA_TYPE
&x
, size_t offset
) const {
36 return toupper(x
.a
[offset
]);
41 inline size_t operator()(const DATA_TYPE
&x
) const{ return x
.a
.size(); }
44 //Pass in an argument to test std::sort
45 int main(int argc
, const char ** argv
) {
47 std::ofstream outfile
;
49 unsigned loopCount
= 1;
50 for (int u
= 1; u
< argc
; ++u
) {
51 if (std::string(argv
[u
]) == "-std")
54 loopCount
= atoi(argv
[u
]);
57 //Run multiple loops, if requested
58 std::vector
<DATA_TYPE
> array
;
59 for (unsigned u
= 0; u
< loopCount
; ++u
) {
60 indata
.open("input.txt", std::ios_base::in
| std::ios_base::binary
);
62 printf("input.txt could not be opened\n");
67 while (!indata
.eof() ) {
68 array
.push_back(inval
);
77 std::sort(array
.begin(), array
.end(), lessthan());
79 string_sort(array
.begin(), array
.end(), bracket(), getsize(), lessthan());
81 elapsed
= static_cast<double>(end
- start
);
83 outfile
.open("standard_sort_out.txt", std::ios_base::out
|
84 std::ios_base::binary
| std::ios_base::trunc
);
86 outfile
.open("boost_sort_out.txt", std::ios_base::out
|
87 std::ios_base::binary
| std::ios_base::trunc
);
89 for (unsigned u
= 0; u
< array
.size(); ++u
)
90 outfile
<< array
[u
].a
<< "\n";
97 printf("std::sort elapsed time %f\n", total
/ CLOCKS_PER_SEC
);
99 printf("spreadsort elapsed time %f\n", total
/ CLOCKS_PER_SEC
);