Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions std/container/rbtree.d
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -801,9 +800,7 @@ if (is(typeof(binaryFun!less(T.init, T.init))))

static private Node allocate(Elem v)
{
auto result = allocate();
Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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, allocate immediately ringed a bell about plugging std.experimental.allocators in there to me too ;)

No problem, I'm glad that I can now effectively help :)

result.value = v;
return result;
return new RBNode(null, null, null, v);
}

/**
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only a stylistic change, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only a stylistic change, right?

It does the same, but the old version wasn't @safe.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see (I missed the cast(Elem[]) last time).

assert(ts.length == 10);
assert(ts.stableInsert(cast(Elem) 11) == 1 && ts.length == 11);
assert(ts.stableInsert(cast(Elem) 7) == 0 && ts.length == 11);
Expand Down Expand Up @@ -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[]);
}

Expand Down Expand Up @@ -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))));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test that verifies that front can be fetched?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test that verifies that front can be fetched?

Added an assert.

static assert(!__traits(compiles, *t3.front.p = 4));
assert(*t3.front.p == 1);
}