Removing an element from a DList

Christian Köstlin christian.koestlin at gmail.com
Wed Oct 18 07:20:32 UTC 2023


On Tuesday, 17 October 2023 at 17:27:19 UTC, Joakim G. wrote:
> For some reason I cannot remove an element from a DList. I 
> tried several range approaches but to no avail. I'm a noob.
>
> In the end I did this:
>
> ```
> private void removeFromWaitingQueue(uint jid) {
>     auto arr = waitingQueue[].array;
>     arr = arr.remove!(job => job.jid == jid);
>     waitingQueue.clear();
>     foreach(job; arr) {
>         waitingQueue.insertBack(job);
>     }
> }
> ```
>
> Any hints?
>
> /J

```d
import std.container : DList;
import std.algorithm : find;
import std.range : take;
import std.stdio : writeln;
import std.range : take;
struct Job
{
     int id;
}
int main(string[] args)
{
     auto list = DList!Job(Job(1), Job(2), Job(3));
     int idToDelete = 2;
     auto toRemove = list[].find!(job => job.id == 
idToDelete).take(1);
     if (!toRemove.empty)
         list.linearRemove(toRemove);

     foreach (i; list[])
     {
         writeln(i);
     }

     return 0;
}
```
works for me ... see https://run.dlang.io/is/OtX820 for a live 
version.

Kind regards,
Christian


More information about the Digitalmars-d-learn mailing list