Skip to content

Commit f83a597

Browse files
committed
Merge branch 'develop' of https://github.com/XuhuaHuang/EmbeddedProgramming into develop
2 parents 1e5eb5f + 3185ba9 commit f83a597

File tree

7 files changed

+158
-91
lines changed

7 files changed

+158
-91
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ add_subdirectory("VariadicTemplateParameter")
4141
add_subdirectory("Vector")
4242
add_subdirectory("View")
4343
add_subdirectory("VirtualPointer")
44-
add_subdirectory("WriteAccessStructralBinding")
44+
add_subdirectory("WriteAccessStructuralBinding")

Util/type_name.hh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @file type_name.hh
3+
* @author Xuhua Huang
4+
* @brief
5+
* @version 0.1
6+
* @date 2025-09-23
7+
*
8+
* @copyright Copyright (c) 2025
9+
*
10+
*/
11+
12+
#ifndef TYPE_NAME_HH
13+
#define TYPE_NAME_HH
14+
15+
#include <string_view>
16+
17+
namespace util {
18+
namespace type {
19+
20+
template <typename T> constexpr std::string_view type_name() {
21+
#if defined(__clang__)
22+
std::string_view p = __PRETTY_FUNCTION__;
23+
return p.substr(p.find('=') + 2, p.rfind(']') - p.find('=') - 2);
24+
#elif defined(__GNUC__)
25+
std::string_view p = __PRETTY_FUNCTION__;
26+
return p.substr(p.find('=') + 2, p.rfind(']') - p.find('=') - 2);
27+
#elif defined(_MSC_VER)
28+
std::string_view p = __FUNCSIG__;
29+
auto start = p.find("type_name<") + 10;
30+
auto end = p.find(">(void)");
31+
return p.substr(start, end - start);
32+
#else
33+
#error "Unsupported compiler"
34+
#endif
35+
}
36+
37+
} // namespace type
38+
} // namespace util
39+
40+
#endif //! TYPE_NAME_HH

ValArray/valarray_notes.cpp

Lines changed: 117 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -15,111 +15,138 @@
1515
*
1616
*/
1717

18-
#include <algorithm>
1918
#include <cassert>
20-
#include <concepts>
19+
#include <cstdint>
2120
#include <iostream>
22-
#include <limits>
21+
#include <memory>
2322
#include <span>
23+
#include <string>
2424
#include <type_traits>
25+
#include <typeinfo>
2526
#include <valarray>
2627

28+
#if defined(__GNUC__)
29+
#include <cxxabi.h>
30+
31+
template <typename T> [[nodiscard]] std::string type_name() {
32+
int status = 0;
33+
std::unique_ptr<char, void (*)(void *)> res{
34+
abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &status),
35+
std::free};
36+
return (status == 0) ? res.get() : typeid(T).name();
37+
}
38+
#endif
39+
2740
#ifndef log
2841
#define log std::cout << __LINE__ << ": " << std::boolalpha
2942
#endif
3043

