Skip to content

Added Jarvis March for C++ #283

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

Merged
merged 8 commits into from
Aug 3, 2018

Conversation

Gorzoid
Copy link
Contributor

@Gorzoid Gorzoid commented Jul 20, 2018

There's a few things I was unsure of while making this, for input I used template ForwardIter, which allows it to be used with multiple datastructures. I wasn't sure, however whether to return a vector, or use an OutputIter argument. The vector option is simpler but kind of ruins the point of user template iterators as input. Due to this algorithm not having a predetermined output size (up to the size of the points), I feel using an OutputIter might lead to buffer overflows if used incorrectly.

Seeing as this is meant to be informative rather than a standardized algorithm, I feel like I should just keep the vector return. Using a vector as the input would hardly change the code and I still kinda like using range based algorithms.

@june128 june128 added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Jul 20, 2018
@Butt4cak3
Copy link
Contributor

Hey @Gorzoid and welcome and thanks for contributing! We currently have a lot of open C++ pull requests, so I think we're a little short on avilable C++ reviewers. Your PR might take a little longer than usual to get reviewed because of this. I hope you don't mind.

@Gorzoid
Copy link
Contributor Author

Gorzoid commented Jul 21, 2018

That's grand, yeah I can see there are a lot C++ PR's.

next_point_it = std::max_element(
start,
end,
[p1](const Point& p2, const Point& p3){
Copy link
Contributor

Choose a reason for hiding this comment

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

[&p1] is slightly better.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is actually incorrect, [p1] will generate better code since the Point struct is fairly small (and so you can pass it in registers). Since you don't need to mutate the captured variable, I would stick with the by value version

};

template<typename ForwardIter>
std::vector<Point> jarvis_march(ForwardIter start, ForwardIter end)
Copy link
Contributor

Choose a reason for hiding this comment

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

Take as input const std::vector<Point> &

};

template<typename ForwardIter>
std::vector<Point> jarvis_march(ForwardIter start, ForwardIter end)
Copy link
Contributor

Choose a reason for hiding this comment

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

It crashes in case of empty set. Should return an empty vector.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is possible to make it work without making empty vector a special case. For instance use while loop instead of do...while. Can you play around and try to refactor code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I take out the do...while I need to add extra code to add the first element. Anyway the main problem is the min_element to find the left most point. If empty it will return points.end(), I can't think of any way to resolve that would result in shorter code.

mika314
mika314 previously approved these changes Jul 22, 2018
leios
leios previously approved these changes Aug 3, 2018
Copy link
Member

@leios leios left a comment

Choose a reason for hiding this comment

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

If it's good enough for antonte, it's good enough for me. (sorry, I missed this one a while ago)

@leios leios dismissed stale reviews from mika314 and themself via 1618007 August 3, 2018 02:33
@leios leios merged commit 4017a1e into algorithm-archivists:master Aug 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants