Struct ScratchBuffer

Reusable contiguous work buffer for algorithms that need temporary typed storage across many processing passes.

struct ScratchBuffer(T);

ScratchBuffer owns one runtime-sized backing allocation and exposes exactly the live prefix as ordinary D indexing and slicing. The usual pattern is to reserve capacity once, fill the buffer during a work phase, consume the live prefix, call reset, and reuse the same allocation in the next phase.

Use ScratchBuffer when capacity reuse matters but automatic container growth would hide allocation policy from the caller. The family is deliberately thread-confined. It is not a pool, arena, synchronized buffer, geometric vector, or live-content reallocator.

Constructors

NameDescription
this (capacity) Establishes backing storage for exactly the requested capacity.
this (rhs) Unique owning storage is never copied implicitly.
this (rhs) Transfers the backing allocation without relocating live T values.

Methods

NameDescription
capacity () Returns the number of backing element slots currently owned.
empty () Reports whether the live prefix is empty.
full () Reports whether every established backing slot is live.
length () Returns the number of currently live elements.
opAssign (rhs) Identity assignment is deliberately unavailable.
opIndex (index) Returns a mutable reference to one live element.
opSlice () Borrows the exact live contiguous prefix as a D slice.
reset () Ends all live element lifetimes and retains the backing allocation.
tryPushBack (value) Appends one element when established capacity has a spare slot.
tryReserve (minCapacity) Ensures at least the requested backing capacity without relocating live elements.

Parameters

NameDescription
T element type stored in the reusable contiguous allocation

Init

.init is a valid empty buffer with zero capacity.

Allocation

Construction or successful empty-buffer tryReserve may acquire backing storage. tryPushBack, indexed access, slicing, and reset do not grow or reacquire storage.

Thread Safety

Instances are not synchronized. Concurrent access requires external synchronization.

Example

Reuse one allocation across independent work phases.

auto scratch = ScratchBuffer!int(4);

// Build temporary input for one algorithm pass.
assert(scratch.tryPushBack(10));
assert(scratch.tryPushBack(20));
assert(scratch[] == [10, 20]);

// End the phase without giving the backing allocation back to the system.
scratch.reset();
assert(scratch.empty);
assert(scratch.capacity == 4);

// The same established capacity is ready for the next phase.
assert(scratch.tryPushBack(30));
assert(scratch[] == [30]);