www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - SList: How do I use linearRemove?

reply "Lemonfiend" <lemon fie.nd> writes:
---
module main;

void main()
{
	struct Tree { int apples; }

	import std.container;
	SList!Tree list;

	Tree tree = Tree(5);
	list.insert(tree);
	//list.linearRemove(tree); // <-- Error. What is the correct way?
}
---
Jun 26 2014
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Lemonfiend:

 	//list.linearRemove(tree); // <-- Error. What is the correct 
 way?
 }
 ---
linearRemove is linear, so I think it accepts a range. Perhaps the "once" range is enough. Bye, bearophile
Jun 26 2014
parent "Lemonfiend" <lemon fie.nd> writes:
 linearRemove is linear, so I think it accepts a range. Perhaps 
 the "once" range is enough.
I cannot find once in the docs. Did you mean std.range.only? Error: function std.container.SList!(Tree).SList.linearRemove (Range r) is not callable using argument types (OnlyResult!(Tree, 1u))
Jun 26 2014
prev sibling parent reply "sigod" <sigod.mail gmail.com> writes:
Take a look at unittests in [std.container.slist][0]:

```
unittest
{
     auto s = SList!int(1, 2, 3, 4, 5);
     auto r = s[];
     popFrontN(r, 3);
     auto r1 = s.linearRemove(r);
     assert(s == SList!int(1, 2, 3));
     assert(r1.empty);
}

unittest
{
     auto s = SList!int(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
     auto r = s[];
     popFrontN(r, 3);
     auto r1 = take(r, 4);
     assert(equal(r1, [4, 5, 6, 7]));
     auto r2 = s.linearRemove(r1);
     assert(s == SList!int(1, 2, 3, 8, 9, 10));
     assert(equal(r2, [8, 9, 10]));
}
```

[0]: 
https://github.com/D-Programming-Language/phobos/blob/master/std/container/slist.d#L575
Jun 26 2014
parent reply "Lemonfiend" <lemon fie.nd> writes:
 unittest
 {
     auto s = SList!int(1, 2, 3, 4, 5);
     auto r = s[];
     popFrontN(r, 3);
     auto r1 = s.linearRemove(r);
     assert(s == SList!int(1, 2, 3));
     assert(r1.empty);
 }
This works: auto s = SList!int(1, 2, 3, 4, 5); auto r = s[]; popFrontN(r, 1); auto r1 = s.linearRemove(r); This doesn't (why?): auto s = SList!int(1, 2, 3, 4, 5); auto r = s[]; auto r1 = s.linearRemove(r); This doesn't (why?): auto s = SList!int(1, 2, 3, 4, 5); auto s2 = SList!int(1, 2, 3, 4, 5); auto r = s2[]; popFrontN(r, 1); auto r1 = s.linearRemove(r); I am at a loss..
Jun 26 2014
next sibling parent "sigod" <sigod.mail gmail.com> writes:
First case is a bug. I'll make pull request.
Not sure about second.
Jun 26 2014
prev sibling parent "sigod" <sigod.mail gmail.com> writes:
On Thursday, 26 June 2014 at 16:50:38 UTC, Lemonfiend wrote:
 This doesn't (why?):
 auto s = SList!int(1, 2, 3, 4, 5);
 auto s2 = SList!int(1, 2, 3, 4, 5);
 auto r = s2[];
 popFrontN(r, 1);
 auto r1 = s.linearRemove(r);
This is intended behavior: https://issues.dlang.org/show_bug.cgi?id=12999
Jun 28 2014