What is the purpose of std::rank? - c++

I just ran into std::rank and I don't really understand what it could be used for. I understand what it does, but can someone please give me a few use cases for it? I wasn't able to find anything useful on a search here.

Nice question.
I'm trying to learn C++11, so I could write something silly but ... suppose you want find the maximum value in a multidimensional array.
I tried to answer this question with std::rank (and SFINAE)
#include <iterator>
#include <iostream>
#include <type_traits>
template <typename X,
typename = typename std::enable_if<0U == std::rank<X>::value>::type>
X maxRank (X const & x)
{ return x; }
template <typename X,
typename = typename std::enable_if<0U != std::rank<X>::value>::type>
typename std::remove_all_extents<X>::type maxRank (X const & x)
{
auto it = std::begin(x);
auto ret = maxRank(*it);
for ( ; it != std::end(x) ; ++it )
{
auto val = maxRank(*it);
if ( val > ret )
ret = val;
}
return ret;
}
int main ()
{
int a0 = 12;
short a1[] = { 23, 7, 42, -19, 0, 95 };
unsigned a2[][2] = { {8U, 9U}, {0U, 77U}, {11U, 9U}, {5U, 3U} };
long a3[][3][2] = { { {123L, 3L}, {-45L, 77L}, {-12L, 678L} },
{ {1L, -54L}, {23L, 99L}, {56L, 1234L} },
{ {-4L, -12L}, {1L, 0L}, {122L, 19L} },
{ {2L, 23L}, {55L, 19L}, {2L, 99L} } };
std::cout << "maxRank a0 = " << maxRank(a0) << '\n';
std::cout << "maxRank a1 = " << maxRank(a1) << '\n';
std::cout << "maxRank a2 = " << maxRank(a2) << '\n';
std::cout << "maxRank a3 = " << maxRank(a3) << '\n';
return 0;
}

Related

visualize boost sml state machine

