removing an item from a dynamic array

Jonathan M Davis jmdavisProg at gmx.com
Tue Oct 25 10:31:27 PDT 2011


On Tuesday, October 25, 2011 10:11 bearophile wrote:
> Dmitry Olshansky:
> > No, it's not a bug. It's the same as c++ STL remove - it operates on
> > range but not on container. To shrink container, update it's length.
> 
> Thank you for your answer, I didn't know this, and I didn't think about
> this possibility because it's weird, it's an in-place operation that
> modifies the data only partially, leaving it in a wrong state. It looks
> like a bad design, bug prone-too. The design of Python del is better.
> (Maybe I'll have to bring this in the main D newsgroup too, because Phobos
> bug reports often get unnoticed). In the meantime I'll add a wrapper
> function to dlibs2.

It operates on a range. It can't do anything else. It has no access to the 
underlying container and can't remove anything from it. The best that it can 
do is move the removed elements to the end of the range and then return a 
slice which is that many elements shorter. C++'s erase has the exact same 
problem. And as long as the range (or iterator) does not have access to its 
underlying container to do make calls on it, that's the best that can be done. 
At least in D, you get a shortened range. With iterators, the situation isn't 
quite as clean. So, D's remove still manages to improve over C++'s erase at 
least somewhat.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list