3144
template <typename T>
32-
requires std::is_arithmetic_v<T> && (!std::is_pointer_v<T>) && (!std::is_reference_v<T>)
33-
inline std::ostream& operator<<(std::ostream& os, const std::valarray<T>& values) noexcept {
34-
os << "[";
35-
if (values.size() >= 1) [[likely]] {
36-
for (const T* it = std::begin(values); it < std::end(values) - 1; it++) {
37-
os << *it << ", ";
38-
}
39-
os << values[values.size() - 1];
45+
requires std::is_arithmetic_v<T> && (!std::is_pointer_v<T>) &&
46+
(!std::is_reference_v<T>)
47+
inline std::ostream &operator<<(std::ostream &os,
48+
const std::valarray<T> &values) noexcept {
49+
os << "[";
50+
if (values.size() >= 1) [[likely]] {
51+
for (const T *it = std::begin(values); it < std::end(values) - 1; it++) {
52+
os << *it << ", ";
4053
}
41-
os << "]";
42-
return os;
54+
os << values[values.size() - 1];
55+
}
56+
os << "]";
57+
return os;
4358
}
4459

4560
template <>
46-
inline std::ostream& operator<<(std::ostream& os, const std::valarray<std::uint8_t>& values) noexcept = delete;
61+
inline std::ostream &
62+
operator<<(std::ostream &os,
63+
const std::valarray<std::uint8_t> &values) noexcept = delete;
4764

4865
int main() {
49-
static_assert(std::is_arithmetic_v<std::uint8_t>);
50-
51-
// log << std::is_arithmetic_v<std::uint8_t> << "\n";
52-
// log << std::numeric_limits<std::uint16_t>::min() << "\n";
53-
// log << std::numeric_limits<std::uint16_t>::max() << "\n";
54-
55-
// create an empty valarray
56-
std::valarray<float_t> val;
57-
log << val << "\n";
58-
log << val.size() << "\n";
59-
val.resize(1);
60-
val[0] = 3.14;
61-
log << val << "\n";
62-
63-
val.resize(2);
64-
val[1] = 2.71;
65-
log << val << "\n";
66-
67-
// create a valarray of size 5, initialized with values 0, 1, 2, 3, 4
68-
std::valarray<int> v{0, 1, 2, 3, 4};
69-
70-
// print the elements of the valarray
71-
log << v << "\n";
72-
73-
// apply a function to each element of the valarray
74-
auto f = [](int x) { return x * x; };
75-
auto v2 = std::valarray<int>(v.apply(f));
76-
77-
log << v2 << "\n";
78-
79-
// perform mathematical operations on the valarray
80-
std::valarray v3 = v + v2;
81-
log << "v3 is of type std::valarray<int>: " << std::is_same_v<decltype(v3), std::valarray<int>> << "\n";
82-
log << v3 << "\n";
83-
84-
auto v4 = v3.apply([](int x) { return x / 2; });
85-
log << "v4 is of type std::valarray<int>: " << std::is_same_v<decltype(v4), std::valarray<int>> << "\n";
86-
log << v4 << "\n";
87-
88-
// increment the value in v
89-
v += 1;
90-
log << v << "\n";
91-
92-
// compute the sum of the valarray
93-
int sum = v.sum();
94-
log << "sum of v: " << sum << "\n";
95-
96-
// compute the square of each element of the valarray
97-
std::valarray<int> v_squared = v * v;
98-
log << v_squared << "\n";
99-
100-
// compute the dot product of v and v_squared
101-
int dot_product = (v * v_squared).sum();
102-
log << "dot product of v and v_squared: " << dot_product << "\n";
103-
104-
v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
105-
log << v << "\n";
106-
107-
// create a slice with start index 2, length 3, and stride 2
108-
// s[2] = 3
109-
// s[2 + 2] == s[4] = 5
110-
// s[2 + 2 + 2] == s[6] = 7
111-
std::slice s{2, 3, 2};
112-
log << typeid(s).name() << "\n"; // class std::slice
113-
114-
// use the slice to create a new valarray
115-
std::valarray<int> result = v[s];
116-
// print the new valarray
117-
log << result << "\n";
118-
119-
// change the value of the original valarray
120-
v[s] = 0;
121-
// print the original valarray
122-
log << v << "\n";
123-
124-
return 0;
66+
static_assert(std::is_arithmetic_v<std::uint8_t>);
67+
68+
// log << std::is_arithmetic_v<std::uint8_t> << "\n";
69+
// log << std::numeric_limits<std::uint16_t>::min() << "\n";
70+
// log << std::numeric_limits<std::uint16_t>::max() << "\n";
71+
72+
// create an empty valarray
73+
std::valarray<float_t> val;
74+
log << val << "\n";
75+
log << val.size() << "\n";
76+
val.resize(1);
77+
val[0] = 3.14F;
78+
log << val << "\n";
79+
80+
val.resize(2);
81+
val[1] = 2.71F;
82+
log << val << "\n";
83+
84+
// create a valarray of size 5, initialized with values 0, 1, 2, 3, 4
85+
std::valarray<int> v{0, 1, 2, 3, 4};
86+
87+
// print the elements of the valarray
88+
log << v << "\n";
89+
90+
// apply a function to each element of the valarray
91+
auto f = [](int x) -> int { return x * x; };
92+
std::valarray v2 = std::valarray<int>(v.apply(f));
93+
94+
log << v2 << "\n";
95+
96+
// perform mathematical operations on the valarray
97+
std::valarray v3 = v + v2;
98+
log << "v3 is of type std::valarray<int>: "
99+
<< std::is_same_v<decltype(v3), std::valarray<int>> << "\n";
100+
log << v3 << "\n";
101+
102+
auto v4 = v3.apply([](int x) -> int { return x / 2; });
103+
log << "v4 is of type std::valarray<int>: "
104+
<< std::is_same_v<decltype(v4), std::valarray<int>> << "\n";
105+
log << typeid(v4).name() << "\n"; // class std::valarray<int>
106+
#if defined(__GNUC__)
107+
log << type_name<decltype(v4)>() << "\n";
108+
#elif defined(_MSC_VER)
109+
log << v4 << "\n";
110+
#endif
111+
112+
// increment the value in v
113+
v += 1;
114+
log << v << "\n";
115+
116+
// compute the sum of the valarray
117+
int sum = v.sum();
118+
log << "sum of v: " << sum << "\n";
119+
120+
// compute the square of each element of the valarray
121+
std::valarray<int> v_squared = v * v;
122+
log << v_squared << "\n";
123+
124+
// compute the dot product of v and v_squared
125+
int dot_product = (v * v_squared).sum();
126+
log << "dot product of v and v_squared: " << dot_product << "\n";
127+
128+
v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
129+
log << v << "\n";
130+
131+
// create a slice with start index 2, length 3, and stride 2
132+
// s[2] = 3
133+
// s[2 + 2] == s[4] = 5
134+
// s[2 + 2 + 2] == s[6] = 7
135+
std::slice s{2, 3, 2};
136+
log << typeid(s).name() << "\n"; // class std::slice
137+
#if defined(__GNUC__)
138+
log << type_name<decltype(s)>() << "\n";
139+
#endif
140+
141+
// use the slice to create a new valarray
142+
std::valarray<int> result = v[s];
143+
// print the new valarray
144+
log << result << "\n";
145+
146+
// change the value of the original valarray
147+
v[s] = 0;
148+
// print the original valarray
149+
log << v << "\n";
150+
151+
return 0;
125152
}
File renamed without changes.

0 commit comments

Comments
 (0)