Skip to contents

Returns a lazy iterator that yields payload elements in priority-ascending order. Use with loop():

Usage

# S3 method for class 'priority_queue'
as_iterator(x)

Arguments

x

A priority_queue.

Value

A coro iterator function.

Details

loop(for (x in pq) print(x))

Traversal is driven by repeated pop_min(): each step is O(log n), so full traversal is O(n log n). Ties within equal priorities are yielded in FIFO insertion order (inherited from pop_min()).

The original x is not modified; the iterator holds a private cursor and partial iteration (e.g. via break) leaves the source intact.

Each yielded value is the bare payload (matching peek_min()). Use fapply() if your callback needs the priority alongside the value, or cast with as_flexseq() for insertion-order iteration.

Examples

pq <- priority_queue("a", "b", "c", priorities = c(3, 1, 2))
loop(for (x in pq) print(x))  # "b", "c", "a"
#> [1] "b"
#> [1] "c"
#> [1] "a"