DoxigAlpha

addManyAt

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.

Function parameters

Parameters

#
self:*Self
index:usize
count:usize

Deprecated.

Functions

#
Managed
Deprecated.
AlignedManaged
Deprecated.
Aligned
A contiguous, growable list of arbitrarily aligned items in memory.

Source

Implementation

#
pub fn addManyAt(self: *Self, index: usize, count: usize) Allocator.Error![]T {
    const new_len = try addOrOom(self.items.len, count);

    if (self.capacity >= new_len)
        return addManyAtAssumeCapacity(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.
    const new_capacity = Aligned(T, alignment).growCapacity(self.capacity, new_len);
    const old_memory = self.allocatedSlice();
    if (self.allocator.remap(old_memory, new_capacity)) |new_memory| {
        self.items.ptr = new_memory.ptr;
        self.capacity = new_memory.len;
        return addManyAtAssumeCapacity(self, index, count);
    }

    // Make a new allocation, avoiding `ensureTotalCapacity` in order
    // to avoid extra memory copies.
    const new_memory = try self.allocator.alignedAlloc(T, alignment, new_capacity);
    const to_move = self.items[index..];
    @memcpy(new_memory[0..index], self.items[0..index]);
    @memcpy(new_memory[index + count ..][0..to_move.len], to_move);
    self.allocator.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.
    return new_memory[index..][0..count];
}