-
Notifications
You must be signed in to change notification settings - Fork 0
/
pizza.hh
101 lines (77 loc) · 2.95 KB
/
pizza.hh
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef _PIZZA_HH_
#define _PIZZA_HH_
#include <type_traits>
#include <cstddef>
#include <array>
namespace {
//Sprawdza czy na liście typów są powtórzenia.
template <typename... Ts>
struct Repetition {
static constexpr bool value = false;
};
template <typename T, typename... Ts>
struct Repetition<T, Ts...> {
static constexpr bool value = Repetition<Ts...>::value ||
(std::is_same<T, Ts>::value ||...);
};
//Oblicza najlepszą smakowitość pizzy dla zadanej liczby kawałków.
template<typename Kind>
constexpr size_t best_yumminess(const size_t max_slices) {
static_assert(Kind::yumminess(0) == 0, "Pizza's function yumminess(0) have to return 0");
auto best_yum = Kind::yumminess(0);
size_t best_i = 0;
for(size_t i = 1; i <= max_slices; ++i) {
auto new_yum = Kind::yumminess(i);
if (new_yum > best_yum) {
best_i = i;
best_yum = new_yum;
}
}
return best_i;
}
}
//Typ szablonowy reprezentujący pizzerię.
template <typename... Kinds>
struct Pizzeria {
static_assert(!Repetition<Kinds...>::value, "Indecisive pizzeria");
private:
//Struktura reprezentująca pizzę.
template <const size_t... Slices>
struct Pizza {
template <typename K>
static constexpr size_t count() {
return ((std::is_same<K, Kinds>::value * Slices)+...);
}
static constexpr auto as_array() {
return std::array<size_t, sizeof...(Slices)>{{Slices...}};
}
typedef Pizzeria<Kinds...>::Pizza<(2 * Slices)...> sliced_type;
private:
//Najlepszy mix danej pizzy z Pizza2.
template <typename Pizza2>
struct Mix {
typedef Pizza<best_yumminess<Kinds>(count<Kinds>()
+ Pizza2::template count<Kinds>())...> type;
};
typedef Pizzeria<Kinds...> my_pizzeria;
template<typename T, typename U>
friend struct best_mix;
};
public:
//Struktura która służy do tworzenia pizzy.
template <typename Kind>
struct make_pizza {
static_assert((std::is_same<Kind, Kinds>::value ||...),
"This pizza is not available in this pizzeria");
typedef Pizza<(std::is_same<Kind, Kinds>::value ? 8 : 0)...> type;
};
};
//Szablon struktury określającej jaką najlepsza możliwą pizzę można złożyć z podanych.
template <typename Pizza1, typename Pizza2>
struct best_mix {
static_assert(std::is_same<typename Pizza1::my_pizzeria,
typename Pizza2::my_pizzeria>::value,
"Pizzas have to be from the same pizzeria");
typedef typename Pizza1::template Mix<Pizza2>::type type;
};
#endif