Negated values in a min-heap
h.push(-x) into a MinHeap to pop the largest: MaxHeap<T> (std.heap)
let a = [3, 1, 4]
var h = MinHeap<Int>()
for x in a:
h.push(-x)
print(-(h.pop()!))let a = [3, 1, 4]
var h = MaxHeap<Int>()
for x in a:
h.push(x)
print(h.pop()!)Why
C++'s priority_queue pops the largest, so a port that wants the largest from MinHeap pushes -x and negates every pop back. Each - must be paired with another, and a forgotten one silently returns the smallest. MaxHeap<T> (std.heap, in the prelude) pops the largest with the same operations and costs, so h.push(x) and h.pop()! say what they mean. For a mixed order, keep Heap and put desc(x) in the entry: h.push((desc(d), v)). The lint fires on h.push(-x) when the checker types h as a min-heap; there is no fix, because every pop must change too.
The practice it serves
A hand-written pattern the language or the standard library already says in one step. Family: S.heap.
Not flagged
A near miss: close to the pattern, but not it.
let a = [3, 1, 4]
var xs: List<Int> = []
for x in a:
xs.push(-x)
var h = MinHeap<Int>()
for x in a:
h.push(x - 1)
print(xs, h.pop()!)Turn it off
For one block, wrap it in with lint(negated_heap: allow):. For a project, add negated_heap = "allow" under [lints] in cp.toml.
Generated from spec/lints/negated_heap, the same record the linter is built from.