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 | #ifdef NDEBUG |
13 | #undef NDEBUG |
14 | #endif |
15 | |
16 | #define BOOST_MOVE_ADAPTIVE_SORT_INVARIANTS |
17 | |
18 | #include <cstdlib> //std::srand |
19 | #include <iostream> //std::cout |
20 | |
21 | #include <boost/config.hpp> |
22 | |
23 | #include <boost/move/unique_ptr.hpp> |
24 | #include <boost/move/algo/detail/merge_sort.hpp> |
25 | #include <boost/move/detail/force_ptr.hpp> |
26 | |
27 | #include "order_type.hpp" |
28 | #include "random_shuffle.hpp" |
29 | |
30 | #include <boost/move/algo/adaptive_merge.hpp> |
31 | #include <boost/move/core.hpp> |
32 | #include <cstdlib> |
33 | |
34 | |
35 | template<class T> |
36 | bool test_random_shuffled(std::size_t const element_count, std::size_t const num_keys, std::size_t const num_iter) |
37 | { |
38 | boost::movelib::unique_ptr<T[]> elements(new T[element_count]); |
39 | boost::movelib::unique_ptr<std::size_t[]> key_reps(new std::size_t[num_keys ? num_keys : element_count]); |
40 | std::cout << "- - N: " << element_count << ", Keys: " << num_keys << ", It: " << num_iter << " \n" ; |
41 | |
42 | //Initialize keys |
43 | for(std::size_t i=0; i < element_count; ++i){ |
44 | std::size_t key = num_keys ? (i % num_keys) : i; |
45 | elements[i].key=key; |
46 | } |
47 | |
48 | std::srand(seed: 0); |
49 | |
50 | for (std::size_t i = 0; i != num_iter; ++i) |
51 | { |
52 | ::random_shuffle(elements.get(), elements.get() + element_count); |
53 | for(std::size_t j = 0; j < (num_keys ? num_keys : element_count); ++j){ |
54 | key_reps[j]=0; |
55 | } |
56 | for(std::size_t j = 0; j < element_count; ++j){ |
57 | elements[j].val = key_reps[elements[j].key]++; |
58 | } |
59 | |
60 | boost::movelib::unique_ptr<char[]> buf(new char [sizeof(T)*(element_count-element_count/2)]); |
61 | |
62 | std::size_t const split = std::size_t(std::rand()) % element_count; |
63 | boost::movelib::merge_sort(elements.get(), elements.get()+split, order_type_less(), boost::move_detail::force_ptr<T*>(buf.get())); |
64 | boost::movelib::merge_sort(elements.get()+split, elements.get()+element_count, order_type_less(), boost::move_detail::force_ptr<T*>(buf.get())); |
65 | |
66 | boost::movelib::adaptive_merge(elements.get(), elements.get()+split, elements.get()+element_count, order_type_less()); |
67 | |
68 | if (!is_order_type_ordered(elements.get(), element_count)) |
69 | { |
70 | std::cout << "\n ERROR\n" ; |
71 | std::abort(); |
72 | } |
73 | } |
74 | return true; |
75 | } |
76 | |
77 | void instantiate_smalldiff_iterators() |
78 | { |
79 | typedef randit<int, short> short_rand_it_t; |
80 | boost::movelib::adaptive_merge(first: short_rand_it_t(), middle: short_rand_it_t(), last: short_rand_it_t(), comp: less_int()); |
81 | |
82 | typedef randit<int, signed char> schar_rand_it_t; |
83 | boost::movelib::adaptive_merge(first: schar_rand_it_t(), middle: schar_rand_it_t(), last: schar_rand_it_t(), comp: less_int()); |
84 | } |
85 | |
86 | int main() |
87 | { |
88 | instantiate_smalldiff_iterators(); |
89 | |
90 | const std::size_t NIter = 100; |
91 | test_random_shuffled<order_move_type>(element_count: 1001, num_keys: 3, num_iter: NIter); |
92 | test_random_shuffled<order_move_type>(element_count: 1001, num_keys: 101, num_iter: NIter); |
93 | test_random_shuffled<order_move_type>(element_count: 1001, num_keys: 874, num_iter: NIter); |
94 | test_random_shuffled<order_move_type>(element_count: 10001, num_keys: 65, num_iter: NIter); |
95 | test_random_shuffled<order_move_type>(element_count: 10001, num_keys: 101, num_iter: NIter); |
96 | test_random_shuffled<order_move_type>(element_count: 10001, num_keys: 1023, num_iter: NIter); |
97 | test_random_shuffled<order_move_type>(element_count: 10001, num_keys: 4095, num_iter: NIter); |
98 | test_random_shuffled<order_move_type>(element_count: 10001, num_keys: 0, num_iter: NIter); |
99 | |
100 | return 0; |
101 | } |
102 | |