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