I am building a larger state machine in boost sml for the first time and need a way to visualize (e.g. export to graphviz) the whole state machine. Any idea how one could do that? Is there any way to iterate over the structure of the state machine and print it?
Disclaimer: I have zero experience with SML (had to find where it lives).
First Attempt, Mmmm
However, trying some things with the visit_current_states interface, I came up with this ... not so nice implementation that maps out a (composite) state machine, given a list of events:
#include <boost/sml.hpp>
namespace sml = boost::sml;
namespace aux = sml::aux;
struct e1 {};
struct e2 {};
struct e3 {};
struct e4 {};
struct e5 {};
struct sub {
auto operator()() const {
using namespace sml;
// clang-format off
return make_transition_table(
*"idle"_s + event<e3> = "sub1"_s
, "sub1"_s + event<e4> = X
);
// clang-format on
}
};
struct composite {
auto operator()() const {
using namespace sml;
// clang-format off
return make_transition_table(
*"idle"_s + event<e1> = "s1"_s
, "s1"_s + event<e2> = state<sub>
, state<sub> + event<e5> = X
);
// clang-format on
}
};
#include <boost/hana.hpp>
#include <boost/core/demangle.hpp>
#include <iostream>
#include <iomanip>
namespace mapper {
namespace hana = boost::hana;
using namespace std::string_literals;
using boost::core::demangle;
template <typename F> struct ycombine {
ycombine(F f):f(f) {}
F f;
template <typename... A>
auto operator()(A... a) const { return f(*this, a...); };
};
hana::tuple<e1,e2,e3,e4,e5> events;
template <class TSM> class Vis {
public:
explicit Vis(const TSM& sm, std::string prefix = "") : sm_{ sm }, prefix(prefix) {}
template <class TSub>
void operator()(aux::string<boost::sml::sm<TSub>>) const {
auto subname = aux::get_type_name<TSub>();
Vis nvis(sm_, prefix + '/' + subname);
sm_.template visit_current_states<aux::identity<TSub>>(nvis);
prefix = nvis.prefix;
}
template <class TState> void operator()(TState state) const {
prefix += "/"s + state.c_str();
}
private:
const TSM& sm_;
public:
mutable std::string prefix;
};
template <typename SM>
std::string get_current(SM const& sm) {
Vis<SM> v{sm};
sm.visit_current_states(v);
return v.prefix;
};
}
int main() {
using namespace mapper;
auto recurse = ycombine {
[](auto self, auto sm) {
hana::for_each(events, [=](auto ev) {
auto clone = sm;
auto from = get_current(clone);
clone.process_event(ev);
auto to = get_current(clone);
if (from != to) {
std::cout
<< std::quoted(from) << " -> "
<< std::quoted(to)
<< " [label=" << std::quoted(demangle(typeid(ev).name())) << "]\n";
self(clone);
}
});
} };
std::cout << "digraph {\n";
recurse(sml::sm<composite>{});
std::cout << "}\n";
}
Though I have no idea how useful this is, at least I could render the result with graphviz:
Second Attempt - READ THE FINE MANUAL :derp:
It looks like there was a cleaner way to do these things, especially the presence of the nested typedef transitions when reading the latest examples:
// $CXX -std=c++14 plant_uml.cpp
#include <boost/sml.hpp>
#include <cassert>
#include <iostream>
#include <string>
#include <typeinfo>
namespace sml = boost::sml;
struct e1 {};
struct e2 {};
struct e3 {};
struct e4 {};
struct guard {
bool operator()() const { return true; }
} guard;
struct action {
void operator()() {}
} action;
struct plant_uml {
auto operator()() const noexcept {
using namespace sml;
return make_transition_table(
*"idle"_s + event<e1> = "s1"_s
, "s1"_s + event<e2> [ guard ] / action = "s2"_s
, "s2"_s + event<e3> [ guard ] = "s1"_s
, "s2"_s + event<e4> / action = X
);
}
};
template <class T>
void dump_transition() noexcept {
auto src_state = std::string{sml::aux::string<typename T::src_state>{}.c_str()};
auto dst_state = std::string{sml::aux::string<typename T::dst_state>{}.c_str()};
if (dst_state == "X") {
dst_state = "[*]";
}
if (T::initial) {
std::cout << "[*] --> " << src_state << std::endl;
}
std::cout << src_state << " --> " << dst_state;
const auto has_event = !sml::aux::is_same<typename T::event, sml::anonymous>::value;
const auto has_guard = !sml::aux::is_same<typename T::guard, sml::front::always>::value;
const auto has_action = !sml::aux::is_same<typename T::action, sml::front::none>::value;
if (has_event || has_guard || has_action) {
std::cout << " :";
}
if (has_event) {
std::cout << " " << boost::sml::aux::get_type_name<typename T::event>();
}
if (has_guard) {
std::cout << " [" << boost::sml::aux::get_type_name<typename T::guard::type>() << "]";
}
if (has_action) {
std::cout << " / " << boost::sml::aux::get_type_name<typename T::action::type>();
}
std::cout << std::endl;
}
template <template <class...> class T, class... Ts>
void dump_transitions(const T<Ts...>&) noexcept {
int _[]{0, (dump_transition<Ts>(), 0)...};
(void)_;
}
template <class SM>
void dump(const SM&) noexcept {
std::cout << "#startuml" << std::endl << std::endl;
dump_transitions(typename SM::transitions{});
std::cout << std::endl << "#enduml" << std::endl;
}
int main() {
sml::sm<plant_uml> sm;
dump(sm);
}
The output, for inspiration:
#startuml
[*] --> idle
idle --> s1 : e1
s1 --> s2 : e2 [guard] / action
s2 --> s1 : e3 [guard]
s2 --> terminate : e4 / action
#enduml
Obviously, not graphviz, but actually looks more "trustworthy" due to the fact that
it resembles some kind of standard UML notation,
was written by the library author(s?)
included details that I wasn't able to (largely because of being unaware of their existence/working)

Sort just one member of the classes in a vector, leaving the other members unchanged

