Re: Remove elements without losing capacity

2022-10-24 Thread Nick Treleaven via Digitalmars-d-learn
On Tuesday, 4 October 2022 at 18:18:41 UTC, Ali Çehreli wrote: A related topic is how the "end slice" never loses that capacity: void main() { auto a = [ 1, 2 ]; auto b = a; assert(a.capacity != 0); assert(b.capacity != 0); b.length--; assert(b.capacity == 0);

Re: Remove elements without losing capacity

2022-10-04 Thread Riccardo M via Digitalmars-d-learn
On Tuesday, 4 October 2022 at 18:18:41 UTC, Ali Çehreli wrote: On 10/4/22 10:59, Riccardo M wrote: > The inherent reason for `remove` to cancel previous capacity and > requiring new allocations is exactly to prevent overwriting data that > could be owned by something else? Yes. A related

Re: Remove elements without losing capacity

2022-10-04 Thread Ali Çehreli via Digitalmars-d-learn
On 10/4/22 10:59, Riccardo M wrote: > The inherent reason for `remove` to cancel previous capacity and > requiring new allocations is exactly to prevent overwriting data that > could be owned by something else? Yes. A related topic is how the "end slice" never loses that capacity: void main()

Re: Remove elements without losing capacity

2022-10-04 Thread Riccardo M via Digitalmars-d-learn
On Tuesday, 4 October 2022 at 15:42:21 UTC, Steven Schveighoffer wrote: Yes, you use `assumeSafeAppend`: ```d arr.length--; arr.assumeSafeAppend; assert(arr.capacity != 0); ``` Now, I want to clarify that you should only use this if you are sure you are done with the data you removed at the

Re: Remove elements without losing capacity

2022-10-04 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/4/22 11:22 AM, Riccardo M wrote: Is it possible to remove elements from a range without losing its capacity? ``` void main() {     import std.algorithm.mutation : remove, SwapStrategy;     import std.stdio : writeln;     int[] arr = [1, 2, 3, 2, 4, 2, 5, 2];     assert(arr.length ==