Documentation
¶
Overview ¶
Package reaper implements a deterministic, bounded rewrite loop over a priority structure.
A Reaper repeatedly removes a single element from a heap, allows a callback to inspect it and optionally emit replacement elements, and then reinserts those elements back into the heap. Each step is strictly bounded: a callback may emit at most a fixed number of elements ("degree") per visit.
The design enforces three core invariants:
- Only one element is popped from the heap at a time.
- Reinsertion is explicitly controlled and bounded.
- Callbacks are invoked without holding heap locks.
This makes Reaper suitable for schedulers, rewrite systems, planners, or any algorithm that requires controlled feedback into a priority queue.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrEmitBufferOverflow = errors.New("emit buffer overflow")
ErrEmitBufferOverflow is returned by the emit function when the number of emitted elements in a single Visit exceeds the configured degree.
Functions ¶
This section is empty.
Types ¶
type Callback ¶
Callback is invoked for each element popped from the heap during a Reap operation.
The callback receives the popped element ("front") and an emit function. The emit function may be used to buffer elements for reinsertion into the heap after the callback returns.
Returning stop == true signals that reaping should terminate early. In that case, the popped element is reinserted into the heap unchanged, and no buffered emissions are committed.
type CallbackFunc ¶
CallbackFunc allows a function to be used as a Callback.
func (CallbackFunc[T]) Visit ¶
func (f CallbackFunc[T]) Visit(front T, emit func(...T) error) (stop bool)
type Heap ¶
Heap is a thread-safe priority structure supporting push and pop operations.
Reaper assumes that all heap mutations are protected by the provided Lock and Unlock methods. Callbacks are always invoked without holding the heap lock.
type Reaper ¶
type Reaper[T any] struct { // contains filtered or unexported fields }
Reaper administrates the controlled feedback loop over a Heap.
Example (Basic) ¶
package main
import (
"cmp"
"fmt"
"sync"
"github.com/codegrapple/reaper"
"github.com/codegrapple/reaper/heapadapter"
"github.com/codegrapple/reaper/sliceheap"
)
func main() {
h := sliceheap.New[int](4, cmp.Less[int], 1, 2, 3)
r := reaper.New[int](2)
// Adapt h to reaper.Heap[int]
heap := heapadapter.New[int](h, &sync.Mutex{})
visit := func(x int, emit func(...int) error) bool {
if x == 1 {
emit(4, 5)
return false
}
return true
}
result := r.Reap(heap, reaper.CallbackFunc[int](visit))
fmt.Println(result)
fmt.Println(h.Slice())
}
Output: Stopped [2 3 4 5]
Example (Consumer) ¶
package main
import (
"cmp"
"fmt"
"sync"
"github.com/codegrapple/reaper"
"github.com/codegrapple/reaper/heapadapter"
"github.com/codegrapple/reaper/sliceheap"
)
func main() {
h := sliceheap.New[int](4, cmp.Less[int], 1, 2, 3)
r := reaper.New[int](0)
heap := heapadapter.New[int](h, &sync.Mutex{})
var visited []int
visit := func(x int, emit func(...int) error) bool {
visited = append(visited, x)
return false
}
result := r.Reap(heap, reaper.CallbackFunc[int](visit))
fmt.Println(result)
fmt.Println(visited)
fmt.Println(heap.Empty())
}
Output: Exhausted [1 2 3] true
func New ¶
New creates a new Reaper with the specified degree.
Degree specifies the maximum number of elements that can be emitted per Visit call. A degree of zero disables emission entirely, turning the Reaper into a pure consumer.
func (*Reaper[T]) Reap ¶
Reap executes the rewrite loop over the provided heap using the given callback.
Reap repeatedly pops a single element from the heap and invokes the callback on it. If the callback emits elements, they are reinserted atomically after the callback returns. If the callback requests terminations, the popped element is restored, the emitted elments are discarded, and Reap returns Stopped.
Reap returns Exhausted if the heap becomes empty.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package heapadapter provides adapters from container/heap to reaper.Heap.
|
Package heapadapter provides adapters from container/heap to reaper.Heap. |
|
Package slicehap provides a minimal generic heap implementation backed by a slice and compatible with container/heap.
|
Package slicehap provides a minimal generic heap implementation backed by a slice and compatible with container/heap. |