-
-
Notifications
You must be signed in to change notification settings - Fork 753
fix issue 17519 - RedBlackTree doesn't like const/immutable elements #5492
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 |
|---|---|---|
|
|
@@ -627,8 +627,7 @@ struct RBNode(V) | |
|
|
||
| Node dup() | ||
| { | ||
| Node copy = new RBNode!V; | ||
| copy.value = value; | ||
| Node copy = new RBNode!V(null, null, null, value); | ||
| copy.color = color; | ||
| if (_left !is null) | ||
| copy.left = _left.dup(); | ||
|
|
@@ -801,9 +800,7 @@ if (is(typeof(binaryFun!less(T.init, T.init)))) | |
|
|
||
| static private Node allocate(Elem v) | ||
| { | ||
| auto result = allocate(); | ||
| result.value = v; | ||
| return result; | ||
| return new RBNode(null, null, null, v); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1190,7 +1187,8 @@ if (is(typeof(binaryFun!less(T.init, T.init)))) | |
| else | ||
| { | ||
| assert(ts.length == 5); | ||
| assert(ts.stableInsert(cast(Elem[])[7, 8, 6, 9, 10, 8]) == 5); | ||
| Elem[] elems = [7, 8, 6, 9, 10, 8]; | ||
| assert(ts.stableInsert(elems) == 5); | ||
|
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. This is only a stylistic change, right?
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.
It does the same, but the old version wasn't
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. I see (I missed the |
||
| assert(ts.length == 10); | ||
| assert(ts.stableInsert(cast(Elem) 11) == 1 && ts.length == 11); | ||
| assert(ts.stableInsert(cast(Elem) 7) == 0 && ts.length == 11); | ||
|
|
@@ -1398,11 +1396,7 @@ assert(equal(rbt[], [5])); | |
| size_t removeKey(U...)(U elems) | ||
| if (allSatisfy!(isImplicitlyConvertibleToElem, U)) | ||
| { | ||
| Elem[U.length] toRemove; | ||
|
|
||
| foreach (i, e; elems) | ||
| toRemove[i] = e; | ||
|
|
||
| Elem[U.length] toRemove = [elems]; | ||
| return removeKey(toRemove[]); | ||
| } | ||
|
|
||
|
|
@@ -2056,3 +2050,16 @@ if ( is(typeof(binaryFun!less((ElementType!Stuff).init, (ElementType!Stuff).init | |
| class C {} | ||
| RedBlackTree!(C, "cast(void*)a < cast(void*) b") tree; | ||
| } | ||
|
|
||
| @safe pure unittest // const/immutable elements (issue 17519) | ||
| { | ||
| RedBlackTree!(immutable int) t1; | ||
| RedBlackTree!(const int) t2; | ||
|
|
||
| import std.algorithm.iteration : map; | ||
| static struct S { int* p; } | ||
| auto t3 = new RedBlackTree!(immutable S, (a, b) => *a.p < *b.p); | ||
| t3.insert([1, 2, 3].map!(x => immutable S(new int(x)))); | ||
|
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. Can you add a test that verifies that
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.
Added an assert. |
||
| static assert(!__traits(compiles, *t3.front.p = 4)); | ||
| assert(*t3.front.p == 1); | ||
| } | ||
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.
As far as I can see, the idea behind
allocate()is to abstract the allocation mechanism. However, in practice it doesn't offer any flexibility, so we can even remove it. But that's better left for another PR.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 idea actually comes from the original library, where I had allocators.
Since this is inside the abstraction for allocation, it is OK, as once std.experimental.allocators is ready, this will get noticed.
Thanks for reviewing, I just logged in this morning to do so and approve, but you beat me to it.
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.
I thought as much. Yeah,
allocateimmediately ringed a bell about plugging std.experimental.allocators in there to me too ;)No problem, I'm glad that I can now effectively help :)