Skip to content

Commit faa514e

Browse files
authored
std.container.rbtree: Return range elements by ref (#10755)
* std.container.rbtree: Return range elements by ref * std.container.rbtree: Add warning about modifying elements by ref
1 parent f20bd75 commit faa514e

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

std/container/rbtree.d

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -679,15 +679,15 @@ private struct RBRange(N)
679679
/**
680680
* Returns the first element in the range
681681
*/
682-
@property Elem front()
682+
ref @property Elem front()
683683
{
684684
return _begin.value;
685685
}
686686

687687
/**
688688
* Returns the last element in the range
689689
*/
690-
@property Elem back()
690+
ref @property Elem back()
691691
{
692692
return _end.prev.value;
693693
}
@@ -737,6 +737,10 @@ private struct RBRange(N)
737737
* elements `a` and `b`, $(D less(a, b) == !less(b, a)). $(D less(a, a)) should
738738
* always equal `false`.
739739
*
740+
* Care should also be taken to not modify elements in the tree (e.g. via `front` /
741+
* `back`, which return by `ref`) in a way which would affect the order defined by
742+
* the `less` predicate.
743+
*
740744
* If `allowDuplicates` is set to `true`, then inserting the same element more than
741745
* once continues to add more elements. If it is `false`, duplicate elements are
742746
* ignored on insertion. If duplicates are allowed, then new elements are
@@ -1036,7 +1040,7 @@ if (is(typeof(binaryFun!less(T.init, T.init))))
10361040
*
10371041
* Complexity: $(BIGOH 1)
10381042
*/
1039-
inout(Elem) front() inout
1043+
ref inout(Elem) front() inout
10401044
{
10411045
return _begin.value;
10421046
}
@@ -1046,7 +1050,7 @@ if (is(typeof(binaryFun!less(T.init, T.init))))
10461050
*
10471051
* Complexity: $(BIGOH log(n))
10481052
*/
1049-
inout(Elem) back() inout
1053+
ref inout(Elem) back() inout
10501054
{
10511055
return _end.prev.value;
10521056
}

0 commit comments

Comments
 (0)