From 0b93dcca87212556860dbf27c1955d7cbfad8c21 Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev Date: Sat, 19 Apr 2025 09:11:16 +0000 Subject: [PATCH 1/2] std.container.rbtree: Return range elements by ref --- std/container/rbtree.d | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/std/container/rbtree.d b/std/container/rbtree.d index 53697029c97..05a05203d45 100644 --- a/std/container/rbtree.d +++ b/std/container/rbtree.d @@ -679,7 +679,7 @@ private struct RBRange(N) /** * Returns the first element in the range */ - @property Elem front() + ref @property Elem front() { return _begin.value; } @@ -687,7 +687,7 @@ private struct RBRange(N) /** * Returns the last element in the range */ - @property Elem back() + ref @property Elem back() { return _end.prev.value; } @@ -1036,7 +1036,7 @@ if (is(typeof(binaryFun!less(T.init, T.init)))) * * Complexity: $(BIGOH 1) */ - inout(Elem) front() inout + ref inout(Elem) front() inout { return _begin.value; } @@ -1046,7 +1046,7 @@ if (is(typeof(binaryFun!less(T.init, T.init)))) * * Complexity: $(BIGOH log(n)) */ - inout(Elem) back() inout + ref inout(Elem) back() inout { return _end.prev.value; } From ab89679e31e4fef1a6e3a2fba226acf30105029b Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev Date: Sun, 20 Apr 2025 09:58:56 +0000 Subject: [PATCH 2/2] std.container.rbtree: Add warning about modifying elements by ref --- std/container/rbtree.d | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/std/container/rbtree.d b/std/container/rbtree.d index 05a05203d45..6ff2a0e9cf8 100644 --- a/std/container/rbtree.d +++ b/std/container/rbtree.d @@ -737,6 +737,10 @@ private struct RBRange(N) * elements `a` and `b`, $(D less(a, b) == !less(b, a)). $(D less(a, a)) should * always equal `false`. * + * Care should also be taken to not modify elements in the tree (e.g. via `front` / + * `back`, which return by `ref`) in a way which would affect the order defined by + * the `less` predicate. + * * If `allowDuplicates` is set to `true`, then inserting the same element more than * once continues to add more elements. If it is `false`, duplicate elements are * ignored on insertion. If duplicates are allowed, then new elements are