lewis
April 3, 2020, 10:19pm
1
Apparently, there is no way to empty! a queue.
Doc for DataStructures.jl shows an empty! method for a stack, but not for a queue.
It is quite difficult to tell what sort of beast a queue is since it wraps dequeue.
When I display my queue in the REPL, the beginning looks like this:
Queue{NamedTuple}(Deque
What follows is an array (well, a vector) of the NamedTuples that have been enqueued.
The appears to get no way to get at that array.
What to do to empty it?
To move on, I switched to a vector as I don’t care about all the fancy semantics. It works.
You can iterate over it, so collect(queue)
works to get a copy of the queue as an array.
Normally, you don’t want to poke around in the internals of some other package’s data structure.
lewis:
What to do to empty it?
empty!(queue)
works.
If you don’t need queue semantics, you definitely shouldn’t use a queue data structure.
1 Like
empty!
is definitively defined for a Queue object:
return s
end
"""
dequeue!(s::Queue)
Removes an element from the front of the queue `s` and returns it.
"""
dequeue!(s::Queue) = popfirst!(s.store)
empty!(s::Queue) = (empty!(s.store); s)
# Iterators
iterate(q::Queue, s...) = iterate(q.store, s...)
reverse_iter(q::Queue) = reverse_iter(q.store)
==(x::Queue, y::Queue) = x.store == y.store
1 Like
lewis
April 3, 2020, 11:27pm
4
I had a function for empty!(df::DataFrame). This blocked the use of empty! from DataStructures. Should I have done import Base.empty! ?
Just asking. The array is adequate to the purpose.
I believe so. If you create a custom empty! for your data type then I believe can can do function Base.empty!()
to ensure you don’t hide the others.
No, because defining your own Base.empty!(df::DataFrame)
would be type piracy , and could easily conflict e.g. if the DataFrames
package decides to implement Base.empty!
.
Either call your function something else, or call Base.empty!(queue)
when you want to use the standard empty!
function.
2 Likes