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

type inconsistency between Collection::size() and index for const object accessors #406

Closed
c-dilks opened this issue Apr 19, 2023 · 4 comments · Fixed by #408
Closed

type inconsistency between Collection::size() and index for const object accessors #406

c-dilks opened this issue Apr 19, 2023 · 4 comments · Fixed by #408

Comments

@c-dilks
Copy link
Contributor

c-dilks commented Apr 19, 2023

  • PODIO version: master

There is a difference between the type size_t returned by size() from a Collection and the type of the index used in the const object accessors, unsigned int:

size_t size() const final;

vs.

/// Returns the const object of given index
{{ class.bare_type }} operator[](unsigned int index) const;
/// Returns the object of a given index
Mutable{{ class.bare_type }} operator[](unsigned int index);
/// Returns the const object of given index
{{ class.bare_type }} at(unsigned int index) const;
/// Returns the object of given index
Mutable{{ class.bare_type }} at(unsigned int index);

Is there a particular reason for this difference?

cf. std::vector, where we have (ignoring C++20 changes for clarity):

size_type size();
reference at( size_type pos );

in other words, std::vector::at takes the same type that std::vector::size() returns.

@c-dilks c-dilks changed the title type inconstency between Collection::size() and index for const object accessors type inconsistency between Collection::size() and index for const object accessors Apr 19, 2023
@tmadlener
Copy link
Collaborator

You are right. I don't see a reason to not be consistent here. This should be fairly straight forward to fix, I think as these are directly passed to the underlying std::deque, e.g.

Mutable{{ class.bare_type }} {{ collection_type }}::operator[](unsigned int index) {
return Mutable{{ class.bare_type }}(m_storage.entries[index]);
}

Out of of curiosity: Does something change with c++20 in this respect? I am asking since you mentioned it explicitly.

@c-dilks
Copy link
Contributor Author

c-dilks commented Apr 20, 2023

According to cppreference.com it's effectively size_type no matter the version:
0
1
2

Also, std::deque::operator[] and std::deque::size() use size_type:
4

Since vector and deque both use std::allocator by default, where size_type is std::size_t, I would assume vector::size_type is the same as deque::size_type (reference SO thread).

@c-dilks
Copy link
Contributor Author

c-dilks commented Apr 20, 2023

I'm also now wondering if we should switch to size_t for VectorMembers and OneToManyRelations, but at least for those the type is always unsigned int. For example:

{% macro multi_relation_handling(relations, get_syntax, with_adder=False) %}
{% for relation in relations %}
{% if with_adder %}
void {{ relation.setter_name(get_syntax, is_relation=True) }}({{ relation.full_type }});
{% endif %}
unsigned int {{ relation.name }}_size() const;
{{ relation.full_type }} {{ relation.getter_name(get_syntax) }}(unsigned int) const;
std::vector<{{ relation.full_type }}>::const_iterator {{ relation.name }}_begin() const;
std::vector<{{ relation.full_type }}>::const_iterator {{ relation.name }}_end() const;
podio::RelationRange<{{ relation.full_type }}> {{ relation.getter_name(get_syntax) }}() const;
{% endfor %}
{%- endmacro %}

Ultimately it looks like Collection::size() is the only odd one out, returning size_t.

@tmadlener
Copy link
Collaborator

It also looks like we have a size_t in the RelationRange that is one of the returns above.

/// convenience overload for size
size_t size() const {
return m_size;

For consistency with the usual c++ containers I think it would be good to have size_t everywhere. From a purely functional point of view, it doesn't really matter probably, because the ObjectID uses an int internally at the moment, and that is essentially the limiting factor for the number of elements in a collection:

/// index of object in collection
int index;

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants