From 7d84842b87193c118ae71eac06d3b2a2cc0552e9 Mon Sep 17 00:00:00 2001 From: carblue Date: Fri, 23 Mar 2018 12:23:47 +0100 Subject: [PATCH 1/2] std.container.rbtree: Fix -dip1000 compilable issues --- dip1000.mak | 2 +- std/container/rbtree.d | 80 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/dip1000.mak b/dip1000.mak index 603a391a498..e2d61be41ab 100644 --- a/dip1000.mak +++ b/dip1000.mak @@ -94,7 +94,7 @@ aa[std.container.array]=-dip1000 aa[std.container.binaryheap]=-dip1000 aa[std.container.dlist]=-dip1000 aa[std.container.package]=-dip1000 -aa[std.container.rbtree]=-dip25 # DROP +aa[std.container.rbtree]=-dip1000 # merged https://github.com/dlang/phobos/pull/6332 aa[std.container.slist]=-dip25 # -dip1000 -version=DIP1000 depends on https://github.com/dlang/phobos/pull/6295 merged aa[std.container.util]=-dip25 # TODO diff --git a/std/container/rbtree.d b/std/container/rbtree.d index a8945f0828b..fef5fa877f8 100644 --- a/std/container/rbtree.d +++ b/std/container/rbtree.d @@ -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) + { + 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) From 141bb8e66d89fb15f806e8e5796db3d598d8300b Mon Sep 17 00:00:00 2001 From: carblue Date: Fri, 23 Mar 2018 12:31:25 +0100 Subject: [PATCH 2/2] std.container.rbtree: Fix -dip1000 compilable issues; space after cast --- std/container/rbtree.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/container/rbtree.d b/std/container/rbtree.d index fef5fa877f8..7ef75c59bc2 100644 --- a/std/container/rbtree.d +++ b/std/container/rbtree.d @@ -2121,7 +2121,7 @@ if ( is(typeof(binaryFun!less((ElementType!Stuff).init, (ElementType!Stuff).init @safe pure unittest { class C {} - RedBlackTree!(C, (a,b) @trusted => 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)