Skip to content

Commit

Permalink
fix issue 17519 - RedBlackTree doesn't like const/immutable elements
Browse files Browse the repository at this point in the history
  • Loading branch information
aG0aep6G authored and PetarKirov committed Jun 18, 2017
1 parent 149331c commit 29c7f36
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions std/container/rbtree.d
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();
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);
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))));
static assert(!__traits(compiles, *t3.front.p = 4));
assert(*t3.front.p == 1);
}

0 comments on commit 29c7f36

Please sign in to comment.