There are tons of answers for sorting a vector of struct in regards to a member variable. That is easy with std::sort and a predicate function, comparing the structs member. Really easy.
But I have a different question. Assume that I have the following struct:
struct Test {
int a{};
int b{};
int toSort{};
};
and a vector of that struct, like for example:
std::vector<Test> tv{ {1,1,9},{2,2,8},{3,3,7},{4,4,6},{5,5,5} };
I do not want to sort the vectors elements, but only the values in the member variable. So the expected output should be equal to:
std::vector<Test> tvSorted{ {1,1,5},{2,2,6},{3,3,7},{4,4,8},{5,5,9} };
I wanted to have the solution to be somehow a generic solution. Then I came up with a (sorry for that) preprocessor-macro-solution. Please see the following example code:
#include <iostream>
#include <vector>
#include <algorithm>
struct Test {
int a{};
int b{};
int toSort{};
};
#define SortSpecial(vec,Struct,Member) \
do { \
std::vector<decltype(Struct::Member)> vt{}; \
std::transform(vec.begin(), vec.end(), std::back_inserter(vt), [](const Struct& s) {return s.Member; }); \
std::sort(vt.begin(), vt.end()); \
std::for_each(vec.begin(), vec.end(), [&vt, i = 0U](Struct & s) mutable {s.Member = vt[i++]; }); \
} while (false)
int main()
{
// Define a vector of struct Test
std::vector<Test> tv{ {1,1,9},{2,2,8},{3,3,7},{4,4,6},{5,5,5} };
for (const Test& t : tv) std::cout << t.a << " " << t.b << " " << t.toSort << "\n";
// Call sort macro
SortSpecial(tv, Test, toSort);
std::cout << "\n\nSorted\n";
for (const Test& t : tv) std::cout << t.a << " " << t.b << " " << t.toSort << "\n";
}
Since macros shouldn't be used in C++, here my questions:
1. Is a solution with the algorithm library possible?
2. Or can this be achieved via templates?
To translate your current solution to a template solution is fairly straight forward.
template <typename T, typename ValueType>
void SpecialSort(std::vector<T>& vec, ValueType T::* mPtr) {
std::vector<ValueType> vt;
std::transform(vec.begin(), vec.end(), std::back_inserter(vt), [&](const T& s) {return s.*mPtr; });
std::sort(vt.begin(), vt.end());
std::for_each(vec.begin(), vec.end(), [&, i = 0U](T& s) mutable {s.*mPtr = vt[i++]; });
}
And we can call it by passing in the vector and a pointer-to-member.
SpecialSort(tv, &Test::toSort);
Somewhow like this (You just need to duplicate, rename and edit the "switchToShort" funtion for the rest of the variables if you want):
#include <iostream>
#include <vector>
struct Test {
int a{};
int b{};
int toSort{};
};
void switchToShort(Test &a, Test &b) {
if (a.toSort > b.toSort) {
int temp = a.toSort;
a.toSort = b.toSort;
b.toSort = temp;
}
}
//void switchToA(Test& a, Test& b) { ... }
//void switchToB(Test& a, Test& b) { ... }
inline void sortMemeberValues(std::vector<Test>& data, void (*funct)(Test&, Test&)) {
for (int i = 0; i < data.size(); i++) {
for (int j = i + 1; j < data.size(); j++) {
(*funct)(data[i], data[j]);
}
}
}
int main() {
std::vector<Test> tv { { 1, 1, 9 }, { 2, 2, 8 }, { 3,3 ,7 }, { 4, 4, 6 }, { 5, 5, 5} };
sortMemeberValues(tv, switchToShort);
//sortMemeberValues(tv, switchToA);
//sortMemeberValues(tv, switchToB);
for (const Test& t : tv) std::cout << t.a << " " << t.b << " " << t.toSort << "\n";
}
With range-v3 (and soon ranges in C++20), you might simply do:
auto r = tv | ranges::view::transform(&Test::toSort);
std::sort(r.begin(), r.end());
Demo

Can I give different behaviours to boost::proto::tag types?

