-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch26.ixx
275 lines (237 loc) · 7.23 KB
/
ch26.ixx
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
export module ch26;
import std;
export namespace ch26 {
namespace exercises {
namespace ex1 {
namespace myanswer {
using namespace std;
template <typename KeyType, typename ValueType>
class KeyValuePair {
public:
explicit KeyValuePair(KeyType key, ValueType value) :m_key{ move(key) }, m_value{ move(value) } {}
const KeyType& getkey() const { return m_key; }
const ValueType& getvalue() const { return m_value; }
void setkey(KeyType key) { m_key = move(key); }
void setvalue(ValueType value) { m_value = move(value); }
private:
KeyType m_key;
ValueType m_value;
};
template<typename KeyType>
class KeyValuePair<KeyType, const char*> {
public:
explicit KeyValuePair(KeyType key, const char* value) :m_key{ move(key) }, m_value{ value } {}
const KeyType& getkey() const { return m_key; }
const string& getvalue() const { return m_value; }
void setkey(KeyType key) { m_key = move(key); }
void setvalue(const char* value) { m_value = value; }
private:
KeyType m_key;
string m_value;
};
void test() {
using namespace std;
{
KeyValuePair<int, string> intstr_pair{ 1,"Hello" };
cout << format("key: {}, value: {}\n", intstr_pair.getkey(), intstr_pair.getvalue());
intstr_pair.setkey(2);
intstr_pair.setvalue("walawalabong");
cout << format("key: {}, value: {}\n", intstr_pair.getkey(), intstr_pair.getvalue());
}
{
KeyValuePair intstr_pair{ 6,"Hello"s };
cout << format("key: {}, value: {}\n", intstr_pair.getkey(), intstr_pair.getvalue());
intstr_pair.setkey(9);
intstr_pair.setvalue("somethingstratigtaion");
cout << format("key: {}, value: {}\n", intstr_pair.getkey(), intstr_pair.getvalue());
}
{
KeyValuePair charstr_pair{ 'a',"Hello" };
cout << format("key: {}, value: {}\n", charstr_pair.getkey(), charstr_pair.getvalue());
charstr_pair.setkey('b');
charstr_pair.setvalue("walawalabong");
cout << format("key: {}, value: {}\n", charstr_pair.getkey(), charstr_pair.getvalue());
}
{
KeyValuePair intint_pair{ 3,4 };
cout << format("key: {}, value: {}\n", intint_pair.getkey(), intint_pair.getvalue());
intint_pair.setkey(1);
intint_pair.setvalue(2);
cout << format("key: {}, value: {}\n", intint_pair.getkey(), intint_pair.getvalue());
}
{
KeyValuePair strstr_pair{ "nice","job" };//would save as string
cout << format("key: {}, value: {}\n", strstr_pair.getkey(), strstr_pair.getvalue());
strstr_pair.setkey("good");
strstr_pair.setvalue("work");
cout << format("key: {}, value: {}\n", strstr_pair.getkey(), strstr_pair.getvalue());
}
};
}
}
namespace ex2 {
namespace myanswer {
using namespace std;
template <unsigned int f>
class Fibonacci
{
public:
static constexpr unsigned long long value{ Fibonacci<f - 2>::value + Fibonacci<f - 1>::value };
};
template <>
class Fibonacci<0>
{
public:
static constexpr unsigned long long value{ 0 };
};
template <>
class Fibonacci<1>
{
public:
static constexpr unsigned long long value{ 1 };
};
template <unsigned int f>
inline constexpr unsigned long long Fibonacci_v = Fibonacci<f>::value;
void test()
{
println("{}", Fibonacci_v<6>);
println("{}", Fibonacci_v<7>);
println("{}", Fibonacci_v<8>);
println("{}", Fibonacci_v<9>);
println("{}", Fibonacci_v<10>);
println("{}", Fibonacci_v<11>);
println("{}", Fibonacci_v<12>);
}
}
}
namespace ex3 {
namespace myanswer {
using namespace std;
consteval unsigned long long fibonacci(unsigned int n) {
unsigned long long a{ 0 };
unsigned long long b{ 1 };
unsigned long long temp{0};
for (unsigned long long ull{ 0 }; ull < n;++ull) {
temp = a + b;
swap(a, b);
swap(b,temp);
}
return a;
}
void test(){
println("{}", fibonacci(0));
println("{}", fibonacci(1));
println("{}", fibonacci(2));
println("{}", fibonacci(3));
println("{}", fibonacci(4));
println("{}", fibonacci(5));
println("{}", fibonacci(6));
println("{}", fibonacci(7));
println("{}", fibonacci(8));
println("{}", fibonacci(9));
println("{}", fibonacci(10));
println("{}", fibonacci(11));
println("{}", fibonacci(12));
}
}
}
namespace ex4 {
namespace myanswer {
using namespace std;
template<typename T, typename... Us>
concept SameTypes = (same_as<T, Us> && ...);
template<typename T, typename... Tn>
requires SameTypes<T, Tn...>
void push_back_values(vector<T>& vt, const Tn&... args) {
(vt.push_back(args), ...);
}
template<typename T, typename... Tn>
requires SameTypes<T, Tn...>
void insert_values(vector<T>& vt, const Tn&... args) {
(vt.insert(end(vt), args), ...);
}
void test() {
vector<int> vi;
vector<string> vs;
vector<double> vd;
push_back_values(vi, 1, 5, 6, 6, 7, 3);
insert_values(vi, 1, 5, 6, 6, 7, 3);
push_back_values(vs, "asdasd"s, "asdcv"s, "cvbbcv"s, "vxcvxcv"s, "Hello vbbi"s, "kop"s);
insert_values(vs, "asdasd"s, "asdcv"s, "cvbbcv"s, "vxcvxcv"s, "Hello vbbi"s, "kop"s);
push_back_values(vd, 4.5, 6.872, 3.14159, 75.0);
insert_values(vd, 4.5, 6.872, 3.14159, 75.0);
for (auto& i : vi) {
print("{} ", i);
}
println("");
for (auto& s : vs) {
print("{} ", s);
}
println("");
for (auto& d : vd) {
print("{} ", d);
}
println("");
}
}
}
namespace ex5 {
namespace myanswer {
using namespace std;
template<typename T1, typename T2>
decltype(auto) multiply(T1 t1, T2 t2) {
if constexpr (is_arithmetic_v<T1> && is_arithmetic_v<T2>) {
return t1 * t2;
}
else {
throw invalid_argument{
"Either " + string{typeid(T1).name()} + " or " + string{typeid(T2).name()} + " is not arithmetic value."
};
}
}
void test() {
try {
println("ex5:");
println("{}", multiply(1, 2));
println("{}", multiply(4.23, 6.789));
println("{}", multiply(2, 3));
println("{}", multiply(4.12, 2));
println("{}", multiply(0, 9.815));
println("{}", multiply(-1, 67.12));
//Testing thowring exception
//multiply("what", 67.12);
//multiply(3, "boo!");
//multiply("good", "job!");
}
catch (const exception& e) {
cerr << e.what() << endl;
}
}
}
}
namespace ex6 {
namespace myanswer {
using namespace std;
template <typename T>
concept Arithmeticable = is_arithmetic_v<T>;
decltype(auto) multiply(const Arithmeticable auto& t1, const Arithmeticable auto& t2)
{
return t1 * t2;
}
void test() {
println("ex6:");
println("{}", multiply(1, 2));
println("{}", multiply(4.23, 6.789));
println("{}", multiply(2, 3));
println("{}", multiply(4.12, 2));
println("{}", multiply(0, 9.815));
println("{}", multiply(-1, 67.12));
//Testing concepts validation
/*multiply("what", 67.12);
multiply(3, "boo!");
multiply("good", "job!");*/
}
}
}
}
}