Skip to content
Merged
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
3 changes: 2 additions & 1 deletion include/boost/sort/common/merge_four.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <vector>
#include <boost/sort/common/util/traits.hpp>
#include <boost/sort/common/range.hpp>
#include <boost/core/addressof.hpp>


namespace boost
Expand Down Expand Up @@ -275,7 +276,7 @@ range<Value_t *> uninit_full_merge4(const range<Value_t *> &dest,
Value_t *it_dest = dest.first;
while (npos > 2)
{
util::construct_object(&(*(it_dest++)),
util::construct_object(boost::addressof(*(it_dest++)),
std::move(*(vrange_input[pos[0]].first++)));
if (vrange_input[pos[0]].size() == 0)
{
Expand Down
7 changes: 4 additions & 3 deletions include/boost/sort/common/util/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <type_traits>
#include <vector>
#include <boost/sort/common/util/traits.hpp>
#include <boost/core/addressof.hpp>

namespace boost
{
Expand Down Expand Up @@ -179,12 +180,12 @@ inline void initialize (Iter_t first, Iter_t last, Value_t & val)
// Code
//------------------------------------------------------------------------
if (first == last) return;
construct_object(&(*first), std::move(val));
construct_object(boost::addressof(*first), std::move(val));

Iter_t it1 = first, it2 = first + 1;
while (it2 != last)
{
construct_object(&(*(it2++)), std::move(*(it1++)));
construct_object(boost::addressof(*(it2++)), std::move(*(it1++)));
}
val = std::move(*(last - 1));
}
Expand Down Expand Up @@ -285,7 +286,7 @@ template<class Iter_t>
inline void destroy(Iter_t first, const Iter_t last)
{
while (first != last)
destroy_object(&(*(first++)));
destroy_object(boost::addressof(*(first++)));
}
//
//-----------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion include/boost/sort/spreadsort/detail/float_sort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Scott McMurray
#include <vector>
#include <limits>
#include <functional>
#include <boost/core/addressof.hpp>
#include <boost/static_assert.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/sort/spreadsort/detail/constants.hpp>
Expand All @@ -43,7 +44,7 @@ namespace spreadsort {
BOOST_STATIC_ASSERT(std::numeric_limits<Data_type>::is_iec559);
BOOST_STATIC_ASSERT(std::numeric_limits<Cast_type>::is_integer);
Cast_type result;
std::memcpy(&result, &(*floatiter), sizeof(Data_type));
std::memcpy(&result, boost::addressof(*floatiter), sizeof(Data_type));
return result;
}

Expand Down
3 changes: 2 additions & 1 deletion include/boost/sort/spreadsort/detail/spreadsort_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Phil Endecott and Frank Gennari
#include <cstring>
#include <limits>
#include <functional>
#include <boost/core/addressof.hpp>
#include <boost/static_assert.hpp>
#include <boost/sort/pdqsort/pdqsort.hpp>
#include <boost/sort/spreadsort/detail/constants.hpp>
Expand Down Expand Up @@ -114,7 +115,7 @@ namespace spreadsort {
cache_end = cache_offset + bin_count;
if (cache_end > bin_cache.size())
bin_cache.resize(cache_end);
return &(bin_cache[cache_offset]);
return boost::addressof(bin_cache[cache_offset]);
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions include/boost/sort/spreadsort/detail/string_sort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Phil Endecott and Frank Gennari
#include <cstring>
#include <limits>
#include <functional>
#include <boost/core/addressof.hpp>
#include <boost/static_assert.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/sort/spreadsort/detail/constants.hpp>
Expand Down Expand Up @@ -207,7 +208,7 @@ namespace spreadsort {
//Swap into place
RandomAccessIter next_bin_start = first;
//handling empty bins
RandomAccessIter * local_bin = &(bin_cache[cache_offset]);
RandomAccessIter * local_bin = boost::addressof(bin_cache[cache_offset]);
next_bin_start += bin_sizes[0];
RandomAccessIter * target_bin;
//Iterating over each element in the bin of empties
Expand Down Expand Up @@ -296,7 +297,7 @@ namespace spreadsort {
unsigned cache_end;
RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset,
cache_end, membin_count);
RandomAccessIter * end_bin = &(bin_cache[cache_offset + max_bin]);
RandomAccessIter * end_bin = boost::addressof(bin_cache[cache_offset + max_bin]);

//Calculating the size of each bin; this takes roughly 10% of runtime
for (RandomAccessIter current = first; current != last; ++current) {
Expand All @@ -316,7 +317,7 @@ namespace spreadsort {
//Swap into place
RandomAccessIter next_bin_start = last;
//handling empty bins
RandomAccessIter * local_bin = &(bin_cache[cache_offset + bin_count]);
RandomAccessIter * local_bin = boost::addressof(bin_cache[cache_offset + bin_count]);
RandomAccessIter lastFull = *local_bin;
//Iterating over each element in the bin of empties
for (RandomAccessIter current = *local_bin; current < next_bin_start;
Expand Down Expand Up @@ -420,7 +421,7 @@ namespace spreadsort {
//Swap into place
RandomAccessIter next_bin_start = first;
//handling empty bins
RandomAccessIter * local_bin = &(bin_cache[cache_offset]);
RandomAccessIter * local_bin = boost::addressof(bin_cache[cache_offset]);
next_bin_start += bin_sizes[0];
RandomAccessIter * target_bin;
//Iterating over each element in the bin of empties
Expand Down Expand Up @@ -521,7 +522,7 @@ namespace spreadsort {
//Swap into place
RandomAccessIter next_bin_start = first;
//handling empty bins
RandomAccessIter * local_bin = &(bin_cache[cache_offset]);
RandomAccessIter * local_bin = boost::addressof(bin_cache[cache_offset]);
next_bin_start += bin_sizes[0];
RandomAccessIter * target_bin;
//Iterating over each element in the bin of empties
Expand Down Expand Up @@ -607,7 +608,7 @@ namespace spreadsort {
unsigned cache_end;
RandomAccessIter * bins = size_bins(bin_sizes, bin_cache, cache_offset,
cache_end, membin_count);
RandomAccessIter *end_bin = &(bin_cache[cache_offset + max_bin]);
RandomAccessIter *end_bin = boost::addressof(bin_cache[cache_offset + max_bin]);

//Calculating the size of each bin; this takes roughly 10% of runtime
for (RandomAccessIter current = first; current != last; ++current) {
Expand All @@ -626,7 +627,7 @@ namespace spreadsort {
//Swap into place
RandomAccessIter next_bin_start = last;
//handling empty bins
RandomAccessIter * local_bin = &(bin_cache[cache_offset + bin_count]);
RandomAccessIter * local_bin = boost::addressof(bin_cache[cache_offset + bin_count]);
RandomAccessIter lastFull = *local_bin;
RandomAccessIter * target_bin;
//Iterating over each element in the bin of empties
Expand Down
3 changes: 2 additions & 1 deletion include/boost/sort/spreadsort/float_sort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Scott McMurray
#include <vector>
#include <cstring>
#include <limits>
#include <boost/core/addressof.hpp>
#include <boost/static_assert.hpp>
#include <boost/sort/spreadsort/detail/constants.hpp>
#include <boost/sort/spreadsort/detail/float_sort.hpp>
Expand Down Expand Up @@ -54,7 +55,7 @@ namespace spreadsort {
BOOST_STATIC_ASSERT(std::numeric_limits<Data_type>::is_iec559);
BOOST_STATIC_ASSERT(std::numeric_limits<Cast_type>::is_integer);
Cast_type result;
std::memcpy(&result, &data, sizeof(Cast_type));
std::memcpy(&result, boost::addressof(data), sizeof(Cast_type));
return result;
}

Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ boost_sort_add_test(string_sort_test string_sort_test.cpp)
boost_sort_add_test(test_block_indirect_sort test_block_indirect_sort.cpp)
boost_sort_add_test(test_flat_stable_sort test_flat_stable_sort.cpp)
boost_sort_add_test(test_insert_sort test_insert_sort.cpp)
boost_sort_add_test(test_overloaded_address test_overloaded_address.cpp)
boost_sort_add_test(test_parallel_stable_sort test_parallel_stable_sort.cpp)
boost_sort_add_test(test_pdqsort test_pdqsort.cpp)
boost_sort_add_test(test_sample_sort test_sample_sort.cpp)
Expand Down
7 changes: 7 additions & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,12 @@ project : requirements
cxx11_noexcept
cxx11_thread_local
cxx11_lambdas ] <optimization>speed <threading>multi : test_parallel_stable_sort ]

[ run test_overloaded_address.cpp
: : : [ requires
cxx11_constexpr
cxx11_noexcept
cxx11_thread_local
cxx11_lambdas ] <optimization>speed <threading>multi : test_overloaded_address ]
;
}
186 changes: 186 additions & 0 deletions test/test_overloaded_address.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// Copyright Steven J. Ross 2014, Francisco Jose Tapia 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)

#include <algorithm>
#include <cstddef>
#include <iterator>
#include <random>
#include <vector>

#include <boost/sort/common/merge_four.hpp>
#include <boost/sort/common/range.hpp>
#include <boost/sort/common/util/algorithm.hpp>
#include <boost/sort/sort.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
#include <boost/test/test_tools.hpp>

namespace {

// Type with deleted operator& to ensure Boost.Sort algorithms do not use raw unary &.
struct DeletedAddress {
int value;

explicit DeletedAddress(int v = 0) : value(v) {}

bool operator<(const DeletedAddress& other) const {
return value < other.value;
}
bool operator==(const DeletedAddress& other) const {
return value == other.value;
}

DeletedAddress* operator&() = delete;
const DeletedAddress* operator&() const = delete;
};

// Functor for integer_sort with types overloading operator&.
struct IntRightShift {
int operator()(const DeletedAddress& x, unsigned offset) const {
return x.value >> offset;
}
};

// Tests low-level memory utilities in common/util/algorithm.hpp
// (initialize and destroy) where raw & was previously used.
void TestCommonUtilAlgorithm() {
const size_t kCount = 16;
alignas(DeletedAddress) char raw_memory[sizeof(DeletedAddress) * kCount];
DeletedAddress* ptr = reinterpret_cast<DeletedAddress*>(raw_memory);

DeletedAddress init_val(42);
boost::sort::common::util::initialize(ptr, ptr + kCount, init_val);
for (size_t i = 0; i < kCount; ++i) {
BOOST_CHECK_EQUAL(ptr[i].value, 42);
}
boost::sort::common::util::destroy(ptr, ptr + kCount);
}

// Tests uninit_full_merge4 in common/merge_four.hpp where construct_object
// previously used raw &(*it_dest++).
void TestUninitFullMerge4() {
std::vector<DeletedAddress> in1;
in1.push_back(DeletedAddress(1));
in1.push_back(DeletedAddress(5));
std::vector<DeletedAddress> in2;
in2.push_back(DeletedAddress(2));
in2.push_back(DeletedAddress(6));
std::vector<DeletedAddress> in3;
in3.push_back(DeletedAddress(3));
in3.push_back(DeletedAddress(7));
std::vector<DeletedAddress> in4;
in4.push_back(DeletedAddress(4));
in4.push_back(DeletedAddress(8));

boost::sort::common::range<std::vector<DeletedAddress>::iterator> ranges[4] = {
boost::sort::common::range<std::vector<DeletedAddress>::iterator>(in1.begin(), in1.end()),
boost::sort::common::range<std::vector<DeletedAddress>::iterator>(in2.begin(), in2.end()),
boost::sort::common::range<std::vector<DeletedAddress>::iterator>(in3.begin(), in3.end()),
boost::sort::common::range<std::vector<DeletedAddress>::iterator>(in4.begin(), in4.end()),
};

const size_t kTotal = 8;
alignas(DeletedAddress) char raw_memory[sizeof(DeletedAddress) * kTotal];
DeletedAddress* out_ptr = reinterpret_cast<DeletedAddress*>(raw_memory);
boost::sort::common::range<DeletedAddress*> dest(out_ptr, out_ptr + kTotal);

auto res = boost::sort::common::uninit_full_merge4(
dest, ranges, 4, std::less<DeletedAddress>());
BOOST_CHECK_EQUAL(res.size(), kTotal);
for (size_t i = 0; i < kTotal; ++i) {
BOOST_CHECK_EQUAL(out_ptr[i].value, static_cast<int>(i + 1));
}
boost::sort::common::util::destroy(out_ptr, out_ptr + kTotal);
}

// Tests all sorting algorithms with DeletedAddress.
void TestSortAlgorithms() {
const size_t kSmallSize = 250;
std::mt19937 gen(12345);
std::uniform_int_distribution<int> dist(0, 50000);

std::vector<DeletedAddress> original(kSmallSize);
for (size_t i = 0; i < kSmallSize; ++i) {
original[i] = DeletedAddress(dist(gen));
}

// pdqsort
{
std::vector<DeletedAddress> v = original;
boost::sort::pdqsort(v.begin(), v.end());
BOOST_CHECK(std::is_sorted(v.begin(), v.end()));
}

// spinsort
{
std::vector<DeletedAddress> v = original;
boost::sort::spinsort(v.begin(), v.end());
BOOST_CHECK(std::is_sorted(v.begin(), v.end()));
}

// flat_stable_sort
{
std::vector<DeletedAddress> v = original;
boost::sort::flat_stable_sort(v.begin(), v.end());
BOOST_CHECK(std::is_sorted(v.begin(), v.end()));
}

// insert_sort
{
std::vector<DeletedAddress> v = original;
boost::sort::insert_sort(v.begin(), v.end());
BOOST_CHECK(std::is_sorted(v.begin(), v.end()));
}

// integer_sort (spreadsort)
{
std::vector<DeletedAddress> v = original;
boost::sort::spreadsort::integer_sort(v.begin(), v.end(), IntRightShift());
BOOST_CHECK(std::is_sorted(v.begin(), v.end()));
}

// Large datasets exercising parallel code paths in sample_sort,
// parallel_stable_sort, and block_indirect_sort.
const size_t kLargeSize = 70000;
std::vector<DeletedAddress> large_original(kLargeSize);
for (size_t i = 0; i < kLargeSize; ++i) {
large_original[i] = DeletedAddress(dist(gen));
}

// sample_sort (parallel mode triggers above 65536 elements)
{
std::vector<DeletedAddress> v = large_original;
boost::sort::sample_sort(v.begin(), v.end());
BOOST_CHECK(std::is_sorted(v.begin(), v.end()));
}

// parallel_stable_sort (parallel mode triggers above 65536 elements)
{
std::vector<DeletedAddress> v = large_original;
boost::sort::parallel_stable_sort(v.begin(), v.end());
BOOST_CHECK(std::is_sorted(v.begin(), v.end()));
}

// block_indirect_sort (parallel mode triggers above 131072 elements for sizeof=4)
const size_t kBlockSize = 140000;
std::vector<DeletedAddress> block_original(kBlockSize);
for (size_t i = 0; i < kBlockSize; ++i) {
block_original[i] = DeletedAddress(dist(gen));
}
{
std::vector<DeletedAddress> v = block_original;
boost::sort::block_indirect_sort(v.begin(), v.end());
BOOST_CHECK(std::is_sorted(v.begin(), v.end()));
}
}

} // namespace

int test_main(int, char*[]) {
TestCommonUtilAlgorithm();
TestUninitFullMerge4();
TestSortAlgorithms();

return 0;
}
Loading