-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibFuzzyShellSort.hpp
77 lines (60 loc) · 2.24 KB
/
FibFuzzyShellSort.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef FIBFUZZYSHELLSORT_HPP
#define FIBFUZZYSHELLSORT_HPP
#include <limits>
#include "ShellSortTemplate.hpp"
#include "IntegralConstantArithmetic.hpp"
namespace FibFuzzyNS {
using namespace IntegralConstantArithmetic;
template <class Numeric, size_t sz>
struct Number {
using fib_type = add_t<typename Number<Numeric, sz - 1>::fib_type, typename Number<Numeric, sz - 2>::fib_type>;
using grand_type = neg_t<typename Number<Numeric, sz - 1>::grand_type>;
using type = add_t<fib_type, grand_type>;
};
template <class Numeric>
struct Number<Numeric, 0> {
using type = IC<Numeric, 1>;
};
template <class Numeric>
struct Number<Numeric, 1> {
using fib_type = IC<Numeric, 5>;
using grand_type = IC<Numeric, -1>;
using type = add_t<fib_type, grand_type>;
};
template <class Numeric>
struct Number<Numeric, 2> {
using fib_type = IC<Numeric, 8>;
using grand_type = IC<Numeric, 1>;
using type = add_t<fib_type, grand_type>;
};
template <class Numeric = int>
class Sequence {
// Fibonacci grand number generator
//
static constexpr size_t table_size() {
constexpr Numeric max_num = std::numeric_limits<Numeric>::max();
Numeric f0(5), f1(8), f2(1);
Numeric grand = -1;
size_t sz = 3;
while ((max_num - f1) >= (f0 + grand)) {
f2 = f1 + f0;
f0 = f1;
f1 = f2;
grand = - grand;
sz++;
}
return sz;
}
template <size_t ...I>
static constexpr decltype(auto) gen_seq(std::index_sequence<I...>) {
return std::integer_sequence<Numeric, Number<Numeric, sizeof...(I) - I - 1>::type::value...>{};
}
public:
using type = decltype(gen_seq(std::make_index_sequence<table_size()>()));
};
template<class Iterator, class Comparator=std::less<>>
void shellsort(Iterator first, Iterator last, Comparator comp = Comparator()) noexcept {
ShellSortTemplate::sort<Iterator, Sequence<typename std::iterator_traits<Iterator>::difference_type>, Comparator>(first, last, comp);
}
};
#endif