Checking if a value is contained in a MutableBinaryHeap?

Hello guys, I need to test if a value is contained in a MutableBinaryHeap.

I may have the following Tuple t:

6-element Vector{Tuple{Int64, Float64}}:
 (1, Inf)
 (2, Inf)
 (3, Inf)
 (4, Inf)
 (5, Inf)
 (6, Inf)

For the Tuple case I can just do:


And check whether a value is contained in a.
The problem is that I need to do this test on a MutableBinaryHeap.

I have the heap h

MutableBinaryHeap((1, 0.0), (2, Inf), (3, Inf), (4, Inf), (5, Inf), (6, Inf))

and doing


gives the following error:

ERROR: MethodError: no method matching iterate(::MutableBinaryHeap{Tuple{Int64, Float64}, Base.Order.By{typeof(last), Base.Order.ForwardOrdering}})

Any idea on how to test is a value is contained on the first element of a MutableBinaryHeap?

Thank you so much in advance.

Waiting for a more specific answer you could try this workaround

a=[(1, 0.0), (2, Inf), (3, Inf), (4, Inf), (5, Inf), (6, Inf)]


findfirst(t->first(t)==(2),[pop!(mbh) for _ in 1:length(mbh)])


searchsorted([pop!(mbh) for _ in 1:length(mbh)],2; by=first)

Would this work for your case? (I wouldn’t usually go for directly accessing fields of structs defined in other packages, as the implementation might change. But it seems unlikely that the implementation in this case will change and it’s a pretty compact solution in my opinion)

using DataStructures

heap = MutableBinaryMinHeap([(1, 0.0), (2, Inf), (3, Inf), (4, Inf), (5, Inf), (6, Inf)])
get_value(node) = node.value


# gives
6-element Vector{Int64}:

The corresponding structs are defined here