-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
278 lines (254 loc) · 11.1 KB
/
main.cpp
File metadata and controls
278 lines (254 loc) · 11.1 KB
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
276
277
278
#include "exceptions/exception.h"
#include "lib_test/t.h"
#include "py_dict.h"
#include "py_enumerate.h"
#include "py_list.h"
#include "py_range.h"
#include "py_set.h"
#include "py_str.h"
#include "py_tuple.h"
#include "pypp_union.h"
#include "pypp_util/create/cstdint.h"
#include "pypp_util/create/others.h"
#include "pypp_util/main_error_handler.h"
#include "pypp_util/print.h"
#include "pypp_util/to_py_str.h"
#include "slice/creators.h"
#include "slice/py_slice.h"
#include <format>
#include <iostream>
// TODO: run clang-format and clang-tidy on all files
// How do you run them on all files?
// You can use the following commands:
// clang-format -i src/*.h src/*.cpp
// clang-tidy src/*.cpp -- -Iinclude -std=c++23
// You might need to adjust the include path and standard version as needed.
// You can also set up a .clang-format file in the root directory of your
// project to define your formatting style.
// TODO: I think I need to add a -DCMAKE_BUILD_TYPE=Release flag to the first
// cmake command too, in order to get dependencies built in Release mode.
// function that returns by reference an argument that is passed by reference
pypp::PyList<int> &get_list(pypp::PyList<int> &list) { return list; }
int main() {
try {
std::cout << "Hello, World!" << std::endl;
// Use an initializer list directly
pypp::PyStr s = pypp::PyStr("This is a test string.");
std::cout << "string size: " << s.len() << std::endl;
pypp::PyList<pypp::PyStr> parts = s.split(pypp::PyStr(" "));
pypp::print(parts);
std::cout << "Parts lens: " << parts.len() << std::endl;
pypp::PyTup<int, double, pypp::PyStr> tup =
pypp::PyTup<int, double, pypp::PyStr>(42, 3.14, std::move(s));
pypp::print(tup);
std::cout << tup.count(42) << std::endl;
std::cout << tup.index(3.14) << std::endl;
std::cout << "Tuple size: " << tup.len() << std::endl;
pypp::PySet<int> my_set = {1, 2, 3, 4, 5};
pypp::print(my_set);
std::cout << "Set size: " << my_set.len() << std::endl;
pypp::PyDict<int, pypp::PyStr> my_dict = {
{1, pypp::PyStr("one")},
{2, pypp::PyStr("two")},
{3, pypp::PyStr("three")},
};
pypp::print(my_dict);
std::cout << "Dict size: " << my_dict.len() << std::endl;
pypp::PyDict<pypp::PyStr, int> my_dict2 = {
{pypp::PyStr("one"), 1},
{pypp::PyStr("two"), 2},
{pypp::PyStr("three"), 3},
};
pypp::print(my_dict2);
pypp::PySlice sl(1, 5, 2);
pypp::PyList<pypp::PyStr> sliced_parts = parts[sl];
pypp::print(sliced_parts);
pypp::print(parts[pypp::py_slice(0, std::nullopt, 2)]);
pypp::PyDict<int, pypp::PyDict<int, int>> nested_dict(
{{0, {{0, 1}}}, {1, {{0, 1}}}});
pypp::print(nested_dict);
// dict assignment
pypp::PyDict<int, int> int_dict =
pypp::PyDict<int, int>({{0, 1}, {1, 2}});
pypp::print(int_dict);
// for loops over lists
pypp::PyList<int> my_py_list = {1, 2, 3};
for (const auto &val : my_py_list) {
std::cout << val;
}
// for loops over sets
pypp::PySet<pypp::PyStr> my_py_set = {
pypp::PyStr("a"), pypp::PyStr("b"), pypp::PyStr("c")};
for (const auto &val : my_py_set) {
std::cout << val;
}
// for loops over dict items
for (const auto &pypp_it_tup : int_dict.items()) {
auto &k = pypp_it_tup.first;
auto &v = pypp_it_tup.second;
std::cout << k << ": " << v << std::endl;
}
// for loops over list of tuples
pypp::PyList<pypp::PyTup<int, int>> list_of_tuples =
pypp::PyList({pypp::PyTup(1, 2), pypp::PyTup(3, 4)});
for (const auto &pypp_it_tup : list_of_tuples) {
auto &first = pypp_it_tup.get<0>();
auto &second = pypp_it_tup.get<1>();
std::cout << first << ", " << second << std::endl;
}
pypp::print(list_of_tuples);
// basic while loops
int i = 0;
while (i < 3) {
std::cout << i << " ";
i += 1;
}
// pypp::str
pypp::print(pypp::str(1));
pypp::print(pypp::str(3.14));
pypp::print(pypp::str("Hello"));
// print
pypp::print(pypp::PyStr("This is a test of the print function."));
pypp::print(std::format("Formatted string: {}", 42));
pypp::print(std::format(
"Formatted string with pypp::PyStr: {}, pypp::PyTup: "
"{}, pypp::PySet: "
"{}, pypp::PyList: {}, pypp::PyDict: {}",
pypp::PyStr("Hello"), pypp::PyTup(1, 2), pypp::PySet({1, 2, 3}),
pypp::PyList({1, 2, 3}), pypp::PyDict<int, int>({{0, 1}})));
// Testing that hashing works
pypp::PyDict<pypp::PyTup<int, int>, pypp::PyStr> dict_of_tups = {
{pypp::PyTup(1, 2), pypp::PyStr("one two")},
{pypp::PyTup(3, 4), pypp::PyStr("three four")},
};
pypp::PyDict<pypp::PyList<int>, pypp::PyStr> dict_of_lists = {
{pypp::PyList<int>({1, 2}), pypp::PyStr("one two")},
{pypp::PyList<int>({3, 4}), pypp::PyStr("three four")},
};
pypp::PyDict<pypp::PyStr, pypp::PyStr> dict_of_strs = {
{pypp::PyStr("one"), pypp::PyStr("1")},
{pypp::PyStr("two"), pypp::PyStr("2")},
};
pypp::print("Are hashable: pypp::PyTup, pypp::PyList, pypp::PyStr");
// pypp::PyRange
for (const auto &i : pypp::PyRange(3)) {
pypp::print(i); // 0, 1, 2
}
for (const auto &i : pypp::PyRange(1, 4)) {
pypp::print(i); // 1, 2, 3
}
for (const auto &i : pypp::PyRange(10, 4, -2)) {
pypp::print(i); // 10, 8, 6
}
for (const auto &i : pypp::PyRange(100, 98)) {
pypp::print(i); // Should print nothing
}
for (const auto &i : pypp::PyRange(50, 100, -1)) {
pypp::print(i); // Should print nothing
}
// slice printing
pypp::print("slice printing");
pypp::print(pypp::py_slice(5, 1, -1).indices(10));
pypp::print(pypp::py_slice(10));
pypp::print(pypp::py_slice(5, 10));
pypp::print("Slice length 5: ",
pypp::py_slice(5, 10).calc_slice_length(100));
pypp::print("Slice length 2: ",
pypp::py_slice(1000).calc_slice_length(2));
pypp::print(pypp::py_slice(5, 10, -5));
pypp::print(pypp::py_slice(1, std::nullopt));
// range printing
pypp::print(pypp::PyRange(10));
pypp::print(pypp::PyRange(5, 10));
pypp::print(pypp::PyRange(5, 10, -1));
pypp::print(pypp::PyRange(5, 10, 1));
// formatting slice and range
pypp::print(std::format("Formatted pypp::PySlice: {}",
pypp::py_slice(1, 10, 2)));
pypp::print(std::format("Formatted pypp::PyRange: {}",
pypp::PyRange(1, 10, 2)));
// Using slice and range in sets (for hashing)
pypp::PySet<pypp::PySlice> slice_set = pypp::PySet({pypp::py_slice(1)});
pypp::print(slice_set);
pypp::PySet<pypp::PyRange> range_set = pypp::PySet({pypp::PyRange(1)});
pypp::print(range_set);
// I need to think about all these references and moves for not just
// pypp::PyTup, but also pypp::PyList, pypp::PySet, and pypp::PyDict,
// etc.. In Python, if you have 2 list variables, and then you add them
// to a second list of size 2, what happens? And what happens if you do
// the same here in the C++?
// It might be ok to just accept that there is a lot of copying for now
// and then later I can optimize it. Even with the copying, I might
// still get big performance improvements over Python for the vast
// majority of cases
pypp::PyList<int> numbers = {10, 20, 30, 40, 50};
pypp::PyTup<pypp::PyStr, pypp::PyList<int>> py_tup(
std::move(pypp::PyStr("Numbers")), std::move(numbers));
py_tup.get<1>().append(60);
pypp::print(numbers);
// Tuple structured binding
pypp::PyTup<int, int, pypp::PyStr, pypp::PyList<int>> tup2(
1, 2, pypp::PyStr("Hello"), pypp::PyList<int>({1, 2, 3}));
auto &[first, second, third, fourht] = tup2; // references
auto [first2, second2, third2, fourth2] = tup2; // copies
pypp::PyList<int> my_list = pypp::PyList({10, 20, 30});
for (const auto &[i, val] : pypp::PyEnumerate(my_list)) {
std::cout << "Index: " << i << ", Value: " << val << std::endl;
}
// Testing function that returns by reference
pypp::PyList<int> numbers2 = {10, 20, 30, 40, 50};
pypp::PyList<int> &list_returned_by_ref = get_list(numbers2);
list_returned_by_ref.append(70);
pypp::print(numbers2);
PseudoCustomTypeCpp pct(42);
std::cout << "pct.a_: " << pct.get_a() << std::endl;
// String min and max
pypp::PyStr abc = pypp::PyStr("abc");
pypp::print(pypp::PyStr("abc min: "), abc.min());
pypp::print(pypp::PyStr("abc max: "), abc.max());
// conversion
double my_float = 3.67;
float my_float_32 = 3.124;
int my_int = 2;
bool my_bool = true;
pypp::print("int_: ", pypp::int_(my_float), pypp::int_(my_float_32),
pypp::int_(my_bool));
pypp::print("float_: ", pypp::float_(my_int), pypp::float_(my_float_32),
pypp::float_(my_bool));
pypp::print("float32_: ", pypp::to_float32(my_int),
pypp::to_float32(my_float), pypp::to_float32(my_bool));
pypp::print("bool: ", pypp::bool_(my_int), pypp::bool_(my_float),
pypp::bool_(my_float_32));
pypp::print("int16_t: ", pypp::to_int16_t(my_int),
pypp::to_int16_t(my_float), pypp::to_int16_t(my_float_32));
// List initialization (example of copying behavior that we want to
// avoid in Py++)
pypp::PyList<int> list1 = {1, 2, 3};
pypp::PyList<pypp::PyList<int>> list2 = {list1, pypp::PyList({4, 5})};
list1.append(10);
pypp::print("list1: ", list1);
pypp::print("list2: ", list2);
// tuple initlialization (example of copying behavior that we want to
// avoid in Py++)
pypp::PyTup<int, pypp::PyList<int>> tup3(1, list1);
list1.append(20);
pypp::print("list1: ", list1);
pypp::print("tup3: ", tup3);
// enumerate with dict items
pypp::PyDict<int, pypp::PyStr> my_dict3 = {
{1, pypp::PyStr("one")},
{2, pypp::PyStr("two")},
{3, pypp::PyStr("three")},
};
for (const auto &[i, item] : pypp::PyEnumerate(my_dict3.items())) {
auto &k = item.first;
auto &v = item.second;
std::cout << "Index: " << i << ", Key: " << k << ", Value: " << v
<< std::endl;
}
return 0;
} catch (...) {
pypp::handle_fatal_exception();
return EXIT_FAILURE;
}
}