Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added xrange adaptor and fix for xstepped_range size #139

Merged
merged 3 commits into from
Feb 13, 2017

Conversation

wolfv
Copy link
Member

@wolfv wolfv commented Feb 12, 2017

still missing tests.

@wolfv
Copy link
Member Author

wolfv commented Feb 13, 2017

I've added tests to this PR.

Note the fix for xstepped_range. Because of default floor rounding, the last value was not selected in some cases. E.g.

xarray<double> a = {1,2,3,4,5};
std::cout << view(a, range(2, 5, 2)); // {3}; --> but should be {3, 5} to match numpy.

{
}

template <class MIN = A, class MAX = B, class STEP = C>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MIN and MAX should be avoided as names, they can lead to conflict with potentially MIN and MAX macros (I know it is really ugly but you can't imagine the number of such horrors you can find in production code). It could be MINI and MAXI or whatever else, as long as it can't conflict with macros.

template <class A, class B, class C>
struct xrange_adaptor
{
xrange_adaptor(A min, B max, C step)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

min and max should be avoided as names, like their uppercase counterpart. Windows define min and max macros lowercase (yes, I'm serious ...); even if we can prevent their generation with the NOMINMAX macro, we should avoid possible conflicts.

else
{
return xstepped_range<int>(size - 1, m_max, m_step);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can simplify a bit this code:

std::size_t min_arg = m_step > 0 ? 0 : size - 1;
return xstepped_range<int>(min_arg, m_max, m_step);

else
{
return xstepped_range<int>(m_min, -1, m_step);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same remark as previous

else
{
return xstepped_range<int>(size - 1, -1, m_step);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same remark as previous

};

template <class A, class B>
inline auto range(A min, B max)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same remark as previous about min and max names.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the "old" range functions also change the parameter names?

Copy link
Member

@JohanMabille JohanMabille Feb 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that something we missed when the first implementation was merged.

}

template <class A, class B, class C>
inline auto range(A min, B max, C step)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same remark as previous


template <class MIN = A, class MAX = B, class STEP = C>
std::enable_if_t<!std::is_integral<MIN>::value && std::is_integral<MAX>::value && std::is_integral<STEP>::value, xstepped_range<int>>
get(std::size_t size)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can inline this function and its overloads.

inline auto xnone()
{
return xnone_tag();
}
Copy link
Member

@JohanMabille JohanMabille Feb 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use placeholders here, so we have a really cool syntax:

auto v = view(exp, range(2, _, 2));

Although _ is not a in the STL yet, it is in the MPL and it may become available in the STL in the future. So we have to be very cautious. A good approach would be to nest it in a placeholder namespace, forcing the client to explicitly use the namespace if he wants to use the placeholder syntax. This way, when the _ placeholder comes in the standard, we can just replace our code by an import of the standard one.
A possible implementation:

// The following is nested in xt namespace of course
namespace placeholders
{
    // xtensor universal placeholder
    struct xtuph
    {
    };

    constexpr xtuph _{};
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats ... really nice!

@JohanMabille
Copy link
Member

Closes #120.

Good job !

@JohanMabille JohanMabille merged commit cea0bce into xtensor-stack:master Feb 13, 2017
@wolfv
Copy link
Member Author

wolfv commented Feb 13, 2017

cool!

@wolfv wolfv deleted the xrange_adaptor branch October 30, 2017 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants