-
-
Notifications
You must be signed in to change notification settings - Fork 746
std.container.rbtree: Fix -dip1000 compilable issues #6332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1401,7 +1401,7 @@ assert(equal(rbt[], [5])); | |
| } | ||
|
|
||
| /++ Ditto +/ | ||
| size_t removeKey(U)(U[] elems) | ||
| size_t removeKey(U)(scope U[] elems) | ||
| if (isImplicitlyConvertible!(U, Elem)) | ||
| { | ||
| immutable lenBefore = length; | ||
|
|
@@ -1724,10 +1724,82 @@ assert(equal(rbt[], [5])); | |
| * Constructor. Pass in an array of elements, or individual elements to | ||
| * initialize the tree with. | ||
| */ | ||
| this(Elem[] elems...) | ||
| this(scope Elem[] elems...) | ||
| { | ||
| _setup(); | ||
| stableInsert(elems); | ||
| foreach (e; elems) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you just copy and paste the code from If this is necessary to get DIP1000 to compile, then something is very wrong.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JackStouffer I was about to ask the same question. Why can't we also instrument
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Frankly, I did expect exactly this question and I know, the fix is ugly, but:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm pretty confident in saying that the wrong answer in any situation is code duplication, especially since you'll have to do the same thing to stable insert itself eventually. The right answer here is to rewrite
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, this is the right answer in this case. I don't know why it's this way, as However, it's a bit disturbing that we wouldn't be able to make
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because that's not so easy: and that's just for one of the two paths:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @WalterBright Your proposal is basically the same what I tried already for std.container.slist (#6295; there it is function insertFront that I duplicated in the ctor to get it to compile) and it didn't work with master, but acc. to @wilzbach analysis with 2.079.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh and if we do
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For reference, the closest I got was this - it "only" requires to wrongly commenting out two lines: Detailsdiff --git a/std/container/rbtree.d b/std/container/rbtree.d
index a8945f082..e9967a764 100644
--- a/std/container/rbtree.d
+++ b/std/container/rbtree.d
@@ -937,7 +937,7 @@ if (is(typeof(binaryFun!less(T.init, T.init))))
debug(RBDoChecks)
check();
++_length;
- return result;
+ return result is null;
}
else
{
@@ -950,7 +950,7 @@ if (is(typeof(binaryFun!less(T.init, T.init))))
}
debug(RBDoChecks)
check();
- return Tuple!(bool, "added", Node, "n")(added, result);
+ return added;
}
}
@@ -1122,7 +1122,7 @@ if (is(typeof(binaryFun!less(T.init, T.init))))
*
* Complexity: $(BIGOH log(n))
*/
- size_t stableInsert(Stuff)(Stuff stuff) if (isImplicitlyConvertible!(Stuff, Elem))
+ size_t stableInsert(Stuff)(scope Stuff stuff) @safe if (isImplicitlyConvertible!(Stuff, Elem))
{
static if (allowDuplicates)
{
@@ -1131,7 +1131,7 @@ if (is(typeof(binaryFun!less(T.init, T.init))))
}
else
{
- return(_add(stuff).added ? 1 : 0);
+ return _add(stuff) ? 1 : 0;
}
}
@@ -1143,7 +1143,7 @@ if (is(typeof(binaryFun!less(T.init, T.init))))
*
* Complexity: $(BIGOH m * log(n))
*/
- size_t stableInsert(Stuff)(Stuff stuff) if (isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, Elem))
+ size_t stableInsert(Stuff)(scope Stuff stuff) @safe if (isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, Elem))
{
size_t result = 0;
static if (allowDuplicates)
@@ -1158,7 +1158,7 @@ if (is(typeof(binaryFun!less(T.init, T.init))))
{
foreach (e; stuff)
{
- if (_add(e).added)
+ if (_add(e))
++result;
}
}
@@ -1401,7 +1401,7 @@ assert(equal(rbt[], [5]));
}
/++ Ditto +/
- size_t removeKey(U)(U[] elems)
+ size_t removeKey(U)(scope U[] elems)
if (isImplicitlyConvertible!(U, Elem))
{
immutable lenBefore = length;
@@ -1423,7 +1423,7 @@ assert(equal(rbt[], [5]));
}
/++ Ditto +/
- size_t removeKey(Stuff)(Stuff stuff)
+ size_t removeKey(Stuff)(scope Stuff stuff)
if (isInputRange!Stuff &&
isImplicitlyConvertible!(ElementType!Stuff, Elem) &&
!isDynamicArray!Stuff)
@@ -1724,7 +1724,7 @@ assert(equal(rbt[], [5]));
* Constructor. Pass in an array of elements, or individual elements to
* initialize the tree with.
*/
- this(Elem[] elems...)
+ this(scope Elem[] elems...)
{
_setup();
stableInsert(elems);
@@ -1733,7 +1733,7 @@ assert(equal(rbt[], [5]));
/**
* Constructor. Pass in a range of elements to initialize the tree with.
*/
- this(Stuff)(Stuff stuff) if (isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, Elem))
+ this(Stuff)(scope Stuff stuff) if (isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, Elem))
{
_setup();
stableInsert(stuff);
@@ -1970,8 +1970,8 @@ if ( is(typeof(binaryFun!less((ElementType!Stuff).init, (ElementType!Stuff).init
auto rbt3 = redBlackTree(chain([0, 1], [7, 5]));
assert(equal(rbt3[], [0, 1, 5, 7]));
- auto rbt4 = redBlackTree(chain(["hello"], ["world"]));
- assert(equal(rbt4[], ["hello", "world"]));
+ //auto rbt4 = redBlackTree(chain(["hello"], ["world"]));
+ //assert(equal(rbt4[], ["hello", "world"]));
auto rbt5 = redBlackTree!true(chain([0, 1], [5, 7, 5]));
assert(equal(rbt5[], [0, 1, 5, 5, 7]));
@@ -2049,7 +2049,7 @@ if ( is(typeof(binaryFun!less((ElementType!Stuff).init, (ElementType!Stuff).init
@safe pure unittest
{
class C {}
- RedBlackTree!(C, "cast(void*)a < cast(void*) b") tree;
+ RedBlackTree!(C, (a,b) @trusted => cast(void*) a < cast(void*) b) tree;
}
@safe pure unittest // const/immutable elements (issue 17519)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to submit this patch as new PR, s.t. it's easier to look at it and improve upon it: #6362 |
||
| { | ||
| Node result; | ||
| static if (!allowDuplicates) | ||
| bool added = true; | ||
|
|
||
| if (!_end.left) | ||
| { | ||
| _end.left = _begin = result = allocate(e); | ||
| } | ||
| else | ||
| { | ||
| Node newParent = _end.left; | ||
| Node nxt; | ||
| while (true) | ||
| { | ||
| if (_less(e, newParent.value)) | ||
| { | ||
| nxt = newParent.left; | ||
| if (nxt is null) | ||
| { | ||
| // | ||
| // add to right of new parent | ||
| // | ||
| newParent.left = result = allocate(e); | ||
| break; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| static if (!allowDuplicates) | ||
| { | ||
| if (!_less(newParent.value, e)) | ||
| { | ||
| result = newParent; | ||
| added = false; | ||
| break; | ||
| } | ||
| } | ||
| nxt = newParent.right; | ||
| if (nxt is null) | ||
| { | ||
| // | ||
| // add to right of new parent | ||
| // | ||
| newParent.right = result = allocate(e); | ||
| break; | ||
| } | ||
| } | ||
| newParent = nxt; | ||
| } | ||
| if (_begin.left) | ||
| _begin = _begin.left; | ||
| } | ||
|
|
||
| static if (allowDuplicates) | ||
| { | ||
| result.setColor(_end); | ||
| debug(RBDoChecks) | ||
| check(); | ||
| ++_length; | ||
| } | ||
| else | ||
| { | ||
| if (added) | ||
| { | ||
| ++_length; | ||
| result.setColor(_end); | ||
| } | ||
| debug(RBDoChecks) | ||
| check(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -2049,7 +2121,7 @@ if ( is(typeof(binaryFun!less((ElementType!Stuff).init, (ElementType!Stuff).init | |
| @safe pure unittest | ||
| { | ||
| class C {} | ||
| RedBlackTree!(C, "cast(void*)a < cast(void*) b") tree; | ||
| RedBlackTree!(C, (a,b) @trusted => cast(void*) a < cast(void*) b) tree; | ||
| } | ||
|
|
||
| @safe pure unittest // const/immutable elements (issue 17519) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the comment doesn't really need to be there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dip1000.mak won't be there eternally; I hope, it will be superfluous in about a month.
Until then, I like the comments as quick reference, for TODOs etc.