|
15 | 15 | * |
16 | 16 | */ |
17 | 17 |
|
18 | | -#include<algorithm> |
19 | 18 | #include<cassert> |
20 | | -#include<concepts> |
| 19 | +#include<cstdint> |
21 | 20 | #include<iostream> |
22 | | -#include<limits> |
| 21 | +#include<memory> |
23 | 22 | #include<span> |
| 23 | +#include<string> |
24 | 24 | #include<type_traits> |
| 25 | +#include<typeinfo> |
25 | 26 | #include<valarray> |
26 | 27 |
|
| 28 | +#if defined(__GNUC__) |
| 29 | +#include<cxxabi.h> |
| 30 | + |
| 31 | +template<typename T> [[nodiscard]] std::stringtype_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 | + |
27 | 40 | #ifndef log |
28 | 41 | #definelog std::cout << __LINE__ <<":" << std::boolalpha |
29 | 42 | #endif |
30 | 43 |
|
31 | 44 | 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,conststd::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 <<","; |
40 | 53 | } |
41 | | - os <<"]"; |
42 | | -return os; |
| 54 | + os << values[values.size() -1]; |
| 55 | + } |
| 56 | + os <<"]"; |
| 57 | +return os; |
43 | 58 | } |
44 | 59 |
|
45 | 60 | 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; |
47 | 64 |
|
48 | 65 | intmain() { |
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 | | -return0; |
| 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 | +return0; |
125 | 152 | } |