www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Mixing std.container.array and Appender

reply axricard <axelrwiko gmail.com> writes:
Hello, is there some structure in standard library that acts like 
Appender but works also on std.container.array Arrays ?
I'd like to have the capacity of Arrays doubling every time new 
memory is allocated.
Appender requires the input to be a dynamic array, which is not 
the case for std.container.array Arrays.
Feb 04
next sibling parent reply IchorDev <zxinsworld gmail.com> writes:
On Wednesday, 4 February 2026 at 08:45:06 UTC, axricard wrote:
 Hello, is there some structure in standard library that acts 
 like Appender but works also on std.container.array Arrays ?
 I'd like to have the capacity of Arrays doubling every time new 
 memory is allocated.
 Appender requires the input to be a dynamic array, which is not 
 the case for std.container.array Arrays.
`Appender` only exists so that you can continually append to a dynamic array without the GC having to look up its `capacity` for every append. `Array` already keeps track of its own [`capacity`](https://dlang.org/phobos/std_container_array.ht `Array` on its own.
Feb 06
parent reply axricard <axelrwiko gmail.com> writes:
On Saturday, 7 February 2026 at 04:31:33 UTC, IchorDev wrote:
 On Wednesday, 4 February 2026 at 08:45:06 UTC, axricard wrote:
 Hello, is there some structure in standard library that acts 
 like Appender but works also on std.container.array Arrays ?
 I'd like to have the capacity of Arrays doubling every time 
 new memory is allocated.
 Appender requires the input to be a dynamic array, which is 
 not the case for std.container.array Arrays.
`Appender` only exists so that you can continually append to a dynamic array without the GC having to look up its `capacity` for every append. `Array` already keeps track of its own [`capacity`](https://dlang.org/phobos/std_container_array.ht `Array` on its own.
I see your point, but my question was more about an other nice property of Appender's: they double their size every time they need to re-allocate, which is not the case in for std.container Arrays. Do I have to write my own wrapper to have such behavior with Array's ? ```D import std; void append(T)(ref T arr, int elem) { arr ~= elem; writefln("capacity after append: %u", arr.capacity); } void main() { writeln("Appender"); auto app = appender!(int[])(); foreach(i; iota(0, 20)) app.append(i); writeln("\n============="); writeln("Array"); auto arr = Array!(int)(); foreach(i; iota(0, 20)) arr.append(i); } ``` Output: ``` Appender capacity after append: 8 capacity after append: 8 capacity after append: 8 capacity after append: 8 capacity after append: 8 capacity after append: 8 capacity after append: 8 capacity after append: 8 capacity after append: 16 capacity after append: 16 capacity after append: 16 capacity after append: 16 capacity after append: 16 capacity after append: 16 capacity after append: 16 capacity after append: 16 capacity after append: 32 capacity after append: 32 capacity after append: 32 capacity after append: 32 ============= Array capacity after append: 1 capacity after append: 2 capacity after append: 4 capacity after append: 4 capacity after append: 7 capacity after append: 7 capacity after append: 7 capacity after append: 11 capacity after append: 11 capacity after append: 11 capacity after append: 11 capacity after append: 17 capacity after append: 17 capacity after append: 17 capacity after append: 17 capacity after append: 17 capacity after append: 17 capacity after append: 26 capacity after append: 26 capacity after append: 26 ```
Feb 09
parent IchorDev <zxinsworld gmail.com> writes:
On Monday, 9 February 2026 at 08:37:25 UTC, axricard wrote:
 I see your point, but my question was more about an other nice 
 property of Appender's: they double their size every time they 
 need to re-allocate, which is not the case in for std.container 
 Arrays. Do I have to write my own wrapper to have such behavior 
 with Array's ?
Yes because that is merely an implementation detail.
Feb 09
prev sibling parent reply user1234 <user1234 12.de> writes:
On Wednesday, 4 February 2026 at 08:45:06 UTC, axricard wrote:
 Hello, is there some structure in standard library that acts 
 like Appender but works also on std.container.array Arrays ?
 I'd like to have the capacity of Arrays doubling every time new 
 memory is allocated.
 Appender requires the input to be a dynamic array, which is not 
 the case for std.container.array Arrays.
To the cost of small run-time overhead one solution is to forward appending in a custom function, e.g ```d void append(T)(ref T arr, int elem) { arr ~= elem; static if (is(T == std.container.array.Array!int)) arr.reserve(nextPow2(arr.capacity-1)); writefln("capacity after append: %u", arr.capacity); } ```
Feb 09
parent user1234 <user1234 12.de> writes:
On Monday, 9 February 2026 at 10:53:32 UTC, user1234 wrote:
 On Wednesday, 4 February 2026 at 08:45:06 UTC, axricard wrote:
 Hello, is there some structure in standard library that acts 
 like Appender but works also on std.container.array Arrays ?
 I'd like to have the capacity of Arrays doubling every time 
 new memory is allocated.
 Appender requires the input to be a dynamic array, which is 
 not the case for std.container.array Arrays.
To the cost of small run-time overhead one solution is to forward appending in a custom function, e.g ```d void append(T)(ref T arr, int elem) { arr ~= elem; static if (is(T == std.container.array.Array!int)) arr.reserve(nextPow2(arr.capacity-1)); writefln("capacity after append: %u", arr.capacity); } ```
my bad, I meant ```d arr.reserve(nextPow2(arr.length+1)); ```
Feb 09