ArrayBuilder

public struct ArrayBuilder[T] { /* private fields */ }

Write-only buffer for efficient array construction. No COW, no RcBox, no isUnique checks — every append writes directly through the pointer.

build() transfers ownership of the buffer into a new Array[T] without copying. The builder resets to empty and can be reused.

Examples

var b = ArrayBuilder[Int64](capacity: 3); b.append(1); b.append(2); b.append(3); let arr = b.build(); // [1, 2, 3], zero-copy

Representation

(ptr: Pointer[T], len: Int64, cap: Int64).

Memory Model

Owns its buffer directly — no reference counting during construction. build() donates the buffer to an Array[T] and leaves the builder empty. deinit frees the buffer if build() was never called.

Properties

public var capacity: Int64 { get }

Allocated capacity in elements.

public var count: Int64 { get }

Number of elements written so far.

public var isEmpty: Bool { get }

True when nothing has been written.

Initializers

public init()

Creates an empty builder with no allocation.

public init(capacity: Int64)

Creates an empty builder with at least capacity elements preallocated.

Methods

public mutating func append(T)

Appends a single element.

public mutating func append(contentsOf: ArraySlice[T])

Appends every element of slice.

public mutating func append[I](from: I) where I: Iterable, I.Item == T

Appends every element produced by iterable.

public mutating func build() -> Array[T]

Transfers the buffer into a new Array[T] without copying. The builder resets to empty and can be reused.

public mutating func clear()

Resets length to zero, keeping the allocated buffer for reuse.

Defined in lang/std/collections/builder.ks