Remove element from DList

Jonathan M Davis jmdavisProg at gmx.com
Sat Oct 6 18:02:36 PDT 2012


On Saturday, October 06, 2012 23:27:31 cal wrote:
> I'd like to remove a single element from a std.container.DList.
> For that I need a range, but I'm not sure how to get a single
> element 'range'.
> 
> I thought something like this might work:
> 
> auto list = DList!int([1,2,3,4]);
> auto r = list[];
> while(r.front != 3)
>     r.popFront;
> 
> list.remove(r.take(1));
> 
> But the the range returned by r.take(1) is not valid as an
> argument to DList.remove. What is the correct way to do this?

The correct way is to use find combined with take (which is what you're doing, 
except that you're not using find). So, something like this should work

void main()
{
    auto list = DList!int([1,2,3,4]);
    list.remove(find(list[], 3).take(1));
}


However, for some reason, this is indeed not working right now. It's a bug. 
Please report it.

But! If you use linearRemove, it _does_ work.

void main()
{
    auto list = DList!int([1,2,3,4]);
    list.linearRemove(find(list[], 3).take(1));
}

I have no idea why remove doesn't work with take (because it's supposed to), 
and I have no idea why remove isn't an alias to linearRemove (or vice versa). 
I would have thought that they'd be one and the same for linked lists, but 
maybe I'm missing something, since I haven't spent a lot of time thinking 
about it.

Regardless, report it as a bug. Using take on the range returned by a 
container should work for _all_ of the various remove functions for _all_ 
containers in std.container. If it doesn't, it's a bug.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list