IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /max/get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /max/get-started.md).

Mojo struct

TopKHeap

struct TopKHeap[T: DType, largest: Bool, M: Int]

Fixed-capacity register heap for per-thread top-M tracking.

Stores up to M (value, index) pairs in registers. During the scan phase, a cached threshold provides O(1) rejection of non-competitive elements. All internal loops are compile-time unrolled to keep data in registers on GPU. Indices are stored as Int32 to reduce register pressure for large block sizes.

Fields​

  • ​vals (InlineArray[Scalar[T], M]):
  • ​idxs (InlineArray[Int32, M]):
  • ​threshold (Scalar[T]):

Implemented traits​

AnyType, ImplicitlyDeletable

Methods​

__init__​

def __init__(out self)

insert​

def insert(mut self, val: Scalar[T], idx: Int)

Insert an element, evicting the worst if full.

best​

def best(self) -> TopK_2[T, largest]

Return the best element, ties broken by smallest index.

Returns a dead TopK_2 (p=-1) when all entries are exhausted.

Returns:

TopK_2[T, largest]

remove​

def remove(mut self, idx: Int)

Remove element by global index, replacing with dead value.