I am trying to use boost proto to lazily evaluate expressions, what I want to do is be able to give different behaviours to tags like +, -, function etc.
function(
terminal(8functionILi2EE)
, plus(
multiplies(
terminal(6tensorILi0EE)
, terminal(6tensorILi1EE)
)
, multiplies(
terminal(6tensorILi2EE)
, terminal(6tensorILi3EE)
)
)
)
For a tree like above, I want to be able to specify how each of the tree nodes should behave.
For eg.
struct context : proto::callable_context< context const >
{
// Values to replace the tensors
std::vector<double> args;
// Define the result type of the zero.
// (This makes the zero_context "callable".)
typedef double result_type;
// Handle the tensors:
template<int I>
double operator()(proto::tag::terminal, tensor<I>) const
{
std::cout << this->args[I] << std::endl;
return this->args[I];
}
template<int I>
void operator()(proto::tag::plus) const
{
std::cout << " + " << std::endl;
}
};
When I do
double result = (_tensorA + _tensorB)(10, 20);
I expect my output to be
10
+
20
But it's just
10
20
Any help would be deeply appreciated! :)
template<int I>
void operator()(proto::tag::plus) const
{
std::cout << " + " << std::endl;
}
The template argument I is non-deducible, so the overload will never be applicable. Drop the template argument:
void operator()(proto::tag::plus) const
{
std::cout << " + " << std::endl;
}
HOWEVER What you really want is intercept the binary operator. Well. Note it's binary. So it has two args:
template<size_t I, size_t J>
void operator()(proto::tag::plus, proto::literal<tensor<I>>&, proto::literal<tensor<J>>&) const {
std::cout << " + " << std::endl;
}
Live On Coliru
However, this blocks further evaluation of the expression tree. Not what you wanted, right. So, let's do a simplisitic re-implementation:
template<size_t I, size_t J>
double operator()(proto::tag::plus, proto::literal<tensor<I>>& a, proto::literal<tensor<J>>& b) const {
auto va = (*this)(proto::tag::terminal{}, a.get());
std::cout << " + " << std::endl;
auto vb = (*this)(proto::tag::terminal{}, b.get());
return va + vb;
}
Live On Coliru
Generic, please
However, something tells me you wanted generic expressions. So t1 + (t2 + t3) should also work, but (t2 + t3) is no literal...
Let's simplify by delegating:
template<typename A, typename B>
double operator()(proto::tag::plus, A& a, A& b) const {
auto va = proto::eval(a, *this);
std::cout << " + " << std::endl;
auto vb = proto::eval(b, *this);
return va + vb;
}
Full Sample
Live On Coliru
#include <boost/proto/proto.hpp>
#include <vector>
namespace proto = boost::proto;
template <size_t N> struct tensor { };
template <size_t N, size_t M> tensor<N+M> operator+(tensor<N>, tensor<M>) { return {}; }
struct context : proto::callable_context< context const >
{
using base_type = proto::callable_context<context const>;
// Values to replace the tensors
std::vector<double> args { 0, 111, 222, 333 };
// Define the result type of the zero.
// (This makes the zero_context "callable".)
typedef double result_type;
// Handle the tensors:
template<size_t I>
double operator()(proto::tag::terminal, tensor<I>) const
{
std::cout << this->args[I] << std::endl;
return this->args[I];
}
template<typename A, typename B>
double operator()(proto::tag::plus, A& a, B& b) const {
auto va = proto::eval(a, *this);
std::cout << " + " << std::endl;
auto vb = proto::eval(b, *this);
return va + vb;
}
};
int main() {
proto::literal<tensor<1> > t1;
proto::literal<tensor<2> > t2;
proto::literal<tensor<3> > t3;
auto r = proto::eval(t1 + (t2 + t3), context());
std::cout << "eval(t1 + (t2 + t3)) = " << r << "\n";
}
Prints
111
+
222
+
333
eval(t1 + (t2 + t3)) = 666

Checking for a specific nested type/tag with boost hana

