Brian Silverman | 1f5d398 | 2018-08-04 23:37:52 -0700 | [diff] [blame^] | 1 | ////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // (C) Copyright Ion Gaztanaga 2015-2016. |
| 4 | // Distributed under the Boost Software License, Version 1.0. |
| 5 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
| 7 | // |
| 8 | // See http://www.boost.org/libs/move for documentation. |
| 9 | // |
| 10 | ////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #include <cstdlib> //std::srand |
| 13 | #include <iostream> //std::cout |
| 14 | |
| 15 | #include <boost/config.hpp> |
| 16 | |
| 17 | #include <boost/move/unique_ptr.hpp> |
| 18 | #include <boost/move/algo/detail/merge_sort.hpp> |
| 19 | |
| 20 | #include "order_type.hpp" |
| 21 | #include "random_shuffle.hpp" |
| 22 | |
| 23 | #include <boost/move/algo/adaptive_merge.hpp> |
| 24 | #include <boost/move/core.hpp> |
| 25 | |
| 26 | |
| 27 | template<class T> |
| 28 | bool test_random_shuffled(std::size_t const element_count, std::size_t const num_keys, std::size_t const num_iter) |
| 29 | { |
| 30 | boost::movelib::unique_ptr<T[]> elements(new T[element_count]); |
| 31 | boost::movelib::unique_ptr<std::size_t[]> key_reps(new std::size_t[num_keys ? num_keys : element_count]); |
| 32 | std::cout << "- - N: " << element_count << ", Keys: " << num_keys << ", It: " << num_iter << " \n"; |
| 33 | |
| 34 | //Initialize keys |
| 35 | for(std::size_t i=0; i < element_count; ++i){ |
| 36 | std::size_t key = num_keys ? (i % num_keys) : i; |
| 37 | elements[i].key=key; |
| 38 | } |
| 39 | |
| 40 | std::srand(0); |
| 41 | |
| 42 | for (std::size_t i = 0; i != num_iter; ++i) |
| 43 | { |
| 44 | ::random_shuffle(elements.get(), elements.get() + element_count); |
| 45 | for(std::size_t i = 0; i < (num_keys ? num_keys : element_count); ++i){ |
| 46 | key_reps[i]=0; |
| 47 | } |
| 48 | for(std::size_t i = 0; i < element_count; ++i){ |
| 49 | elements[i].val = key_reps[elements[i].key]++; |
| 50 | } |
| 51 | |
| 52 | boost::movelib::unique_ptr<char[]> buf(new char [sizeof(T)*(element_count-element_count/2)]); |
| 53 | |
| 54 | std::size_t const split = std::size_t(std::rand()) % element_count; |
| 55 | boost::movelib::merge_sort(elements.get(), elements.get()+split, order_type_less(), (T*)buf.get()); |
| 56 | boost::movelib::merge_sort(elements.get()+split, elements.get()+element_count, order_type_less(), (T*)buf.get()); |
| 57 | |
| 58 | boost::movelib::adaptive_merge(elements.get(), elements.get()+split, elements.get()+element_count, order_type_less()); |
| 59 | |
| 60 | if (!is_order_type_ordered(elements.get(), element_count)) |
| 61 | { |
| 62 | std::cout << "\n ERROR\n"; |
| 63 | throw int(0); |
| 64 | } |
| 65 | } |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | int main() |
| 70 | { |
| 71 | const std::size_t NIter = 100; |
| 72 | test_random_shuffled<order_move_type>(10001, 3, NIter); |
| 73 | test_random_shuffled<order_move_type>(10001, 65, NIter); |
| 74 | test_random_shuffled<order_move_type>(10001, 101, NIter); |
| 75 | test_random_shuffled<order_move_type>(10001, 1023, NIter); |
| 76 | test_random_shuffled<order_move_type>(10001, 4095, NIter); |
| 77 | test_random_shuffled<order_move_type>(10001, 0, NIter); |
| 78 | |
| 79 | return 0; |
| 80 | } |