Hi,
My first Julia code below is a simple array-based priority queue with some performance tests for push and pop.
Next step would be to make this into something callable like
pq1 = pq(Float64,Any)
push_pq!(pq1, 1.1, 2.3)
push_pq!(pq1, 1.2, “toto”)
(t, data) = pop_pq!(pq1)
Looking at PriorityQueue.jl blows my mind. There must be something more concise to start with.
I was looking around but did not find this topic treated, maybe missing the right word for it.
Any kind soul to help me with the missing link?
lockerKeys = Int32[] # locker keys
data = Any[] # locker contents
events = Tuple{Float64, Int16}[] # event array (time, lockerKey)
function push_pq!(t, d)
# add event to priority queue
len = length(events)
# locker keys run from 1:n
if length(lockerKeys) < len + 1
push!(lockerKeys, len + 1)
end
# store event data in locker array
key = lockerKeys[len + 1]
if length(data) < len + 1
push!(data, d)
else
data[key] = d
end
# insert (time, key) in priority queue
left = 1
right = len
while left <= right
mid = div(left + right, 2)
if events[mid][1] > t
right = mid - 1
else
left = mid + 1
end
end
insert!(events, left, (t, key))
end
function pop_pq!()
# remove earliest event from priority queue
(t, key) = popfirst!(events)
# return key
lockerKeys[length(events) + 1] = key
return (t, data[key])
end
#########################################################
function testm(m, n)
# run pq with n total and max m pending events
for i = 1:n
push_pq!(i + m*rand(), rand());
if i > m
(t, d) = pop_pq!();
end
end
end
function test1()
for m = 1:10:100
@time testm(m, 1e5)
end
end
test1()