Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ exe alrbreaker : alrbreaker.cpp ;
exe binaryalrbreaker : binaryalrbreaker.cpp ;
exe caseinsensitive : caseinsensitive.cpp ;
exe generalizedstruct : generalizedstruct.cpp ;
exe timsort : timsortsample.cpp ;

# benchmarks need to be built with linkflags="-lboost_system -lboost_thread"
#exe parallelint : parallelint.cpp boost_system ;
Expand Down
5 changes: 5 additions & 0 deletions doc/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,8 @@ explicit images ;
# : inspect # test name
# ;

###############################################################################
alias boostdoc ;
explicit boostdoc ;
alias boostrelease : standalone ;
explicit boostrelease ;
89 changes: 89 additions & 0 deletions example/timsortalreadysorted.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.com>, 2016
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/ for latest version.
*/

#include <boost/sort/timsort.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;

#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]);
}
//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<DATA_TYPE> 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<char *>(&(array[v++])), uSize );
//Run multiple loops, if requested
for (unsigned u = 0; u < loopCount; ++u) {
clock_t start, end;
double elapsed;
start = clock();
if (stdSort)
//std::sort(&(array[0]), &(array[0]) + uCount);
std::sort(array.begin(), array.end());
else {
printf("call\n");
//integer_sort(&(array[0]), &(array[0]) + uCount);
timsort(array.begin(), array.end());
}
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();
}
if (u)
total += elapsed;
}
if (stdSort)
printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
else
printf("timsort elapsed time %f\n", total / CLOCKS_PER_SEC);
return 0;
}
97 changes: 97 additions & 0 deletions example/timsortmostlysorted.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.com>, 2016
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/ for latest version.
*/

#include <boost/sort/timsort.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;

#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 mostly-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<DATA_TYPE> 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<char *>(&(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.begin(), array.end());
else
timsort(array.begin(), array.end());
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();
}
if (u)
total += elapsed;
}
if (stdSort)
printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
else
printf("timsort elapsed time %f\n", total / CLOCKS_PER_SEC);
return 0;
}

92 changes: 92 additions & 0 deletions example/timsortsample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.com>, 2016
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/ for latest version.
*/

#include <boost/sort/timsort.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;

#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<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)
std::sort(array.begin(), array.end());
else
boost::sort::timsort(array.begin(), array.end());
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();
}
input.close();
if (stdSort)
printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
else
printf("timsort elapsed time %f\n", total / CLOCKS_PER_SEC);
return 0;
}
42 changes: 42 additions & 0 deletions include/boost/sort/spreadsort/float_sort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Scott McMurray
#include <boost/static_assert.hpp>
#include <boost/sort/spreadsort/detail/constants.hpp>
#include <boost/sort/spreadsort/detail/float_sort.hpp>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>

namespace boost {
namespace sort {
Expand Down Expand Up @@ -90,6 +92,18 @@ Some performance plots of runtime vs. n and log(range) are provided:\n
detail::float_sort(first, last);
}

/*!
\brief Floating-point sort algorithm using range.

\param[in] range Range [first, last) for sorting.

*/
template <class Range>
inline void float_sort(Range& range)
{
float_sort(boost::begin(range), boost::end(range));
}

/*!
\brief Floating-point sort algorithm using random access iterators with just right-shift functor.

Expand All @@ -108,6 +122,19 @@ Some performance plots of runtime vs. n and log(range) are provided:\n
detail::float_sort(first, last, rshift(*first, 0), rshift);
}

/*!
\brief Floating-point sort algorithm using range with just right-shift functor.

\param[in] range Range [first, last) for sorting.
\param[in] rshift Functor that returns the result of shifting the value_type right a specified number of bits.

*/
template <class Range, class Right_shift>
inline void float_sort(Range& range, Right_shift rshift)
{
float_sort(boost::begin(range), boost::end(range), rshift);
}


/*!
\brief Float sort algorithm using random access iterators with both right-shift and user-defined comparison operator.
Expand All @@ -127,6 +154,21 @@ Some performance plots of runtime vs. n and log(range) are provided:\n
else
detail::float_sort(first, last, rshift(*first, 0), rshift, comp);
}


/*!
\brief Float sort algorithm using range with both right-shift and user-defined comparison operator.

\param[in] range Range [first, last) for sorting.
\param[in] rshift Functor that returns the result of shifting the value_type right a specified number of bits.
\param[in] comp A binary functor that returns whether the first element passed to it should go before the second in order.
*/

template <class Range, class Right_shift, class Compare>
inline void float_sort(Range& range, Right_shift rshift, Compare comp)
{
float_sort(boost::begin(range), boost::end(range), rshift, comp);
}
}
}
}
Expand Down
Loading