Commit 93679765 authored by Lefebvre, Jordan's avatar Lefebvre, Jordan
Browse files

Sortperm2

parent 442550b8
......@@ -12,6 +12,29 @@ struct RADIX_PUBLIC super_sad_necessary_hack_for_dll_lib_creation
void foo_method();
};
template <typename iterator, typename compare_type>
RADIX_PUBLIC void sort_permutation(iterator start, iterator end,
std::vector<std::size_t> &permutation,
compare_type &comparator)
{
// create list of indices the size of the incoming data
auto size = std::distance(start, end);
radix_tagged_line("Size of data=" << size);
if (permutation.size() != size)
{
permutation.resize(size);
}
// initialize list with initial index, starting at zero
std::iota(permutation.begin(), permutation.end(), 0);
// order ordering according to comparator
std::sort(permutation.begin(), permutation.end(),
[&](std::size_t data_i, std::size_t data_j) {
radix_line("P2-Comparing [" << data_i << ", " << data_j << "]");
return comparator(*(start + data_i), *(start + data_j));
});
}
template <typename list_type, typename compare_type>
RADIX_PUBLIC std::vector<size_t> sort_permutation(const list_type &data,
compare_type &comparator)
......
#include <iostream>
#include <random>
#include "gtest/gtest.h"
#include "radixalgorithm/ordering.hh"
......@@ -15,15 +16,19 @@ TEST(radixalgorithm, Ordering)
return a < b;
};
std::vector<size_t> permutation = radix::sort_permutation(list, comparator);
std::vector<size_t> perm2;
radix::sort_permutation(list.begin(), list.end(), perm2, comparator);
// test edges
EXPECT_EQ(permutation[0], 5);
EXPECT_EQ(permutation[6], 6);
EXPECT_EQ(perm2[0], 5);
EXPECT_EQ(perm2[6], 6);
std::cout << "Ordering:" << std::endl;
for (size_t i = 0; i < permutation.size(); ++i)
{
std::cout << "index " << i << ". (" << list[i] << ") recieves value from "
<< permutation[i] << " (" << list[permutation[i]] << ")"
<< std::endl;
<< permutation[i] << " (" << list[permutation[i]] << ") p2("
<< list[perm2[i]] << ")" << std::endl;
}
//
// sort list given the
......@@ -38,3 +43,50 @@ TEST(radixalgorithm, Ordering)
std::cout << "[" << list[i] << "," << list2[i] << "]" << std::endl;
}
}
TEST(radixalgorithm, DISABLED_BenchmarkOrdering)
{
std::vector<double> giant_list(1000000);
// First create an instance of an engine.
std::random_device rnd_device;
// Specify the engine and distribution.
std::mt19937 mersenne_engine{rnd_device()};
std::uniform_real_distribution<double> dist{1, 1000};
auto gen = [&dist, &mersenne_engine]() { return dist(mersenne_engine); };
generate(std::begin(giant_list), std::end(giant_list), gen);
auto comparator = [](double a, double b) { return a < b; };
radix::Timer timer1, timer2, timer3;
timer1.start();
// test time for return
std::vector<size_t> perm1;
{
perm1 = radix::sort_permutation(giant_list, comparator);
}
timer1.stop();
timer2.start();
std::vector<size_t> perm2(giant_list.size());
{
radix::sort_permutation(giant_list.begin(), giant_list.end(), perm2,
comparator);
}
timer2.stop();
timer3.start();
{
radix::sort_permutation(giant_list.begin(), giant_list.end(), perm2,
comparator);
}
timer3.stop();
std::cout << "Perm1 took " << timer1.duration() / 1e9 << " seconds"
<< std::endl;
std::cout << "Perm2 took " << timer2.duration() / 1e9 << " seconds"
<< std::endl;
std::cout << "Perm3 took " << timer3.duration() / 1e9 << " seconds"
<< std::endl;
EXPECT_EQ(giant_list[perm1[0]], giant_list[perm2[0]]);
EXPECT_EQ(giant_list[perm1[perm1.size() - 1]],
giant_list[perm2[perm2.size() - 1]]);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment