-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_1.cpp
More file actions
50 lines (39 loc) · 1.4 KB
/
example_1.cpp
File metadata and controls
50 lines (39 loc) · 1.4 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
#include <array>
#include <string_view>
template <typename CT, size_t S>
class Tensor {
using T = typename std::remove_const<CT>::type;
public:
constexpr Tensor(const CT* data = nullptr) {
if (data != nullptr) {
for (size_t i = 0; i < S; ++i) {
this->data[i] = data[i];
}
}
}
consteval const T* get_data() const { return data; }
static consteval size_t size() { return S; }
template <typename T2, size_t S2>
constexpr auto operator+(const Tensor<T2, S2>& other) const {
static_assert(S == S2, "Tensors must be the same size to be added.");
std::array<T, S> sum{};
for (size_t i = 0; i < this->size(); ++i) {
sum[i] = this->data[i] + other.get_data()[i];
}
return Tensor<T, S>(sum.data());
}
consteval std::string_view dump() const { return std::string_view(data, S - 1); }
private:
T data[S] = {};
};
template <typename CT, size_t S>
Tensor(const CT (&)[S]) -> Tensor<CT, S>;
int main() {
constexpr char encrypted_message[] = "Hello world!";
constexpr int decryption_key[] = {-5, -58, -65, -76, -6, 83, -87, -2, -17, -5, 5, 66, 0};
constexpr auto tensor_1 = Tensor(encrypted_message);
constexpr auto tensor_2 = Tensor(decryption_key);
constexpr auto tensor_3 = tensor_1 + tensor_2;
static_assert(tensor_3.dump() == "C++ is magic");
return 0;
}