=============== LIBRARY RULES =============== From library maintainers: - Import path is github.com/aalpar/deheap; the module has no dependencies outside the Go standard library. - For cmp.Ordered element types, use the generic API: deheap.New[T](), deheap.From(items...), then methods on *Deheap[T]. - For custom element types, implement container/heap's heap.Interface and call the package-level functions (deheap.Init, Push, Pop, PopMax, Remove, Fix). Do not call container/heap functions on the same value; they maintain a different invariant. - Call deheap.Init(h) once on a heap.Interface value built from unordered data before any other deheap function. - Pop and PopMax on an empty heap return nil (interface API) or the zero value of T (generic API); check Len() first when the zero value is a valid element. - Peek and PeekMax panic on an empty heap; guard with Len() > 0. - For a fixed-capacity heap, use NewBounded or FromBounded and insert with Offer, which evicts the largest element when full. Push ignores MaxLen. - Use PushPop / PushPopMax instead of Push followed by Pop / PopMax; they are cheaper (no slice growth in the generic API, no bubble-up when the pushed element is already the extremum). - DrainAsc and DrainDesc return iter.Seq[T] and consume the heap; breaking out early leaves the remaining elements in a valid heap. - After mutating an element in place, call Fix with its index; Remove and Fix take a slice index, not a value. ### Install deheap with go get Source: https://github.com/aalpar/deheap/blob/master/docs/index.mdx Install the deheap package with the Go module tool. Requires Go 1.23 or later and has zero external dependencies. ```bash go get github.com/aalpar/deheap ``` -------------------------------- ### Use deheap package-level functions with a custom heap.Interface type Source: https://github.com/aalpar/deheap/blob/master/docs/interface-api.mdx Implement heap.Interface for a custom type and use package-level functions from deheap. The example shows initialization, push, pop, pop-max, fix, and verify operations. The heap is a min-heap by default; PopMax returns the maximum element. Verify returns true when the heap is valid. ```go import "github.com/aalpar/deheap" type IntHeap []int func (h IntHeap) Len() int { return len(h) } func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] } func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *IntHeap) Push(x interface{}) { *h = append(*h, x.(int)) } func (h *IntHeap) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[:n-1] return x } h := &IntHeap{2, 1, 5, 6} deheap.Init(h) deheap.Push(h, 3) min := deheap.Pop(h) max := deheap.PopMax(h) // Update an element and restore heap order. (*h)[0] = 42 deheap.Fix(h, 0) // Check if the heap is valid. fmt.Println(deheap.Verify(h)) // true ``` -------------------------------- ### Run benchmarks with go test -bench . -benchmem Source: https://github.com/aalpar/deheap/blob/master/docs/implementation.mdx Command to run all benchmarks with memory allocation statistics. The output shows benchmark results for various operations on Apple M4 Max with Go 1.23. ```text $ go test -bench . -benchmem goos: darwin goarch: arm64 pkg: github.com/aalpar/deheap cpu: Apple M4 Max BenchmarkMin4-16 261428533 4.784 ns/op 0 B/op 0 allocs/op BenchmarkBaselinePush-16 1000000000 6.411 ns/op 42 B/op 0 allocs/op BenchmarkPush-16 44383689 23.16 ns/op 50 B/op 0 allocs/op BenchmarkPop-16 5666707 326.3 ns/op 7 B/op 0 allocs/op BenchmarkPopMax-16 5644575 319.7 ns/op 7 B/op 0 allocs/op BenchmarkPushPop-16 5098502 297.2 ns/op 65 B/op 1 allocs/op BenchmarkHeapPushPop-16 6203408 256.5 ns/op 56 B/op 1 allocs/op BenchmarkHeapPop-16 8475319 234.7 ns/op 7 B/op 0 allocs/op BenchmarkHeapPush-16 46837188 21.57 ns/op 48 B/op 0 allocs/op BenchmarkOrderedPush-16 100000000 12.38 ns/op 45 B/op 0 allocs/op BenchmarkOrderedPop-16 8680756 249.4 ns/op 0 B/op 0 allocs/op BenchmarkOrderedPopMax-16 10103798 237.3 ns/op 0 B/op 0 allocs/op BenchmarkOrderedPushPop-16 8926204 233.2 ns/op 44 B/op 0 allocs/op PASS ``` -------------------------------- ### Run deheap tests, benchmarks, and fuzz targets Source: https://github.com/aalpar/deheap/blob/master/docs/implementation.mdx Run the full test suite, benchmarks, and fuzz targets for the deheap package. The first command executes all tests, the second runs benchmarks, and the third runs fuzz tests. ```bash go test ./... # run all tests go test -bench . ./... # run benchmarks go test -fuzz . # run fuzz tests ``` -------------------------------- ### Use deheap generic API for cmp.Ordered types Source: https://github.com/aalpar/deheap/blob/master/docs/generic-api.mdx Use the type-safe generic API for cmp.Ordered types such as int, float64, and string. Construct a heap from existing elements with deheap.From or incrementally with deheap.New, then access both extrema in O(1) via Peek and PeekMax, remove from either end in O(log n) with Pop and PopMax, remove by index with Remove, update an element and restore heap order with Fix, and verify heap validity with Verify. ```go import "github.com/aalpar/deheap" // Construct from existing elements. h := deheap.From(5, 3, 8, 1, 9) // Or build incrementally. h := deheap.New[int]() h.Push(5) h.Push(3) // O(1) access to both extrema. fmt.Println(h.Peek()) // smallest fmt.Println(h.PeekMax()) // largest // O(log n) removal from either end. min := h.Pop() max := h.PopMax() // Remove by index. val := h.Remove(2) // Update an element and restore heap order. h.Push(10) h.Push(20) // ... mutate the element at index 0 ... h.Fix(0) // Check if the heap is valid. fmt.Println(h.Verify()) // true ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.