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

Add front and back to list,vector, deque and array containers #695

Merged
merged 6 commits into from
Jul 10, 2023

Conversation

HGuillemet
Copy link
Contributor

Add front() and back() to the containers that support them.
This is for convenience only, since front() should be equivalent to begin().access(), so feel free to reject it.

@saudet
Copy link
Member

saudet commented Jul 9, 2023

This is for convenience only, since front() should be equivalent to begin().access(), so feel free to reject it.

Right, so generate methods like these instead just like for push_back() and pop_back():

"public " + valueType.annotations + valueType.javaName + " front() { return begin().access(); }\n"
"public " + valueType.annotations + valueType.javaName + " back() { return rbegin().access(); }\n"

That only works if we can assume rbegin() is always available, so maybe just do front() for now if you don't need back(). It looks like std::prev() is the way to go for that, but it's only available since C++11 and may not work with all custom templates either anyway.

@saudet
Copy link
Member

saudet commented Jul 9, 2023

Oh, it looks like std::prev() just loops over all elements until it reaches the end:
https://en.cppreference.com/w/cpp/iterator/advance
Please do that here as well.

@HGuillemet
Copy link
Contributor Author

I understand that you'd like to have the option to define containers that follow some model (like std::vector) but don't have all the standard API of this model. For instance some "vector" templates that don't implement front(). Right ?

In this case we'd probably not try to implement non-essential API like front and back in Java, since the best-suited implementation can vary. As you said, back could be implemented as rbegin().access(), or get(size()-1) or end().prev(), etc... That choice may be important for long structures.

I was mainly thinking that users would like an easy way to get a pointer to the underlying arrays for continuous array-like structures. But begin().access() or get(0) is fine.

So let's give up this idea ? Or only implement front ?

@saudet
Copy link
Member

saudet commented Jul 9, 2023

Yes, using get() sounds like a better idea, for both front() and back().

@HGuillemet
Copy link
Contributor Author

... if we have a long index.

What about this last commit ?

@saudet
Copy link
Member

saudet commented Jul 9, 2023

Seems to be working well. We could remove the annotations from valueType.javaName though.

@saudet
Copy link
Member

saudet commented Jul 10, 2023

It would be nice to have those that uses get() be put just before the get() function. It also makes more sense to have it generated next to the get() function it uses anyway. The one that uses the iterator, before insert() would be nicer I think.

@saudet saudet merged commit ec90945 into bytedeco:master Jul 10, 2023
@HGuillemet HGuillemet deleted the add_front_back branch July 10, 2023 14:49
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