Can assumeSafeAppend() grab more and more capacity?
ag0aep6g via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jun 6 22:43:06 PDT 2017
On 06/07/2017 12:12 AM, Ali Çehreli wrote:
> On 06/06/2017 12:13 PM, Jesse Phillips wrote:
> > On Monday, 5 June 2017 at 23:17:46 UTC, Ali Çehreli wrote:
> >> auto a = [ 1, 2, 3, 4 ];
> >> auto b = a;
[...]
>
> The only issue remaining for me is the part that you've quoted:
Jesse Phillips didn't quote the spec. I guess you mean me.
For reference, the spec quote again [1]: "one may use the .capacity
property to determine how many elements can be appended to the array
without reallocating."
> If we
> can trust capacity per spec, like we would trust after calling
> reserve(), then a and b in my code above are counter examples where both
> a and b have capacity initially but one of them will lose its capacity
> as soon as the other one gains an element.
`reserve` works the same. `reserve`d capacity is still capacity and it
can get snapped away by another slice.
----
void main()
{
import std.stdio;
int[] foo;
writeln(foo.capacity); /* 0 */
foo.reserve(10);
writeln(foo.capacity); /* 15 */
int[] bar = foo;
bar ~= 1;
writeln(foo.capacity); /* 0 -- bar took the capacity */
writeln(bar.capacity); /* 15 */
}
----
You understand the spec to say that because `foo.capacity` is 15 at one
point, you should then be able to put 15 elements into `foo` without
relocation. And what `bar` does in the meantime shouldn't matter.
I don't think the spec is supposed to make that strong a guarantee, but
I see how it can be interpreted that way. Maybe it should be
reworded/amended to describe the actual behavior more precisely.
[1] http://dlang.org/spec/arrays.html#resize
More information about the Digitalmars-d-learn
mailing list