Having fun with boost::hana. I wish to check for a specific nested type that acts like a tag in another type, so I borrow from hana::when_valid example and defined a class is_S along with its SFINAE-enabled specialization:
#include <iostream>
#include <boost/hana/core/when.hpp>
namespace hana = boost::hana;
#define V(x) std::cout << x << std::endl
struct S_tag { };
struct S {
using tag = S_tag;
};
struct T {
using tag = int;
};
template< typename T, typename = hana::when< true > >
struct is_S {
static constexpr bool value = false;
};
template< typename T >
struct is_S< T, hana::when_valid< typename T::tag > > {
static constexpr bool value = std::is_same<
typename T::tag, S_tag >::value;
};
int main () {
std::cout << "is_S ( S { }) = "; V ((is_S< S >::value));
std::cout << "is_S ( T { }) = "; V ((is_S< T >::value));
std::cout << "is_S (float { }) = "; V ((is_S< float >::value));
return 0;
}
This prints:
$ clang++ -std=c++1z sfinae.cpp && ./a.out | c++filt
is_S ( S { }) = 1
is_S ( T { }) = 0
is_S (float { }) = 0
Is there a simpler/shorter/more succinct way of writing the same check, in keeping with value-type computation of hana philosophy?
Here's what I might write:
#include <boost/hana.hpp>
#include <iostream>
namespace hana = boost::hana;
struct S_tag { };
struct S { using tag = S_tag; };
struct T { using tag = int; };
auto tag_of = [](auto t) -> hana::type<typename decltype(t)::type::tag> {
return {};
};
auto is_S = [](auto t) {
return hana::sfinae(tag_of)(t) == hana::just(hana::type<S_tag>{});
};
int main() {
std::cout << "is_S ( S { }) = " << is_S(hana::type<S>{})() << std::endl;
std::cout << "is_S ( T { }) = " << is_S(hana::type<T>{})() << std::endl;
std::cout << "is_S (float { }) = " << is_S(hana::type<float>{})() << std::endl;
}
I woukd be tempted by:
template<class...T>
constexpr std::integral_constant<bool,false> is_S(T const&...){ return {}; }
template<class T>
constexpr
std::integral_constant<bool,std::is_same<typename T::tag,S_tag>{}>
is_S(T const&){ return {}; }

C++ Sign function - From MatLab?

I'm trying to re-write some MatLab code in C++ and I've come across this:
currentsign = sign(vector(i));
I have looked on the internet and found this link: http://www.mathworks.co.uk/help/techdoc/ref/sign.html
I'm just wondering if there's a sign function in C++? If not, can anyone suggest any tutorials on creating it.
Thank you :)
template <typename T>
int sign (const T &val) { return (val > 0) - (val < 0); }
Credit due to Ambroz Bizjak.
template <typename T>
std::vector<int> sign (const std::vector<T> &v) {
std::vector<int> r(v.size());
std::transform(v.begin(), v.end(), r.begin(), (int(*)(const T&))sign);
return r;
}
Full example on ideone.
I would suggest
First, write a function of functor that takes a single element and returns 1, -1 or 0 depending on the element's value
Second, use std::transform together with this function/functor to take an input container and fill a second container with the desired values
template <typename T>
int signum(const T& val) {
// implement signum logic
}
#include <vector>
#include <algorithm>
int main() {
std::vector<int> data = ....;
std::vector<int> signs(data.size());
std::transform(data.begin(), data.end(), signs.begin(), signum<int>);
}
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
inline int get_signum(int val) {
return val < 0 ? -1
: val == 0 ? 0
: 1;
}
int main() {
std::vector<int> values;
for (int i = -5; i < 6; ++i)
values.push_back(i);
std::vector<int> signum(values.size());
std::transform(values.begin(), values.end(), signum.begin(), get_signum);
for (int i = 0; i < values.size(); ++i) {
std::cout << std::setw(2) << values[i] << ' ' << signum[i] << std::endl;
}
return 0;
}
Well You can do It at compile time using template Specialization.
You can use sign<n>::Positive, sign<n>::Negetive and sign<n>::Zero also you can use sign<n>::Sign which is 1|0|-1 which is same as sign of Matlab.
#include <iostream>
template<int n>
struct sign{
enum{
Positive = (n > 0),
Negetive = (n < 0),
Zero = 0,
Sign = ((n > 0) ? 1 : -1)
};
};
template<>
struct sign<0>{
enum{
Positive = 0,
Negetive = 0,
Zero = 1,
Sign = 0
};
};
int main(){
std::cout << sign<0>::Positive << sign<0>::Negetive << sign<0>::Zero << sign<0>::Sign << std::endl;
std::cout << sign<1>::Positive << sign<1>::Negetive << sign<1>::Zero << sign<1>::Sign << std::endl;
std::cout << sign<-1>::Positive << sign<-1>::Negetive << sign<-1>::Zero << sign<-1>::Sign << std::endl;
return 0;
}
You used to do sign(n) there and here you will do sign<n>::Sign.
C99 has signbit() and copysign(), which seem to be implemented in glibc on Linux. You didn't specify what platform you're on though, so I'm not sure that helps...