This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Public Function
Add count new elements at position index, which have undefined values. Returns a slice pointing to the newly allocated elements, which becomes invalid after various ArrayList operations. Invalidates pre-existing pointers to elements at and after index. Invalidates all pre-existing element pointers if capacity must be increased to accommodate the new elements. Asserts that the index is in bounds or equal to the length.
pubfnaddManyAt(
self: *Self,
gpa: Allocator,
index: usize,
count: usize,
) Allocator.Error![]T {
constnew_len = tryaddOrOom(self.items.len, count);
self.pointer_stability.assertUnlocked();
if (self.capacity >= new_len)
returnaddManyAtAssumeCapacity(self, index, count);
// Here we avoid copying allocated but unused bytes by// attempting a resize in place, and falling back to allocating// a new buffer and doing our own copy. With a realloc() call,// the allocator implementation would pointlessly copy our// extra capacity.constnew_capacity = Aligned(T, alignment).growCapacity(new_len);
constold_memory = self.allocatedSlice();
if (gpa.remap(old_memory, new_capacity)) |new_memory| {
self.items.ptr = new_memory.ptr;
self.capacity = new_memory.len;
returnaddManyAtAssumeCapacity(self, index, count);
}
// Make a new allocation, avoiding `ensureTotalCapacity` in order// to avoid extra memory copies.constnew_memory = trygpa.alignedAlloc(T, alignment, new_capacity);
constto_move = self.items[index..];
@memcpy(new_memory[0..index], self.items[0..index]);
@memcpy(new_memory[index + count ..][0..to_move.len], to_move);
gpa.free(old_memory);
self.items = new_memory[0..new_len];
self.capacity = new_memory.len;
// The inserted elements at `new_memory[index..][0..count]` have// already been set to `undefined` by memory allocation.returnnew_memory[index..][0..count];
}
Add
countnew elements at positionindex, which haveundefinedvalues. Returns a slice pointing to the newly allocated elements, which becomes invalid after variousArrayListoperations. Invalidates pre-existing pointers to elements at and afterindex. Invalidates all pre-existing element pointers if capacity must be increased to accommodate the new elements. Asserts that the index is in bounds or equal to the length.