-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Expose capacity
in an array
#11507
Comments
I'm actually not sure how much telling the raw capacity is without taking the complexity of the buffer offset into account.
When I only care about adding elements to the back, I can't think of any new use case it could help solving, but I'm happy to hear about them. |
Note that |
I don't think it's broken. You can definitely use it. The definition of "remaining" is just ambiguous: It can be understood as the free capacity between last element and the end of the capacity. However, it can also be the part of the capacity that remains after subtracting the offset. The former is probably better because that's what you're most likely interested in. But both are possible and usable, it's just a matter of subtracting or adding I wouldn't oppose changing the defintion to mean the free space at the end. But I don't think we can do that in place, even in a major release. It would be a silent breaking change. If we change the definition, we need to rename the method. |
I think the public API should return the free spaces before and after the elements in a 2- class Array(T)
def free_capacity
{@offset_to_buffer, @capacity - @offset_to_buffer - @size}
end
end
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# realloc'd, 10 spaces to the right
x << 11
x << 12
# realloc'd, 20 spaces to the left
x.unshift 13
x.free_capacity # => {19, 8} Then I would personally deprecate |
@HertzDevil I think the issue there is that it exposes the internal implementation to the API, which most users don't even know about. Then, if we choose a different internal implementation in the future, then we're setting ourselves up for a breaking change on the API. Perhaps it makes sense to name it more verbose with something like |
My point is that any public API of |
I'm with @HertzDevil. If in the future we decide to not have |
Yeah that's fair - if we remove the "left side" capacity in the future, then the API would just expose 0 |
Just my 2 cents, but please don't expose internal implementation details in the API. If you do that, you will never be able to change the internal implementation without introducing at least one breaking change. I can't think of any use case where knowing the remaining capacity, or any capacity at all, helps you take a decision in order to X. |
I could see having an |
If #11485 sees the light, it would be handy to have a way of knowing the capacity of the array. Currently, there is an undocumented public method
remaining_capacity
, which returns@capacity - @offset_to_buffer
. This is not ideal, as it only calculates how many elements can be added at the end of the array. I suggest to deprecate it, and instead have a public getter for@capacity
. Then, the number of elements that can be added to an arraya
can be calculated asa.capacity - a.size
.The text was updated successfully, but these errors were encountered: