container stuff

Bill Baxter wbaxter at gmail.com
Thu May 27 09:12:20 PDT 2010


On Thu, May 27, 2010 at 5:44 AM, Don <nospam at nospam.com> wrote:
> bearophile wrote:
>>
>> Don:
>>>
>>> When is it better to do it that way, rather than just iterating over all
>>> elements, and then completely empty the container?
>>> (Just curious -- I'm having trouble thinking of a use case for this
>>> feature).
>>
>> I'm having troubles understanding why two persons have troubles seeing use
>> cases for this feature :-)
>>
>> Iterating over the container and then emptying the container is two
>> operations, you have to keep in mind to empty it, while if you pop items out
>> of it progressively you just need to keep in mind to do one thing, and you
>> avoid forgetting the final cleaning.
>
> Yes, but if I understand correctly, the only reason to have removeAny _as a
> primitive_ is for speed. And iterating over the container followed by a
> single removal is almost always going to be much faster.
>
> If, however, speed is not critical, removeAny can be a generic function --
> call removeFront() if present, else call removeBack().
> And your examples would work just fine with that.
>
> I'm having trouble identifying a use case where it needs to be a primitive.
>

Think of a graph algorithm where you add all the nodes you know about
to a Set.  Pop one, process it, and then add any nodes it's connected
to that you haven't seen yet back to the Set.  Repeat until nothing
left to pop.

--bb


More information about the Digitalmars-d mailing list