From 29c7f36df591e3137233460940cdd824a745786c Mon Sep 17 00:00:00 2001 From: anonymous Date: Sun, 18 Jun 2017 00:52:12 +0200 Subject: [PATCH 1/5] fix issue 17519 - RedBlackTree doesn't like const/immutable elements --- std/container/rbtree.d | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/std/container/rbtree.d b/std/container/rbtree.d index 5342b1692e4..7db3ca61408 100644 --- a/std/container/rbtree.d +++ b/std/container/rbtree.d @@ -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); 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)))); + static assert(!__traits(compiles, *t3.front.p = 4)); + assert(*t3.front.p == 1); +} From 6b32ac2e49921ffd1e5653b534a9c72c89b214e3 Mon Sep 17 00:00:00 2001 From: Jack Stouffer Date: Sat, 17 Jun 2017 18:57:21 -0400 Subject: [PATCH 2/5] Added migration guide to the pattern changelog --- changelog/pattern-deprecate.dd | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/changelog/pattern-deprecate.dd b/changelog/pattern-deprecate.dd index 2fe5214fe66..fd14f94333b 100644 --- a/changelog/pattern-deprecate.dd +++ b/changelog/pattern-deprecate.dd @@ -8,3 +8,60 @@ $(MREF std, regex) and $(MREF std, algorithm). They will be removed from $(MREF std, string) on May 2018. If you still need to use these, please see $(LINK2 https://github.com/dlang/undeaD, undeaD). + +The following are examples of the deprecated functions, and their modern replacements. +Note that the new code is based on the range model, and therefore does not have +mutate against the original string, unlike the old code. + +$(REF munch, std, string): +------- +import std.algorithm; +import std.ascii; +import std.string; +import std.utf; + +string s = "\tabc"; + +// old +s.munch(whitespace); + +// new +size_t i = s.byCodeUnit.countUntil!(a => !isWhite(a)); +s = s[i .. $]; +------- + +$(REF inPattern, std, string): +------- +import std.string; +import std.regex; + +// old +if (inPattern('x', "a-z")) { ... } + +// new +if ("x".matchFirst(regex("[a-z]"))) { ... } +------- + +$(REF removechars, std, string): +------- +import std.string; +import std.regex; + +// old +"abc".removechars("a-z"); + +// new +"abc".replaceAll(regex("[a-z]"), ""); +------- + +$(REF squeeze, std, string): +------- +import std.algorithm; +import std.string; + +// old +"hello".squeeze; + +// new +"hello".uniq; +------- \ No newline at end of file From 0f01083944b2e0231fdb75ffb171e0a45d786cf6 Mon Sep 17 00:00:00 2001 From: Sebastian Wilzbach Date: Thu, 22 Jun 2017 05:11:27 +0200 Subject: [PATCH 3/5] Add missing closing parenthesis to changelog entry --- changelog/split-std-datetime.dd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/split-std-datetime.dd b/changelog/split-std-datetime.dd index 656979a6cac..13f080340ec 100644 --- a/changelog/split-std-datetime.dd +++ b/changelog/split-std-datetime.dd @@ -28,7 +28,7 @@ $(MREF std,datetime,timezone) contains the time zone types. $(MREF std,datetime,package) contains StopWatch and the benchmarking functions (so, they can only be imported via std.datetime and not via a submodule). As those functions use $(REF TickDuration,core,time) (which is being replaced by -$(REF MonoTime,core,time), they are slated for deprecation. +$(REF MonoTime,core,time), they are slated for deprecation). $(MREF std,datetime,stopwatch) has been added. It contains versions of StopWatch and benchmark which have almost the same API as the existing symbols, From d2c5c53ab7fd369aa555e7061b5c849f3adee1a2 Mon Sep 17 00:00:00 2001 From: Sebastian Wilzbach Date: Fri, 23 Jun 2017 15:29:45 +0200 Subject: [PATCH 4/5] Fix REF links in std.file.slurp --- std/file.d | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/std/file.d b/std/file.d index 1fff42df695..d39093a37fe 100644 --- a/std/file.d +++ b/std/file.d @@ -4178,8 +4178,8 @@ auto dirEntries(string path, string pattern, SpanMode mode, * Reads a file line by line and parses the line into a single value or a * $(REF Tuple, std,typecons) of values depending on the length of `Types`. * The lines are parsed using the specified format string. The format string is - * passed to $(REF formattedRead, std,format), and therefore must conform to the - * format string specification outlined in $(MREF format). + * passed to $(REF formattedRead, std,_format), and therefore must conform to the + * format string specification outlined in $(MREF std, _format). * * Params: * Types = the types that each of the elements in the line should be returned as @@ -4193,7 +4193,7 @@ auto dirEntries(string path, string pattern, SpanMode mode, * Throws: * `Exception` if the format string is malformed. Also, throws `Exception` * if any of the lines in the file are not fully consumed by the call - * to $(REF formattedRead, std,format). Meaning that no empty lines or lines + * to $(REF formattedRead, std,_format). Meaning that no empty lines or lines * with extra characters are allowed. */ Select!(Types.length == 1, Types[0][], Tuple!(Types)[]) From 29b5a688c5dad69165508d1eec865115b016f7b2 Mon Sep 17 00:00:00 2001 From: Martin Nowak Date: Sat, 24 Jun 2017 16:47:03 +0200 Subject: [PATCH 5/5] address review comments - also add ref to replacement function --- changelog/pattern-deprecate.dd | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/changelog/pattern-deprecate.dd b/changelog/pattern-deprecate.dd index fd14f94333b..12bedbeb189 100644 --- a/changelog/pattern-deprecate.dd +++ b/changelog/pattern-deprecate.dd @@ -10,10 +10,8 @@ $(MREF std, string) on May 2018. If you still need to use these, please see $(LINK2 https://github.com/dlang/undeaD, undeaD). The following are examples of the deprecated functions, and their modern replacements. -Note that the new code is based on the range model, and therefore does not have -mutate against the original string, unlike the old code. -$(REF munch, std, string): +Use $(REF find, std, algorithm, searching) to replace $(REF munch, std, string): ------- import std.algorithm; import std.ascii; @@ -26,11 +24,10 @@ string s = "\tabc"; s.munch(whitespace); // new -size_t i = s.byCodeUnit.countUntil!(a => !isWhite(a)); -s = s[i .. $]; +s = s.find!(a => !isWhite(a)); ------- -$(REF inPattern, std, string): +Use $(REF matchFirst, std, regex) to replace $(REF inPattern, std, string): ------- import std.string; import std.regex; @@ -42,7 +39,7 @@ if (inPattern('x', "a-z")) { ... } if ("x".matchFirst(regex("[a-z]"))) { ... } ------- -$(REF removechars, std, string): +Use $(REF replaceAll, std, regex) to replace $(REF removechars, std, string): ------- import std.string; import std.regex; @@ -54,7 +51,7 @@ import std.regex; "abc".replaceAll(regex("[a-z]"), ""); ------- -$(REF squeeze, std, string): +Use $(REF uniq, std, algorithm, iteration) to replace $(REF squeeze, std, string): ------- import std.algorithm; import std.string; @@ -64,4 +61,4 @@ import std.string; // new "hello".uniq; -------- \ No newline at end of file +-------