Skip to content

Add public Verify API and Fix operation#5

Merged
aalpar merged 1 commit into
masterfrom
feature/verify-api
Mar 8, 2026
Merged

Add public Verify API and Fix operation#5
aalpar merged 1 commit into
masterfrom
feature/verify-api

Conversation

@aalpar
Copy link
Copy Markdown
Owner

@aalpar aalpar commented Mar 8, 2026

Summary

  • Add Verify to both API surfaces — Verify(h heap.Interface) bool (v1) and (*Deheap[T]).Verify() bool (generic) — for checking the min-max heap invariant in O(n)
  • Add Fix to both API surfaces for re-establishing heap order after mutating an element at a known index in O(log n)
  • Improve Init/From to use Floyd's bottom-up construction with a valid-heap fast path
  • Replace the test-only isHeap helper with Verify assertions across all unit, randomized, and fuzz tests
  • Add testable examples for Verify on both API surfaces

Test plan

  • go test ./... passes
  • Verify assertions added to all test and fuzz functions
  • Testable examples for both ExampleVerify and ExampleDeheap_Verify

@aalpar aalpar requested a review from Copilot March 8, 2026 16:57
@aalpar aalpar added the enhancement New feature or request label Mar 8, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds public heap validation and repair primitives (Verify and Fix) across both the v1 (heap.Interface) and generic (Deheap[T]) APIs, and updates heap construction (Init / From) to use a Floyd bottom-up build with a fast-path when the input is already valid.

Changes:

  • Add Verify (O(n)) and Fix (O(log n)) to both API surfaces.
  • Update Init / From to skip work when already valid, otherwise build via Floyd bottom-up heapification.
  • Replace the test-only isHeap helper with Verify assertions; add new unit/fuzz coverage and examples for Verify.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
deheap.go Adds v1 Fix and Verify, plus valid helper; updates Init to use validity fast-path + Floyd build.
ordered.go Updates From to Floyd build with validity fast-path; adds generic Fix, Verify, and orderedValid.
deheap_test.go Replaces isHeap usage with Verify; adds Fix unit/random/fuzz coverage; removes isHeap helper.
ordered_test.go Adds Verify assertions broadly; adds Fix unit/random/fuzz coverage for the generic heap.
example_intheap_test.go Adds a testable example for v1 Verify.
example_ordered_test.go Adds a testable example for generic (*Deheap[T]).Verify().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread deheap.go Outdated
break
}
p := hparent(v)
if h.Less(p, v) == min {
Copy link

Copilot AI Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Fix's cross-level check after swapping with a grandchild, the condition h.Less(p, v) == min is asymmetric: when min == false it becomes true for both p > v and p == v (because Less is strict), causing unnecessary swaps/bubbleups on equal values and making the intent harder to reason about. Consider spelling this out explicitly (min-level: swap if parent < child; max-level: swap if child < parent), matching the generic ordered.go implementation and avoiding equality-triggered work.

Suggested change
if h.Less(p, v) == min {
if (min && h.Less(p, v)) || (!min && h.Less(v, p)) {

Copilot uses AI. Check for mistakes.
Comment thread ordered.go
// Fix re-establishes the heap ordering after the element at index i
// has changed its value. Equivalent to, but cheaper than, Remove(i)
// followed by Push of the new value.
//
Copy link

Copilot AI Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix is a public method and (like Remove) will panic if i is out of bounds for non-empty heaps. Consider documenting the bounds requirement (and panic behavior) in the comment so callers know i must be in [0, Len()).

Suggested change
//
//
// The index i must be in the range [0, d.Len()).
// It panics if i is out of bounds.
//

Copilot uses AI. Check for mistakes.
@aalpar aalpar force-pushed the feature/verify-api branch from 3274d43 to 44fb71a Compare March 8, 2026 17:06
Add Verify to both API surfaces for checking the min-max heap
invariant. The v1 API gets a package-level Verify(h heap.Interface)
function; the generic API gets a Verify() method on Deheap[T].

Also adds Fix (re-establish heap ordering after mutating an element
at a known index) to both surfaces, improves Init/From to use
Floyd's bottom-up construction with a valid-heap fast path, and
replaces the test-only isHeap helper with Verify assertions
throughout all test and fuzz functions.
@aalpar aalpar force-pushed the feature/verify-api branch from 44fb71a to 3c90740 Compare March 8, 2026 17:41
@aalpar aalpar merged commit b485e25 into master Mar 8, 2026
4 checks passed
@aalpar aalpar deleted the feature/verify-api branch March 8, 2026 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants